feat: /titles/{id} endpoint implemented
This commit is contained in:
parent
cefbbec1dc
commit
47989ab10d
5 changed files with 283 additions and 114 deletions
|
|
@ -58,15 +58,15 @@ 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
|
||||
// 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 {
|
||||
log.Errorf("%v", err)
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("query GetTitleTags: %v", err)
|
||||
}
|
||||
|
||||
var oapi_tag_names oapi.Tags
|
||||
|
|
@ -74,8 +74,7 @@ func (s Server) GetTagsByTitleId(ctx context.Context, id int64) (oapi.Tags, erro
|
|||
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
|
||||
return nil, fmt.Errorf("unmarshalling title_tag: %v", err)
|
||||
}
|
||||
oapi_tag_names = append(oapi_tag_names, oapi_tag_name)
|
||||
}
|
||||
|
|
@ -89,8 +88,7 @@ func (s Server) GetImage(ctx context.Context, id int64) (oapi.Image, error) {
|
|||
|
||||
sqlc_image, err := s.db.GetImageByID(ctx, id)
|
||||
if err != nil {
|
||||
log.Errorf("%v", err)
|
||||
return oapi_image, err
|
||||
return oapi_image, fmt.Errorf("query GetImageByID: %v", err)
|
||||
}
|
||||
//can cast and dont use brain cause all this fiels required
|
||||
oapi_image.Id = &sqlc_image.ID
|
||||
|
|
@ -106,8 +104,7 @@ func (s Server) GetStudio(ctx context.Context, id int64) (oapi.Studio, error) {
|
|||
|
||||
sqlc_studio, err := s.db.GetStudioByID(ctx, id)
|
||||
if err != nil {
|
||||
log.Errorf("%v", err)
|
||||
return oapi_studio, err
|
||||
return oapi_studio, fmt.Errorf("query GetStudioByID: %v", err)
|
||||
}
|
||||
|
||||
oapi_studio.Id = &sqlc_studio.ID
|
||||
|
|
@ -116,16 +113,82 @@ func (s Server) GetStudio(ctx context.Context, id int64) (oapi.Studio, error) {
|
|||
|
||||
oapi_illust, err := s.GetImage(ctx, *sqlc_studio.IllustID)
|
||||
if err != nil {
|
||||
log.Errorf("%v", err)
|
||||
return oapi_studio, err
|
||||
return oapi_studio, fmt.Errorf("GetImage: %v", err)
|
||||
}
|
||||
oapi_studio.Poster = &oapi_illust
|
||||
|
||||
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
|
||||
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
|
||||
err = json.Unmarshal(title.EpisodesLen, &episodes_lens)
|
||||
if err != nil {
|
||||
return oapi_title, fmt.Errorf("unmarshal EpisodesLen: %v", err)
|
||||
}
|
||||
|
||||
oapi_tag_names, err := s.GetTagsByTitleId(ctx, title.ID)
|
||||
if err != nil {
|
||||
return oapi_title, fmt.Errorf("GetTagsByTitleId: %v", err)
|
||||
}
|
||||
|
||||
oapi_image, err := s.GetImage(ctx, *title.PosterID)
|
||||
if err != nil {
|
||||
return oapi_title, fmt.Errorf("GetImage: %v", err)
|
||||
}
|
||||
|
||||
oapi_studio, err := s.GetStudio(ctx, title.StudioID)
|
||||
if err != nil {
|
||||
return oapi_title, fmt.Errorf("GetStudio: %v", err)
|
||||
}
|
||||
|
||||
oapi_title = oapi.Title{
|
||||
|
||||
Id: title.ID,
|
||||
Poster: &oapi_image,
|
||||
Rating: title.Rating,
|
||||
RatingCount: title.RatingCount,
|
||||
ReleaseSeason: (*oapi.ReleaseSeason)(title.ReleaseSeason),
|
||||
ReleaseYear: title.ReleaseYear,
|
||||
Studio: &oapi_studio,
|
||||
Tags: oapi_tag_names,
|
||||
TitleNames: title_names,
|
||||
TitleStatus: (*oapi.TitleStatus)(&title.TitleStatus),
|
||||
EpisodesAired: title.EpisodesAired,
|
||||
EpisodesAll: title.EpisodesAll,
|
||||
EpisodesLen: &episodes_lens,
|
||||
}
|
||||
return oapi_title, nil
|
||||
}
|
||||
|
||||
func (s Server) GetTitleTitleId(ctx context.Context, request oapi.GetTitleTitleIdRequestObject) (oapi.GetTitleTitleIdResponseObject, error) {
|
||||
var oapi_title oapi.Title
|
||||
|
||||
sqlc_title, err := s.db.GetTitleByID(ctx, request.TitleId)
|
||||
if err != nil {
|
||||
log.Errorf("%v", err)
|
||||
return oapi.GetTitleTitleId500Response{}, nil
|
||||
}
|
||||
|
||||
oapi_title, err = s.mapTitle(ctx, sqlc_title)
|
||||
if err != nil {
|
||||
log.Errorf("%v", err)
|
||||
return oapi.GetTitleTitleId500Response{}, nil
|
||||
}
|
||||
|
||||
return oapi.GetTitleTitleId200JSONResponse(oapi_title), nil
|
||||
}
|
||||
|
||||
func (s Server) GetTitle(ctx context.Context, request oapi.GetTitleRequestObject) (oapi.GetTitleResponseObject, error) {
|
||||
var result []oapi.Title
|
||||
var opai_titles []oapi.Title
|
||||
|
||||
word := Word2Sqlc(request.Params.Word)
|
||||
status, err := TitleStatus2Sqlc(request.Params.Status)
|
||||
|
|
@ -158,56 +221,13 @@ func (s Server) GetTitle(ctx context.Context, request oapi.GetTitleRequestObject
|
|||
|
||||
for _, title := range titles {
|
||||
|
||||
var title_names TitleNames
|
||||
err := json.Unmarshal(title.TitleNames, &title_names)
|
||||
t, err := s.mapTitle(ctx, title)
|
||||
if err != nil {
|
||||
log.Errorf("invalid JSON for %s: %v", "TitleNames", err)
|
||||
return oapi.GetTitle500Response{}, err
|
||||
log.Errorf("%v", err)
|
||||
return oapi.GetTitle500Response{}, nil
|
||||
}
|
||||
|
||||
var episodes_lens EpisodeLens
|
||||
err = json.Unmarshal(title.EpisodesLen, &episodes_lens)
|
||||
if err != nil {
|
||||
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,
|
||||
Poster: &oapi_image,
|
||||
Rating: title.Rating,
|
||||
RatingCount: title.RatingCount,
|
||||
ReleaseSeason: (*oapi.ReleaseSeason)(title.ReleaseSeason),
|
||||
ReleaseYear: title.ReleaseYear,
|
||||
Studio: &oapi_studio,
|
||||
Tags: oapi_tag_names,
|
||||
TitleNames: *title_names,
|
||||
TitleStatus: (*oapi.TitleStatus)(&title.TitleStatus),
|
||||
EpisodesAired: title.EpisodesAired,
|
||||
EpisodesAll: title.EpisodesAll,
|
||||
EpisodesLen: episodes_lens,
|
||||
}
|
||||
result = append(result, t)
|
||||
opai_titles = append(opai_titles, t)
|
||||
}
|
||||
|
||||
return oapi.GetTitle200JSONResponse(result), nil
|
||||
return oapi.GetTitle200JSONResponse(opai_titles), nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -72,10 +72,10 @@ RETURNING id, tag_names;
|
|||
-- DELETE FROM users
|
||||
-- WHERE user_id = $1;
|
||||
|
||||
--name: GetTitleByID :one
|
||||
-- name: GetTitleByID :one
|
||||
SELECT *
|
||||
FROM titles
|
||||
WHERE id = sqlc.arg("title_id")::bigint;
|
||||
WHERE id = sqlc.arg('title_id')::bigint;
|
||||
|
||||
-- name: SearchTitles :many
|
||||
SELECT
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue