173 lines
5.1 KiB
Go
173 lines
5.1 KiB
Go
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 {
|
|
// db *sqlc.Queries
|
|
// }
|
|
|
|
// func NewServer(db *sqlc.Queries) Server {
|
|
// return Server{db: db}
|
|
// }
|
|
|
|
// func parseInt64(s string) (int32, error) {
|
|
// i, err := strconv.ParseInt(s, 10, 64)
|
|
// return int32(i), err
|
|
// }
|
|
|
|
func mapUser(u sqlc.GetUserByIDRow) oapi.User {
|
|
return oapi.User{
|
|
AvatarId: u.AvatarID,
|
|
CreationDate: &u.CreationDate,
|
|
DispName: u.DispName,
|
|
Id: &u.ID,
|
|
Mail: (*types.Email)(u.Mail),
|
|
Nickname: u.Nickname,
|
|
UserDesc: u.UserDesc,
|
|
}
|
|
}
|
|
|
|
func (s Server) GetUsersUserId(ctx context.Context, req oapi.GetUsersUserIdRequestObject) (oapi.GetUsersUserIdResponseObject, error) {
|
|
userID, err := parseInt64(req.UserId)
|
|
if err != nil {
|
|
return oapi.GetUsersUserId404Response{}, nil
|
|
}
|
|
user, err := s.db.GetUserByID(context.TODO(), int64(userID))
|
|
if err != nil {
|
|
if err == pgx.ErrNoRows {
|
|
return oapi.GetUsersUserId404Response{}, nil
|
|
}
|
|
return nil, err
|
|
}
|
|
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) {
|
|
|
|
oapi_usertitles := make([]oapi.UserTitle, 0)
|
|
|
|
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
|
|
}
|