fix
This commit is contained in:
parent
abf09e5f5e
commit
df45a327e6
5 changed files with 60 additions and 35 deletions
|
|
@ -44,8 +44,8 @@ type ReleaseSeason string
|
||||||
// Studio defines model for Studio.
|
// Studio defines model for Studio.
|
||||||
type Studio struct {
|
type Studio struct {
|
||||||
Description *string `json:"description,omitempty"`
|
Description *string `json:"description,omitempty"`
|
||||||
Id *int64 `json:"id,omitempty"`
|
Id int64 `json:"id"`
|
||||||
Name *string `json:"name,omitempty"`
|
Name string `json:"name"`
|
||||||
Poster *Image `json:"poster,omitempty"`
|
Poster *Image `json:"poster,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -639,6 +639,14 @@ func (response GetTitleTitleId200JSONResponse) VisitGetTitleTitleIdResponse(w ht
|
||||||
return json.NewEncoder(w).Encode(response)
|
return json.NewEncoder(w).Encode(response)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type GetTitleTitleId204Response struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (response GetTitleTitleId204Response) VisitGetTitleTitleIdResponse(w http.ResponseWriter) error {
|
||||||
|
w.WriteHeader(204)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
type GetTitleTitleId400Response struct {
|
type GetTitleTitleId400Response struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -94,6 +94,8 @@ paths:
|
||||||
description: Request params are not correct
|
description: Request params are not correct
|
||||||
'500':
|
'500':
|
||||||
description: Unknown server error
|
description: Unknown server error
|
||||||
|
'204':
|
||||||
|
description: No title found
|
||||||
|
|
||||||
# patch:
|
# patch:
|
||||||
# summary: Update title info
|
# summary: Update title info
|
||||||
|
|
@ -636,6 +638,9 @@ components:
|
||||||
|
|
||||||
Studio:
|
Studio:
|
||||||
type: object
|
type: object
|
||||||
|
required:
|
||||||
|
- id
|
||||||
|
- name
|
||||||
properties:
|
properties:
|
||||||
id:
|
id:
|
||||||
type: integer
|
type: integer
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package handlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"database/sql"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
oapi "nyanimedb/api"
|
oapi "nyanimedb/api"
|
||||||
|
|
@ -11,13 +12,10 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func Word2Sqlc(s *string) *string {
|
func Word2Sqlc(s *string) *string {
|
||||||
// TODO: merge two ifs
|
if s == nil || *s == "" {
|
||||||
if s == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if *s == "" {
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -59,21 +57,19 @@ func ReleaseSeason2sqlc(s *oapi.ReleaseSeason) (*sqlc.ReleaseSeasonT, error) {
|
||||||
return &t, nil
|
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) {
|
func (s Server) GetTagsByTitleId(ctx context.Context, id int64) (oapi.Tags, error) {
|
||||||
|
|
||||||
sqlc_title_tags, err := s.db.GetTitleTags(ctx, id)
|
sqlc_title_tags, err := s.db.GetTitleTags(ctx, id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if err == sql.ErrNoRows {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
return nil, fmt.Errorf("query GetTitleTags: %v", err)
|
return nil, fmt.Errorf("query GetTitleTags: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: check if underlying slice initialized as nil
|
oapi_tag_names := make(oapi.Tags, 1)
|
||||||
var oapi_tag_names oapi.Tags
|
|
||||||
for _, title_tag := range sqlc_title_tags {
|
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)
|
err = json.Unmarshal(title_tag, &oapi_tag_name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("unmarshalling title_tag: %v", err)
|
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
|
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)
|
sqlc_image, err := s.db.GetImageByID(ctx, id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if err == sql.ErrNoRows {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
return oapi_image, fmt.Errorf("query GetImageByID: %v", err)
|
return oapi_image, fmt.Errorf("query GetImageByID: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: clearer comment
|
//can cast and dont use brain cause all this fields required in image table
|
||||||
//can cast and dont use brain cause all this fiels required
|
|
||||||
oapi_image.Id = &sqlc_image.ID
|
oapi_image.Id = &sqlc_image.ID
|
||||||
oapi_image.ImagePath = &sqlc_image.ImagePath
|
oapi_image.ImagePath = &sqlc_image.ImagePath
|
||||||
storageTypeStr := string(sqlc_image.StorageType) // или fmt.Sprint(...), если int
|
storageTypeStr := string(sqlc_image.StorageType)
|
||||||
oapi_image.StorageType = &storageTypeStr
|
oapi_image.StorageType = &storageTypeStr
|
||||||
|
|
||||||
return oapi_image, nil
|
return oapi_image, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s Server) GetStudio(ctx context.Context, id int64) (oapi.Studio, error) {
|
func (s Server) GetStudio(ctx context.Context, id int64) (*oapi.Studio, error) {
|
||||||
// TODO: check all possibly nil fields
|
|
||||||
var oapi_studio oapi.Studio
|
var oapi_studio oapi.Studio
|
||||||
|
|
||||||
sqlc_studio, err := s.db.GetStudioByID(ctx, id)
|
sqlc_studio, err := s.db.GetStudioByID(ctx, id)
|
||||||
if err != nil {
|
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.Name = sqlc_studio.StudioName
|
||||||
oapi_studio.Description = sqlc_studio.StudioDesc
|
oapi_studio.Description = sqlc_studio.StudioDesc
|
||||||
|
|
||||||
oapi_illust, err := s.GetImage(ctx, *sqlc_studio.IllustID)
|
oapi_illust, err := s.GetImage(ctx, *sqlc_studio.IllustID)
|
||||||
if err != nil {
|
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) {
|
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 oapi_title oapi.Title
|
||||||
|
|
||||||
var title_names map[string][]string
|
title_names := make(map[string][]string, 1)
|
||||||
err := json.Unmarshal(title.TitleNames, &title_names)
|
err := json.Unmarshal(title.TitleNames, &title_names)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return oapi_title, fmt.Errorf("unmarshal TitleNames: %v", err)
|
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)
|
err = json.Unmarshal(title.EpisodesLen, &episodes_lens)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return oapi_title, fmt.Errorf("unmarshal EpisodesLen: %v", err)
|
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 {
|
if err != nil {
|
||||||
return oapi_title, fmt.Errorf("GetTagsByTitleId: %v", err)
|
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)
|
oapi_image, err := s.GetImage(ctx, *title.PosterID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return oapi_title, fmt.Errorf("GetImage: %v", err)
|
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)
|
oapi_studio, err := s.GetStudio(ctx, title.StudioID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return oapi_title, fmt.Errorf("GetStudio: %v", err)
|
return oapi_title, fmt.Errorf("GetStudio: %v", err)
|
||||||
}
|
}
|
||||||
|
if oapi_studio != nil {
|
||||||
|
oapi_title.Studio = oapi_studio
|
||||||
|
}
|
||||||
|
|
||||||
if title.ReleaseSeason != nil {
|
if title.ReleaseSeason != nil {
|
||||||
rs := oapi.ReleaseSeason(*title.ReleaseSeason)
|
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.TitleStatus = &ts
|
||||||
|
|
||||||
oapi_title.Id = title.ID
|
oapi_title.Id = title.ID
|
||||||
oapi_title.Poster = &oapi_image
|
|
||||||
oapi_title.Rating = title.Rating
|
oapi_title.Rating = title.Rating
|
||||||
oapi_title.RatingCount = title.RatingCount
|
oapi_title.RatingCount = title.RatingCount
|
||||||
oapi_title.ReleaseYear = title.ReleaseYear
|
oapi_title.ReleaseYear = title.ReleaseYear
|
||||||
oapi_title.Studio = &oapi_studio
|
|
||||||
oapi_title.Tags = oapi_tag_names
|
|
||||||
oapi_title.TitleNames = title_names
|
oapi_title.TitleNames = title_names
|
||||||
oapi_title.EpisodesAired = title.EpisodesAired
|
oapi_title.EpisodesAired = title.EpisodesAired
|
||||||
oapi_title.EpisodesAll = title.EpisodesAll
|
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)
|
sqlc_title, err := s.db.GetTitleByID(ctx, request.TitleId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if err == sql.ErrNoRows {
|
||||||
|
return oapi.GetTitleTitleId204Response{}, nil
|
||||||
|
}
|
||||||
log.Errorf("%v", err)
|
log.Errorf("%v", err)
|
||||||
return oapi.GetTitleTitleId500Response{}, nil
|
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) {
|
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)
|
word := Word2Sqlc(request.Params.Word)
|
||||||
status, err := TitleStatus2Sqlc(request.Params.Status)
|
status, err := TitleStatus2Sqlc(request.Params.Status)
|
||||||
|
|
|
||||||
|
|
@ -54,7 +54,7 @@ CREATE TABLE users (
|
||||||
|
|
||||||
CREATE TABLE studios (
|
CREATE TABLE studios (
|
||||||
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||||
studio_name text UNIQUE,
|
studio_name text NOT NULL UNIQUE,
|
||||||
illust_id bigint REFERENCES images (id),
|
illust_id bigint REFERENCES images (id),
|
||||||
studio_desc text
|
studio_desc text
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -233,7 +233,7 @@ type Signal struct {
|
||||||
|
|
||||||
type Studio struct {
|
type Studio struct {
|
||||||
ID int64 `json:"id"`
|
ID int64 `json:"id"`
|
||||||
StudioName *string `json:"studio_name"`
|
StudioName string `json:"studio_name"`
|
||||||
IllustID *int64 `json:"illust_id"`
|
IllustID *int64 `json:"illust_id"`
|
||||||
StudioDesc *string `json:"studio_desc"`
|
StudioDesc *string `json:"studio_desc"`
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue