diff --git a/api/api.gen.go b/api/api.gen.go index 3f4f5dd..5222930 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,omitempty"` - Name *string `json:"name,omitempty"` + Id int64 `json:"id"` + Name string `json:"name"` Poster *Image `json:"poster,omitempty"` } @@ -639,6 +639,14 @@ 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 9ea20f4..7c83426 100644 --- a/api/openapi.yaml +++ b/api/openapi.yaml @@ -94,6 +94,8 @@ paths: description: Request params are not correct '500': description: Unknown server error + '204': + description: No title found # patch: # summary: Update title info @@ -636,6 +638,9 @@ 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 e5fcf18..c66fef5 100644 --- a/modules/backend/handlers/titles.go +++ b/modules/backend/handlers/titles.go @@ -2,6 +2,7 @@ package handlers import ( "context" + "database/sql" "encoding/json" "fmt" oapi "nyanimedb/api" @@ -11,12 +12,10 @@ import ( ) func Word2Sqlc(s *string) *string { - if s == nil { - return nil - } - if *s == "" { + if s == nil || *s == "" { return nil } + return s } @@ -58,20 +57,19 @@ 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) } - var oapi_tag_names oapi.Tags + oapi_tag_names := make(oapi.Tags, 1) for _, title_tag := range sqlc_title_tags { - var oapi_tag_name map[string]string + oapi_tag_name := make(map[string]string, 1) err = json.Unmarshal(title_tag, &oapi_tag_name) if err != nil { return nil, fmt.Errorf("unmarshalling title_tag: %v", err) @@ -82,56 +80,65 @@ 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 fiels required + //can cast and dont use brain cause all this fields required in image table oapi_image.Id = &sqlc_image.ID oapi_image.ImagePath = &sqlc_image.ImagePath - storageTypeStr := string(sqlc_image.StorageType) // или fmt.Sprint(...), если int + storageTypeStr := string(sqlc_image.StorageType) 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 { - return oapi_studio, fmt.Errorf("query GetStudioByID: %v", err) + if err == sql.ErrNoRows { + return nil, nil + } + 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) + return &oapi_studio, fmt.Errorf("GetImage: %v", err) + } + if oapi_illust != nil { + oapi_studio.Poster = oapi_illust } - 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 - var title_names map[string][]string + title_names := make(map[string][]string, 1) err := json.Unmarshal(title.TitleNames, &title_names) if err != nil { return oapi_title, fmt.Errorf("unmarshal TitleNames: %v", err) } - var episodes_lens map[string]float64 + episodes_lens := make(map[string]float64, 1) err = json.Unmarshal(title.EpisodesLen, &episodes_lens) if err != nil { return oapi_title, fmt.Errorf("unmarshal EpisodesLen: %v", err) @@ -141,16 +148,25 @@ 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) @@ -163,12 +179,9 @@ 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 @@ -182,6 +195,9 @@ 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 } @@ -196,7 +212,7 @@ func (s Server) GetTitleTitleId(ctx context.Context, request oapi.GetTitleTitleI } func (s Server) GetTitle(ctx context.Context, request oapi.GetTitleRequestObject) (oapi.GetTitleResponseObject, error) { - var opai_titles []oapi.Title + opai_titles := make([]oapi.Title, 1) word := Word2Sqlc(request.Params.Word) status, err := TitleStatus2Sqlc(request.Params.Status) diff --git a/modules/backend/queries.sql b/modules/backend/queries.sql index 0570604..a4ed1fe 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 review_id, user_id, title_id, image_ids, review_text, creation_date --- FROM reviews --- WHERE review_id = $1; +-- name: GetReviewByID :one +SELECT * +FROM reviews +WHERE review_id = sqlc.arg('review_id')::bigint; -- -- name: CreateReview :one -- INSERT INTO reviews (user_id, title_id, image_ids, review_text, creation_date) @@ -157,7 +157,7 @@ OFFSET sqlc.narg('offset')::int; -- 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 97f881d..81f2801 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 UNIQUE, + studio_name text NOT NULL UNIQUE, illust_id bigint REFERENCES images (id), studio_desc text ); diff --git a/sql/models.go b/sql/models.go index a36c6fa..a593504 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 8539d8d..c5e6f8a 100644 --- a/sql/queries.sql.go +++ b/sql/queries.sql.go @@ -41,6 +41,55 @@ 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