feat: implemented /users/{id} api route
This commit is contained in:
parent
71e2661fb9
commit
948e036e8c
10 changed files with 381 additions and 982 deletions
123
api/api.gen.go
123
api/api.gen.go
|
|
@ -8,28 +8,48 @@ import (
|
|||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/oapi-codegen/runtime"
|
||||
strictgin "github.com/oapi-codegen/runtime/strictmiddleware/gin"
|
||||
openapi_types "github.com/oapi-codegen/runtime/types"
|
||||
)
|
||||
|
||||
// Title defines model for Title.
|
||||
type Title map[string]interface{}
|
||||
// User defines model for User.
|
||||
type User struct {
|
||||
// AvatarId ID of the user avatar (references images table)
|
||||
AvatarId *int64 `json:"avatar_id"`
|
||||
|
||||
// GetTitleParams defines parameters for GetTitle.
|
||||
type GetTitleParams struct {
|
||||
Query *string `form:"query,omitempty" json:"query,omitempty"`
|
||||
Limit *int `form:"limit,omitempty" json:"limit,omitempty"`
|
||||
Offset *int `form:"offset,omitempty" json:"offset,omitempty"`
|
||||
// CreationDate Timestamp when the user was created
|
||||
CreationDate time.Time `json:"creation_date"`
|
||||
|
||||
// DispName Display name
|
||||
DispName *string `json:"disp_name,omitempty"`
|
||||
|
||||
// Id Unique user ID (primary key)
|
||||
Id *int64 `json:"id,omitempty"`
|
||||
|
||||
// Mail User email
|
||||
Mail *openapi_types.Email `json:"mail,omitempty"`
|
||||
|
||||
// Nickname Username (alphanumeric + _ or -)
|
||||
Nickname string `json:"nickname"`
|
||||
|
||||
// UserDesc User description
|
||||
UserDesc *string `json:"user_desc,omitempty"`
|
||||
}
|
||||
|
||||
// GetUsersUserIdParams defines parameters for GetUsersUserId.
|
||||
type GetUsersUserIdParams struct {
|
||||
Fields *string `form:"fields,omitempty" json:"fields,omitempty"`
|
||||
}
|
||||
|
||||
// ServerInterface represents all server handlers.
|
||||
type ServerInterface interface {
|
||||
// Get titles
|
||||
// (GET /title)
|
||||
GetTitle(c *gin.Context, params GetTitleParams)
|
||||
// Get user info
|
||||
// (GET /users/{user_id})
|
||||
GetUsersUserId(c *gin.Context, userId string, params GetUsersUserIdParams)
|
||||
}
|
||||
|
||||
// ServerInterfaceWrapper converts contexts to parameters.
|
||||
|
|
@ -41,37 +61,22 @@ type ServerInterfaceWrapper struct {
|
|||
|
||||
type MiddlewareFunc func(c *gin.Context)
|
||||
|
||||
// GetTitle operation middleware
|
||||
func (siw *ServerInterfaceWrapper) GetTitle(c *gin.Context) {
|
||||
// GetUsersUserId operation middleware
|
||||
func (siw *ServerInterfaceWrapper) GetUsersUserId(c *gin.Context) {
|
||||
|
||||
var err error
|
||||
|
||||
// ------------- Path parameter "user_id" -------------
|
||||
var userId string
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
// Parameter object where we will unmarshal all parameters from the context
|
||||
var params GetTitleParams
|
||||
|
||||
// ------------- Optional query parameter "query" -------------
|
||||
|
||||
err = runtime.BindQueryParameter("form", true, false, "query", c.Request.URL.Query(), ¶ms.Query)
|
||||
if err != nil {
|
||||
siw.ErrorHandler(c, fmt.Errorf("Invalid format for parameter query: %w", err), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// ------------- Optional query parameter "limit" -------------
|
||||
|
||||
err = runtime.BindQueryParameter("form", true, false, "limit", c.Request.URL.Query(), ¶ms.Limit)
|
||||
if err != nil {
|
||||
siw.ErrorHandler(c, fmt.Errorf("Invalid format for parameter limit: %w", err), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// ------------- Optional query parameter "offset" -------------
|
||||
|
||||
err = runtime.BindQueryParameter("form", true, false, "offset", c.Request.URL.Query(), ¶ms.Offset)
|
||||
if err != nil {
|
||||
siw.ErrorHandler(c, fmt.Errorf("Invalid format for parameter offset: %w", err), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
var params GetUsersUserIdParams
|
||||
|
||||
// ------------- Optional query parameter "fields" -------------
|
||||
|
||||
|
|
@ -88,7 +93,7 @@ func (siw *ServerInterfaceWrapper) GetTitle(c *gin.Context) {
|
|||
}
|
||||
}
|
||||
|
||||
siw.Handler.GetTitle(c, params)
|
||||
siw.Handler.GetUsersUserId(c, userId, params)
|
||||
}
|
||||
|
||||
// GinServerOptions provides options for the Gin server.
|
||||
|
|
@ -118,39 +123,40 @@ func RegisterHandlersWithOptions(router gin.IRouter, si ServerInterface, options
|
|||
ErrorHandler: errorHandler,
|
||||
}
|
||||
|
||||
router.GET(options.BaseURL+"/title", wrapper.GetTitle)
|
||||
router.GET(options.BaseURL+"/users/:user_id", wrapper.GetUsersUserId)
|
||||
}
|
||||
|
||||
type GetTitleRequestObject struct {
|
||||
Params GetTitleParams
|
||||
type GetUsersUserIdRequestObject struct {
|
||||
UserId string `json:"user_id"`
|
||||
Params GetUsersUserIdParams
|
||||
}
|
||||
|
||||
type GetTitleResponseObject interface {
|
||||
VisitGetTitleResponse(w http.ResponseWriter) error
|
||||
type GetUsersUserIdResponseObject interface {
|
||||
VisitGetUsersUserIdResponse(w http.ResponseWriter) error
|
||||
}
|
||||
|
||||
type GetTitle200JSONResponse []Title
|
||||
type GetUsersUserId200JSONResponse User
|
||||
|
||||
func (response GetTitle200JSONResponse) VisitGetTitleResponse(w http.ResponseWriter) error {
|
||||
func (response GetUsersUserId200JSONResponse) VisitGetUsersUserIdResponse(w http.ResponseWriter) error {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(200)
|
||||
|
||||
return json.NewEncoder(w).Encode(response)
|
||||
}
|
||||
|
||||
type GetTitle204Response struct {
|
||||
type GetUsersUserId404Response struct {
|
||||
}
|
||||
|
||||
func (response GetTitle204Response) VisitGetTitleResponse(w http.ResponseWriter) error {
|
||||
w.WriteHeader(204)
|
||||
func (response GetUsersUserId404Response) VisitGetUsersUserIdResponse(w http.ResponseWriter) error {
|
||||
w.WriteHeader(404)
|
||||
return nil
|
||||
}
|
||||
|
||||
// StrictServerInterface represents all server handlers.
|
||||
type StrictServerInterface interface {
|
||||
// Get titles
|
||||
// (GET /title)
|
||||
GetTitle(ctx context.Context, request GetTitleRequestObject) (GetTitleResponseObject, error)
|
||||
// Get user info
|
||||
// (GET /users/{user_id})
|
||||
GetUsersUserId(ctx context.Context, request GetUsersUserIdRequestObject) (GetUsersUserIdResponseObject, error)
|
||||
}
|
||||
|
||||
type StrictHandlerFunc = strictgin.StrictGinHandlerFunc
|
||||
|
|
@ -165,17 +171,18 @@ type strictHandler struct {
|
|||
middlewares []StrictMiddlewareFunc
|
||||
}
|
||||
|
||||
// GetTitle operation middleware
|
||||
func (sh *strictHandler) GetTitle(ctx *gin.Context, params GetTitleParams) {
|
||||
var request GetTitleRequestObject
|
||||
// GetUsersUserId operation middleware
|
||||
func (sh *strictHandler) GetUsersUserId(ctx *gin.Context, userId string, params GetUsersUserIdParams) {
|
||||
var request GetUsersUserIdRequestObject
|
||||
|
||||
request.UserId = userId
|
||||
request.Params = params
|
||||
|
||||
handler := func(ctx *gin.Context, request interface{}) (interface{}, error) {
|
||||
return sh.ssi.GetTitle(ctx, request.(GetTitleRequestObject))
|
||||
return sh.ssi.GetUsersUserId(ctx, request.(GetUsersUserIdRequestObject))
|
||||
}
|
||||
for _, middleware := range sh.middlewares {
|
||||
handler = middleware(handler, "GetTitle")
|
||||
handler = middleware(handler, "GetUsersUserId")
|
||||
}
|
||||
|
||||
response, err := handler(ctx, request)
|
||||
|
|
@ -183,8 +190,8 @@ func (sh *strictHandler) GetTitle(ctx *gin.Context, params GetTitleParams) {
|
|||
if err != nil {
|
||||
ctx.Error(err)
|
||||
ctx.Status(http.StatusInternalServerError)
|
||||
} else if validResponse, ok := response.(GetTitleResponseObject); ok {
|
||||
if err := validResponse.VisitGetTitleResponse(ctx.Writer); err != nil {
|
||||
} else if validResponse, ok := response.(GetUsersUserIdResponseObject); ok {
|
||||
if err := validResponse.VisitGetUsersUserIdResponse(ctx.Writer); err != nil {
|
||||
ctx.Error(err)
|
||||
}
|
||||
} else if response != nil {
|
||||
|
|
|
|||
298
api/openapi.yaml
298
api/openapi.yaml
|
|
@ -5,40 +5,40 @@ info:
|
|||
servers:
|
||||
- url: https://api.example.com
|
||||
paths:
|
||||
/title:
|
||||
get:
|
||||
summary: Get titles
|
||||
parameters:
|
||||
- in: query
|
||||
name: query
|
||||
schema:
|
||||
type: string
|
||||
- in: query
|
||||
name: limit
|
||||
schema:
|
||||
type: integer
|
||||
default: 10
|
||||
- in: query
|
||||
name: offset
|
||||
schema:
|
||||
type: integer
|
||||
default: 0
|
||||
- in: query
|
||||
name: fields
|
||||
schema:
|
||||
type: string
|
||||
default: all
|
||||
responses:
|
||||
'200':
|
||||
description: List of titles
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: array
|
||||
items:
|
||||
$ref: '#/components/schemas/Title'
|
||||
'204':
|
||||
description: No titles found
|
||||
# /title:
|
||||
# get:
|
||||
# summary: Get titles
|
||||
# parameters:
|
||||
# - in: query
|
||||
# name: query
|
||||
# schema:
|
||||
# type: string
|
||||
# - in: query
|
||||
# name: limit
|
||||
# schema:
|
||||
# type: integer
|
||||
# default: 10
|
||||
# - in: query
|
||||
# name: offset
|
||||
# schema:
|
||||
# type: integer
|
||||
# default: 0
|
||||
# - in: query
|
||||
# name: fields
|
||||
# schema:
|
||||
# type: string
|
||||
# default: all
|
||||
# responses:
|
||||
# '200':
|
||||
# description: List of titles
|
||||
# content:
|
||||
# application/json:
|
||||
# schema:
|
||||
# type: array
|
||||
# items:
|
||||
# $ref: '#/components/schemas/Title'
|
||||
# '204':
|
||||
# description: No titles found
|
||||
|
||||
# /title/{title_id}:
|
||||
# get:
|
||||
|
|
@ -124,122 +124,122 @@ paths:
|
|||
# '204':
|
||||
# description: No reviews found
|
||||
|
||||
# /users/{user_id}:
|
||||
# get:
|
||||
# summary: Get user info
|
||||
# parameters:
|
||||
# - in: path
|
||||
# name: user_id
|
||||
# required: true
|
||||
# schema:
|
||||
# type: string
|
||||
# - in: query
|
||||
# name: fields
|
||||
# schema:
|
||||
# type: string
|
||||
# default: all
|
||||
# responses:
|
||||
# '200':
|
||||
# description: User info
|
||||
# content:
|
||||
# application/json:
|
||||
# schema:
|
||||
# $ref: '#/components/schemas/User'
|
||||
# '404':
|
||||
# description: User not found
|
||||
/users/{user_id}:
|
||||
get:
|
||||
summary: Get user info
|
||||
parameters:
|
||||
- in: path
|
||||
name: user_id
|
||||
required: true
|
||||
schema:
|
||||
type: string
|
||||
- in: query
|
||||
name: fields
|
||||
schema:
|
||||
type: string
|
||||
default: all
|
||||
responses:
|
||||
'200':
|
||||
description: User info
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/User'
|
||||
'404':
|
||||
description: User not found
|
||||
|
||||
# patch:
|
||||
# summary: Update user
|
||||
# parameters:
|
||||
# - in: path
|
||||
# name: user_id
|
||||
# required: true
|
||||
# schema:
|
||||
# type: string
|
||||
# requestBody:
|
||||
# required: true
|
||||
# content:
|
||||
# application/json:
|
||||
# schema:
|
||||
# $ref: '#/components/schemas/User'
|
||||
# responses:
|
||||
# '200':
|
||||
# description: Update result
|
||||
# content:
|
||||
# application/json:
|
||||
# schema:
|
||||
# type: object
|
||||
# properties:
|
||||
# success:
|
||||
# type: boolean
|
||||
# error:
|
||||
# type: string
|
||||
# patch:
|
||||
# summary: Update user
|
||||
# parameters:
|
||||
# - in: path
|
||||
# name: user_id
|
||||
# required: true
|
||||
# schema:
|
||||
# type: string
|
||||
# requestBody:
|
||||
# required: true
|
||||
# content:
|
||||
# application/json:
|
||||
# schema:
|
||||
# $ref: '#/components/schemas/User'
|
||||
# responses:
|
||||
# '200':
|
||||
# description: Update result
|
||||
# content:
|
||||
# application/json:
|
||||
# schema:
|
||||
# type: object
|
||||
# properties:
|
||||
# success:
|
||||
# type: boolean
|
||||
# error:
|
||||
# type: string
|
||||
|
||||
# delete:
|
||||
# summary: Delete user
|
||||
# parameters:
|
||||
# - in: path
|
||||
# name: user_id
|
||||
# required: true
|
||||
# schema:
|
||||
# type: string
|
||||
# responses:
|
||||
# '200':
|
||||
# description: Delete result
|
||||
# content:
|
||||
# application/json:
|
||||
# schema:
|
||||
# type: object
|
||||
# properties:
|
||||
# success:
|
||||
# type: boolean
|
||||
# error:
|
||||
# type: string
|
||||
# delete:
|
||||
# summary: Delete user
|
||||
# parameters:
|
||||
# - in: path
|
||||
# name: user_id
|
||||
# required: true
|
||||
# schema:
|
||||
# type: string
|
||||
# responses:
|
||||
# '200':
|
||||
# description: Delete result
|
||||
# content:
|
||||
# application/json:
|
||||
# schema:
|
||||
# type: object
|
||||
# properties:
|
||||
# success:
|
||||
# type: boolean
|
||||
# error:
|
||||
# type: string
|
||||
|
||||
# /users:
|
||||
# get:
|
||||
# summary: Search user
|
||||
# parameters:
|
||||
# - in: query
|
||||
# name: query
|
||||
# schema:
|
||||
# type: string
|
||||
# - in: query
|
||||
# name: fields
|
||||
# schema:
|
||||
# type: string
|
||||
# responses:
|
||||
# '200':
|
||||
# description: List of users
|
||||
# content:
|
||||
# application/json:
|
||||
# schema:
|
||||
# type: array
|
||||
# items:
|
||||
# $ref: '#/components/schemas/User'
|
||||
# /users:
|
||||
# get:
|
||||
# summary: Search user
|
||||
# parameters:
|
||||
# - in: query
|
||||
# name: query
|
||||
# schema:
|
||||
# type: string
|
||||
# - in: query
|
||||
# name: fields
|
||||
# schema:
|
||||
# type: string
|
||||
# responses:
|
||||
# '200':
|
||||
# description: List of users
|
||||
# content:
|
||||
# application/json:
|
||||
# schema:
|
||||
# type: array
|
||||
# items:
|
||||
# $ref: '#/components/schemas/User'
|
||||
|
||||
# post:
|
||||
# summary: Add new user
|
||||
# requestBody:
|
||||
# required: true
|
||||
# content:
|
||||
# application/json:
|
||||
# schema:
|
||||
# $ref: '#/components/schemas/User'
|
||||
# responses:
|
||||
# '200':
|
||||
# description: Add result
|
||||
# content:
|
||||
# application/json:
|
||||
# schema:
|
||||
# type: object
|
||||
# properties:
|
||||
# success:
|
||||
# type: boolean
|
||||
# error:
|
||||
# type: string
|
||||
# user_json:
|
||||
# $ref: '#/components/schemas/User'
|
||||
# post:
|
||||
# summary: Add new user
|
||||
# requestBody:
|
||||
# required: true
|
||||
# content:
|
||||
# application/json:
|
||||
# schema:
|
||||
# $ref: '#/components/schemas/User'
|
||||
# responses:
|
||||
# '200':
|
||||
# description: Add result
|
||||
# content:
|
||||
# application/json:
|
||||
# schema:
|
||||
# type: object
|
||||
# properties:
|
||||
# success:
|
||||
# type: boolean
|
||||
# error:
|
||||
# type: string
|
||||
# user_json:
|
||||
# $ref: '#/components/schemas/User'
|
||||
|
||||
# /users/{user_id}/titles:
|
||||
# get:
|
||||
|
|
@ -541,14 +541,14 @@ components:
|
|||
User:
|
||||
type: object
|
||||
properties:
|
||||
user_id:
|
||||
id:
|
||||
type: integer
|
||||
format: int32
|
||||
format: int64
|
||||
description: Unique user ID (primary key)
|
||||
example: 1
|
||||
avatar_id:
|
||||
type: integer
|
||||
format: int32
|
||||
format: int64
|
||||
description: ID of the user avatar (references images table)
|
||||
nullable: true
|
||||
example: null
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue