From cdfa14cece9584ad059c10dbff061d4a00028098 Mon Sep 17 00:00:00 2001 From: Iron_Felix Date: Wed, 19 Nov 2025 00:41:54 +0300 Subject: [PATCH 1/5] feat: cursor added --- api/api.gen.go | 106 ++++++++++++++++++++++++++++++++++++++++------- api/openapi.yaml | 56 +++++++++++++++++++++---- 2 files changed, 139 insertions(+), 23 deletions(-) diff --git a/api/api.gen.go b/api/api.gen.go index 74a2d52..427f5af 100644 --- a/api/api.gen.go +++ b/api/api.gen.go @@ -24,6 +24,14 @@ const ( Winter ReleaseSeason = "winter" ) +// Defines values for TitleSort. +const ( + Id TitleSort = "id" + Rating TitleSort = "rating" + Views TitleSort = "views" + Year TitleSort = "year" +) + // Defines values for TitleStatus. const ( TitleStatusFinished TitleStatus = "finished" @@ -91,6 +99,9 @@ type Title struct { AdditionalProperties map[string]interface{} `json:"-"` } +// TitleSort Title sort order +type TitleSort string + // TitleStatus Title status type TitleStatus string @@ -139,6 +150,9 @@ type Cursor = string // GetTitlesParams defines parameters for GetTitles. type GetTitlesParams struct { + Cursor *Cursor `form:"cursor,omitempty" json:"cursor,omitempty"` + Sort *TitleSort `form:"sort,omitempty" json:"sort,omitempty"` + SortForward *bool `form:"sort_forward,omitempty" json:"sort_forward,omitempty"` Word *string `form:"word,omitempty" json:"word,omitempty"` Status *TitleStatus `form:"status,omitempty" json:"status,omitempty"` Rating *float64 `form:"rating,omitempty" json:"rating,omitempty"` @@ -161,11 +175,15 @@ type GetUsersUserIdParams struct { // GetUsersUserIdTitlesParams defines parameters for GetUsersUserIdTitles. type GetUsersUserIdTitlesParams struct { - Cursor *Cursor `form:"cursor,omitempty" json:"cursor,omitempty"` - Query *string `form:"query,omitempty" json:"query,omitempty"` - Limit *int `form:"limit,omitempty" json:"limit,omitempty"` - Offset *int `form:"offset,omitempty" json:"offset,omitempty"` - Fields *string `form:"fields,omitempty" json:"fields,omitempty"` + Cursor *Cursor `form:"cursor,omitempty" json:"cursor,omitempty"` + Word *string `form:"word,omitempty" json:"word,omitempty"` + Status *TitleStatus `form:"status,omitempty" json:"status,omitempty"` + WatchStatus *UserTitleStatus `form:"watch_status,omitempty" json:"watch_status,omitempty"` + Rating *float64 `form:"rating,omitempty" json:"rating,omitempty"` + ReleaseYear *int32 `form:"release_year,omitempty" json:"release_year,omitempty"` + ReleaseSeason *ReleaseSeason `form:"release_season,omitempty" json:"release_season,omitempty"` + Limit *int32 `form:"limit,omitempty" json:"limit,omitempty"` + Fields *string `form:"fields,omitempty" json:"fields,omitempty"` } // Getter for additional properties for Title. Returns the specified @@ -580,6 +598,30 @@ func (siw *ServerInterfaceWrapper) GetTitles(c *gin.Context) { // Parameter object where we will unmarshal all parameters from the context var params GetTitlesParams + // ------------- Optional query parameter "cursor" ------------- + + err = runtime.BindQueryParameter("form", true, false, "cursor", c.Request.URL.Query(), ¶ms.Cursor) + if err != nil { + siw.ErrorHandler(c, fmt.Errorf("Invalid format for parameter cursor: %w", err), http.StatusBadRequest) + return + } + + // ------------- Optional query parameter "sort" ------------- + + err = runtime.BindQueryParameter("form", true, false, "sort", c.Request.URL.Query(), ¶ms.Sort) + if err != nil { + siw.ErrorHandler(c, fmt.Errorf("Invalid format for parameter sort: %w", err), http.StatusBadRequest) + return + } + + // ------------- Optional query parameter "sort_forward" ------------- + + err = runtime.BindQueryParameter("form", true, false, "sort_forward", c.Request.URL.Query(), ¶ms.SortForward) + if err != nil { + siw.ErrorHandler(c, fmt.Errorf("Invalid format for parameter sort_forward: %w", err), http.StatusBadRequest) + return + } + // ------------- Optional query parameter "word" ------------- err = runtime.BindQueryParameter("form", true, false, "word", c.Request.URL.Query(), ¶ms.Word) @@ -749,11 +791,51 @@ func (siw *ServerInterfaceWrapper) GetUsersUserIdTitles(c *gin.Context) { return } - // ------------- Optional query parameter "query" ------------- + // ------------- Optional query parameter "word" ------------- - err = runtime.BindQueryParameter("form", true, false, "query", c.Request.URL.Query(), ¶ms.Query) + err = runtime.BindQueryParameter("form", true, false, "word", c.Request.URL.Query(), ¶ms.Word) if err != nil { - siw.ErrorHandler(c, fmt.Errorf("Invalid format for parameter query: %w", err), http.StatusBadRequest) + siw.ErrorHandler(c, fmt.Errorf("Invalid format for parameter word: %w", err), http.StatusBadRequest) + return + } + + // ------------- Optional query parameter "status" ------------- + + err = runtime.BindQueryParameter("form", true, false, "status", c.Request.URL.Query(), ¶ms.Status) + if err != nil { + siw.ErrorHandler(c, fmt.Errorf("Invalid format for parameter status: %w", err), http.StatusBadRequest) + return + } + + // ------------- Optional query parameter "watch_status" ------------- + + err = runtime.BindQueryParameter("form", true, false, "watch_status", c.Request.URL.Query(), ¶ms.WatchStatus) + if err != nil { + siw.ErrorHandler(c, fmt.Errorf("Invalid format for parameter watch_status: %w", err), http.StatusBadRequest) + return + } + + // ------------- Optional query parameter "rating" ------------- + + err = runtime.BindQueryParameter("form", true, false, "rating", c.Request.URL.Query(), ¶ms.Rating) + if err != nil { + siw.ErrorHandler(c, fmt.Errorf("Invalid format for parameter rating: %w", err), http.StatusBadRequest) + return + } + + // ------------- Optional query parameter "release_year" ------------- + + err = runtime.BindQueryParameter("form", true, false, "release_year", c.Request.URL.Query(), ¶ms.ReleaseYear) + if err != nil { + siw.ErrorHandler(c, fmt.Errorf("Invalid format for parameter release_year: %w", err), http.StatusBadRequest) + return + } + + // ------------- Optional query parameter "release_season" ------------- + + err = runtime.BindQueryParameter("form", true, false, "release_season", c.Request.URL.Query(), ¶ms.ReleaseSeason) + if err != nil { + siw.ErrorHandler(c, fmt.Errorf("Invalid format for parameter release_season: %w", err), http.StatusBadRequest) return } @@ -765,14 +847,6 @@ func (siw *ServerInterfaceWrapper) GetUsersUserIdTitles(c *gin.Context) { 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 - } - // ------------- Optional query parameter "fields" ------------- err = runtime.BindQueryParameter("form", true, false, "fields", c.Request.URL.Query(), ¶ms.Fields) diff --git a/api/openapi.yaml b/api/openapi.yaml index a33fe89..a6ae12c 100644 --- a/api/openapi.yaml +++ b/api/openapi.yaml @@ -11,6 +11,13 @@ paths: get: summary: Get titles parameters: + - $ref: '#/components/parameters/cursor' + - $ref: '#/components/parameters/title_sort' + - in: query + name: sort_forward + default: true + schema: + type: boolean - in: query name: word schema: @@ -289,19 +296,37 @@ paths: schema: type: string - in: query - name: query + name: word schema: type: string + - in: query + name: status + schema: + $ref: '#/components/schemas/TitleStatus' + - in: query + name: watch_status + schema: + $ref: '#/components/schemas/UserTitleStatus' + - in: query + name: rating + schema: + type: number + format: double + - in: query + name: release_year + schema: + type: integer + format: int32 + - in: query + name: release_season + schema: + $ref: '#/components/schemas/ReleaseSeason' - in: query name: limit schema: type: integer + format: int32 default: 10 - - in: query - name: offset - schema: - type: integer - default: 0 - in: query name: fields schema: @@ -577,14 +602,31 @@ paths: components: parameters: - cursor: + cursor: # example base64( {id: 1, param: 2019}) in: query name: cursor required: false schema: type: string + + title_sort: + in: query + name: sort + required: false + schema: + $ref: '#/components/schemas/TitleSort' schemas: + TitleSort: + type: string + description: Title sort order + default: id + enum: + - id + - year + - rating + - views + Image: type: object properties: From 9ed09500ed773b53c63afcb217e236a2764095f7 Mon Sep 17 00:00:00 2001 From: nihonium Date: Wed, 19 Nov 2025 01:42:40 +0300 Subject: [PATCH 2/5] refact: slightly refactored openapi spec --- api/openapi.yaml | 438 +++-------------------------------------------- 1 file changed, 26 insertions(+), 412 deletions(-) diff --git a/api/openapi.yaml b/api/openapi.yaml index a6ae12c..b52ba58 100644 --- a/api/openapi.yaml +++ b/api/openapi.yaml @@ -1,4 +1,4 @@ -openapi: 3.1.1 +openapi: 3.0.4 info: title: Titles, Users, Reviews, Tags, and Media API version: 1.0.0 @@ -15,9 +15,9 @@ paths: - $ref: '#/components/parameters/title_sort' - in: query name: sort_forward - default: true schema: type: boolean + default: true - in: query name: word schema: @@ -59,13 +59,22 @@ paths: default: all responses: '200': - description: List of titles + description: List of titles with cursor content: application/json: schema: - type: array - items: - $ref: '#/components/schemas/Title' + type: object + properties: + data: + type: array + items: + $ref: '#/components/schemas/Title' + description: List of titles + cursor: + $ref: "#/components/schemas/CursorObj" + required: + - data + - cursor '204': description: No titles found '400': @@ -104,66 +113,6 @@ paths: '204': description: No title found -# patch: -# summary: Update title info -# parameters: -# - in: path -# name: title_id -# required: true -# schema: -# type: string -# requestBody: -# required: true -# content: -# application/json: -# schema: -# $ref: '#/components/schemas/Title' -# responses: -# '200': -# description: Update result -# content: -# application/json: -# schema: -# type: object -# properties: -# success: -# type: boolean -# error: -# type: string -# user_json: -# $ref: '#/components/schemas/User' - -# /titles/{title_id}/reviews: -# get: -# summary: Get title reviews -# parameters: -# - in: path -# name: title_id -# required: true -# schema: -# type: string -# - in: query -# name: limit -# schema: -# type: integer -# default: 10 -# - in: query -# name: offset -# schema: -# type: integer -# default: 0 -# responses: -# '200': -# description: List of reviews -# content: -# application/json: -# schema: -# type: array -# items: -# $ref: '#/components/schemas/Review' -# '204': -# description: No reviews found - /users/{user_id}: get: summary: Get user info @@ -192,99 +141,6 @@ paths: '500': description: Unknown server error - # 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 - - # /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' - /users/{user_id}/titles/: get: summary: Get user titles @@ -348,258 +204,6 @@ paths: '500': description: Unknown server error -# post: -# summary: Add user title -# parameters: -# - in: path -# name: user_id -# required: true -# schema: -# type: string -# requestBody: -# required: true -# content: -# application/json: -# schema: -# type: object -# properties: -# title_id: -# type: string -# status: -# type: string -# responses: -# '200': -# description: Add result -# content: -# application/json: -# schema: -# type: object -# properties: -# success: -# type: boolean -# error: -# type: string - -# patch: -# summary: Update user title -# parameters: -# - in: path -# name: user_id -# required: true -# schema: -# type: string -# requestBody: -# required: true -# content: -# application/json: -# schema: -# $ref: '#/components/schemas/UserTitle' -# responses: -# '200': -# description: Update result -# content: -# application/json: -# schema: -# type: object -# properties: -# success: -# type: boolean -# error: -# type: string - -# delete: -# summary: Delete user title -# parameters: -# - in: path -# name: user_id -# required: true -# schema: -# type: string -# - in: query -# name: title_id -# schema: -# type: string -# responses: -# '200': -# description: Delete result -# content: -# application/json: -# schema: -# type: object -# properties: -# success: -# type: boolean -# error: -# type: string - -# /users/{user_id}/reviews: -# get: -# summary: Get user reviews -# parameters: -# - in: path -# name: user_id -# required: true -# schema: -# type: string -# - in: query -# name: limit -# schema: -# type: integer -# default: 10 -# - in: query -# name: offset -# schema: -# type: integer -# default: 0 -# responses: -# '200': -# description: List of reviews -# content: -# application/json: -# schema: -# type: array -# items: -# $ref: '#/components/schemas/Review' - -# /reviews: -# post: -# summary: Add review -# requestBody: -# required: true -# content: -# application/json: -# schema: -# $ref: '#/components/schemas/Review' -# responses: -# '200': -# description: Add result -# content: -# application/json: -# schema: -# type: object -# properties: -# success: -# type: boolean -# error: -# type: string - -# /reviews/{review_id}: -# patch: -# summary: Update review -# parameters: -# - in: path -# name: review_id -# required: true -# schema: -# type: string -# requestBody: -# required: true -# content: -# application/json: -# schema: -# $ref: '#/components/schemas/Review' -# responses: -# '200': -# description: Update result -# content: -# application/json: -# schema: -# type: object -# properties: -# success: -# type: boolean -# error: -# type: string -# delete: -# summary: Delete review -# parameters: -# - in: path -# name: review_id -# required: true -# schema: -# type: string -# responses: -# '200': -# description: Delete result -# content: -# application/json: -# schema: -# type: object -# properties: -# success: -# type: boolean -# error: -# type: string - -# /tags: -# get: -# summary: Get tags -# parameters: -# - 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 -# responses: -# '200': -# description: List of tags -# content: -# application/json: -# schema: -# type: array -# items: -# $ref: '#/components/schemas/Tag' - -# /media: -# post: -# summary: Upload image -# responses: -# '200': -# description: Upload result -# content: -# application/json: -# schema: -# type: object -# properties: -# success: -# type: boolean -# error: -# type: string -# image_id: -# type: string - -# get: -# summary: Get image path -# parameters: -# - in: query -# name: image_id -# required: true -# schema: -# type: string -# responses: -# '200': -# description: Image path -# content: -# application/json: -# schema: -# type: object -# properties: -# success: -# type: boolean -# error: -# type: string -# image_path: -# type: string - components: parameters: cursor: # example base64( {id: 1, param: 2019}) @@ -617,6 +221,17 @@ components: $ref: '#/components/schemas/TitleSort' schemas: + CursorObj: + type: object + required: + - id + properties: + id: + type: integer + format: int64 + param: + type: string + TitleSort: type: string description: Title sort order @@ -778,7 +393,6 @@ components: type: integer format: int64 description: ID of the user avatar (references images table) - nullable: true example: null mail: type: string From 2025bb451fbde55c50f235117674aaea1ba751ee Mon Sep 17 00:00:00 2001 From: Iron_Felix Date: Wed, 19 Nov 2025 03:14:41 +0300 Subject: [PATCH 3/5] refact: openapi splitted into separate files --- api/openapi.yaml | 441 +------------------------ api/parameters/_index.yaml | 4 + api/parameters/cursor.yaml | 5 + api/parameters/title_sort.yaml | 5 + api/paths/titles-id.yaml | 29 ++ api/paths/titles.yaml | 73 ++++ api/paths/users-id-titles.yaml | 61 ++++ api/paths/users-id.yaml | 26 ++ api/schemas/CursorObj.yaml | 9 + api/schemas/Image.yaml | 9 + api/schemas/Review.yaml | 2 + api/schemas/Studio.yaml | 14 + api/schemas/Tag.yaml | 8 + api/schemas/Tags.yaml | 11 + api/schemas/Title.yaml | 63 ++++ api/schemas/TitleSort.yaml | 8 + api/schemas/User.yaml | 40 +++ api/schemas/UserTitle.yaml | 24 ++ api/schemas/_index.yaml | 26 ++ api/schemas/enums/ReleaseSeason.yaml | 7 + api/schemas/enums/TitleStatus.yaml | 6 + api/schemas/enums/UserTitleStatus.yaml | 7 + 22 files changed, 443 insertions(+), 435 deletions(-) create mode 100644 api/parameters/_index.yaml create mode 100644 api/parameters/cursor.yaml create mode 100644 api/parameters/title_sort.yaml create mode 100644 api/paths/titles-id.yaml create mode 100644 api/paths/titles.yaml create mode 100644 api/paths/users-id-titles.yaml create mode 100644 api/paths/users-id.yaml create mode 100644 api/schemas/CursorObj.yaml create mode 100644 api/schemas/Image.yaml create mode 100644 api/schemas/Review.yaml create mode 100644 api/schemas/Studio.yaml create mode 100644 api/schemas/Tag.yaml create mode 100644 api/schemas/Tags.yaml create mode 100644 api/schemas/Title.yaml create mode 100644 api/schemas/TitleSort.yaml create mode 100644 api/schemas/User.yaml create mode 100644 api/schemas/UserTitle.yaml create mode 100644 api/schemas/_index.yaml create mode 100644 api/schemas/enums/ReleaseSeason.yaml create mode 100644 api/schemas/enums/TitleStatus.yaml create mode 100644 api/schemas/enums/UserTitleStatus.yaml diff --git a/api/openapi.yaml b/api/openapi.yaml index b52ba58..281fe82 100644 --- a/api/openapi.yaml +++ b/api/openapi.yaml @@ -8,444 +8,15 @@ servers: paths: /titles: - get: - summary: Get titles - parameters: - - $ref: '#/components/parameters/cursor' - - $ref: '#/components/parameters/title_sort' - - in: query - name: sort_forward - schema: - type: boolean - default: true - - in: query - name: word - schema: - type: string - - in: query - name: status - schema: - $ref: '#/components/schemas/TitleStatus' - - in: query - name: rating - schema: - type: number - format: double - - in: query - name: release_year - schema: - type: integer - format: int32 - - in: query - name: release_season - schema: - $ref: '#/components/schemas/ReleaseSeason' - - in: query - name: limit - schema: - type: integer - format: int32 - default: 10 - - in: query - name: offset - schema: - type: integer - format: int32 - default: 0 - - in: query - name: fields - schema: - type: string - default: all - responses: - '200': - description: List of titles with cursor - content: - application/json: - schema: - type: object - properties: - data: - type: array - items: - $ref: '#/components/schemas/Title' - description: List of titles - cursor: - $ref: "#/components/schemas/CursorObj" - required: - - data - - cursor - '204': - description: No titles found - '400': - description: Request params are not correct - '500': - description: Unknown server error - + $ref: "./paths/titles.yaml" /titles/{title_id}: - get: - summary: Get title description - parameters: - - in: path - name: title_id - required: true - schema: - type: integer - format: int64 - - in: query - name: fields - schema: - type: string - default: all - responses: - '200': - description: Title description - content: - application/json: - schema: - $ref: '#/components/schemas/Title' - '404': - description: Title not found - '400': - description: Request params are not correct - '500': - description: Unknown server error - '204': - description: No title found - + $ref: "./paths/titles-id.yaml" /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 - '400': - description: Request params are not correct - '500': - description: Unknown server error - + $ref: "./paths/users-id.yaml" /users/{user_id}/titles/: - get: - summary: Get user titles - parameters: - - $ref: '#/components/parameters/cursor' - - in: path - name: user_id - required: true - schema: - type: string - - in: query - name: word - schema: - type: string - - in: query - name: status - schema: - $ref: '#/components/schemas/TitleStatus' - - in: query - name: watch_status - schema: - $ref: '#/components/schemas/UserTitleStatus' - - in: query - name: rating - schema: - type: number - format: double - - in: query - name: release_year - schema: - type: integer - format: int32 - - in: query - name: release_season - schema: - $ref: '#/components/schemas/ReleaseSeason' - - in: query - name: limit - schema: - type: integer - format: int32 - default: 10 - - in: query - name: fields - schema: - type: string - default: all - responses: - '200': - description: List of user titles - content: - application/json: - schema: - type: array - items: - $ref: '#/components/schemas/UserTitle' - '204': - description: No titles found - '400': - description: Request params are not correct - '500': - description: Unknown server error - + $ref: "./paths/users-id-titles.yaml" components: parameters: - cursor: # example base64( {id: 1, param: 2019}) - in: query - name: cursor - required: false - schema: - type: string - - title_sort: - in: query - name: sort - required: false - schema: - $ref: '#/components/schemas/TitleSort' - + $ref: "./parameters/_index.yaml" schemas: - CursorObj: - type: object - required: - - id - properties: - id: - type: integer - format: int64 - param: - type: string - - TitleSort: - type: string - description: Title sort order - default: id - enum: - - id - - year - - rating - - views - - Image: - type: object - properties: - id: - type: integer - format: int64 - storage_type: - type: string - image_path: - type: string - - TitleStatus: - type: string - description: Title status - enum: - - finished - - ongoing - - planned - - ReleaseSeason: - type: string - description: Title release season - enum: - - winter - - spring - - summer - - fall - - UserTitleStatus: - type: string - description: User's title status - enum: - - finished - - planned - - dropped - - in-progress - - Review: - type: object - additionalProperties: true - - Tag: - type: object - description: "A localized tag: keys are language codes (ISO 639-1), values are tag names" - additionalProperties: - type: string - example: - en: "Shojo" - ru: "Сёдзё" - ja: "少女" - - Tags: - type: array - description: "Array of localized tags" - items: - $ref: '#/components/schemas/Tag' - example: - - en: "Shojo" - ru: "Сёдзё" - ja: "少女" - - en: "Shounen" - ru: "Сёнен" - ja: "少年" - - Studio: - type: object - required: - - id - - name - properties: - id: - type: integer - format: int64 - name: - type: string - poster: - $ref: '#/components/schemas/Image' - description: - type: string - - - Title: - type: object - required: - - id - - title_names - - tags - properties: - id: - type: integer - format: int64 - description: Unique title ID (primary key) - example: 1 - title_names: - type: object - description: "Localized titles. Key = language (ISO 639-1), value = list of names" - additionalProperties: - type: array - items: - type: string - example: "Attack on Titan" - minItems: 1 - example: ["Attack on Titan", "AoT"] - example: - en: ["Attack on Titan", "AoT"] - ru: ["Атака титанов", "Титаны"] - ja: ["進撃の巨人"] - studio: - $ref: '#/components/schemas/Studio' - tags: - $ref: '#/components/schemas/Tags' - poster: - $ref: '#/components/schemas/Image' - title_status: - $ref: '#/components/schemas/TitleStatus' - rating: - type: number - format: double - rating_count: - type: integer - format: int32 - release_year: - type: integer - format: int32 - release_season: - $ref: '#/components/schemas/ReleaseSeason' - episodes_aired: - type: integer - format: int32 - episodes_all: - type: integer - format: int32 - episodes_len: - type: object - additionalProperties: - type: number - format: double - additionalProperties: true - - User: - type: object - properties: - id: - type: integer - format: int64 - description: Unique user ID (primary key) - example: 1 - avatar_id: - type: integer - format: int64 - description: ID of the user avatar (references images table) - example: null - mail: - type: string - format: email - description: User email - example: "john.doe@example.com" - nickname: - type: string - description: Username (alphanumeric + _ or -) - maxLength: 16 - example: "john_doe_42" - disp_name: - type: string - description: Display name - maxLength: 32 - example: "John Doe" - user_desc: - type: string - description: User description - maxLength: 512 - example: "Just a regular user." - creation_date: - type: string - format: date-time - description: Timestamp when the user was created - example: "2025-10-10T23:45:47.908073Z" - required: - - user_id - - nickname - # - creation_date - - UserTitle: - 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: true + $ref: "./schemas/_index.yaml" diff --git a/api/parameters/_index.yaml b/api/parameters/_index.yaml new file mode 100644 index 0000000..6249e7d --- /dev/null +++ b/api/parameters/_index.yaml @@ -0,0 +1,4 @@ +cursor: + $ref: "./cursor.yaml" +title_sort: + $ref: "./title_sort.yaml" \ No newline at end of file diff --git a/api/parameters/cursor.yaml b/api/parameters/cursor.yaml new file mode 100644 index 0000000..1730f18 --- /dev/null +++ b/api/parameters/cursor.yaml @@ -0,0 +1,5 @@ +in: query +name: cursor +required: false +schema: + type: string diff --git a/api/parameters/title_sort.yaml b/api/parameters/title_sort.yaml new file mode 100644 index 0000000..615294b --- /dev/null +++ b/api/parameters/title_sort.yaml @@ -0,0 +1,5 @@ +in: query +name: sort +required: false +schema: + $ref: '../schemas/TitleSort.yaml' diff --git a/api/paths/titles-id.yaml b/api/paths/titles-id.yaml new file mode 100644 index 0000000..01fa504 --- /dev/null +++ b/api/paths/titles-id.yaml @@ -0,0 +1,29 @@ +get: + summary: Get title description + parameters: + - in: path + name: title_id + required: true + schema: + type: integer + format: int64 + - in: query + name: fields + schema: + type: string + default: all + responses: + '200': + description: Title description + content: + application/json: + schema: + $ref: "../schemas/Title.yaml" + '404': + description: Title not found + '400': + description: Request params are not correct + '500': + description: Unknown server error + '204': + description: No title found diff --git a/api/paths/titles.yaml b/api/paths/titles.yaml new file mode 100644 index 0000000..4fd010d --- /dev/null +++ b/api/paths/titles.yaml @@ -0,0 +1,73 @@ +get: + summary: Get titles + parameters: + - $ref: ../parameters/cursor.yaml + - $ref: ../parameters/title_sort.yaml + - in: query + name: sort_forward + schema: + type: boolean + default: true + - in: query + name: word + schema: + type: string + - in: query + name: status + schema: + $ref: '../schemas/enums/TitleStatus.yaml' + - in: query + name: rating + schema: + type: number + format: double + - in: query + name: release_year + schema: + type: integer + format: int32 + - in: query + name: release_season + schema: + $ref: '../schemas/enums/ReleaseSeason.yaml' + - in: query + name: limit + schema: + type: integer + format: int32 + default: 10 + - in: query + name: offset + schema: + type: integer + format: int32 + default: 0 + - in: query + name: fields + schema: + type: string + default: all + responses: + '200': + description: List of titles with cursor + content: + application/json: + schema: + type: object + properties: + data: + type: array + items: + $ref: '../schemas/Title.yaml' + description: List of titles + cursor: + $ref: '../schemas/CursorObj.yaml' + required: + - data + - cursor + '204': + description: No titles found + '400': + description: Request params are not correct + '500': + description: Unknown server error diff --git a/api/paths/users-id-titles.yaml b/api/paths/users-id-titles.yaml new file mode 100644 index 0000000..0cde5af --- /dev/null +++ b/api/paths/users-id-titles.yaml @@ -0,0 +1,61 @@ +get: + summary: Get user titles + parameters: + - $ref: '../parameters/cursor.yaml' + - in: path + name: user_id + required: true + schema: + type: string + - in: query + name: word + schema: + type: string + - in: query + name: status + schema: + $ref: '../schemas/enums/TitleStatus.yaml' + - in: query + name: watch_status + schema: + $ref: '../schemas/enums/UserTitleStatus.yaml' + - in: query + name: rating + schema: + type: number + format: double + - in: query + name: release_year + schema: + type: integer + format: int32 + - in: query + name: release_season + schema: + $ref: '../schemas/enums/ReleaseSeason.yaml' + - in: query + name: limit + schema: + type: integer + format: int32 + default: 10 + - in: query + name: fields + schema: + type: string + default: all + responses: + '200': + description: List of user titles + content: + application/json: + schema: + type: array + items: + $ref: '../schemas/UserTitle.yaml' + '204': + description: No titles found + '400': + description: Request params are not correct + '500': + description: Unknown server error diff --git a/api/paths/users-id.yaml b/api/paths/users-id.yaml new file mode 100644 index 0000000..0acdb81 --- /dev/null +++ b/api/paths/users-id.yaml @@ -0,0 +1,26 @@ +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: '../schemas/User.yaml' + '404': + description: User not found + '400': + description: Request params are not correct + '500': + description: Unknown server error diff --git a/api/schemas/CursorObj.yaml b/api/schemas/CursorObj.yaml new file mode 100644 index 0000000..5a3c96c --- /dev/null +++ b/api/schemas/CursorObj.yaml @@ -0,0 +1,9 @@ +type: object +required: + - id +properties: + id: + type: integer + format: int64 + param: + type: string diff --git a/api/schemas/Image.yaml b/api/schemas/Image.yaml new file mode 100644 index 0000000..7226b29 --- /dev/null +++ b/api/schemas/Image.yaml @@ -0,0 +1,9 @@ +type: object +properties: + id: + type: integer + format: int64 + storage_type: + type: string + image_path: + type: string diff --git a/api/schemas/Review.yaml b/api/schemas/Review.yaml new file mode 100644 index 0000000..a116dde --- /dev/null +++ b/api/schemas/Review.yaml @@ -0,0 +1,2 @@ +type: object +additionalProperties: true diff --git a/api/schemas/Studio.yaml b/api/schemas/Studio.yaml new file mode 100644 index 0000000..35b40a8 --- /dev/null +++ b/api/schemas/Studio.yaml @@ -0,0 +1,14 @@ +type: object +required: + - id + - name +properties: + id: + type: integer + format: int64 + name: + type: string + poster: + $ref: ./Image.yaml + description: + type: string diff --git a/api/schemas/Tag.yaml b/api/schemas/Tag.yaml new file mode 100644 index 0000000..7239b10 --- /dev/null +++ b/api/schemas/Tag.yaml @@ -0,0 +1,8 @@ +type: object +description: 'A localized tag: keys are language codes (ISO 639-1), values are tag names' +additionalProperties: + type: string +example: + en: Shojo + ru: Сёдзё + ja: 少女 diff --git a/api/schemas/Tags.yaml b/api/schemas/Tags.yaml new file mode 100644 index 0000000..ca8c4fd --- /dev/null +++ b/api/schemas/Tags.yaml @@ -0,0 +1,11 @@ +type: array +description: Array of localized tags +items: + $ref: ./Tag.yaml +example: +- en: Shojo + ru: Сёдзё + ja: 少女 +- en: Shounen + ru: Сёнен + ja: 少年 diff --git a/api/schemas/Title.yaml b/api/schemas/Title.yaml new file mode 100644 index 0000000..7497d1f --- /dev/null +++ b/api/schemas/Title.yaml @@ -0,0 +1,63 @@ +type: object +required: + - id + - title_names + - tags +properties: + id: + type: integer + format: int64 + description: Unique title ID (primary key) + example: 1 + title_names: + type: object + description: Localized titles. Key = language (ISO 639-1), value = list of names + additionalProperties: + type: array + items: + type: string + example: Attack on Titan + minItems: 1 + example: + - Attack on Titan + - AoT + example: + en: + - Attack on Titan + - AoT + ru: + - Атака титанов + - Титаны + ja: + - 進撃の巨人 + studio: + $ref: ./Studio.yaml + tags: + $ref: ./Tags.yaml + poster: + $ref: ./Image.yaml + title_status: + $ref: ./enums/TitleStatus.yaml + rating: + type: number + format: double + rating_count: + type: integer + format: int32 + release_year: + type: integer + format: int32 + release_season: + $ref: ./enums/ReleaseSeason.yaml + episodes_aired: + type: integer + format: int32 + episodes_all: + type: integer + format: int32 + episodes_len: + type: object + additionalProperties: + type: number + format: double +additionalProperties: true diff --git a/api/schemas/TitleSort.yaml b/api/schemas/TitleSort.yaml new file mode 100644 index 0000000..d8ce8f7 --- /dev/null +++ b/api/schemas/TitleSort.yaml @@ -0,0 +1,8 @@ +type: string +description: Title sort order +default: id +enum: + - id + - year + - rating + - views diff --git a/api/schemas/User.yaml b/api/schemas/User.yaml new file mode 100644 index 0000000..8b4d88d --- /dev/null +++ b/api/schemas/User.yaml @@ -0,0 +1,40 @@ +type: object +properties: + id: + type: integer + format: int64 + description: Unique user ID (primary key) + example: 1 + avatar_id: + type: integer + format: int64 + description: ID of the user avatar (references images table) + example: null + mail: + type: string + format: email + description: User email + example: john.doe@example.com + nickname: + type: string + description: Username (alphanumeric + _ or -) + maxLength: 16 + example: john_doe_42 + disp_name: + type: string + description: Display name + maxLength: 32 + example: John Doe + user_desc: + type: string + description: User description + maxLength: 512 + example: Just a regular user. + creation_date: + type: string + format: date-time + description: Timestamp when the user was created + example: '2025-10-10T23:45:47.908073Z' +required: + - user_id + - nickname diff --git a/api/schemas/UserTitle.yaml b/api/schemas/UserTitle.yaml new file mode 100644 index 0000000..658e350 --- /dev/null +++ b/api/schemas/UserTitle.yaml @@ -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: true diff --git a/api/schemas/_index.yaml b/api/schemas/_index.yaml new file mode 100644 index 0000000..ac49f37 --- /dev/null +++ b/api/schemas/_index.yaml @@ -0,0 +1,26 @@ +CursorObj: + $ref: ./CursorObj.yaml +TitleSort: + $ref: "./TitleSort.yaml" +Image: + $ref: "./Image.yaml" +TitleStatus: + $ref: "./enums/TitleStatus.yaml" +ReleaseSeason: + $ref: "./enums/ReleaseSeason.yaml" +UserTitleStatus: + $ref: "./enums/UserTitleStatus.yaml" +Review: + $ref: "./Review.yaml" +Tag: + $ref: "./Tag.yaml" +Tags: + $ref: "./Tags.yaml" +Studio: + $ref: "./Studio.yaml" +Title: + $ref: "./Title.yaml" +User: + $ref: "./User.yaml" +UserTitle: + $ref: "./UserTitle.yaml" diff --git a/api/schemas/enums/ReleaseSeason.yaml b/api/schemas/enums/ReleaseSeason.yaml new file mode 100644 index 0000000..5cf988d --- /dev/null +++ b/api/schemas/enums/ReleaseSeason.yaml @@ -0,0 +1,7 @@ +type: string +description: Title release season +enum: + - winter + - spring + - summer + - fall diff --git a/api/schemas/enums/TitleStatus.yaml b/api/schemas/enums/TitleStatus.yaml new file mode 100644 index 0000000..0bfce7d --- /dev/null +++ b/api/schemas/enums/TitleStatus.yaml @@ -0,0 +1,6 @@ +type: string +description: Title status +enum: + - finished + - ongoing + - planned diff --git a/api/schemas/enums/UserTitleStatus.yaml b/api/schemas/enums/UserTitleStatus.yaml new file mode 100644 index 0000000..0b1a90d --- /dev/null +++ b/api/schemas/enums/UserTitleStatus.yaml @@ -0,0 +1,7 @@ +type: string +description: User's title status +enum: + - finished + - planned + - dropped + - in-progress From fbf3f1d3a28fa45f0698aece2e824f7568de1e8b Mon Sep 17 00:00:00 2001 From: Iron_Felix Date: Wed, 19 Nov 2025 03:57:44 +0300 Subject: [PATCH 4/5] feat: now use _build to build --- api/_build/oapi-codegen.yaml | 6 + api/_build/openapi.yaml | 436 +++++++++++++++++++++++++++++++++++ api/api.gen.go | 15 +- api/paths/titles.yaml | 4 +- 4 files changed, 457 insertions(+), 4 deletions(-) create mode 100644 api/_build/oapi-codegen.yaml create mode 100644 api/_build/openapi.yaml diff --git a/api/_build/oapi-codegen.yaml b/api/_build/oapi-codegen.yaml new file mode 100644 index 0000000..32e029a --- /dev/null +++ b/api/_build/oapi-codegen.yaml @@ -0,0 +1,6 @@ +package: oapi +generate: + strict-server: true + gin-server: true + models: true +output: api/api.gen.go \ No newline at end of file diff --git a/api/_build/openapi.yaml b/api/_build/openapi.yaml new file mode 100644 index 0000000..5ff77e0 --- /dev/null +++ b/api/_build/openapi.yaml @@ -0,0 +1,436 @@ +openapi: 3.0.4 +info: + title: 'Titles, Users, Reviews, Tags, and Media API' + version: 1.0.0 +servers: + - url: /api/v1 +paths: + /titles: + get: + summary: Get titles + parameters: + - $ref: '#/components/parameters/cursor' + - $ref: '#/components/parameters/title_sort' + - in: query + name: sort_forward + schema: + type: boolean + default: true + - in: query + name: word + schema: + type: string + - in: query + name: status + schema: + $ref: '#/components/schemas/TitleStatus' + - in: query + name: rating + schema: + type: number + format: double + - in: query + name: release_year + schema: + type: integer + format: int32 + - in: query + name: release_season + schema: + $ref: '#/components/schemas/ReleaseSeason' + - in: query + name: limit + schema: + type: integer + format: int32 + default: 10 + - in: query + name: offset + schema: + type: integer + format: int32 + default: 0 + - in: query + name: fields + schema: + type: string + default: all + responses: + '200': + description: List of titles with cursor + content: + application/json: + schema: + type: object + properties: + data: + type: array + items: + $ref: '#/components/schemas/Title' + description: List of titles + cursor: + $ref: '#/components/schemas/CursorObj' + required: + - data + - cursor + '204': + description: No titles found + '400': + description: Request params are not correct + '500': + description: Unknown server error + '/titles/{title_id}': + get: + summary: Get title description + parameters: + - in: path + name: title_id + required: true + schema: + type: integer + format: int64 + - in: query + name: fields + schema: + type: string + default: all + responses: + '200': + description: Title description + content: + application/json: + schema: + $ref: '#/components/schemas/Title' + '204': + description: No title found + '400': + description: Request params are not correct + '404': + description: Title not found + '500': + description: Unknown server error + '/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' + '400': + description: Request params are not correct + '404': + description: User not found + '500': + description: Unknown server error + '/users/{user_id}/titles/': + get: + summary: Get user titles + parameters: + - $ref: '#/components/parameters/cursor' + - in: path + name: user_id + required: true + schema: + type: string + - in: query + name: word + schema: + type: string + - in: query + name: status + schema: + $ref: '#/components/schemas/TitleStatus' + - in: query + name: watch_status + schema: + $ref: '#/components/schemas/UserTitleStatus' + - in: query + name: rating + schema: + type: number + format: double + - in: query + name: release_year + schema: + type: integer + format: int32 + - in: query + name: release_season + schema: + $ref: '#/components/schemas/ReleaseSeason' + - in: query + name: limit + schema: + type: integer + format: int32 + default: 10 + - in: query + name: fields + schema: + type: string + default: all + responses: + '200': + description: List of user titles + content: + application/json: + schema: + type: array + items: + $ref: '#/components/schemas/UserTitle' + '204': + description: No titles found + '400': + description: Request params are not correct + '500': + description: Unknown server error +components: + parameters: + cursor: + in: query + name: cursor + required: false + schema: + type: string + title_sort: + in: query + name: sort + required: false + schema: + $ref: '#/components/schemas/TitleSort' + schemas: + CursorObj: + type: object + required: + - id + properties: + id: + type: integer + format: int64 + param: + type: string + TitleSort: + type: string + description: Title sort order + default: id + enum: + - id + - year + - rating + - views + Image: + type: object + properties: + id: + type: integer + format: int64 + storage_type: + type: string + image_path: + type: string + TitleStatus: + type: string + description: Title status + enum: + - finished + - ongoing + - planned + ReleaseSeason: + type: string + description: Title release season + enum: + - winter + - spring + - summer + - fall + UserTitleStatus: + type: string + description: User's title status + enum: + - finished + - planned + - dropped + - in-progress + Review: + type: object + additionalProperties: true + Tag: + type: object + description: 'A localized tag: keys are language codes (ISO 639-1), values are tag names' + additionalProperties: + type: string + example: + en: Shojo + ru: Сёдзё + ja: 少女 + Tags: + type: array + description: Array of localized tags + items: + $ref: '#/components/schemas/Tag' + example: + - en: Shojo + ru: Сёдзё + ja: 少女 + - en: Shounen + ru: Сёнен + ja: 少年 + Studio: + type: object + required: + - id + - name + properties: + id: + type: integer + format: int64 + name: + type: string + poster: + $ref: '#/components/schemas/Image' + description: + type: string + Title: + type: object + required: + - id + - title_names + - tags + properties: + id: + type: integer + format: int64 + description: Unique title ID (primary key) + example: 1 + title_names: + type: object + description: 'Localized titles. Key = language (ISO 639-1), value = list of names' + additionalProperties: + type: array + items: + type: string + example: Attack on Titan + minItems: 1 + example: + - Attack on Titan + - AoT + example: + en: + - Attack on Titan + - AoT + ru: + - Атака титанов + - Титаны + ja: + - 進撃の巨人 + studio: + $ref: '#/components/schemas/Studio' + tags: + $ref: '#/components/schemas/Tags' + poster: + $ref: '#/components/schemas/Image' + title_status: + $ref: '#/components/schemas/TitleStatus' + rating: + type: number + format: double + rating_count: + type: integer + format: int32 + release_year: + type: integer + format: int32 + release_season: + $ref: '#/components/schemas/ReleaseSeason' + episodes_aired: + type: integer + format: int32 + episodes_all: + type: integer + format: int32 + episodes_len: + type: object + additionalProperties: + type: number + format: double + additionalProperties: true + User: + type: object + properties: + id: + type: integer + format: int64 + description: Unique user ID (primary key) + example: 1 + avatar_id: + type: integer + format: int64 + description: ID of the user avatar (references images table) + example: null + mail: + type: string + format: email + description: User email + example: john.doe@example.com + nickname: + type: string + description: Username (alphanumeric + _ or -) + maxLength: 16 + example: john_doe_42 + disp_name: + type: string + description: Display name + maxLength: 32 + example: John Doe + user_desc: + type: string + description: User description + maxLength: 512 + example: Just a regular user. + creation_date: + type: string + format: date-time + description: Timestamp when the user was created + example: '2025-10-10T23:45:47.908073Z' + required: + - user_id + - nickname + UserTitle: + 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: true diff --git a/api/api.gen.go b/api/api.gen.go index 427f5af..f252a5a 100644 --- a/api/api.gen.go +++ b/api/api.gen.go @@ -47,6 +47,12 @@ const ( UserTitleStatusPlanned UserTitleStatus = "planned" ) +// CursorObj defines model for CursorObj. +type CursorObj struct { + Id int64 `json:"id"` + Param *string `json:"param,omitempty"` +} + // Image defines model for Image. type Image struct { Id *int64 `json:"id,omitempty"` @@ -108,7 +114,7 @@ type TitleStatus string // User defines model for User. type User struct { // AvatarId ID of the user avatar (references images table) - AvatarId *int64 `json:"avatar_id"` + AvatarId *int64 `json:"avatar_id,omitempty"` // CreationDate Timestamp when the user was created CreationDate *time.Time `json:"creation_date,omitempty"` @@ -906,7 +912,12 @@ type GetTitlesResponseObject interface { VisitGetTitlesResponse(w http.ResponseWriter) error } -type GetTitles200JSONResponse []Title +type GetTitles200JSONResponse struct { + Cursor CursorObj `json:"cursor"` + + // Data List of titles + Data []Title `json:"data"` +} func (response GetTitles200JSONResponse) VisitGetTitlesResponse(w http.ResponseWriter) error { w.Header().Set("Content-Type", "application/json") diff --git a/api/paths/titles.yaml b/api/paths/titles.yaml index 4fd010d..e868ed6 100644 --- a/api/paths/titles.yaml +++ b/api/paths/titles.yaml @@ -1,8 +1,8 @@ get: summary: Get titles parameters: - - $ref: ../parameters/cursor.yaml - - $ref: ../parameters/title_sort.yaml + - $ref: "../parameters/cursor.yaml" + - $ref: "../parameters/title_sort.yaml" - in: query name: sort_forward schema: From 34d9341e75dbdf36ebbf9258f82e5065fc38909c Mon Sep 17 00:00:00 2001 From: Iron_Felix Date: Wed, 19 Nov 2025 03:58:46 +0300 Subject: [PATCH 5/5] feat: cursor stub added --- modules/backend/handlers/titles.go | 6 +- modules/backend/queries.sql | 62 +++++++++++- sql/migrations/000001_init.up.sql | 2 +- sql/models.go | 13 ++- sql/queries.sql.go | 151 +++++++++++++++++++++++++++-- 5 files changed, 214 insertions(+), 20 deletions(-) diff --git a/modules/backend/handlers/titles.go b/modules/backend/handlers/titles.go index 46ff982..e8a3bff 100644 --- a/modules/backend/handlers/titles.go +++ b/modules/backend/handlers/titles.go @@ -218,6 +218,9 @@ func (s Server) GetTitlesTitleId(ctx context.Context, request oapi.GetTitlesTitl func (s Server) GetTitles(ctx context.Context, request oapi.GetTitlesRequestObject) (oapi.GetTitlesResponseObject, error) { opai_titles := make([]oapi.Title, 0) + cursor := oapi.CursorObj{ + Id: 1, + } word := Word2Sqlc(request.Params.Word) status, err := TitleStatus2Sqlc(request.Params.Status) @@ -237,7 +240,6 @@ func (s Server) GetTitles(ctx context.Context, request oapi.GetTitlesRequestObje Rating: request.Params.Rating, ReleaseYear: request.Params.ReleaseYear, ReleaseSeason: season, - Offset: request.Params.Offset, Limit: request.Params.Limit, }) if err != nil { @@ -258,5 +260,5 @@ func (s Server) GetTitles(ctx context.Context, request oapi.GetTitlesRequestObje opai_titles = append(opai_titles, t) } - return oapi.GetTitles200JSONResponse(opai_titles), nil + return oapi.GetTitles200JSONResponse{Cursor: cursor, Data: opai_titles}, nil } diff --git a/modules/backend/queries.sql b/modules/backend/queries.sql index 423be37..8962895 100644 --- a/modules/backend/queries.sql +++ b/modules/backend/queries.sql @@ -107,9 +107,67 @@ WHERE AND (sqlc.narg('rating')::float IS NULL OR rating >= sqlc.narg('rating')::float) AND (sqlc.narg('release_year')::int IS NULL OR release_year = sqlc.narg('release_year')::int) AND (sqlc.narg('release_season')::release_season_t IS NULL OR release_season = sqlc.narg('release_season')::release_season_t) +ORDER BY + -- Основной ключ: выбранное поле + CASE + WHEN sqlc.arg(forward)::boolean AND sqlc.arg(sort_by)::text = 'id' THEN id + WHEN sqlc.arg(forward)::boolean AND sqlc.arg(sort_by)::text = 'name' THEN name + WHEN sqlc.arg(forward)::boolean AND sqlc.arg(sort_by)::text = 'year' THEN release_year + WHEN sqlc.arg(forward)::boolean AND sqlc.arg(sort_by)::text = 'rating' THEN rating + WHEN sqlc.arg(forward)::boolean AND sqlc.arg(sort_by)::text = 'views' THEN views + END ASC, + CASE + WHEN NOT sqlc.arg(forward)::boolean AND sqlc.arg(sort_by)::text = 'id' THEN id + WHEN NOT sqlc.arg(forward)::boolean AND sqlc.arg(sort_by)::text = 'name' THEN name + WHEN NOT sqlc.arg(forward)::boolean AND sqlc.arg(sort_by)::text = 'year' THEN release_year + WHEN NOT sqlc.arg(forward)::boolean AND sqlc.arg(sort_by)::text = 'rating' THEN rating + WHEN NOT sqlc.arg(forward)::boolean AND sqlc.arg(sort_by)::text = 'views' THEN views + END DESC, -LIMIT COALESCE(sqlc.narg('limit')::int, 100) -- 100 is default limit -OFFSET sqlc.narg('offset')::int; + -- Вторичный ключ: id — только если НЕ сортируем по id + CASE + WHEN sqlc.arg(sort_by)::text != 'id' AND sqlc.arg(forward)::boolean THEN id + END ASC, + CASE + WHEN sqlc.arg(sort_by)::text != 'id' AND NOT sqlc.arg(forward)::boolean THEN id + END DESC +LIMIT COALESCE(sqlc.narg('limit')::int, 100); -- 100 is default limit +-- OFFSET sqlc.narg('offset')::int; + +-- name: SearchUserTitles :many +SELECT + * +FROM usertitles as u +JOIN titles as t ON (u.title_id = t.id) +WHERE + CASE + WHEN sqlc.narg('word')::text IS NOT NULL THEN + ( + SELECT bool_and( + EXISTS ( + SELECT 1 + FROM jsonb_each_text(t.title_names) AS t(key, val) + WHERE val ILIKE pattern + ) + ) + FROM unnest( + ARRAY( + SELECT '%' || trim(w) || '%' + FROM unnest(string_to_array(sqlc.narg('word')::text, ' ')) AS w + WHERE trim(w) <> '' + ) + ) AS pattern + ) + ELSE true + END + + AND (sqlc.narg('status')::title_status_t IS NULL OR t.title_status = sqlc.narg('status')::title_status_t) + AND (sqlc.narg('rating')::float IS NULL OR t.rating >= sqlc.narg('rating')::float) + AND (sqlc.narg('release_year')::int IS NULL OR t.release_year = sqlc.narg('release_year')::int) + AND (sqlc.narg('release_season')::release_season_t IS NULL OR t.release_season = sqlc.narg('release_season')::release_season_t) + AND (sqlc.narg('usertitle_status')::usertitle_status_t IS NULL OR u.usertitle_status = sqlc.narg('usertitle_status')::usertitle_status_t) + +LIMIT COALESCE(sqlc.narg('limit')::int, 100); -- 100 is default limit -- -- name: ListTitles :many -- SELECT title_id, title_names, studio_id, poster_id, signal_ids, diff --git a/sql/migrations/000001_init.up.sql b/sql/migrations/000001_init.up.sql index 49cca3d..e6ed628 100644 --- a/sql/migrations/000001_init.up.sql +++ b/sql/migrations/000001_init.up.sql @@ -59,7 +59,7 @@ CREATE TABLE studios ( ); CREATE TABLE titles ( - // TODO: anime type (film, season etc) + -- // TODO: anime type (film, season etc) id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY, -- example {"ru": ["Атака титанов", "Атака Титанов"],"en": ["Attack on Titan", "AoT"],"ja": ["進撃の巨人", "しんげきのきょじん"]} title_names jsonb NOT NULL, diff --git a/sql/models.go b/sql/models.go index a593504..93cecca 100644 --- a/sql/models.go +++ b/sql/models.go @@ -212,7 +212,6 @@ type Review struct { ID int64 `json:"id"` Data string `json:"data"` Rating *int32 `json:"rating"` - IllustID *int64 `json:"illust_id"` UserID *int64 `json:"user_id"` TitleID *int64 `json:"title_id"` CreatedAt pgtype.Timestamptz `json:"created_at"` @@ -277,10 +276,10 @@ type User struct { } type Usertitle struct { - UserID int64 `json:"user_id"` - TitleID int64 `json:"title_id"` - Status UsertitleStatusT `json:"status"` - Rate *int32 `json:"rate"` - ReviewText *string `json:"review_text"` - ReviewDate pgtype.Timestamptz `json:"review_date"` + UserID int64 `json:"user_id"` + TitleID int64 `json:"title_id"` + Status UsertitleStatusT `json:"status"` + Rate *int32 `json:"rate"` + ReviewID *int64 `json:"review_id"` + Ctime pgtype.Timestamptz `json:"ctime"` } diff --git a/sql/queries.sql.go b/sql/queries.sql.go index c5e6f8a..4e28f40 100644 --- a/sql/queries.sql.go +++ b/sql/queries.sql.go @@ -8,6 +8,8 @@ package sqlc import ( "context" "time" + + "github.com/jackc/pgx/v5/pgtype" ) const createImage = `-- name: CreateImage :one @@ -31,7 +33,7 @@ func (q *Queries) CreateImage(ctx context.Context, arg CreateImageParams) (Image const getImageByID = `-- name: GetImageByID :one SELECT id, storage_type, image_path FROM images -WHERE id = $1 +WHERE id = $1::bigint ` func (q *Queries) GetImageByID(ctx context.Context, illustID int64) (Image, error) { @@ -44,11 +46,13 @@ func (q *Queries) GetImageByID(ctx context.Context, illustID int64) (Image, erro const getReviewByID = `-- name: GetReviewByID :one -SELECT id, data, rating, illust_id, user_id, title_id, created_at + +SELECT id, data, rating, user_id, title_id, created_at FROM reviews WHERE review_id = $1::bigint ` +// 100 is default limit // -- name: ListTitles :many // SELECT title_id, title_names, studio_id, poster_id, signal_ids, // @@ -82,7 +86,6 @@ func (q *Queries) GetReviewByID(ctx context.Context, reviewID int64) (Review, er &i.ID, &i.Data, &i.Rating, - &i.IllustID, &i.UserID, &i.TitleID, &i.CreatedAt, @@ -312,9 +315,18 @@ WHERE AND ($3::float IS NULL OR rating >= $3::float) AND ($4::int IS NULL OR release_year = $4::int) AND ($5::release_season_t IS NULL OR release_season = $5::release_season_t) - -LIMIT COALESCE($7::int, 100) -- 100 is default limit -OFFSET $6::int +ORDER BY CASE + WHEN $6::boolean AND $7::text = 'name' THEN name + WHEN $8::boolean AND $7::text = 'id' THEN id + WHEN $8::boolean AND $7::text = 'name' THEN name + WHEN $8::boolean AND $7::text = 'id' THEN id +END ASC, CASE + WHEN NOT $8::boolean AND $7::text = 'name' THEN name + WHEN NOT $8::boolean AND $7::text = 'id' THEN id + WHEN NOT $8::boolean AND $7::text = 'name' THEN name + WHEN NOT $8::boolean AND $7::text = 'id' THEN id +END DESC +LIMIT COALESCE($9::int, 100) ` type SearchTitlesParams struct { @@ -323,7 +335,9 @@ type SearchTitlesParams struct { Rating *float64 `json:"rating"` ReleaseYear *int32 `json:"release_year"` ReleaseSeason *ReleaseSeasonT `json:"release_season"` - Offset *int32 `json:"offset"` + Forward bool `json:"forward"` + OrderBy string `json:"order_by"` + Reverse bool `json:"reverse"` Limit *int32 `json:"limit"` } @@ -334,7 +348,9 @@ func (q *Queries) SearchTitles(ctx context.Context, arg SearchTitlesParams) ([]T arg.Rating, arg.ReleaseYear, arg.ReleaseSeason, - arg.Offset, + arg.Forward, + arg.OrderBy, + arg.Reverse, arg.Limit, ) if err != nil { @@ -368,3 +384,122 @@ func (q *Queries) SearchTitles(ctx context.Context, arg SearchTitlesParams) ([]T } return items, nil } + +const searchUserTitles = `-- name: SearchUserTitles :many + +SELECT + user_id, title_id, status, rate, review_id, ctime, id, title_names, studio_id, poster_id, title_status, rating, rating_count, release_year, release_season, season, episodes_aired, episodes_all, episodes_len +FROM usertitles as u +JOIN titles as t ON (u.title_id = t.id) +WHERE + CASE + WHEN $1::text IS NOT NULL THEN + ( + SELECT bool_and( + EXISTS ( + SELECT 1 + FROM jsonb_each_text(t.title_names) AS t(key, val) + WHERE val ILIKE pattern + ) + ) + FROM unnest( + ARRAY( + SELECT '%' || trim(w) || '%' + FROM unnest(string_to_array($1::text, ' ')) AS w + WHERE trim(w) <> '' + ) + ) AS pattern + ) + ELSE true + END + + AND ($2::title_status_t IS NULL OR t.title_status = $2::title_status_t) + AND ($3::float IS NULL OR t.rating >= $3::float) + AND ($4::int IS NULL OR t.release_year = $4::int) + AND ($5::release_season_t IS NULL OR t.release_season = $5::release_season_t) + AND ($6::usertitle_status_t IS NULL OR u.usertitle_status = $6::usertitle_status_t) + +LIMIT COALESCE($7::int, 100) +` + +type SearchUserTitlesParams struct { + Word *string `json:"word"` + Status *TitleStatusT `json:"status"` + Rating *float64 `json:"rating"` + ReleaseYear *int32 `json:"release_year"` + ReleaseSeason *ReleaseSeasonT `json:"release_season"` + UsertitleStatus NullUsertitleStatusT `json:"usertitle_status"` + Limit *int32 `json:"limit"` +} + +type SearchUserTitlesRow struct { + UserID int64 `json:"user_id"` + TitleID int64 `json:"title_id"` + Status UsertitleStatusT `json:"status"` + Rate *int32 `json:"rate"` + ReviewID *int64 `json:"review_id"` + Ctime pgtype.Timestamptz `json:"ctime"` + ID int64 `json:"id"` + TitleNames []byte `json:"title_names"` + StudioID int64 `json:"studio_id"` + PosterID *int64 `json:"poster_id"` + TitleStatus TitleStatusT `json:"title_status"` + Rating *float64 `json:"rating"` + RatingCount *int32 `json:"rating_count"` + ReleaseYear *int32 `json:"release_year"` + ReleaseSeason *ReleaseSeasonT `json:"release_season"` + Season *int32 `json:"season"` + EpisodesAired *int32 `json:"episodes_aired"` + EpisodesAll *int32 `json:"episodes_all"` + EpisodesLen []byte `json:"episodes_len"` +} + +// 100 is default limit +// OFFSET sqlc.narg('offset')::int; +func (q *Queries) SearchUserTitles(ctx context.Context, arg SearchUserTitlesParams) ([]SearchUserTitlesRow, error) { + rows, err := q.db.Query(ctx, searchUserTitles, + arg.Word, + arg.Status, + arg.Rating, + arg.ReleaseYear, + arg.ReleaseSeason, + arg.UsertitleStatus, + arg.Limit, + ) + if err != nil { + return nil, err + } + defer rows.Close() + var items []SearchUserTitlesRow + for rows.Next() { + var i SearchUserTitlesRow + if err := rows.Scan( + &i.UserID, + &i.TitleID, + &i.Status, + &i.Rate, + &i.ReviewID, + &i.Ctime, + &i.ID, + &i.TitleNames, + &i.StudioID, + &i.PosterID, + &i.TitleStatus, + &i.Rating, + &i.RatingCount, + &i.ReleaseYear, + &i.ReleaseSeason, + &i.Season, + &i.EpisodesAired, + &i.EpisodesAll, + &i.EpisodesLen, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +}