feat: query for usertitles written

This commit is contained in:
Iron_Felix 2025-11-22 07:53:50 +03:00
parent ba68b5ee04
commit 32566fe7a2
8 changed files with 497 additions and 250 deletions

View file

@ -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)