feat: AddUserTitle implemented
All checks were successful
Build and Deploy Go App / build (push) Successful in 5m23s
Build and Deploy Go App / deploy (push) Successful in 28s

This commit is contained in:
Iron_Felix 2025-11-24 09:34:05 +03:00
parent 15a681c622
commit 76df4d8592
10 changed files with 749 additions and 39 deletions

View file

@ -141,7 +141,82 @@ paths:
description: User not found
'500':
description: Unknown server error
'/users/{user_id}/titles/':
patch:
summary: Partially update a user account
description: |
Update selected user profile fields (excluding password).
Password updates must be done via the dedicated auth-service (`/auth/`).
Fields not provided in the request body remain unchanged.
operationId: updateUser
parameters:
- name: user_id
in: path
required: true
schema:
type: integer
format: int64
description: User ID (primary key)
example: 123
requestBody:
required: true
content:
application/json:
schema:
type: object
properties:
avatar_id:
type: integer
format: int64
nullable: true
description: ID of the user avatar (references `images.id`); set to `null` to remove avatar
example: 42
mail:
type: string
format: email
pattern: '^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\\.[a-zA-Z0-9_-]+$'
description: User email (must be unique and valid)
example: john.doe.updated@example.com
nickname:
type: string
pattern: '^[a-zA-Z0-9_-]{3,16}$'
description: 'Username (alphanumeric + `_` or `-`, 316 chars)'
maxLength: 16
minLength: 3
example: john_doe_43
disp_name:
type: string
description: Display name
maxLength: 32
example: John Smith
user_desc:
type: string
description: User description / bio
maxLength: 512
example: Just a curious developer.
additionalProperties: false
description: Only provided fields are updated. Omitted fields remain unchanged.
responses:
'200':
description: User updated successfully. Returns updated user representation (excluding sensitive fields).
content:
application/json:
schema:
$ref: '#/components/schemas/User'
'400':
description: 'Invalid input (e.g., validation failed, nickname/email conflict, malformed JSON)'
'401':
description: Unauthorized — missing or invalid authentication token
'403':
description: 'Forbidden — user is not allowed to modify this resource (e.g., not own profile & no admin rights)'
'404':
description: User not found
'409':
description: 'Conflict — e.g., requested `nickname` or `mail` already taken by another user'
'422':
description: 'Unprocessable Entity — semantic errors not caught by schema (e.g., invalid `avatar_id`)'
'500':
description: Unknown server error
'/users/{user_id}/titles':
get:
summary: Get user titles
parameters:
@ -231,6 +306,70 @@ paths:
description: Request params are not correct
'500':
description: Unknown server error
post:
summary: Add a title to a user
description: 'User adding title to list af watched, status required'
operationId: addUserTitle
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:
$ref: '#/components/schemas/UserTitle'
responses:
'200':
description: Title successfully added to user
content:
application/json:
schema:
type: object
properties:
data:
type: object
required:
- user_id
- title_id
- status
properties:
user_id:
type: integer
format: int64
title_id:
type: integer
format: int64
status:
$ref: '#/components/schemas/UserTitleStatus'
rate:
type: integer
format: int32
review_id:
type: integer
format: int64
ctime:
type: string
format: date-time
additionalProperties: false
'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 assign titles to this user
'404':
description: User or Title not found
'409':
description: Conflict — title already assigned to user (if applicable)
'500':
description: Internal server error
components:
parameters:
cursor:

View file

@ -181,6 +181,24 @@ type GetUsersUserIdParams struct {
Fields *string `form:"fields,omitempty" json:"fields,omitempty"`
}
// UpdateUserJSONBody defines parameters for UpdateUser.
type UpdateUserJSONBody struct {
// AvatarId ID of the user avatar (references `images.id`); set to `null` to remove avatar
AvatarId *int64 `json:"avatar_id"`
// DispName Display name
DispName *string `json:"disp_name,omitempty"`
// Mail User email (must be unique and valid)
Mail *openapi_types.Email `json:"mail,omitempty"`
// Nickname Username (alphanumeric + `_` or `-`, 316 chars)
Nickname *string `json:"nickname,omitempty"`
// UserDesc User description / bio
UserDesc *string `json:"user_desc,omitempty"`
}
// GetUsersUserIdTitlesParams defines parameters for GetUsersUserIdTitles.
type GetUsersUserIdTitlesParams struct {
Cursor *Cursor `form:"cursor,omitempty" json:"cursor,omitempty"`
@ -199,6 +217,12 @@ type GetUsersUserIdTitlesParams struct {
Fields *string `form:"fields,omitempty" json:"fields,omitempty"`
}
// UpdateUserJSONRequestBody defines body for UpdateUser for application/json ContentType.
type UpdateUserJSONRequestBody UpdateUserJSONBody
// AddUserTitleJSONRequestBody defines body for AddUserTitle for application/json ContentType.
type AddUserTitleJSONRequestBody = UserTitle
// Getter for additional properties for Title. Returns the specified
// element and whether it was found
func (a Title) Get(fieldName string) (value interface{}, found bool) {
@ -591,9 +615,15 @@ type ServerInterface interface {
// Get user info
// (GET /users/{user_id})
GetUsersUserId(c *gin.Context, userId string, params GetUsersUserIdParams)
// Partially update a user account
// (PATCH /users/{user_id})
UpdateUser(c *gin.Context, userId int64)
// Get user titles
// (GET /users/{user_id}/titles/)
// (GET /users/{user_id}/titles)
GetUsersUserIdTitles(c *gin.Context, userId string, params GetUsersUserIdTitlesParams)
// Add a title to a user
// (POST /users/{user_id}/titles)
AddUserTitle(c *gin.Context, userId int64)
}
// ServerInterfaceWrapper converts contexts to parameters.
@ -781,6 +811,30 @@ func (siw *ServerInterfaceWrapper) GetUsersUserId(c *gin.Context) {
siw.Handler.GetUsersUserId(c, userId, params)
}
// UpdateUser operation middleware
func (siw *ServerInterfaceWrapper) UpdateUser(c *gin.Context) {
var err error
// ------------- Path parameter "user_id" -------------
var userId int64
err = runtime.BindStyledParameterWithOptions("simple", "user_id", c.Param("user_id"), &userId, runtime.BindStyledParameterOptions{Explode: false, Required: true})
if err != nil {
siw.ErrorHandler(c, fmt.Errorf("Invalid format for parameter user_id: %w", err), http.StatusBadRequest)
return
}
for _, middleware := range siw.HandlerMiddlewares {
middleware(c)
if c.IsAborted() {
return
}
}
siw.Handler.UpdateUser(c, userId)
}
// GetUsersUserIdTitles operation middleware
func (siw *ServerInterfaceWrapper) GetUsersUserIdTitles(c *gin.Context) {
@ -904,6 +958,30 @@ func (siw *ServerInterfaceWrapper) GetUsersUserIdTitles(c *gin.Context) {
siw.Handler.GetUsersUserIdTitles(c, userId, params)
}
// AddUserTitle operation middleware
func (siw *ServerInterfaceWrapper) AddUserTitle(c *gin.Context) {
var err error
// ------------- Path parameter "user_id" -------------
var userId int64
err = runtime.BindStyledParameterWithOptions("simple", "user_id", c.Param("user_id"), &userId, runtime.BindStyledParameterOptions{Explode: false, Required: true})
if err != nil {
siw.ErrorHandler(c, fmt.Errorf("Invalid format for parameter user_id: %w", err), http.StatusBadRequest)
return
}
for _, middleware := range siw.HandlerMiddlewares {
middleware(c)
if c.IsAborted() {
return
}
}
siw.Handler.AddUserTitle(c, userId)
}
// GinServerOptions provides options for the Gin server.
type GinServerOptions struct {
BaseURL string
@ -934,7 +1012,9 @@ func RegisterHandlersWithOptions(router gin.IRouter, si ServerInterface, options
router.GET(options.BaseURL+"/titles", wrapper.GetTitles)
router.GET(options.BaseURL+"/titles/:title_id", wrapper.GetTitlesTitleId)
router.GET(options.BaseURL+"/users/:user_id", wrapper.GetUsersUserId)
router.GET(options.BaseURL+"/users/:user_id/titles/", wrapper.GetUsersUserIdTitles)
router.PATCH(options.BaseURL+"/users/:user_id", wrapper.UpdateUser)
router.GET(options.BaseURL+"/users/:user_id/titles", wrapper.GetUsersUserIdTitles)
router.POST(options.BaseURL+"/users/:user_id/titles", wrapper.AddUserTitle)
}
type GetTitlesRequestObject struct {
@ -1075,6 +1155,80 @@ func (response GetUsersUserId500Response) VisitGetUsersUserIdResponse(w http.Res
return nil
}
type UpdateUserRequestObject struct {
UserId int64 `json:"user_id"`
Body *UpdateUserJSONRequestBody
}
type UpdateUserResponseObject interface {
VisitUpdateUserResponse(w http.ResponseWriter) error
}
type UpdateUser200JSONResponse User
func (response UpdateUser200JSONResponse) VisitUpdateUserResponse(w http.ResponseWriter) error {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
return json.NewEncoder(w).Encode(response)
}
type UpdateUser400Response struct {
}
func (response UpdateUser400Response) VisitUpdateUserResponse(w http.ResponseWriter) error {
w.WriteHeader(400)
return nil
}
type UpdateUser401Response struct {
}
func (response UpdateUser401Response) VisitUpdateUserResponse(w http.ResponseWriter) error {
w.WriteHeader(401)
return nil
}
type UpdateUser403Response struct {
}
func (response UpdateUser403Response) VisitUpdateUserResponse(w http.ResponseWriter) error {
w.WriteHeader(403)
return nil
}
type UpdateUser404Response struct {
}
func (response UpdateUser404Response) VisitUpdateUserResponse(w http.ResponseWriter) error {
w.WriteHeader(404)
return nil
}
type UpdateUser409Response struct {
}
func (response UpdateUser409Response) VisitUpdateUserResponse(w http.ResponseWriter) error {
w.WriteHeader(409)
return nil
}
type UpdateUser422Response struct {
}
func (response UpdateUser422Response) VisitUpdateUserResponse(w http.ResponseWriter) error {
w.WriteHeader(422)
return nil
}
type UpdateUser500Response struct {
}
func (response UpdateUser500Response) VisitUpdateUserResponse(w http.ResponseWriter) error {
w.WriteHeader(500)
return nil
}
type GetUsersUserIdTitlesRequestObject struct {
UserId string `json:"user_id"`
Params GetUsersUserIdTitlesParams
@ -1120,6 +1274,83 @@ func (response GetUsersUserIdTitles500Response) VisitGetUsersUserIdTitlesRespons
return nil
}
type AddUserTitleRequestObject struct {
UserId int64 `json:"user_id"`
Body *AddUserTitleJSONRequestBody
}
type AddUserTitleResponseObject interface {
VisitAddUserTitleResponse(w http.ResponseWriter) error
}
type AddUserTitle200JSONResponse struct {
Data *struct {
Ctime *time.Time `json:"ctime,omitempty"`
Rate *int32 `json:"rate,omitempty"`
ReviewId *int64 `json:"review_id,omitempty"`
// Status User's title status
Status UserTitleStatus `json:"status"`
TitleId int64 `json:"title_id"`
UserId int64 `json:"user_id"`
} `json:"data,omitempty"`
}
func (response AddUserTitle200JSONResponse) VisitAddUserTitleResponse(w http.ResponseWriter) error {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
return json.NewEncoder(w).Encode(response)
}
type AddUserTitle400Response struct {
}
func (response AddUserTitle400Response) VisitAddUserTitleResponse(w http.ResponseWriter) error {
w.WriteHeader(400)
return nil
}
type AddUserTitle401Response struct {
}
func (response AddUserTitle401Response) VisitAddUserTitleResponse(w http.ResponseWriter) error {
w.WriteHeader(401)
return nil
}
type AddUserTitle403Response struct {
}
func (response AddUserTitle403Response) VisitAddUserTitleResponse(w http.ResponseWriter) error {
w.WriteHeader(403)
return nil
}
type AddUserTitle404Response struct {
}
func (response AddUserTitle404Response) VisitAddUserTitleResponse(w http.ResponseWriter) error {
w.WriteHeader(404)
return nil
}
type AddUserTitle409Response struct {
}
func (response AddUserTitle409Response) VisitAddUserTitleResponse(w http.ResponseWriter) error {
w.WriteHeader(409)
return nil
}
type AddUserTitle500Response struct {
}
func (response AddUserTitle500Response) VisitAddUserTitleResponse(w http.ResponseWriter) error {
w.WriteHeader(500)
return nil
}
// StrictServerInterface represents all server handlers.
type StrictServerInterface interface {
// Get titles
@ -1131,9 +1362,15 @@ type StrictServerInterface interface {
// Get user info
// (GET /users/{user_id})
GetUsersUserId(ctx context.Context, request GetUsersUserIdRequestObject) (GetUsersUserIdResponseObject, error)
// Partially update a user account
// (PATCH /users/{user_id})
UpdateUser(ctx context.Context, request UpdateUserRequestObject) (UpdateUserResponseObject, error)
// Get user titles
// (GET /users/{user_id}/titles/)
// (GET /users/{user_id}/titles)
GetUsersUserIdTitles(ctx context.Context, request GetUsersUserIdTitlesRequestObject) (GetUsersUserIdTitlesResponseObject, error)
// Add a title to a user
// (POST /users/{user_id}/titles)
AddUserTitle(ctx context.Context, request AddUserTitleRequestObject) (AddUserTitleResponseObject, error)
}
type StrictHandlerFunc = strictgin.StrictGinHandlerFunc
@ -1231,6 +1468,41 @@ func (sh *strictHandler) GetUsersUserId(ctx *gin.Context, userId string, params
}
}
// UpdateUser operation middleware
func (sh *strictHandler) UpdateUser(ctx *gin.Context, userId int64) {
var request UpdateUserRequestObject
request.UserId = userId
var body UpdateUserJSONRequestBody
if err := ctx.ShouldBindJSON(&body); err != nil {
ctx.Status(http.StatusBadRequest)
ctx.Error(err)
return
}
request.Body = &body
handler := func(ctx *gin.Context, request interface{}) (interface{}, error) {
return sh.ssi.UpdateUser(ctx, request.(UpdateUserRequestObject))
}
for _, middleware := range sh.middlewares {
handler = middleware(handler, "UpdateUser")
}
response, err := handler(ctx, request)
if err != nil {
ctx.Error(err)
ctx.Status(http.StatusInternalServerError)
} else if validResponse, ok := response.(UpdateUserResponseObject); ok {
if err := validResponse.VisitUpdateUserResponse(ctx.Writer); err != nil {
ctx.Error(err)
}
} else if response != nil {
ctx.Error(fmt.Errorf("unexpected response type: %T", response))
}
}
// GetUsersUserIdTitles operation middleware
func (sh *strictHandler) GetUsersUserIdTitles(ctx *gin.Context, userId string, params GetUsersUserIdTitlesParams) {
var request GetUsersUserIdTitlesRequestObject
@ -1258,3 +1530,38 @@ func (sh *strictHandler) GetUsersUserIdTitles(ctx *gin.Context, userId string, p
ctx.Error(fmt.Errorf("unexpected response type: %T", response))
}
}
// AddUserTitle operation middleware
func (sh *strictHandler) AddUserTitle(ctx *gin.Context, userId int64) {
var request AddUserTitleRequestObject
request.UserId = userId
var body AddUserTitleJSONRequestBody
if err := ctx.ShouldBindJSON(&body); err != nil {
ctx.Status(http.StatusBadRequest)
ctx.Error(err)
return
}
request.Body = &body
handler := func(ctx *gin.Context, request interface{}) (interface{}, error) {
return sh.ssi.AddUserTitle(ctx, request.(AddUserTitleRequestObject))
}
for _, middleware := range sh.middlewares {
handler = middleware(handler, "AddUserTitle")
}
response, err := handler(ctx, request)
if err != nil {
ctx.Error(err)
ctx.Status(http.StatusInternalServerError)
} else if validResponse, ok := response.(AddUserTitleResponseObject); ok {
if err := validResponse.VisitAddUserTitleResponse(ctx.Writer); err != nil {
ctx.Error(err)
}
} else if response != nil {
ctx.Error(fmt.Errorf("unexpected response type: %T", response))
}
}

View file

@ -13,8 +13,9 @@ paths:
$ref: "./paths/titles-id.yaml"
/users/{user_id}:
$ref: "./paths/users-id.yaml"
/users/{user_id}/titles/:
/users/{user_id}/titles:
$ref: "./paths/users-id-titles.yaml"
components:
parameters:
$ref: "./parameters/_index.yaml"

View file

@ -87,3 +87,55 @@ get:
description: Request params are not correct
'500':
description: Unknown server error
post:
summary: Add a title to a user
description: User adding title to list af watched, status required
operationId: addUserTitle
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:
$ref: '../schemas/UserTitle.yaml'
responses:
'200':
description: Title successfully added to user
content:
application/json:
schema:
type: object
properties:
# success:
# type: boolean
# example: true
# error:
# type: string
# nullable: true
# example: null
data:
$ref: '../schemas/UserTitleMini.yaml'
# required:
# - success
# - error
'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 assign titles to this user
'404':
description: User or Title not found
'409':
description: Conflict — title already assigned to user (if applicable)
'500':
description: Internal server error

View file

@ -0,0 +1,24 @@
type: object
required:
- user_id
- title_id
- status
properties:
user_id:
type: integer
format: int64
title_id:
type: integer
format: int64
status:
$ref: ./enums/UserTitleStatus.yaml
rate:
type: integer
format: int32
review_id:
type: integer
format: int64
ctime:
type: string
format: date-time
additionalProperties: false

View file

@ -0,0 +1,26 @@
type: object
properties:
avatar_id:
type: integer
format: int64
nullable: true
description: ID of the user avatar (references `images.id`); set to `null` to remove avatar
example: 42
mail:
type: string
format: email
pattern: '^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\\.[a-zA-Z0-9_-]+$'
description: User email (must be unique and valid)
example: john.doe.updated@example.com
disp_name:
type: string
description: Display name
maxLength: 32
example: John Smith
user_desc:
type: string
description: User description / bio
maxLength: 512
example: Just a curious developer.
additionalProperties: false
description: Only provided fields are updated. Omitted fields remain unchanged.