feat: get title func written
This commit is contained in:
parent
ae01eec0fd
commit
e8783a0e9d
1 changed files with 43 additions and 4 deletions
|
|
@ -2,6 +2,7 @@ package handlers
|
|||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
oapi "nyanimedb/api"
|
||||
sqlc "nyanimedb/sql"
|
||||
|
|
@ -40,9 +41,10 @@ func ReleaseSeason2sqlc(s *oapi.ReleaseSeason) (*sqlc.ReleaseSeasonT, error) {
|
|||
if s == nil {
|
||||
return nil, nil
|
||||
}
|
||||
//TODO
|
||||
var t sqlc.ReleaseSeasonT
|
||||
if *s == "winter" {
|
||||
t = "winter"
|
||||
if *s == oapi.Winter {
|
||||
t = sqlc.ReleaseSeasonTWinter
|
||||
} else if *s == "spring" {
|
||||
t = "spring"
|
||||
} else if *s == "summer" {
|
||||
|
|
@ -55,6 +57,23 @@ func ReleaseSeason2sqlc(s *oapi.ReleaseSeason) (*sqlc.ReleaseSeasonT, error) {
|
|||
return &t, nil
|
||||
}
|
||||
|
||||
// unmarshall jsonb to map[string][]string
|
||||
func jsonb2map4names(b []byte) (*map[string][]string, error) {
|
||||
var t map[string][]string
|
||||
if err := json.Unmarshal(b, &t); err != nil {
|
||||
return nil, fmt.Errorf("invalid title_names JSON for title: %w", err)
|
||||
}
|
||||
return &t, nil
|
||||
}
|
||||
|
||||
func jsonb2map4len(b []byte) (*map[string]float64, error) {
|
||||
var t map[string]float64
|
||||
if err := json.Unmarshal(b, &t); err != nil {
|
||||
return nil, fmt.Errorf("invalid episodes_len JSON for title: %w", err)
|
||||
}
|
||||
return &t, nil
|
||||
}
|
||||
|
||||
func (s Server) GetTitle(ctx context.Context, request oapi.GetTitleRequestObject) (oapi.GetTitleResponseObject, error) {
|
||||
var result []oapi.Title
|
||||
|
||||
|
|
@ -85,9 +104,29 @@ func (s Server) GetTitle(ctx context.Context, request oapi.GetTitleRequestObject
|
|||
}
|
||||
|
||||
for _, title := range titles {
|
||||
title_names, err := jsonb2map4names(title.TitleNames)
|
||||
if err != nil {
|
||||
log.Errorf("%v", err)
|
||||
return oapi.GetTitle500Response{}, err
|
||||
}
|
||||
episodes_lens, err := jsonb2map4len(title.EpisodesLen)
|
||||
if err != nil {
|
||||
log.Errorf("%v", err)
|
||||
return oapi.GetTitle500Response{}, err
|
||||
}
|
||||
t := oapi.Title{
|
||||
Rating: title.Rating,
|
||||
Id: &title.ID,
|
||||
PosterId: title.PosterID,
|
||||
Rating: title.Rating,
|
||||
RatingCount: title.RatingCount,
|
||||
ReleaseSeason: (*oapi.ReleaseSeason)(title.ReleaseSeason),
|
||||
ReleaseYear: title.ReleaseYear,
|
||||
StudioId: &title.StudioID,
|
||||
TitleNames: title_names,
|
||||
TitleStatus: (*oapi.TitleStatus)(&title.TitleStatus),
|
||||
EpisodesAired: title.EpisodesAired,
|
||||
EpisodesAll: title.EpisodesAll,
|
||||
EpisodesLen: episodes_lens,
|
||||
}
|
||||
result = append(result, t)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue