feat: GetTitles now returns all the field needed

This commit is contained in:
Iron_Felix 2025-11-16 02:38:36 +03:00
parent d1180a426f
commit 13a283ae8d
6 changed files with 119 additions and 51 deletions

View file

@ -46,8 +46,7 @@ type Studio struct {
Description *string `json:"description,omitempty"` Description *string `json:"description,omitempty"`
Id *int64 `json:"id,omitempty"` Id *int64 `json:"id,omitempty"`
Name *string `json:"name,omitempty"` Name *string `json:"name,omitempty"`
PosterId *int64 `json:"poster_id,omitempty"` Poster *Image `json:"poster,omitempty"`
PosterPath *string `json:"poster_path,omitempty"`
} }
// Tag A localized tag: keys are language codes (ISO 639-1), values are tag names // Tag A localized tag: keys are language codes (ISO 639-1), values are tag names

View file

@ -637,11 +637,8 @@ components:
format: int64 format: int64
name: name:
type: string type: string
poster_id: poster:
type: integer $ref: '#/components/schemas/Image'
format: int64
poster_path:
type: string
description: description:
type: string type: string

View file

@ -25,13 +25,14 @@ func TitleStatus2Sqlc(s *oapi.TitleStatus) (*sqlc.TitleStatusT, error) {
return nil, nil return nil, nil
} }
var t sqlc.TitleStatusT var t sqlc.TitleStatusT
if *s == "finished" { switch *s {
t = "finished" case oapi.Finished:
} else if *s == "ongoing" { t = sqlc.TitleStatusTFinished
t = "ongoing" case oapi.Ongoing:
} else if *s == "planned" { t = sqlc.TitleStatusTOngoing
t = "planned" case oapi.Planned:
} else { t = sqlc.TitleStatusTPlanned
default:
return nil, fmt.Errorf("unexpected tittle status: %s", *s) return nil, fmt.Errorf("unexpected tittle status: %s", *s)
} }
return &t, nil return &t, nil
@ -41,41 +42,86 @@ func ReleaseSeason2sqlc(s *oapi.ReleaseSeason) (*sqlc.ReleaseSeasonT, error) {
if s == nil { if s == nil {
return nil, nil return nil, nil
} }
//TODO
var t sqlc.ReleaseSeasonT var t sqlc.ReleaseSeasonT
if *s == oapi.Winter { switch *s {
case oapi.Winter:
t = sqlc.ReleaseSeasonTWinter t = sqlc.ReleaseSeasonTWinter
} else if *s == "spring" { case oapi.Spring:
t = "spring" t = sqlc.ReleaseSeasonTSpring
} else if *s == "summer" { case oapi.Summer:
t = "summer" t = sqlc.ReleaseSeasonTSummer
} else if *s == "fall" { case oapi.Fall:
t = "fall" t = sqlc.ReleaseSeasonTFall
} else { default:
return nil, fmt.Errorf("unexpected release season: %s", *s) return nil, fmt.Errorf("unexpected release season: %s", *s)
} }
return &t, nil return &t, nil
} }
type TileNames *map[string][]string type TitleNames *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)
}
return t, nil
}
type EpisodeLens *map[string]float64 type EpisodeLens *map[string]float64
type TagNames []map[string]string
func jsonb2EpisodeLens(b []byte) (EpisodeLens, error) { func (s Server) GetTagsByTitleId(ctx context.Context, id int64) (oapi.Tags, error) {
var t EpisodeLens sqlc_title_tags, err := s.db.GetTitleTags(ctx, id)
if err := json.Unmarshal(b, t); err != nil { if err != nil {
return nil, fmt.Errorf("invalid episodes_len JSON for title: %w", err) 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
}
func (s Server) GetImage(ctx context.Context, id int64) (oapi.Image, error) {
var oapi_image oapi.Image
sqlc_image, err := s.db.GetImageByID(ctx, id)
if err != nil {
log.Errorf("%v", err)
return oapi_image, err
}
//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) { 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, Limit: request.Params.Limit,
}) })
if err != nil { if err != nil {
log.Errorf("%v", err)
return oapi.GetTitle500Response{}, nil return oapi.GetTitle500Response{}, nil
} }
if len(titles) == 0 { if len(titles) == 0 {
@ -110,26 +157,50 @@ func (s Server) GetTitle(ctx context.Context, request oapi.GetTitleRequestObject
} }
for _, title := range titles { for _, title := range titles {
title_names, err := jsonb2TitleNames(title.TitleNames)
var title_names TitleNames
err := json.Unmarshal(title.TitleNames, &title_names)
if err != nil { if err != nil {
log.Errorf("%v", err) log.Errorf("invalid JSON for %s: %v", "TitleNames", err)
return oapi.GetTitle500Response{}, 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 { if err != nil {
log.Errorf("%v", err) log.Errorf("invalid JSON for %s: %v", "EpisodeLens", err)
return oapi.GetTitle500Response{}, 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{ t := oapi.Title{
Id: &title.ID,
PosterId: title.PosterID, Id: title.ID,
Poster: &oapi_image,
Rating: title.Rating, Rating: title.Rating,
RatingCount: title.RatingCount, RatingCount: title.RatingCount,
ReleaseSeason: (*oapi.ReleaseSeason)(title.ReleaseSeason), ReleaseSeason: (*oapi.ReleaseSeason)(title.ReleaseSeason),
ReleaseYear: title.ReleaseYear, ReleaseYear: title.ReleaseYear,
StudioId: &title.StudioID, Studio: &oapi_studio,
// StudioName: , Tags: oapi_tag_names,
TitleNames: title_names, TitleNames: *title_names,
TitleStatus: (*oapi.TitleStatus)(&title.TitleStatus), TitleStatus: (*oapi.TitleStatus)(&title.TitleStatus),
EpisodesAired: title.EpisodesAired, EpisodesAired: title.EpisodesAired,
EpisodesAll: title.EpisodesAll, EpisodesAll: title.EpisodesAll,

View file

@ -1,7 +1,7 @@
-- name: GetImageByID :one -- name: GetImageByID :one
SELECT id, storage_type, image_path SELECT id, storage_type, image_path
FROM images FROM images
WHERE id = $1; WHERE id = sqlc.arg('illust_id');
-- name: CreateImage :one -- name: CreateImage :one
INSERT INTO images (storage_type, image_path) INSERT INTO images (storage_type, image_path)
@ -17,7 +17,7 @@ WHERE id = $1;
-- name: GetStudioByID :one -- name: GetStudioByID :one
SELECT * SELECT *
FROM studios FROM studios
WHERE id = sqlc.arg('studio_id')::int; WHERE id = sqlc.arg('studio_id')::bigint;
-- name: InsertStudio :one -- name: InsertStudio :one
INSERT INTO studios (studio_name, illust_id, studio_desc) INSERT INTO studios (studio_name, illust_id, studio_desc)

View file

@ -14,6 +14,7 @@ CREATE TABLE providers (
CREATE TABLE tags ( CREATE TABLE tags (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY, id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
-- example: { "ru": "Сёдзё", "en": "Shojo", "jp": "少女"}
tag_names jsonb NOT NULL tag_names jsonb NOT NULL
); );

View file

@ -34,8 +34,8 @@ FROM images
WHERE id = $1 WHERE id = $1
` `
func (q *Queries) GetImageByID(ctx context.Context, id int64) (Image, error) { func (q *Queries) GetImageByID(ctx context.Context, illustID int64) (Image, error) {
row := q.db.QueryRow(ctx, getImageByID, id) row := q.db.QueryRow(ctx, getImageByID, illustID)
var i Image var i Image
err := row.Scan(&i.ID, &i.StorageType, &i.ImagePath) err := row.Scan(&i.ID, &i.StorageType, &i.ImagePath)
return i, err return i, err
@ -44,10 +44,10 @@ func (q *Queries) GetImageByID(ctx context.Context, id int64) (Image, error) {
const getStudioByID = `-- name: GetStudioByID :one const getStudioByID = `-- name: GetStudioByID :one
SELECT id, studio_name, illust_id, studio_desc SELECT id, studio_name, illust_id, studio_desc
FROM studios 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) row := q.db.QueryRow(ctx, getStudioByID, studioID)
var i Studio var i Studio
err := row.Scan( err := row.Scan(