fix
This commit is contained in:
parent
abf09e5f5e
commit
df45a327e6
5 changed files with 60 additions and 35 deletions
|
|
@ -2,6 +2,7 @@ package handlers
|
|||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
oapi "nyanimedb/api"
|
||||
|
|
@ -11,13 +12,10 @@ import (
|
|||
)
|
||||
|
||||
func Word2Sqlc(s *string) *string {
|
||||
// TODO: merge two ifs
|
||||
if s == nil {
|
||||
return nil
|
||||
}
|
||||
if *s == "" {
|
||||
if s == nil || *s == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
|
|
@ -59,21 +57,19 @@ 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
|
||||
|
||||
func (s Server) GetTagsByTitleId(ctx context.Context, id int64) (oapi.Tags, error) {
|
||||
|
||||
sqlc_title_tags, err := s.db.GetTitleTags(ctx, id)
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, fmt.Errorf("query GetTitleTags: %v", err)
|
||||
}
|
||||
|
||||
// TODO: check if underlying slice initialized as nil
|
||||
var oapi_tag_names oapi.Tags
|
||||
oapi_tag_names := make(oapi.Tags, 1)
|
||||
for _, title_tag := range sqlc_title_tags {
|
||||
var oapi_tag_name map[string]string
|
||||
oapi_tag_name := make(map[string]string, 1)
|
||||
err = json.Unmarshal(title_tag, &oapi_tag_name)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unmarshalling title_tag: %v", err)
|
||||
|
|
@ -84,58 +80,65 @@ func (s Server) GetTagsByTitleId(ctx context.Context, id int64) (oapi.Tags, erro
|
|||
return oapi_tag_names, nil
|
||||
}
|
||||
|
||||
func (s Server) GetImage(ctx context.Context, id int64) (oapi.Image, error) {
|
||||
func (s Server) GetImage(ctx context.Context, id int64) (*oapi.Image, error) {
|
||||
|
||||
var oapi_image oapi.Image
|
||||
var oapi_image *oapi.Image
|
||||
|
||||
sqlc_image, err := s.db.GetImageByID(ctx, id)
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, nil
|
||||
}
|
||||
return oapi_image, fmt.Errorf("query GetImageByID: %v", err)
|
||||
}
|
||||
|
||||
// TODO: clearer comment
|
||||
//can cast and dont use brain cause all this fiels required
|
||||
//can cast and dont use brain cause all this fields required in image table
|
||||
oapi_image.Id = &sqlc_image.ID
|
||||
oapi_image.ImagePath = &sqlc_image.ImagePath
|
||||
storageTypeStr := string(sqlc_image.StorageType) // или fmt.Sprint(...), если int
|
||||
storageTypeStr := string(sqlc_image.StorageType)
|
||||
oapi_image.StorageType = &storageTypeStr
|
||||
|
||||
return oapi_image, nil
|
||||
}
|
||||
|
||||
func (s Server) GetStudio(ctx context.Context, id int64) (oapi.Studio, error) {
|
||||
// TODO: check all possibly nil fields
|
||||
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 {
|
||||
return oapi_studio, fmt.Errorf("query GetStudioByID: %v", err)
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, nil
|
||||
}
|
||||
return &oapi_studio, fmt.Errorf("query GetStudioByID: %v", err)
|
||||
}
|
||||
|
||||
oapi_studio.Id = &sqlc_studio.ID
|
||||
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 {
|
||||
return oapi_studio, fmt.Errorf("GetImage: %v", err)
|
||||
return &oapi_studio, fmt.Errorf("GetImage: %v", err)
|
||||
}
|
||||
if oapi_illust != nil {
|
||||
oapi_studio.Poster = oapi_illust
|
||||
}
|
||||
oapi_studio.Poster = &oapi_illust
|
||||
|
||||
return oapi_studio, nil
|
||||
return &oapi_studio, nil
|
||||
}
|
||||
|
||||
func (s Server) mapTitle(ctx context.Context, title sqlc.Title) (oapi.Title, error) {
|
||||
// TODO: check all possibly nil fields
|
||||
|
||||
var oapi_title oapi.Title
|
||||
|
||||
var title_names map[string][]string
|
||||
title_names := make(map[string][]string, 1)
|
||||
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
|
||||
episodes_lens := make(map[string]float64, 1)
|
||||
err = json.Unmarshal(title.EpisodesLen, &episodes_lens)
|
||||
if err != nil {
|
||||
return oapi_title, fmt.Errorf("unmarshal EpisodesLen: %v", err)
|
||||
|
|
@ -145,16 +148,25 @@ func (s Server) mapTitle(ctx context.Context, title sqlc.Title) (oapi.Title, err
|
|||
if err != nil {
|
||||
return oapi_title, fmt.Errorf("GetTagsByTitleId: %v", err)
|
||||
}
|
||||
if oapi_tag_names != nil {
|
||||
oapi_title.Tags = oapi_tag_names
|
||||
}
|
||||
|
||||
oapi_image, err := s.GetImage(ctx, *title.PosterID)
|
||||
if err != nil {
|
||||
return oapi_title, fmt.Errorf("GetImage: %v", err)
|
||||
}
|
||||
if oapi_image != nil {
|
||||
oapi_title.Poster = oapi_image
|
||||
}
|
||||
|
||||
oapi_studio, err := s.GetStudio(ctx, title.StudioID)
|
||||
if err != nil {
|
||||
return oapi_title, fmt.Errorf("GetStudio: %v", err)
|
||||
}
|
||||
if oapi_studio != nil {
|
||||
oapi_title.Studio = oapi_studio
|
||||
}
|
||||
|
||||
if title.ReleaseSeason != nil {
|
||||
rs := oapi.ReleaseSeason(*title.ReleaseSeason)
|
||||
|
|
@ -167,12 +179,9 @@ func (s Server) mapTitle(ctx context.Context, title sqlc.Title) (oapi.Title, err
|
|||
oapi_title.TitleStatus = &ts
|
||||
|
||||
oapi_title.Id = title.ID
|
||||
oapi_title.Poster = &oapi_image
|
||||
oapi_title.Rating = title.Rating
|
||||
oapi_title.RatingCount = title.RatingCount
|
||||
oapi_title.ReleaseYear = title.ReleaseYear
|
||||
oapi_title.Studio = &oapi_studio
|
||||
oapi_title.Tags = oapi_tag_names
|
||||
oapi_title.TitleNames = title_names
|
||||
oapi_title.EpisodesAired = title.EpisodesAired
|
||||
oapi_title.EpisodesAll = title.EpisodesAll
|
||||
|
|
@ -186,6 +195,9 @@ func (s Server) GetTitleTitleId(ctx context.Context, request oapi.GetTitleTitleI
|
|||
|
||||
sqlc_title, err := s.db.GetTitleByID(ctx, request.TitleId)
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return oapi.GetTitleTitleId204Response{}, nil
|
||||
}
|
||||
log.Errorf("%v", err)
|
||||
return oapi.GetTitleTitleId500Response{}, nil
|
||||
}
|
||||
|
|
@ -200,7 +212,7 @@ func (s Server) GetTitleTitleId(ctx context.Context, request oapi.GetTitleTitleI
|
|||
}
|
||||
|
||||
func (s Server) GetTitle(ctx context.Context, request oapi.GetTitleRequestObject) (oapi.GetTitleResponseObject, error) {
|
||||
var opai_titles []oapi.Title
|
||||
opai_titles := make([]oapi.Title, 1)
|
||||
|
||||
word := Word2Sqlc(request.Params.Word)
|
||||
status, err := TitleStatus2Sqlc(request.Params.Status)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue