feat: query for usertitles written
This commit is contained in:
parent
ba68b5ee04
commit
32566fe7a2
8 changed files with 497 additions and 250 deletions
|
|
@ -1,6 +1,10 @@
|
|||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
oapi "nyanimedb/api"
|
||||
sqlc "nyanimedb/sql"
|
||||
"strconv"
|
||||
)
|
||||
|
|
@ -13,70 +17,75 @@ func NewServer(db *sqlc.Queries) Server {
|
|||
return Server{db: db}
|
||||
}
|
||||
|
||||
// type Cursor interface {
|
||||
// ParseCursor(sortBy oapi.TitleSort, data oapi.Cursor) (Cursor, error)
|
||||
func (s Server) mapTitle(ctx context.Context, title sqlc.SearchTitlesRow) (oapi.Title, error) {
|
||||
|
||||
// Values() map[string]interface{}
|
||||
// // for logs only
|
||||
// Type() string
|
||||
// }
|
||||
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)
|
||||
}
|
||||
|
||||
// type CursorByID struct {
|
||||
// ID int64
|
||||
// }
|
||||
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)
|
||||
}
|
||||
|
||||
// func (c CursorByID) ParseCursor(sortBy oapi.TitleSort, data oapi.Cursor) (Cursor, error) {
|
||||
// var cur CursorByID
|
||||
// if err := json.Unmarshal(data, &cur); err != nil {
|
||||
// return nil, fmt.Errorf("invalid cursor (id): %w", err)
|
||||
// }
|
||||
// if cur.ID == 0 {
|
||||
// return nil, errors.New("cursor id must be non-zero")
|
||||
// }
|
||||
// return cur, nil
|
||||
// }
|
||||
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)
|
||||
}
|
||||
|
||||
// func (c CursorByID) Values() map[string]interface{} {
|
||||
// return map[string]interface{}{
|
||||
// "cursor_id": c.ID,
|
||||
// "cursor_year": nil,
|
||||
// "cursor_rating": nil,
|
||||
// }
|
||||
// }
|
||||
var oapi_studio oapi.Studio
|
||||
|
||||
// func (c CursorByID) Type() string { return "id" }
|
||||
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
|
||||
}
|
||||
|
||||
// func NewCursor(sortBy string) (Cursor, error) {
|
||||
// switch Type(sortBy) {
|
||||
// case TypeID:
|
||||
// return CursorByID{}, nil
|
||||
// case TypeYear:
|
||||
// return CursorByYear{}, nil
|
||||
// case TypeRating:
|
||||
// return CursorByRating{}, nil
|
||||
// default:
|
||||
// return nil, fmt.Errorf("unsupported sort_by: %q", sortBy)
|
||||
// }
|
||||
// }
|
||||
var oapi_image oapi.Image
|
||||
|
||||
// decodes a base64-encoded JSON string into a CursorObj
|
||||
// Returns the parsed CursorObj and an error
|
||||
// func parseCursor(encoded oapi.Cursor) (*oapi.CursorObj, error) {
|
||||
if title.PosterID != nil {
|
||||
oapi_image.Id = title.PosterID
|
||||
oapi_image.ImagePath = title.TitleImagePath
|
||||
oapi_image.StorageType = &title.TitleStorageType
|
||||
}
|
||||
|
||||
// // Decode base64
|
||||
// decoded, err := base64.StdEncoding.DecodeString(encoded)
|
||||
// if err != nil {
|
||||
// return nil, fmt.Errorf("parseCursor: %v", err)
|
||||
// }
|
||||
var release_season oapi.ReleaseSeason
|
||||
if title.ReleaseSeason != nil {
|
||||
release_season = oapi.ReleaseSeason(*title.ReleaseSeason)
|
||||
}
|
||||
|
||||
// // Parse JSON
|
||||
// var cursor oapi.CursorObj
|
||||
// if err := json.Unmarshal(decoded, &cursor); err != nil {
|
||||
// return nil, fmt.Errorf("parseCursor: %v", err)
|
||||
// }
|
||||
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 &cursor, nil
|
||||
// }
|
||||
return oapi_title, nil
|
||||
}
|
||||
|
||||
func parseInt64(s string) (int32, error) {
|
||||
i, err := strconv.ParseInt(s, 10, 64)
|
||||
|
|
|
|||
|
|
@ -158,78 +158,6 @@ func (s Server) GetStudio(ctx context.Context, id int64) (*oapi.Studio, error) {
|
|||
return &oapi_studio, nil
|
||||
}
|
||||
|
||||
func (s Server) mapTitle(ctx context.Context, title sqlc.SearchTitlesRow) (oapi.Title, error) {
|
||||
|
||||
// var oapi_title oapi.Title
|
||||
|
||||
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 (s Server) GetTitlesTitleId(ctx context.Context, request oapi.GetTitlesTitleIdRequestObject) (oapi.GetTitlesTitleIdResponseObject, error) {
|
||||
var oapi_title oapi.Title
|
||||
|
||||
|
|
|
|||
|
|
@ -2,12 +2,15 @@ package handlers
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
oapi "nyanimedb/api"
|
||||
sqlc "nyanimedb/sql"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
"github.com/oapi-codegen/runtime/types"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// type Server struct {
|
||||
|
|
@ -50,33 +53,120 @@ func (s Server) GetUsersUserId(ctx context.Context, req oapi.GetUsersUserIdReque
|
|||
return oapi.GetUsersUserId200JSONResponse(mapUser(user)), nil
|
||||
}
|
||||
|
||||
func sqlDate2oapi(p_date pgtype.Timestamptz) (time.Time, error) {
|
||||
return time.Time{}, nil
|
||||
}
|
||||
|
||||
type SqlcUserStatus struct {
|
||||
dropped string
|
||||
finished string
|
||||
planned string
|
||||
in_progress string
|
||||
}
|
||||
|
||||
func UserTitleStatus2Sqlc(s *[]oapi.UserTitleStatus) (*SqlcUserStatus, error) {
|
||||
var sqlc_status SqlcUserStatus
|
||||
if s == nil {
|
||||
return &sqlc_status, nil
|
||||
}
|
||||
for _, t := range *s {
|
||||
switch t {
|
||||
case oapi.UserTitleStatusFinished:
|
||||
sqlc_status.finished = "finished"
|
||||
case oapi.UserTitleStatusDropped:
|
||||
sqlc_status.dropped = "dropped"
|
||||
case oapi.UserTitleStatusPlanned:
|
||||
sqlc_status.planned = "planned"
|
||||
case oapi.UserTitleStatusInProgress:
|
||||
sqlc_status.in_progress = "in-progress"
|
||||
default:
|
||||
return nil, fmt.Errorf("unexpected tittle status: %s", t)
|
||||
}
|
||||
}
|
||||
return &sqlc_status, nil
|
||||
}
|
||||
|
||||
func (s Server) GetUsersUserIdTitles(ctx context.Context, request oapi.GetUsersUserIdTitlesRequestObject) (oapi.GetUsersUserIdTitlesResponseObject, error) {
|
||||
|
||||
var rate int32 = 9
|
||||
var review_id int64 = 3
|
||||
time := time.Date(2025, 1, 15, 10, 30, 0, 0, time.UTC)
|
||||
oapi_usertitles := make([]oapi.UserTitle, 0)
|
||||
|
||||
var userTitles = []oapi.UserTitle{
|
||||
{
|
||||
UserId: 101,
|
||||
TitleId: 2001,
|
||||
Status: oapi.UserTitleStatusFinished,
|
||||
Rate: &rate,
|
||||
Ctime: &time,
|
||||
},
|
||||
{
|
||||
UserId: 102,
|
||||
TitleId: 2002,
|
||||
Status: oapi.UserTitleStatusInProgress,
|
||||
ReviewId: &review_id,
|
||||
Ctime: &time,
|
||||
},
|
||||
{
|
||||
UserId: 103,
|
||||
TitleId: 2003,
|
||||
Status: oapi.UserTitleStatusDropped,
|
||||
Ctime: &time,
|
||||
},
|
||||
word := Word2Sqlc(request.Params.Word)
|
||||
status, err := TitleStatus2Sqlc(request.Params.Status)
|
||||
if err != nil {
|
||||
log.Errorf("%v", err)
|
||||
return oapi.GetUsersUserIdTitles400Response{}, err
|
||||
}
|
||||
|
||||
season, err := ReleaseSeason2sqlc(request.Params.ReleaseSeason)
|
||||
if err != nil {
|
||||
log.Errorf("%v", err)
|
||||
return oapi.GetUsersUserIdTitles400Response{}, err
|
||||
}
|
||||
|
||||
// Forward bool `json:"forward"`
|
||||
// SortBy string `json:"sort_by"`
|
||||
// CursorYear *int32 `json:"cursor_year"`
|
||||
// CursorID *int64 `json:"cursor_id"`
|
||||
// CursorRating *float64 `json:"cursor_rating"`
|
||||
// Word *string `json:"word"`
|
||||
// Ongoing string `json:"ongoing"`
|
||||
// Planned string `json:"planned"`
|
||||
// Dropped string `json:"dropped"`
|
||||
// InProgress string `json:"in-progress"`
|
||||
// Finished string `json:"finished"`
|
||||
// Rate *int32 `json:"rate"`
|
||||
// Rating *float64 `json:"rating"`
|
||||
// ReleaseYear *int32 `json:"release_year"`
|
||||
// ReleaseSeason *ReleaseSeasonT `json:"release_season"`
|
||||
// Limit *int32 `json:"limit"`
|
||||
params := sqlc.SearchTitlesParams{
|
||||
Word: word,
|
||||
Ongoing: status.ongoing,
|
||||
Finished: status.finished,
|
||||
Planned: status.planned,
|
||||
Rating: request.Params.Rating,
|
||||
ReleaseYear: request.Params.ReleaseYear,
|
||||
ReleaseSeason: season,
|
||||
Forward: true,
|
||||
SortBy: "id",
|
||||
Limit: request.Params.Limit,
|
||||
}
|
||||
|
||||
if request.Params.SortForward != nil {
|
||||
params.Forward = *request.Params.SortForward
|
||||
}
|
||||
if request.Params.Sort != nil {
|
||||
params.SortBy = string(*request.Params.Sort)
|
||||
if request.Params.Cursor != nil {
|
||||
err := ParseCursorInto(string(*request.Params.Sort), string(*request.Params.Cursor), ¶ms)
|
||||
if err != nil {
|
||||
log.Errorf("%v", err)
|
||||
return oapi.GetTitles400Response{}, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_sqlc_title := sqlc.SearchTitlesRow{
|
||||
ID: sqlc_title.ID,
|
||||
StudioID: sqlc_title.StudioID,
|
||||
PosterID: sqlc_title.PosterID,
|
||||
TitleStatus: sqlc_title.TitleStatus,
|
||||
Rating: sqlc_title.Rating,
|
||||
RatingCount: sqlc_title.RatingCount,
|
||||
ReleaseYear: sqlc_title.ReleaseYear,
|
||||
ReleaseSeason: sqlc_title.ReleaseSeason,
|
||||
Season: sqlc_title.Season,
|
||||
EpisodesAired: sqlc_title.EpisodesAired,
|
||||
EpisodesAll: sqlc_title.EpisodesAll,
|
||||
EpisodesLen: sqlc_title.EpisodesLen,
|
||||
TitleStorageType: sqlc_title.TitleStorageType,
|
||||
TitleImagePath: sqlc_title.TitleImagePath,
|
||||
TagNames: sqlc_title.TitleNames,
|
||||
StudioName: sqlc_title.StudioName,
|
||||
StudioIllustID: sqlc_title.StudioIllustID,
|
||||
StudioDesc: sqlc_title.StudioDesc,
|
||||
StudioStorageType: sqlc_title.StudioStorageType,
|
||||
StudioImagePath: sqlc_title.StudioImagePath,
|
||||
}
|
||||
|
||||
return oapi.GetUsersUserIdTitles200JSONResponse(userTitles), nil
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue