Merge branch 'dev-ars' into dev
This commit is contained in:
commit
246fdc86b5
11 changed files with 405 additions and 72 deletions
|
|
@ -1,7 +1,6 @@
|
|||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
oapi "nyanimedb/api"
|
||||
|
|
@ -17,11 +16,11 @@ func NewServer(db *sqlc.Queries) Server {
|
|||
return Server{db: db}
|
||||
}
|
||||
|
||||
func sql2StorageType(s *sqlc.StorageTypeT) (*oapi.ImageStorageType, error) {
|
||||
func sql2StorageType(s *sqlc.StorageTypeT) (*oapi.StorageType, error) {
|
||||
if s == nil {
|
||||
return nil, nil
|
||||
}
|
||||
var t oapi.ImageStorageType
|
||||
var t oapi.StorageType
|
||||
switch *s {
|
||||
case sqlc.StorageTypeTLocal:
|
||||
t = oapi.Local
|
||||
|
|
@ -33,7 +32,7 @@ func sql2StorageType(s *sqlc.StorageTypeT) (*oapi.ImageStorageType, error) {
|
|||
return &t, nil
|
||||
}
|
||||
|
||||
func (s Server) mapTitle(ctx context.Context, title sqlc.GetTitleByIDRow) (oapi.Title, error) {
|
||||
func (s Server) mapTitle(title sqlc.GetTitleByIDRow) (oapi.Title, error) {
|
||||
|
||||
oapi_title := oapi.Title{
|
||||
EpisodesAired: title.EpisodesAired,
|
||||
|
|
|
|||
|
|
@ -144,7 +144,7 @@ func (s Server) GetTitlesTitleId(ctx context.Context, request oapi.GetTitlesTitl
|
|||
return oapi.GetTitlesTitleId500Response{}, nil
|
||||
}
|
||||
|
||||
oapi_title, err = s.mapTitle(ctx, sqlc_title)
|
||||
oapi_title, err = s.mapTitle(sqlc_title)
|
||||
if err != nil {
|
||||
log.Errorf("%v", err)
|
||||
return oapi.GetTitlesTitleId500Response{}, nil
|
||||
|
|
@ -238,7 +238,7 @@ func (s Server) GetTitles(ctx context.Context, request oapi.GetTitlesRequestObje
|
|||
// _title.TitleStorageType = string(s)
|
||||
// }
|
||||
|
||||
t, err := s.mapTitle(ctx, _title)
|
||||
t, err := s.mapTitle(_title)
|
||||
if err != nil {
|
||||
log.Errorf("%v", err)
|
||||
return oapi.GetTitles500Response{}, nil
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package handlers
|
|||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
oapi "nyanimedb/api"
|
||||
sqlc "nyanimedb/sql"
|
||||
|
|
@ -9,24 +10,12 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
"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, error) {
|
||||
i := oapi.Image{
|
||||
Id: u.AvatarID,
|
||||
|
|
@ -202,7 +191,7 @@ func (s Server) mapUsertitle(ctx context.Context, t sqlc.SearchUserTitlesRow) (o
|
|||
// StudioImagePath: title.StudioImagePath,
|
||||
}
|
||||
|
||||
oapi_title, err := s.mapTitle(ctx, _title)
|
||||
oapi_title, err := s.mapTitle(_title)
|
||||
if err != nil {
|
||||
return oapi_usertitle, fmt.Errorf("mapUsertitle: %v", err)
|
||||
}
|
||||
|
|
@ -368,19 +357,26 @@ func (s Server) AddUserTitle(ctx context.Context, request oapi.AddUserTitleReque
|
|||
}
|
||||
|
||||
params := sqlc.InsertUserTitleParams{
|
||||
UserID: request.UserId,
|
||||
TitleID: request.Body.TitleId,
|
||||
Status: *status,
|
||||
Rate: request.Body.Rate,
|
||||
ReviewID: request.Body.ReviewId,
|
||||
UserID: request.UserId,
|
||||
TitleID: request.Body.TitleId,
|
||||
Status: *status,
|
||||
Rate: request.Body.Rate,
|
||||
}
|
||||
|
||||
user_title, err := s.db.InsertUserTitle(ctx, params)
|
||||
if err != nil {
|
||||
log.Errorf("%v", err)
|
||||
return oapi.AddUserTitle500Response{}, nil
|
||||
var pgErr *pgconn.PgError
|
||||
if errors.As(err, &pgErr) {
|
||||
// fmt.Println(pgErr.Message) // => syntax error at end of input
|
||||
// fmt.Println(pgErr.Code) // => 42601
|
||||
if pgErr.Code == "23505" { //duplicate key value
|
||||
return oapi.AddUserTitle409Response{}, nil
|
||||
}
|
||||
} else {
|
||||
log.Errorf("%v", err)
|
||||
return oapi.AddUserTitle500Response{}, nil
|
||||
}
|
||||
}
|
||||
|
||||
oapi_status, err := sql2usertitlestatus(user_title.Status)
|
||||
if err != nil {
|
||||
log.Errorf("%v", err)
|
||||
|
|
@ -406,3 +402,13 @@ func (s Server) AddUserTitle(ctx context.Context, request oapi.AddUserTitleReque
|
|||
|
||||
return oapi.AddUserTitle200JSONResponse(oapi_usertitle), nil
|
||||
}
|
||||
|
||||
// DeleteUserTitle implements oapi.StrictServerInterface.
|
||||
func (s Server) DeleteUserTitle(ctx context.Context, request oapi.DeleteUserTitleRequestObject) (oapi.DeleteUserTitleResponseObject, error) {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// UpdateUserTitle implements oapi.StrictServerInterface.
|
||||
func (s Server) UpdateUserTitle(ctx context.Context, request oapi.UpdateUserTitleRequestObject) (oapi.UpdateUserTitleResponseObject, error) {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -461,21 +461,13 @@ VALUES (
|
|||
)
|
||||
RETURNING user_id, title_id, status, rate, review_id, ctime;
|
||||
|
||||
-- -- name: UpdateUserTitle :one
|
||||
-- UPDATE usertitles
|
||||
-- SET
|
||||
-- status = COALESCE(sqlc.narg('status'), status),
|
||||
-- rate = COALESCE(sqlc.narg('rate'), rate),
|
||||
-- review_id = COALESCE(sqlc.narg('review_id'), review_id)
|
||||
-- WHERE user_id = $1 AND title_id = $2
|
||||
-- RETURNING *;
|
||||
|
||||
-- -- name: DeleteUserTitle :exec
|
||||
-- DELETE FROM usertitles
|
||||
-- WHERE user_id = $1 AND ($2::int IS NULL OR title_id = $2);
|
||||
|
||||
-- -- name: ListTags :many
|
||||
-- SELECT tag_id, tag_names
|
||||
-- FROM tags
|
||||
-- ORDER BY tag_id
|
||||
-- LIMIT $1 OFFSET $2;
|
||||
-- name: UpdateUserTitle :one
|
||||
-- Fails with sql.ErrNoRows if (user_id, title_id) not found
|
||||
UPDATE usertitles
|
||||
SET
|
||||
status = COALESCE(sqlc.narg('status')::usertitle_status_t, status),
|
||||
rate = COALESCE(sqlc.narg('rate')::int, rate)
|
||||
WHERE
|
||||
user_id = sqlc.arg('user_id')
|
||||
AND title_id = sqlc.arg('title_id')
|
||||
RETURNING *;
|
||||
Loading…
Add table
Add a link
Reference in a new issue