feat: GetTitles now returns all the field needed
This commit is contained in:
parent
d1180a426f
commit
13a283ae8d
6 changed files with 119 additions and 51 deletions
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue