213 lines
5.2 KiB
Go
213 lines
5.2 KiB
Go
package handlers
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
oapi "nyanimedb/api"
|
|
sqlc "nyanimedb/sql"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
func Word2Sqlc(s *string) *string {
|
|
if s == nil {
|
|
return nil
|
|
}
|
|
if *s == "" {
|
|
return nil
|
|
}
|
|
return s
|
|
}
|
|
|
|
func TitleStatus2Sqlc(s *oapi.TitleStatus) (*sqlc.TitleStatusT, error) {
|
|
if s == nil {
|
|
return nil, nil
|
|
}
|
|
var t sqlc.TitleStatusT
|
|
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
|
|
}
|
|
|
|
func ReleaseSeason2sqlc(s *oapi.ReleaseSeason) (*sqlc.ReleaseSeasonT, error) {
|
|
if s == nil {
|
|
return nil, nil
|
|
}
|
|
var t sqlc.ReleaseSeasonT
|
|
switch *s {
|
|
case oapi.Winter:
|
|
t = sqlc.ReleaseSeasonTWinter
|
|
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 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
|
|
}
|
|
|
|
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) {
|
|
var result []oapi.Title
|
|
|
|
word := Word2Sqlc(request.Params.Word)
|
|
status, err := TitleStatus2Sqlc(request.Params.Status)
|
|
if err != nil {
|
|
log.Errorf("%v", err)
|
|
return oapi.GetTitle400Response{}, err
|
|
}
|
|
season, err := ReleaseSeason2sqlc(request.Params.ReleaseSeason)
|
|
if err != nil {
|
|
log.Errorf("%v", err)
|
|
return oapi.GetTitle400Response{}, err
|
|
}
|
|
// param = nil means it will not be used
|
|
titles, err := s.db.SearchTitles(ctx, sqlc.SearchTitlesParams{
|
|
Word: word,
|
|
Status: status,
|
|
Rating: request.Params.Rating,
|
|
ReleaseYear: request.Params.ReleaseYear,
|
|
ReleaseSeason: season,
|
|
Offset: request.Params.Offset,
|
|
Limit: request.Params.Limit,
|
|
})
|
|
if err != nil {
|
|
log.Errorf("%v", err)
|
|
return oapi.GetTitle500Response{}, nil
|
|
}
|
|
if len(titles) == 0 {
|
|
return oapi.GetTitle204Response{}, nil
|
|
}
|
|
|
|
for _, title := range titles {
|
|
|
|
var title_names TitleNames
|
|
err := json.Unmarshal(title.TitleNames, &title_names)
|
|
if err != nil {
|
|
log.Errorf("invalid JSON for %s: %v", "TitleNames", err)
|
|
return oapi.GetTitle500Response{}, err
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
return oapi.GetTitle200JSONResponse(result), nil
|
|
}
|