151 lines
3.8 KiB
Go
151 lines
3.8 KiB
Go
package handlers
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
oapi "nyanimedb/api"
|
|
sqlc "nyanimedb/sql"
|
|
"strconv"
|
|
)
|
|
|
|
type Server struct {
|
|
db *sqlc.Queries
|
|
}
|
|
|
|
func NewServer(db *sqlc.Queries) Server {
|
|
return Server{db: db}
|
|
}
|
|
|
|
func sql2StorageType(s *sqlc.StorageTypeT) (*oapi.ImageStorageType, error) {
|
|
if s == nil {
|
|
return nil, nil
|
|
}
|
|
var t oapi.ImageStorageType
|
|
switch *s {
|
|
case sqlc.StorageTypeTLocal:
|
|
t = oapi.Local
|
|
case sqlc.StorageTypeTS3:
|
|
t = oapi.S3
|
|
default:
|
|
return nil, fmt.Errorf("unexpected storage type: %s", *s)
|
|
}
|
|
return &t, nil
|
|
}
|
|
|
|
func (s Server) mapTitle(ctx context.Context, title sqlc.GetTitleByIDRow) (oapi.Title, error) {
|
|
|
|
oapi_title := oapi.Title{
|
|
EpisodesAired: title.EpisodesAired,
|
|
EpisodesAll: title.EpisodesAll,
|
|
// EpisodesLen: &episodes_lens,
|
|
Id: title.ID,
|
|
// Poster: &oapi_image,
|
|
Rating: title.Rating,
|
|
RatingCount: title.RatingCount,
|
|
// ReleaseSeason: &release_season,
|
|
ReleaseYear: title.ReleaseYear,
|
|
// Studio: &oapi_studio,
|
|
// Tags: oapi_tag_names,
|
|
// TitleNames: title_names,
|
|
// TitleStatus: oapi_status,
|
|
// AdditionalProperties:
|
|
}
|
|
|
|
title_names := make(map[string][]string, 0)
|
|
err := json.Unmarshal(title.TitleNames, &title_names)
|
|
if err != nil {
|
|
return oapi.Title{}, fmt.Errorf("unmarshal TitleNames: %v", err)
|
|
}
|
|
oapi_title.TitleNames = title_names
|
|
|
|
if len(title.EpisodesLen) > 0 {
|
|
episodes_lens := make(map[string]float64, 0)
|
|
err = json.Unmarshal(title.EpisodesLen, &episodes_lens)
|
|
if err != nil {
|
|
return oapi.Title{}, fmt.Errorf("unmarshal EpisodesLen: %v", err)
|
|
}
|
|
oapi_title.EpisodesLen = &episodes_lens
|
|
}
|
|
|
|
oapi_tag_names := make(oapi.Tags, 0)
|
|
err = json.Unmarshal(title.TagNames, &oapi_tag_names)
|
|
if err != nil {
|
|
return oapi.Title{}, fmt.Errorf("unmarshalling title_tag: %v", err)
|
|
}
|
|
oapi_title.Tags = oapi_tag_names
|
|
|
|
var oapi_studio oapi.Studio
|
|
if title.StudioName != nil {
|
|
oapi_studio.Name = *title.StudioName
|
|
}
|
|
if title.StudioID != 0 {
|
|
oapi_studio.Id = title.StudioID
|
|
oapi_studio.Description = title.StudioDesc
|
|
if title.StudioIllustID != nil {
|
|
oapi_studio.Poster = &oapi.Image{}
|
|
oapi_studio.Poster.Id = title.StudioIllustID
|
|
oapi_studio.Poster.ImagePath = title.StudioImagePath
|
|
|
|
s, err := sql2StorageType(title.StudioStorageType)
|
|
if err != nil {
|
|
return oapi.Title{}, fmt.Errorf("mapTitle, studio storage type: %v", err)
|
|
}
|
|
oapi_studio.Poster.StorageType = s
|
|
|
|
}
|
|
}
|
|
oapi_title.Studio = &oapi_studio
|
|
|
|
var oapi_image oapi.Image
|
|
|
|
if title.PosterID != nil {
|
|
oapi_image.Id = title.PosterID
|
|
oapi_image.ImagePath = title.TitleImagePath
|
|
s, err := sql2StorageType(title.TitleStorageType)
|
|
if err != nil {
|
|
return oapi.Title{}, fmt.Errorf("mapTitle, title starage type: %v", err)
|
|
}
|
|
oapi_image.StorageType = s
|
|
}
|
|
oapi_title.Poster = &oapi_image
|
|
|
|
var release_season oapi.ReleaseSeason
|
|
if title.ReleaseSeason != nil {
|
|
release_season = oapi.ReleaseSeason(*title.ReleaseSeason)
|
|
}
|
|
oapi_title.ReleaseSeason = &release_season
|
|
|
|
oapi_status, err := TitleStatus2oapi(&title.TitleStatus)
|
|
if err != nil {
|
|
return oapi.Title{}, fmt.Errorf("TitleStatus2oapi: %v", err)
|
|
}
|
|
oapi_title.TitleStatus = oapi_status
|
|
|
|
return oapi_title, nil
|
|
}
|
|
|
|
func parseInt64(s string) (int64, error) {
|
|
i, err := strconv.ParseInt(s, 10, 64)
|
|
return i, err
|
|
}
|
|
|
|
func TitleStatus2Sqlc(s *[]oapi.TitleStatus) ([]sqlc.TitleStatusT, error) {
|
|
var sqlc_status []sqlc.TitleStatusT
|
|
if s == nil {
|
|
return nil, nil
|
|
}
|
|
for _, t := range *s {
|
|
switch t {
|
|
case oapi.TitleStatusFinished:
|
|
sqlc_status = append(sqlc_status, sqlc.TitleStatusTFinished)
|
|
case oapi.TitleStatusOngoing:
|
|
sqlc_status = append(sqlc_status, sqlc.TitleStatusTOngoing)
|
|
case oapi.TitleStatusPlanned:
|
|
sqlc_status = append(sqlc_status, sqlc.TitleStatusTPlanned)
|
|
default:
|
|
return nil, fmt.Errorf("unexpected tittle status: %s", t)
|
|
}
|
|
}
|
|
return sqlc_status, nil
|
|
}
|