feat: titles.go added
This commit is contained in:
parent
c2dc762700
commit
d2450ffc89
3 changed files with 130 additions and 0 deletions
96
modules/backend/handlers/titles.go
Normal file
96
modules/backend/handlers/titles.go
Normal file
|
|
@ -0,0 +1,96 @@
|
|||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"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
|
||||
if *s == "finished" {
|
||||
t = "finished"
|
||||
} else if *s == "ongoing" {
|
||||
t = "ongoing"
|
||||
} else if *s == "planned" {
|
||||
t = "planned"
|
||||
} else {
|
||||
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
|
||||
if *s == "winter" {
|
||||
t = "winter"
|
||||
} else if *s == "spring" {
|
||||
t = "spring"
|
||||
} else if *s == "summer" {
|
||||
t = "summer"
|
||||
} else if *s == "fall" {
|
||||
t = "fall"
|
||||
} else {
|
||||
return nil, fmt.Errorf("unexpected release season: %s", *s)
|
||||
}
|
||||
return &t, 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,
|
||||
})
|
||||
if err != nil {
|
||||
return oapi.GetTitle500Response{}, nil
|
||||
}
|
||||
if len(titles) == 0 {
|
||||
return oapi.GetTitle204Response{}, nil
|
||||
}
|
||||
|
||||
for _, title := range titles {
|
||||
t := oapi.Title{
|
||||
Rating: title.Rating,
|
||||
PosterId: title.PosterID,
|
||||
}
|
||||
result = append(result, t)
|
||||
}
|
||||
|
||||
return oapi.GetTitle200JSONResponse(result), nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue