feat: patch usertitle described
All checks were successful
Build and Deploy Go App / build (push) Successful in 5m48s
Build and Deploy Go App / deploy (push) Successful in 29s

This commit is contained in:
Iron_Felix 2025-11-27 03:46:40 +03:00
parent 759679990a
commit cb9fba6fbc
4 changed files with 133 additions and 214 deletions

View file

@ -328,13 +328,9 @@ paths:
schema: schema:
type: object type: object
required: required:
- user_id
- title_id - title_id
- status - status
properties: properties:
user_id:
type: integer
format: int64
title_id: title_id:
type: integer type: integer
format: int64 format: int64
@ -343,21 +339,12 @@ paths:
rate: rate:
type: integer type: integer
format: int32 format: int32
review_id:
type: integer
format: int64
ctime:
type: string
format: date-time
responses: responses:
'200': '200':
description: Title successfully added to user description: Title successfully added to user
content: content:
application/json: application/json:
schema: schema:
type: object
properties:
data:
type: object type: object
required: required:
- user_id - user_id
@ -394,6 +381,53 @@ paths:
description: Conflict — title already assigned to user (if applicable) description: Conflict — title already assigned to user (if applicable)
'500': '500':
description: Internal server error description: Internal server error
patch:
summary: Update a usertitle
description: User updating title list of watched
operationId: updateUserTitle
parameters:
- name: user_id
in: path
required: true
schema:
type: integer
format: int64
description: ID of the user to assign the title to
example: 123
requestBody:
required: true
content:
application/json:
schema:
type: object
required:
- title_id
properties:
title_id:
type: integer
format: int64
status:
$ref: '#/components/schemas/UserTitleStatus'
rate:
type: integer
format: int32
responses:
'200':
description: Title successfully updated
content:
application/json:
schema:
$ref: '#/paths/~1users~1%7Buser_id%7D~1titles/post/responses/200/content/application~1json/schema'
'400':
description: 'Invalid request body (missing fields, invalid types, etc.)'
'401':
description: Unauthorized — missing or invalid auth token
'403':
description: Forbidden — user not allowed to update title
'404':
description: User or Title not found
'500':
description: Internal server error
components: components:
parameters: parameters:
cursor: cursor:
@ -629,4 +663,3 @@ components:
ctime: ctime:
type: string type: string
format: date-time format: date-time
additionalProperties: true

View file

@ -154,7 +154,6 @@ type UserTitle struct {
Status UserTitleStatus `json:"status"` Status UserTitleStatus `json:"status"`
Title *Title `json:"title,omitempty"` Title *Title `json:"title,omitempty"`
UserId int64 `json:"user_id"` UserId int64 `json:"user_id"`
AdditionalProperties map[string]interface{} `json:"-"`
} }
// UserTitleStatus User's title status // UserTitleStatus User's title status
@ -486,145 +485,6 @@ func (a Title) MarshalJSON() ([]byte, error) {
return json.Marshal(object) return json.Marshal(object)
} }
// Getter for additional properties for UserTitle. Returns the specified
// element and whether it was found
func (a UserTitle) Get(fieldName string) (value interface{}, found bool) {
if a.AdditionalProperties != nil {
value, found = a.AdditionalProperties[fieldName]
}
return
}
// Setter for additional properties for UserTitle
func (a *UserTitle) Set(fieldName string, value interface{}) {
if a.AdditionalProperties == nil {
a.AdditionalProperties = make(map[string]interface{})
}
a.AdditionalProperties[fieldName] = value
}
// Override default JSON handling for UserTitle to handle AdditionalProperties
func (a *UserTitle) UnmarshalJSON(b []byte) error {
object := make(map[string]json.RawMessage)
err := json.Unmarshal(b, &object)
if err != nil {
return err
}
if raw, found := object["ctime"]; found {
err = json.Unmarshal(raw, &a.Ctime)
if err != nil {
return fmt.Errorf("error reading 'ctime': %w", err)
}
delete(object, "ctime")
}
if raw, found := object["rate"]; found {
err = json.Unmarshal(raw, &a.Rate)
if err != nil {
return fmt.Errorf("error reading 'rate': %w", err)
}
delete(object, "rate")
}
if raw, found := object["review_id"]; found {
err = json.Unmarshal(raw, &a.ReviewId)
if err != nil {
return fmt.Errorf("error reading 'review_id': %w", err)
}
delete(object, "review_id")
}
if raw, found := object["status"]; found {
err = json.Unmarshal(raw, &a.Status)
if err != nil {
return fmt.Errorf("error reading 'status': %w", err)
}
delete(object, "status")
}
if raw, found := object["title"]; found {
err = json.Unmarshal(raw, &a.Title)
if err != nil {
return fmt.Errorf("error reading 'title': %w", err)
}
delete(object, "title")
}
if raw, found := object["user_id"]; found {
err = json.Unmarshal(raw, &a.UserId)
if err != nil {
return fmt.Errorf("error reading 'user_id': %w", err)
}
delete(object, "user_id")
}
if len(object) != 0 {
a.AdditionalProperties = make(map[string]interface{})
for fieldName, fieldBuf := range object {
var fieldVal interface{}
err := json.Unmarshal(fieldBuf, &fieldVal)
if err != nil {
return fmt.Errorf("error unmarshaling field %s: %w", fieldName, err)
}
a.AdditionalProperties[fieldName] = fieldVal
}
}
return nil
}
// Override default JSON handling for UserTitle to handle AdditionalProperties
func (a UserTitle) MarshalJSON() ([]byte, error) {
var err error
object := make(map[string]json.RawMessage)
if a.Ctime != nil {
object["ctime"], err = json.Marshal(a.Ctime)
if err != nil {
return nil, fmt.Errorf("error marshaling 'ctime': %w", err)
}
}
if a.Rate != nil {
object["rate"], err = json.Marshal(a.Rate)
if err != nil {
return nil, fmt.Errorf("error marshaling 'rate': %w", err)
}
}
if a.ReviewId != nil {
object["review_id"], err = json.Marshal(a.ReviewId)
if err != nil {
return nil, fmt.Errorf("error marshaling 'review_id': %w", err)
}
}
object["status"], err = json.Marshal(a.Status)
if err != nil {
return nil, fmt.Errorf("error marshaling 'status': %w", err)
}
if a.Title != nil {
object["title"], err = json.Marshal(a.Title)
if err != nil {
return nil, fmt.Errorf("error marshaling 'title': %w", err)
}
}
object["user_id"], err = json.Marshal(a.UserId)
if err != nil {
return nil, fmt.Errorf("error marshaling 'user_id': %w", err)
}
for fieldName, field := range a.AdditionalProperties {
object[fieldName], err = json.Marshal(field)
if err != nil {
return nil, fmt.Errorf("error marshaling '%s': %w", fieldName, err)
}
}
return json.Marshal(object)
}
// ServerInterface represents all server handlers. // ServerInterface represents all server handlers.
type ServerInterface interface { type ServerInterface interface {
// Get titles // Get titles
@ -1313,7 +1173,6 @@ type AddUserTitleResponseObject interface {
} }
type AddUserTitle200JSONResponse struct { type AddUserTitle200JSONResponse struct {
Data *struct {
Ctime *time.Time `json:"ctime,omitempty"` Ctime *time.Time `json:"ctime,omitempty"`
Rate *int32 `json:"rate,omitempty"` Rate *int32 `json:"rate,omitempty"`
ReviewId *int64 `json:"review_id,omitempty"` ReviewId *int64 `json:"review_id,omitempty"`
@ -1322,7 +1181,6 @@ type AddUserTitle200JSONResponse struct {
Status UserTitleStatus `json:"status"` Status UserTitleStatus `json:"status"`
TitleId int64 `json:"title_id"` TitleId int64 `json:"title_id"`
UserId int64 `json:"user_id"` UserId int64 `json:"user_id"`
} `json:"data,omitempty"`
} }
func (response AddUserTitle200JSONResponse) VisitAddUserTitleResponse(w http.ResponseWriter) error { func (response AddUserTitle200JSONResponse) VisitAddUserTitleResponse(w http.ResponseWriter) error {

View file

@ -110,13 +110,9 @@ post:
schema: schema:
type: object type: object
required: required:
- user_id
- title_id - title_id
- status - status
properties: properties:
user_id:
type: integer
format: int64
title_id: title_id:
type: integer type: integer
format: int64 format: int64
@ -125,12 +121,6 @@ post:
rate: rate:
type: integer type: integer
format: int32 format: int32
review_id:
type: integer
format: int64
ctime:
type: string
format: date-time
responses: responses:
'200': '200':
@ -138,20 +128,8 @@ post:
content: content:
application/json: application/json:
schema: schema:
type: object
properties:
# success:
# type: boolean
# example: true
# error:
# type: string
# nullable: true
# example: null
data:
$ref: '../schemas/UserTitleMini.yaml' $ref: '../schemas/UserTitleMini.yaml'
# required:
# - success
# - error
'400': '400':
description: Invalid request body (missing fields, invalid types, etc.) description: Invalid request body (missing fields, invalid types, etc.)
'401': '401':
@ -164,3 +142,53 @@ post:
description: Conflict — title already assigned to user (if applicable) description: Conflict — title already assigned to user (if applicable)
'500': '500':
description: Internal server error description: Internal server error
patch:
summary: Update a usertitle
description: User updating title list of watched
operationId: updateUserTitle
parameters:
- name: user_id
in: path
required: true
schema:
type: integer
format: int64
description: ID of the user to assign the title to
example: 123
requestBody:
required: true
content:
application/json:
schema:
type: object
required:
- title_id
properties:
title_id:
type: integer
format: int64
status:
$ref: ../schemas/enums/UserTitleStatus.yaml
rate:
type: integer
format: int32
responses:
'200':
description: Title successfully updated
content:
application/json:
schema:
$ref: '../schemas/UserTitleMini.yaml'
'400':
description: Invalid request body (missing fields, invalid types, etc.)
'401':
description: Unauthorized — missing or invalid auth token
'403':
description: Forbidden — user not allowed to update title
'404':
description: User or Title not found
'500':
description: Internal server error

View file

@ -360,7 +360,7 @@ func (s Server) UpdateUser(ctx context.Context, request oapi.UpdateUserRequestOb
} }
func (s Server) AddUserTitle(ctx context.Context, request oapi.AddUserTitleRequestObject) (oapi.AddUserTitleResponseObject, error) { func (s Server) AddUserTitle(ctx context.Context, request oapi.AddUserTitleRequestObject) (oapi.AddUserTitleResponseObject, error) {
//TODO: add review if exists
status, err := UserTitleStatus2Sqlc1(&request.Body.Status) status, err := UserTitleStatus2Sqlc1(&request.Body.Status)
if err != nil { if err != nil {
log.Errorf("%v", err) log.Errorf("%v", err)
@ -404,5 +404,5 @@ func (s Server) AddUserTitle(ctx context.Context, request oapi.AddUserTitleReque
UserId: user_title.UserID, UserId: user_title.UserID,
} }
return oapi.AddUserTitle200JSONResponse{Data: &oapi_usertitle}, nil return oapi.AddUserTitle200JSONResponse(oapi_usertitle), nil
} }