diff --git a/api/api.gen.go b/api/api.gen.go index 5222930..3f4f5dd 100644 --- a/api/api.gen.go +++ b/api/api.gen.go @@ -44,8 +44,8 @@ type ReleaseSeason string // Studio defines model for Studio. type Studio struct { Description *string `json:"description,omitempty"` - Id int64 `json:"id"` - Name string `json:"name"` + Id *int64 `json:"id,omitempty"` + Name *string `json:"name,omitempty"` Poster *Image `json:"poster,omitempty"` } @@ -639,14 +639,6 @@ func (response GetTitleTitleId200JSONResponse) VisitGetTitleTitleIdResponse(w ht return json.NewEncoder(w).Encode(response) } -type GetTitleTitleId204Response struct { -} - -func (response GetTitleTitleId204Response) VisitGetTitleTitleIdResponse(w http.ResponseWriter) error { - w.WriteHeader(204) - return nil -} - type GetTitleTitleId400Response struct { } diff --git a/api/openapi.yaml b/api/openapi.yaml index 7c83426..9ea20f4 100644 --- a/api/openapi.yaml +++ b/api/openapi.yaml @@ -94,8 +94,6 @@ paths: description: Request params are not correct '500': description: Unknown server error - '204': - description: No title found # patch: # summary: Update title info @@ -638,9 +636,6 @@ components: Studio: type: object - required: - - id - - name properties: id: type: integer diff --git a/modules/backend/handlers/titles.go b/modules/backend/handlers/titles.go index c66fef5..e5fcf18 100644 --- a/modules/backend/handlers/titles.go +++ b/modules/backend/handlers/titles.go @@ -2,7 +2,6 @@ package handlers import ( "context" - "database/sql" "encoding/json" "fmt" oapi "nyanimedb/api" @@ -12,10 +11,12 @@ import ( ) func Word2Sqlc(s *string) *string { - if s == nil || *s == "" { + if s == nil { + return nil + } + if *s == "" { return nil } - return s } @@ -57,19 +58,20 @@ func ReleaseSeason2sqlc(s *oapi.ReleaseSeason) (*sqlc.ReleaseSeasonT, error) { return &t, nil } +// type TitleNames map[string][]string +// type EpisodeLens map[string]float64 +// type TagNames []map[string]string + func (s Server) GetTagsByTitleId(ctx context.Context, id int64) (oapi.Tags, error) { sqlc_title_tags, err := s.db.GetTitleTags(ctx, id) if err != nil { - if err == sql.ErrNoRows { - return nil, nil - } return nil, fmt.Errorf("query GetTitleTags: %v", err) } - oapi_tag_names := make(oapi.Tags, 1) + var oapi_tag_names oapi.Tags for _, title_tag := range sqlc_title_tags { - oapi_tag_name := make(map[string]string, 1) + var oapi_tag_name map[string]string err = json.Unmarshal(title_tag, &oapi_tag_name) if err != nil { return nil, fmt.Errorf("unmarshalling title_tag: %v", err) @@ -80,65 +82,56 @@ func (s Server) GetTagsByTitleId(ctx context.Context, id int64) (oapi.Tags, erro return oapi_tag_names, nil } -func (s Server) GetImage(ctx context.Context, id int64) (*oapi.Image, error) { +func (s Server) GetImage(ctx context.Context, id int64) (oapi.Image, error) { - var oapi_image *oapi.Image + var oapi_image oapi.Image sqlc_image, err := s.db.GetImageByID(ctx, id) if err != nil { - if err == sql.ErrNoRows { - return nil, nil - } return oapi_image, fmt.Errorf("query GetImageByID: %v", err) } - //can cast and dont use brain cause all this fields required in image table + //can cast and dont use brain cause all this fiels required oapi_image.Id = &sqlc_image.ID oapi_image.ImagePath = &sqlc_image.ImagePath - storageTypeStr := string(sqlc_image.StorageType) + storageTypeStr := string(sqlc_image.StorageType) // или fmt.Sprint(...), если int oapi_image.StorageType = &storageTypeStr return oapi_image, nil } -func (s Server) GetStudio(ctx context.Context, id int64) (*oapi.Studio, error) { +func (s Server) GetStudio(ctx context.Context, id int64) (oapi.Studio, error) { var oapi_studio oapi.Studio sqlc_studio, err := s.db.GetStudioByID(ctx, id) if err != nil { - if err == sql.ErrNoRows { - return nil, nil - } - return &oapi_studio, fmt.Errorf("query GetStudioByID: %v", err) + return oapi_studio, fmt.Errorf("query GetStudioByID: %v", err) } - oapi_studio.Id = sqlc_studio.ID + oapi_studio.Id = &sqlc_studio.ID oapi_studio.Name = sqlc_studio.StudioName oapi_studio.Description = sqlc_studio.StudioDesc oapi_illust, err := s.GetImage(ctx, *sqlc_studio.IllustID) if err != nil { - return &oapi_studio, fmt.Errorf("GetImage: %v", err) - } - if oapi_illust != nil { - oapi_studio.Poster = oapi_illust + return oapi_studio, fmt.Errorf("GetImage: %v", err) } + oapi_studio.Poster = &oapi_illust - return &oapi_studio, nil + return oapi_studio, nil } func (s Server) mapTitle(ctx context.Context, title sqlc.Title) (oapi.Title, error) { - var oapi_title oapi.Title - title_names := make(map[string][]string, 1) + var title_names map[string][]string err := json.Unmarshal(title.TitleNames, &title_names) if err != nil { return oapi_title, fmt.Errorf("unmarshal TitleNames: %v", err) } - episodes_lens := make(map[string]float64, 1) + var episodes_lens map[string]float64 err = json.Unmarshal(title.EpisodesLen, &episodes_lens) if err != nil { return oapi_title, fmt.Errorf("unmarshal EpisodesLen: %v", err) @@ -148,25 +141,16 @@ func (s Server) mapTitle(ctx context.Context, title sqlc.Title) (oapi.Title, err if err != nil { return oapi_title, fmt.Errorf("GetTagsByTitleId: %v", err) } - if oapi_tag_names != nil { - oapi_title.Tags = oapi_tag_names - } oapi_image, err := s.GetImage(ctx, *title.PosterID) if err != nil { return oapi_title, fmt.Errorf("GetImage: %v", err) } - if oapi_image != nil { - oapi_title.Poster = oapi_image - } oapi_studio, err := s.GetStudio(ctx, title.StudioID) if err != nil { return oapi_title, fmt.Errorf("GetStudio: %v", err) } - if oapi_studio != nil { - oapi_title.Studio = oapi_studio - } if title.ReleaseSeason != nil { rs := oapi.ReleaseSeason(*title.ReleaseSeason) @@ -179,9 +163,12 @@ func (s Server) mapTitle(ctx context.Context, title sqlc.Title) (oapi.Title, err oapi_title.TitleStatus = &ts oapi_title.Id = title.ID + oapi_title.Poster = &oapi_image oapi_title.Rating = title.Rating oapi_title.RatingCount = title.RatingCount oapi_title.ReleaseYear = title.ReleaseYear + oapi_title.Studio = &oapi_studio + oapi_title.Tags = oapi_tag_names oapi_title.TitleNames = title_names oapi_title.EpisodesAired = title.EpisodesAired oapi_title.EpisodesAll = title.EpisodesAll @@ -195,9 +182,6 @@ func (s Server) GetTitleTitleId(ctx context.Context, request oapi.GetTitleTitleI sqlc_title, err := s.db.GetTitleByID(ctx, request.TitleId) if err != nil { - if err == sql.ErrNoRows { - return oapi.GetTitleTitleId204Response{}, nil - } log.Errorf("%v", err) return oapi.GetTitleTitleId500Response{}, nil } @@ -212,7 +196,7 @@ func (s Server) GetTitleTitleId(ctx context.Context, request oapi.GetTitleTitleI } func (s Server) GetTitle(ctx context.Context, request oapi.GetTitleRequestObject) (oapi.GetTitleResponseObject, error) { - opai_titles := make([]oapi.Title, 1) + var opai_titles []oapi.Title word := Word2Sqlc(request.Params.Word) status, err := TitleStatus2Sqlc(request.Params.Status) diff --git a/modules/backend/queries.sql b/modules/backend/queries.sql index a4ed1fe..0570604 100644 --- a/modules/backend/queries.sql +++ b/modules/backend/queries.sql @@ -135,10 +135,10 @@ OFFSET sqlc.narg('offset')::int; -- WHERE title_id = sqlc.arg('title_id') -- RETURNING *; --- name: GetReviewByID :one -SELECT * -FROM reviews -WHERE review_id = sqlc.arg('review_id')::bigint; +-- -- name: GetReviewByID :one +-- SELECT review_id, user_id, title_id, image_ids, review_text, creation_date +-- FROM reviews +-- WHERE review_id = $1; -- -- name: CreateReview :one -- INSERT INTO reviews (user_id, title_id, image_ids, review_text, creation_date) @@ -157,7 +157,7 @@ WHERE review_id = sqlc.arg('review_id')::bigint; -- DELETE FROM reviews -- WHERE review_id = $1; --- name: ListReviewsByTitle :many +-- -- name: ListReviewsByTitle :many -- SELECT review_id, user_id, title_id, image_ids, review_text, creation_date -- FROM reviews -- WHERE title_id = $1 diff --git a/sql/migrations/000001_init.up.sql b/sql/migrations/000001_init.up.sql index 81f2801..97f881d 100644 --- a/sql/migrations/000001_init.up.sql +++ b/sql/migrations/000001_init.up.sql @@ -54,7 +54,7 @@ CREATE TABLE users ( CREATE TABLE studios ( id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY, - studio_name text NOT NULL UNIQUE, + studio_name text UNIQUE, illust_id bigint REFERENCES images (id), studio_desc text ); diff --git a/sql/models.go b/sql/models.go index a593504..a36c6fa 100644 --- a/sql/models.go +++ b/sql/models.go @@ -233,7 +233,7 @@ type Signal struct { type Studio struct { ID int64 `json:"id"` - StudioName string `json:"studio_name"` + StudioName *string `json:"studio_name"` IllustID *int64 `json:"illust_id"` StudioDesc *string `json:"studio_desc"` } diff --git a/sql/queries.sql.go b/sql/queries.sql.go index c5e6f8a..8539d8d 100644 --- a/sql/queries.sql.go +++ b/sql/queries.sql.go @@ -41,55 +41,6 @@ func (q *Queries) GetImageByID(ctx context.Context, illustID int64) (Image, erro return i, err } -const getReviewByID = `-- name: GetReviewByID :one - - -SELECT id, data, rating, illust_id, user_id, title_id, created_at -FROM reviews -WHERE review_id = $1::bigint -` - -// -- name: ListTitles :many -// SELECT title_id, title_names, studio_id, poster_id, signal_ids, -// -// title_status, rating, rating_count, release_year, release_season, -// season, episodes_aired, episodes_all, episodes_len -// -// FROM titles -// ORDER BY title_id -// LIMIT $1 OFFSET $2; -// -- name: UpdateTitle :one -// UPDATE titles -// SET -// -// title_names = COALESCE(sqlc.narg('title_names'), title_names), -// studio_id = COALESCE(sqlc.narg('studio_id'), studio_id), -// poster_id = COALESCE(sqlc.narg('poster_id'), poster_id), -// signal_ids = COALESCE(sqlc.narg('signal_ids'), signal_ids), -// title_status = COALESCE(sqlc.narg('title_status'), title_status), -// release_year = COALESCE(sqlc.narg('release_year'), release_year), -// release_season = COALESCE(sqlc.narg('release_season'), release_season), -// episodes_aired = COALESCE(sqlc.narg('episodes_aired'), episodes_aired), -// episodes_all = COALESCE(sqlc.narg('episodes_all'), episodes_all), -// episodes_len = COALESCE(sqlc.narg('episodes_len'), episodes_len) -// -// WHERE title_id = sqlc.arg('title_id') -// RETURNING *; -func (q *Queries) GetReviewByID(ctx context.Context, reviewID int64) (Review, error) { - row := q.db.QueryRow(ctx, getReviewByID, reviewID) - var i Review - err := row.Scan( - &i.ID, - &i.Data, - &i.Rating, - &i.IllustID, - &i.UserID, - &i.TitleID, - &i.CreatedAt, - ) - return i, err -} - const getStudioByID = `-- name: GetStudioByID :one SELECT id, studio_name, illust_id, studio_desc FROM studios