fix: now 409 on try to add existing usertitle
This commit is contained in:
parent
e0a68d7d0f
commit
f2589e05e8
3 changed files with 33 additions and 28 deletions
|
|
@ -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")
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue