Merge branch 'dev-ars' into dev
This commit is contained in:
commit
76d28d0170
7 changed files with 113 additions and 35 deletions
|
|
@ -44,8 +44,8 @@ type ReleaseSeason string
|
|||
// Studio defines model for Studio.
|
||||
type Studio struct {
|
||||
Description *string `json:"description,omitempty"`
|
||||
Id *int64 `json:"id,omitempty"`
|
||||
Name *string `json:"name,omitempty"`
|
||||
Id int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Poster *Image `json:"poster,omitempty"`
|
||||
}
|
||||
|
||||
|
|
@ -639,6 +639,14 @@ func (response GetTitleTitleId200JSONResponse) VisitGetTitleTitleIdResponse(w ht
|
|||
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 {
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -94,6 +94,8 @@ paths:
|
|||
description: Request params are not correct
|
||||
'500':
|
||||
description: Unknown server error
|
||||
'204':
|
||||
description: No title found
|
||||
|
||||
# patch:
|
||||
# summary: Update title info
|
||||
|
|
@ -636,6 +638,9 @@ components:
|
|||
|
||||
Studio:
|
||||
type: object
|
||||
required:
|
||||
- id
|
||||
- name
|
||||
properties:
|
||||
id:
|
||||
type: integer
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package handlers
|
|||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
oapi "nyanimedb/api"
|
||||
|
|
@ -11,12 +12,10 @@ import (
|
|||
)
|
||||
|
||||
func Word2Sqlc(s *string) *string {
|
||||
if s == nil {
|
||||
return nil
|
||||
}
|
||||
if *s == "" {
|
||||
if s == nil || *s == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
|
|
@ -58,20 +57,19 @@ func ReleaseSeason2sqlc(s *oapi.ReleaseSeason) (*sqlc.ReleaseSeasonT, error) {
|
|||
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) {
|
||||
|
||||
sqlc_title_tags, err := s.db.GetTitleTags(ctx, id)
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, fmt.Errorf("query GetTitleTags: %v", err)
|
||||
}
|
||||
|
||||
var oapi_tag_names oapi.Tags
|
||||
oapi_tag_names := make(oapi.Tags, 1)
|
||||
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)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unmarshalling title_tag: %v", err)
|
||||
|
|
@ -82,56 +80,65 @@ func (s Server) GetTagsByTitleId(ctx context.Context, id int64) (oapi.Tags, erro
|
|||
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)
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, nil
|
||||
}
|
||||
return oapi_image, fmt.Errorf("query GetImageByID: %v", err)
|
||||
}
|
||||
|
||||
//can cast and dont use brain cause all this fiels required
|
||||
//can cast and dont use brain cause all this fields required in image table
|
||||
oapi_image.Id = &sqlc_image.ID
|
||||
oapi_image.ImagePath = &sqlc_image.ImagePath
|
||||
storageTypeStr := string(sqlc_image.StorageType) // или fmt.Sprint(...), если int
|
||||
storageTypeStr := string(sqlc_image.StorageType)
|
||||
oapi_image.StorageType = &storageTypeStr
|
||||
|
||||
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) {
|
||||
|
||||
var oapi_studio oapi.Studio
|
||||
|
||||
sqlc_studio, err := s.db.GetStudioByID(ctx, id)
|
||||
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.Description = sqlc_studio.StudioDesc
|
||||
|
||||
oapi_illust, err := s.GetImage(ctx, *sqlc_studio.IllustID)
|
||||
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) {
|
||||
|
||||
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)
|
||||
if err != nil {
|
||||
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)
|
||||
if err != nil {
|
||||
return oapi_title, fmt.Errorf("unmarshal EpisodesLen: %v", err)
|
||||
|
|
@ -141,16 +148,25 @@ func (s Server) mapTitle(ctx context.Context, title sqlc.Title) (oapi.Title, err
|
|||
if err != nil {
|
||||
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)
|
||||
if err != nil {
|
||||
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)
|
||||
if err != nil {
|
||||
return oapi_title, fmt.Errorf("GetStudio: %v", err)
|
||||
}
|
||||
if oapi_studio != nil {
|
||||
oapi_title.Studio = oapi_studio
|
||||
}
|
||||
|
||||
if title.ReleaseSeason != nil {
|
||||
rs := oapi.ReleaseSeason(*title.ReleaseSeason)
|
||||
|
|
@ -163,12 +179,9 @@ func (s Server) mapTitle(ctx context.Context, title sqlc.Title) (oapi.Title, err
|
|||
oapi_title.TitleStatus = &ts
|
||||
|
||||
oapi_title.Id = title.ID
|
||||
oapi_title.Poster = &oapi_image
|
||||
oapi_title.Rating = title.Rating
|
||||
oapi_title.RatingCount = title.RatingCount
|
||||
oapi_title.ReleaseYear = title.ReleaseYear
|
||||
oapi_title.Studio = &oapi_studio
|
||||
oapi_title.Tags = oapi_tag_names
|
||||
oapi_title.TitleNames = title_names
|
||||
oapi_title.EpisodesAired = title.EpisodesAired
|
||||
oapi_title.EpisodesAll = title.EpisodesAll
|
||||
|
|
@ -182,6 +195,9 @@ func (s Server) GetTitleTitleId(ctx context.Context, request oapi.GetTitleTitleI
|
|||
|
||||
sqlc_title, err := s.db.GetTitleByID(ctx, request.TitleId)
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return oapi.GetTitleTitleId204Response{}, nil
|
||||
}
|
||||
log.Errorf("%v", err)
|
||||
return oapi.GetTitleTitleId500Response{}, nil
|
||||
}
|
||||
|
|
@ -196,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) {
|
||||
var opai_titles []oapi.Title
|
||||
opai_titles := make([]oapi.Title, 1)
|
||||
|
||||
word := Word2Sqlc(request.Params.Word)
|
||||
status, err := TitleStatus2Sqlc(request.Params.Status)
|
||||
|
|
|
|||
|
|
@ -135,10 +135,10 @@ OFFSET sqlc.narg('offset')::int;
|
|||
-- WHERE title_id = sqlc.arg('title_id')
|
||||
-- RETURNING *;
|
||||
|
||||
-- -- name: GetReviewByID :one
|
||||
-- SELECT review_id, user_id, title_id, image_ids, review_text, creation_date
|
||||
-- FROM reviews
|
||||
-- WHERE review_id = $1;
|
||||
-- name: GetReviewByID :one
|
||||
SELECT *
|
||||
FROM reviews
|
||||
WHERE review_id = sqlc.arg('review_id')::bigint;
|
||||
|
||||
-- -- name: CreateReview :one
|
||||
-- INSERT INTO reviews (user_id, title_id, image_ids, review_text, creation_date)
|
||||
|
|
@ -157,7 +157,7 @@ OFFSET sqlc.narg('offset')::int;
|
|||
-- DELETE FROM reviews
|
||||
-- WHERE review_id = $1;
|
||||
|
||||
-- -- name: ListReviewsByTitle :many
|
||||
-- name: ListReviewsByTitle :many
|
||||
-- SELECT review_id, user_id, title_id, image_ids, review_text, creation_date
|
||||
-- FROM reviews
|
||||
-- WHERE title_id = $1
|
||||
|
|
|
|||
|
|
@ -54,7 +54,7 @@ CREATE TABLE users (
|
|||
|
||||
CREATE TABLE studios (
|
||||
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
studio_name text UNIQUE,
|
||||
studio_name text NOT NULL UNIQUE,
|
||||
illust_id bigint REFERENCES images (id),
|
||||
studio_desc text
|
||||
);
|
||||
|
|
|
|||
|
|
@ -233,7 +233,7 @@ type Signal struct {
|
|||
|
||||
type Studio struct {
|
||||
ID int64 `json:"id"`
|
||||
StudioName *string `json:"studio_name"`
|
||||
StudioName string `json:"studio_name"`
|
||||
IllustID *int64 `json:"illust_id"`
|
||||
StudioDesc *string `json:"studio_desc"`
|
||||
}
|
||||
|
|
|
|||
|
|
@ -41,6 +41,55 @@ func (q *Queries) GetImageByID(ctx context.Context, illustID int64) (Image, erro
|
|||
return i, err
|
||||
}
|
||||
|
||||
const getReviewByID = `-- name: GetReviewByID :one
|
||||
|
||||
|
||||
SELECT id, data, rating, illust_id, user_id, title_id, created_at
|
||||
FROM reviews
|
||||
WHERE review_id = $1::bigint
|
||||
`
|
||||
|
||||
// -- name: ListTitles :many
|
||||
// SELECT title_id, title_names, studio_id, poster_id, signal_ids,
|
||||
//
|
||||
// title_status, rating, rating_count, release_year, release_season,
|
||||
// season, episodes_aired, episodes_all, episodes_len
|
||||
//
|
||||
// FROM titles
|
||||
// ORDER BY title_id
|
||||
// LIMIT $1 OFFSET $2;
|
||||
// -- name: UpdateTitle :one
|
||||
// UPDATE titles
|
||||
// SET
|
||||
//
|
||||
// title_names = COALESCE(sqlc.narg('title_names'), title_names),
|
||||
// studio_id = COALESCE(sqlc.narg('studio_id'), studio_id),
|
||||
// poster_id = COALESCE(sqlc.narg('poster_id'), poster_id),
|
||||
// signal_ids = COALESCE(sqlc.narg('signal_ids'), signal_ids),
|
||||
// title_status = COALESCE(sqlc.narg('title_status'), title_status),
|
||||
// release_year = COALESCE(sqlc.narg('release_year'), release_year),
|
||||
// release_season = COALESCE(sqlc.narg('release_season'), release_season),
|
||||
// episodes_aired = COALESCE(sqlc.narg('episodes_aired'), episodes_aired),
|
||||
// episodes_all = COALESCE(sqlc.narg('episodes_all'), episodes_all),
|
||||
// episodes_len = COALESCE(sqlc.narg('episodes_len'), episodes_len)
|
||||
//
|
||||
// WHERE title_id = sqlc.arg('title_id')
|
||||
// RETURNING *;
|
||||
func (q *Queries) GetReviewByID(ctx context.Context, reviewID int64) (Review, error) {
|
||||
row := q.db.QueryRow(ctx, getReviewByID, reviewID)
|
||||
var i Review
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.Data,
|
||||
&i.Rating,
|
||||
&i.IllustID,
|
||||
&i.UserID,
|
||||
&i.TitleID,
|
||||
&i.CreatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getStudioByID = `-- name: GetStudioByID :one
|
||||
SELECT id, studio_name, illust_id, studio_desc
|
||||
FROM studios
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue