93 lines
2.3 KiB
Go
93 lines
2.3 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 (s Server) mapTitle(ctx context.Context, title sqlc.SearchTitlesRow) (oapi.Title, error) {
|
|
|
|
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)
|
|
}
|
|
|
|
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_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)
|
|
}
|
|
|
|
var oapi_studio oapi.Studio
|
|
|
|
oapi_studio.Id = title.StudioID
|
|
if title.StudioName != nil {
|
|
oapi_studio.Name = *title.StudioName
|
|
}
|
|
oapi_studio.Description = title.StudioDesc
|
|
if title.StudioIllustID != nil {
|
|
oapi_studio.Poster.Id = title.StudioIllustID
|
|
oapi_studio.Poster.ImagePath = title.StudioImagePath
|
|
oapi_studio.Poster.StorageType = &title.StudioStorageType
|
|
}
|
|
|
|
var oapi_image oapi.Image
|
|
|
|
if title.PosterID != nil {
|
|
oapi_image.Id = title.PosterID
|
|
oapi_image.ImagePath = title.TitleImagePath
|
|
oapi_image.StorageType = &title.TitleStorageType
|
|
}
|
|
|
|
var release_season oapi.ReleaseSeason
|
|
if title.ReleaseSeason != nil {
|
|
release_season = oapi.ReleaseSeason(*title.ReleaseSeason)
|
|
}
|
|
|
|
oapi_status, err := TitleStatus2oapi(&title.TitleStatus)
|
|
if err != nil {
|
|
return oapi.Title{}, fmt.Errorf("TitleStatus2oapi: %v", err)
|
|
}
|
|
oapi_title := oapi.Title{
|
|
EpisodesAired: title.EpisodesAired,
|
|
EpisodesAll: title.EpisodesAired,
|
|
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:
|
|
}
|
|
|
|
return oapi_title, nil
|
|
}
|
|
|
|
func parseInt64(s string) (int32, error) {
|
|
i, err := strconv.ParseInt(s, 10, 64)
|
|
return int32(i), err
|
|
}
|