diff --git a/api/api.gen.go b/api/api.gen.go index b3c51f6..0057c22 100644 --- a/api/api.gen.go +++ b/api/api.gen.go @@ -46,8 +46,7 @@ type Studio struct { Description *string `json:"description,omitempty"` Id *int64 `json:"id,omitempty"` Name *string `json:"name,omitempty"` - PosterId *int64 `json:"poster_id,omitempty"` - PosterPath *string `json:"poster_path,omitempty"` + Poster *Image `json:"poster,omitempty"` } // Tag A localized tag: keys are language codes (ISO 639-1), values are tag names diff --git a/api/openapi.yaml b/api/openapi.yaml index d523f06..4628c6d 100644 --- a/api/openapi.yaml +++ b/api/openapi.yaml @@ -637,11 +637,8 @@ components: format: int64 name: type: string - poster_id: - type: integer - format: int64 - poster_path: - type: string + poster: + $ref: '#/components/schemas/Image' description: type: string diff --git a/modules/backend/handlers/titles.go b/modules/backend/handlers/titles.go index 99217ca..182f450 100644 --- a/modules/backend/handlers/titles.go +++ b/modules/backend/handlers/titles.go @@ -25,13 +25,14 @@ func TitleStatus2Sqlc(s *oapi.TitleStatus) (*sqlc.TitleStatusT, error) { return nil, nil } var t sqlc.TitleStatusT - if *s == "finished" { - t = "finished" - } else if *s == "ongoing" { - t = "ongoing" - } else if *s == "planned" { - t = "planned" - } else { + switch *s { + case oapi.Finished: + t = sqlc.TitleStatusTFinished + case oapi.Ongoing: + t = sqlc.TitleStatusTOngoing + case oapi.Planned: + t = sqlc.TitleStatusTPlanned + default: return nil, fmt.Errorf("unexpected tittle status: %s", *s) } return &t, nil @@ -41,41 +42,86 @@ func ReleaseSeason2sqlc(s *oapi.ReleaseSeason) (*sqlc.ReleaseSeasonT, error) { if s == nil { return nil, nil } - //TODO var t sqlc.ReleaseSeasonT - if *s == oapi.Winter { + switch *s { + case oapi.Winter: t = sqlc.ReleaseSeasonTWinter - } else if *s == "spring" { - t = "spring" - } else if *s == "summer" { - t = "summer" - } else if *s == "fall" { - t = "fall" - } else { + case oapi.Spring: + t = sqlc.ReleaseSeasonTSpring + case oapi.Summer: + t = sqlc.ReleaseSeasonTSummer + case oapi.Fall: + t = sqlc.ReleaseSeasonTFall + default: return nil, fmt.Errorf("unexpected release season: %s", *s) } return &t, nil } -type TileNames *map[string][]string +type TitleNames *map[string][]string +type EpisodeLens *map[string]float64 +type TagNames []map[string]string -// unmarshall jsonb to map[string][]string -func jsonb2TitleNames(b []byte) (TileNames, error) { - var t TileNames - if err := json.Unmarshal(b, t); err != nil { - return nil, fmt.Errorf("invalid title_names JSON for title: %w", err) +func (s Server) GetTagsByTitleId(ctx context.Context, id int64) (oapi.Tags, error) { + sqlc_title_tags, err := s.db.GetTitleTags(ctx, id) + if err != nil { + log.Errorf("%v", err) + return nil, err } - return t, nil + + var oapi_tag_names oapi.Tags + for _, title_tag := range sqlc_title_tags { + var oapi_tag_name map[string]string + err = json.Unmarshal(title_tag, &oapi_tag_name) + if err != nil { + log.Errorf("invalid JSON for %s: %v", "TagNames", err) + return nil, err + } + oapi_tag_names = append(oapi_tag_names, oapi_tag_name) + } + + return oapi_tag_names, nil } -type EpisodeLens *map[string]float64 +func (s Server) GetImage(ctx context.Context, id int64) (oapi.Image, error) { -func jsonb2EpisodeLens(b []byte) (EpisodeLens, error) { - var t EpisodeLens - if err := json.Unmarshal(b, t); err != nil { - return nil, fmt.Errorf("invalid episodes_len JSON for title: %w", err) + var oapi_image oapi.Image + + sqlc_image, err := s.db.GetImageByID(ctx, id) + if err != nil { + log.Errorf("%v", err) + return oapi_image, err } - return t, nil + //can cast and dont use brain cause all this fiels required + oapi_image.Id = &sqlc_image.ID + oapi_image.ImagePath = &sqlc_image.ImagePath + oapi_image.StorageType = (*string)(&sqlc_image.StorageType) + + return oapi_image, nil +} + +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 { + log.Errorf("%v", err) + return oapi_studio, err + } + + 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 { + log.Errorf("%v", err) + return oapi_studio, err + } + oapi_studio.Poster = &oapi_illust + + return oapi_studio, nil } func (s Server) GetTitle(ctx context.Context, request oapi.GetTitleRequestObject) (oapi.GetTitleResponseObject, error) { @@ -103,6 +149,7 @@ func (s Server) GetTitle(ctx context.Context, request oapi.GetTitleRequestObject Limit: request.Params.Limit, }) if err != nil { + log.Errorf("%v", err) return oapi.GetTitle500Response{}, nil } if len(titles) == 0 { @@ -110,26 +157,50 @@ func (s Server) GetTitle(ctx context.Context, request oapi.GetTitleRequestObject } for _, title := range titles { - title_names, err := jsonb2TitleNames(title.TitleNames) + + var title_names TitleNames + err := json.Unmarshal(title.TitleNames, &title_names) if err != nil { - log.Errorf("%v", err) + log.Errorf("invalid JSON for %s: %v", "TitleNames", err) return oapi.GetTitle500Response{}, err } - episodes_lens, err := jsonb2EpisodeLens(title.EpisodesLen) + + var episodes_lens EpisodeLens + err = json.Unmarshal(title.EpisodesLen, &episodes_lens) if err != nil { - log.Errorf("%v", err) + log.Errorf("invalid JSON for %s: %v", "EpisodeLens", err) return oapi.GetTitle500Response{}, err } + + oapi_tag_names, err := s.GetTagsByTitleId(ctx, title.ID) + if err != nil { + log.Errorf("error while getting tags %v", err) + return oapi.GetTitle500Response{}, err + } + + oapi_image, err := s.GetImage(ctx, *title.PosterID) + if err != nil { + log.Errorf("error while getting image %v", err) + return oapi.GetTitle500Response{}, err + } + + oapi_studio, err := s.GetStudio(ctx, title.StudioID) + if err != nil { + log.Errorf("error while getting studio %v", err) + return oapi.GetTitle500Response{}, err + } + t := oapi.Title{ - Id: &title.ID, - PosterId: title.PosterID, + + Id: title.ID, + Poster: &oapi_image, Rating: title.Rating, RatingCount: title.RatingCount, ReleaseSeason: (*oapi.ReleaseSeason)(title.ReleaseSeason), ReleaseYear: title.ReleaseYear, - StudioId: &title.StudioID, - // StudioName: , - TitleNames: title_names, + Studio: &oapi_studio, + Tags: oapi_tag_names, + TitleNames: *title_names, TitleStatus: (*oapi.TitleStatus)(&title.TitleStatus), EpisodesAired: title.EpisodesAired, EpisodesAll: title.EpisodesAll, diff --git a/modules/backend/queries.sql b/modules/backend/queries.sql index a4c0bb9..7717f9f 100644 --- a/modules/backend/queries.sql +++ b/modules/backend/queries.sql @@ -1,7 +1,7 @@ -- name: GetImageByID :one SELECT id, storage_type, image_path FROM images -WHERE id = $1; +WHERE id = sqlc.arg('illust_id'); -- name: CreateImage :one INSERT INTO images (storage_type, image_path) @@ -17,7 +17,7 @@ WHERE id = $1; -- name: GetStudioByID :one SELECT * FROM studios -WHERE id = sqlc.arg('studio_id')::int; +WHERE id = sqlc.arg('studio_id')::bigint; -- name: InsertStudio :one INSERT INTO studios (studio_name, illust_id, studio_desc) diff --git a/sql/migrations/000001_init.up.sql b/sql/migrations/000001_init.up.sql index c325dc8..669143a 100644 --- a/sql/migrations/000001_init.up.sql +++ b/sql/migrations/000001_init.up.sql @@ -14,6 +14,7 @@ CREATE TABLE providers ( CREATE TABLE tags ( id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY, + -- example: { "ru": "Сёдзё", "en": "Shojo", "jp": "少女"} tag_names jsonb NOT NULL ); diff --git a/sql/queries.sql.go b/sql/queries.sql.go index a73889c..865ec73 100644 --- a/sql/queries.sql.go +++ b/sql/queries.sql.go @@ -34,8 +34,8 @@ FROM images WHERE id = $1 ` -func (q *Queries) GetImageByID(ctx context.Context, id int64) (Image, error) { - row := q.db.QueryRow(ctx, getImageByID, id) +func (q *Queries) GetImageByID(ctx context.Context, illustID int64) (Image, error) { + row := q.db.QueryRow(ctx, getImageByID, illustID) var i Image err := row.Scan(&i.ID, &i.StorageType, &i.ImagePath) return i, err @@ -44,10 +44,10 @@ func (q *Queries) GetImageByID(ctx context.Context, id int64) (Image, error) { const getStudioByID = `-- name: GetStudioByID :one SELECT id, studio_name, illust_id, studio_desc FROM studios -WHERE id = $1::int +WHERE id = $1::bigint ` -func (q *Queries) GetStudioByID(ctx context.Context, studioID int32) (Studio, error) { +func (q *Queries) GetStudioByID(ctx context.Context, studioID int64) (Studio, error) { row := q.db.QueryRow(ctx, getStudioByID, studioID) var i Studio err := row.Scan(