From eed50a3f395eb825d1ab3cd199fc0541cabac4f0 Mon Sep 17 00:00:00 2001 From: Le Dien Phuc Date: Tue, 10 Oct 2023 09:01:43 +0700 Subject: [PATCH] feat: tono command permission manage (#1214) --- docs/docs.go | 122 +++++++++++++++++- docs/swagger.json | 122 +++++++++++++++++- docs/swagger.yaml | 100 +++++++++++--- ...714-add-tono-command-permissions-table.sql | 13 ++ pkg/entities/tono.go | 13 ++ pkg/handler/handler.go | 3 + pkg/handler/tono/interface.go | 7 + pkg/handler/tono/tono.go | 51 ++++++++ pkg/model/tono_command_permission.go | 13 ++ pkg/repo/pg/repo.go | 2 + pkg/repo/repo.go | 2 + pkg/repo/tono_command_permission/pg.go | 24 ++++ pkg/repo/tono_command_permission/query.go | 5 + pkg/repo/tono_command_permission/store.go | 7 + pkg/request/tono_command_permission.go | 5 + pkg/response/tono.go | 7 + pkg/routes/v1.go | 6 + 17 files changed, 473 insertions(+), 29 deletions(-) create mode 100644 migrations/schemas/20231010071714-add-tono-command-permissions-table.sql create mode 100644 pkg/entities/tono.go create mode 100644 pkg/handler/tono/interface.go create mode 100644 pkg/handler/tono/tono.go create mode 100644 pkg/model/tono_command_permission.go create mode 100644 pkg/repo/tono_command_permission/pg.go create mode 100644 pkg/repo/tono_command_permission/query.go create mode 100644 pkg/repo/tono_command_permission/store.go create mode 100644 pkg/request/tono_command_permission.go create mode 100644 pkg/response/tono.go diff --git a/docs/docs.go b/docs/docs.go index 0a1a6f3f..fd25664f 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -5112,6 +5112,36 @@ const docTemplate = `{ } } }, + "/tono/command-permissions": { + "get": { + "description": "Get Tono command permissions", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Tono" + ], + "summary": "Get Tono command permissions", + "parameters": [ + { + "type": "string", + "name": "code", + "in": "query" + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/response.TonoCommandPermissions" + } + } + } + } + }, "/users": { "post": { "description": "Create User", @@ -7382,6 +7412,32 @@ const docTemplate = `{ } } }, + "model.TonoCommandPermission": { + "type": "object", + "properties": { + "code": { + "type": "string" + }, + "created_at": { + "type": "string" + }, + "description": { + "type": "string" + }, + "discord_permission_flag": { + "type": "string" + }, + "id": { + "type": "integer" + }, + "need_dm": { + "type": "boolean" + }, + "updated_at": { + "type": "string" + } + } + }, "model.User": { "type": "object", "properties": { @@ -8428,6 +8484,9 @@ const docTemplate = `{ "channel_name": { "type": "string" }, + "channel_url": { + "type": "string" + }, "each": { "type": "boolean" }, @@ -9501,7 +9560,7 @@ const docTemplate = `{ "address": { "type": "string" }, - "created_at": { + "createdAt": { "type": "string" }, "holders": { @@ -9513,22 +9572,22 @@ const docTemplate = `{ "price": { "type": "number" }, - "price_change_percentage": { + "priceChangePercentage": { "type": "number" }, - "profile_checked": { + "profileChecked": { "type": "boolean" }, "supply": { "type": "integer" }, - "twitter_pfp_url": { + "twitterPfpUrl": { "type": "string" }, - "twitter_username": { + "twitterUsername": { "type": "string" }, - "updated_at": { + "updatedAt": { "type": "string" } } @@ -9552,7 +9611,7 @@ const docTemplate = `{ "type": "string" }, "metadata": { - "$ref": "#/definitions/response.FriendTechKey" + "$ref": "#/definitions/response.TrackedFriendTechKey" }, "profile_id": { "type": "string" @@ -12213,6 +12272,17 @@ const docTemplate = `{ } } }, + "response.TonoCommandPermissions": { + "type": "object", + "properties": { + "data": { + "type": "array", + "items": { + "$ref": "#/definitions/model.TonoCommandPermission" + } + } + } + }, "response.TopUser": { "type": "object", "properties": { @@ -12238,6 +12308,44 @@ const docTemplate = `{ } } }, + "response.TrackedFriendTechKey": { + "type": "object", + "properties": { + "address": { + "type": "string" + }, + "created_at": { + "type": "string" + }, + "holders": { + "type": "integer" + }, + "id": { + "type": "integer" + }, + "price": { + "type": "number" + }, + "price_change_percentage": { + "type": "number" + }, + "profile_checked": { + "type": "boolean" + }, + "supply": { + "type": "integer" + }, + "twitter_pfp_url": { + "type": "string" + }, + "twitter_username": { + "type": "string" + }, + "updated_at": { + "type": "string" + } + } + }, "response.TransferTokenV2Data": { "type": "object", "properties": { diff --git a/docs/swagger.json b/docs/swagger.json index 7a51a406..621038ba 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -5104,6 +5104,36 @@ } } }, + "/tono/command-permissions": { + "get": { + "description": "Get Tono command permissions", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Tono" + ], + "summary": "Get Tono command permissions", + "parameters": [ + { + "type": "string", + "name": "code", + "in": "query" + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/response.TonoCommandPermissions" + } + } + } + } + }, "/users": { "post": { "description": "Create User", @@ -7374,6 +7404,32 @@ } } }, + "model.TonoCommandPermission": { + "type": "object", + "properties": { + "code": { + "type": "string" + }, + "created_at": { + "type": "string" + }, + "description": { + "type": "string" + }, + "discord_permission_flag": { + "type": "string" + }, + "id": { + "type": "integer" + }, + "need_dm": { + "type": "boolean" + }, + "updated_at": { + "type": "string" + } + } + }, "model.User": { "type": "object", "properties": { @@ -8420,6 +8476,9 @@ "channel_name": { "type": "string" }, + "channel_url": { + "type": "string" + }, "each": { "type": "boolean" }, @@ -9493,7 +9552,7 @@ "address": { "type": "string" }, - "created_at": { + "createdAt": { "type": "string" }, "holders": { @@ -9505,22 +9564,22 @@ "price": { "type": "number" }, - "price_change_percentage": { + "priceChangePercentage": { "type": "number" }, - "profile_checked": { + "profileChecked": { "type": "boolean" }, "supply": { "type": "integer" }, - "twitter_pfp_url": { + "twitterPfpUrl": { "type": "string" }, - "twitter_username": { + "twitterUsername": { "type": "string" }, - "updated_at": { + "updatedAt": { "type": "string" } } @@ -9544,7 +9603,7 @@ "type": "string" }, "metadata": { - "$ref": "#/definitions/response.FriendTechKey" + "$ref": "#/definitions/response.TrackedFriendTechKey" }, "profile_id": { "type": "string" @@ -12205,6 +12264,17 @@ } } }, + "response.TonoCommandPermissions": { + "type": "object", + "properties": { + "data": { + "type": "array", + "items": { + "$ref": "#/definitions/model.TonoCommandPermission" + } + } + } + }, "response.TopUser": { "type": "object", "properties": { @@ -12230,6 +12300,44 @@ } } }, + "response.TrackedFriendTechKey": { + "type": "object", + "properties": { + "address": { + "type": "string" + }, + "created_at": { + "type": "string" + }, + "holders": { + "type": "integer" + }, + "id": { + "type": "integer" + }, + "price": { + "type": "number" + }, + "price_change_percentage": { + "type": "number" + }, + "profile_checked": { + "type": "boolean" + }, + "supply": { + "type": "integer" + }, + "twitter_pfp_url": { + "type": "string" + }, + "twitter_username": { + "type": "string" + }, + "updated_at": { + "type": "string" + } + } + }, "response.TransferTokenV2Data": { "type": "object", "properties": { diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 1d0be848..9ef9f20e 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -753,6 +753,23 @@ definitions: symbol: type: string type: object + model.TonoCommandPermission: + properties: + code: + type: string + created_at: + type: string + description: + type: string + discord_permission_flag: + type: string + id: + type: integer + need_dm: + type: boolean + updated_at: + type: string + type: object model.User: properties: discriminator: @@ -1444,6 +1461,8 @@ definitions: type: string channel_name: type: string + channel_url: + type: string each: type: boolean guild_id: @@ -2144,7 +2163,7 @@ definitions: properties: address: type: string - created_at: + createdAt: type: string holders: type: integer @@ -2152,17 +2171,17 @@ definitions: type: integer price: type: number - price_change_percentage: + priceChangePercentage: type: number - profile_checked: + profileChecked: type: boolean supply: type: integer - twitter_pfp_url: + twitterPfpUrl: type: string - twitter_username: + twitterUsername: type: string - updated_at: + updatedAt: type: string type: object response.FriendTechKeyWatchlistItemResponse: @@ -2178,7 +2197,7 @@ definitions: key_address: type: string metadata: - $ref: '#/definitions/response.FriendTechKey' + $ref: '#/definitions/response.TrackedFriendTechKey' profile_id: type: string updated_at: @@ -2664,14 +2683,14 @@ definitions: type: string usd: type: number - usd_14d_change: - type: number usd_1h_change: type: number usd_1y_change: type: number usd_7d_change: type: number + usd_14d_change: + type: number usd_24h_change: type: number usd_24h_vol: @@ -3292,21 +3311,21 @@ definitions: additionalProperties: type: number type: object - price_change_percentage_7d: + price_change_percentage_1h: type: number - price_change_percentage_7d_in_currency: + price_change_percentage_1h_in_currency: additionalProperties: type: number type: object - price_change_percentage_14d: + price_change_percentage_7d: type: number - price_change_percentage_14d_in_currency: + price_change_percentage_7d_in_currency: additionalProperties: type: number type: object - price_change_percentage_1h: + price_change_percentage_14d: type: number - price_change_percentage_1h_in_currency: + price_change_percentage_14d_in_currency: additionalProperties: type: number type: object @@ -3909,6 +3928,13 @@ definitions: value: type: number type: object + response.TonoCommandPermissions: + properties: + data: + items: + $ref: '#/definitions/model.TonoCommandPermission' + type: array + type: object response.TopUser: properties: author: @@ -3925,6 +3951,31 @@ definitions: data: $ref: '#/definitions/response.FriendTechKeyWatchlistItemResponse' type: object + response.TrackedFriendTechKey: + properties: + address: + type: string + created_at: + type: string + holders: + type: integer + id: + type: integer + price: + type: number + price_change_percentage: + type: number + profile_checked: + type: boolean + supply: + type: integer + twitter_pfp_url: + type: string + twitter_username: + type: string + updated_at: + type: string + type: object response.TransferTokenV2Data: properties: amount_each: @@ -7384,6 +7435,25 @@ paths: summary: OffChain Tip Bot - Transfer token v2 tags: - Tip + /tono/command-permissions: + get: + consumes: + - application/json + description: Get Tono command permissions + parameters: + - in: query + name: code + type: string + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/response.TonoCommandPermissions' + summary: Get Tono command permissions + tags: + - Tono /users: post: consumes: diff --git a/migrations/schemas/20231010071714-add-tono-command-permissions-table.sql b/migrations/schemas/20231010071714-add-tono-command-permissions-table.sql new file mode 100644 index 00000000..6a2468e8 --- /dev/null +++ b/migrations/schemas/20231010071714-add-tono-command-permissions-table.sql @@ -0,0 +1,13 @@ +-- +migrate Up +CREATE TABLE IF NOT EXISTS "tono_command_permissions" ( + id SERIAL PRIMARY KEY, + code TEXT, + discord_permission_flag DECIMAL, + description text, + need_dm BOOL, + created_at TIMESTAMPTZ DEFAULT NOW(), + updated_at TIMESTAMPTZ DEFAULT NOW() +); + +-- +migrate Down +DROP TABLE IF EXISTS "tono_command_permissions"; \ No newline at end of file diff --git a/pkg/entities/tono.go b/pkg/entities/tono.go new file mode 100644 index 00000000..30c49a3c --- /dev/null +++ b/pkg/entities/tono.go @@ -0,0 +1,13 @@ +package entities + +import ( + "github.com/defipod/mochi/pkg/model" + tonocommandpermission "github.com/defipod/mochi/pkg/repo/tono_command_permission" + "github.com/defipod/mochi/pkg/request" +) + +func (e *Entity) GetTonoCommandPermissions(req request.TonoCommandPermissionsRequest) ([]model.TonoCommandPermission, error) { + return e.repo.TonoCommandPermission.List(tonocommandpermission.ListQuery{ + Code: req.Code, + }) +} diff --git a/pkg/handler/handler.go b/pkg/handler/handler.go index 205b8354..6ec4ea02 100644 --- a/pkg/handler/handler.go +++ b/pkg/handler/handler.go @@ -24,6 +24,7 @@ import ( productdata "github.com/defipod/mochi/pkg/handler/product-data" "github.com/defipod/mochi/pkg/handler/swap" "github.com/defipod/mochi/pkg/handler/tip" + "github.com/defipod/mochi/pkg/handler/tono" "github.com/defipod/mochi/pkg/handler/user" "github.com/defipod/mochi/pkg/handler/vault" "github.com/defipod/mochi/pkg/handler/verify" @@ -62,6 +63,7 @@ type Handler struct { ProductData productdata.IHandler Dexes dexes.IHandler Onboarding onboarding.IHandler + Tono tono.IHandler } func New(entities *entities.Entity, logger logger.Logger) *Handler { @@ -94,5 +96,6 @@ func New(entities *entities.Entity, logger logger.Logger) *Handler { ProductData: productdata.New(entities, logger), Dexes: dexes.New(entities, logger), Onboarding: onboarding.New(entities, logger), + Tono: tono.New(entities, logger), } } diff --git a/pkg/handler/tono/interface.go b/pkg/handler/tono/interface.go new file mode 100644 index 00000000..c7f47da9 --- /dev/null +++ b/pkg/handler/tono/interface.go @@ -0,0 +1,7 @@ +package tono + +import "github.com/gin-gonic/gin" + +type IHandler interface { + TonoCommandPermissions(c *gin.Context) +} diff --git a/pkg/handler/tono/tono.go b/pkg/handler/tono/tono.go new file mode 100644 index 00000000..526f9b20 --- /dev/null +++ b/pkg/handler/tono/tono.go @@ -0,0 +1,51 @@ +package tono + +import ( + "net/http" + + "github.com/gin-gonic/gin" + + "github.com/defipod/mochi/pkg/entities" + "github.com/defipod/mochi/pkg/logger" + "github.com/defipod/mochi/pkg/request" + "github.com/defipod/mochi/pkg/response" +) + +type Handler struct { + entities *entities.Entity + log logger.Logger +} + +func New(entities *entities.Entity, logger logger.Logger) IHandler { + return &Handler{ + entities: entities, + log: logger, + } +} + +// TonoCommandPermissions godoc +// @Summary Get Tono command permissions +// @Description Get Tono command permissions +// @Tags Tono +// @Accept json +// @Produce json +// @Param req query request.TonoCommandPermissionsRequest true "request" +// @Success 200 {object} response.TonoCommandPermissions +// @Router /tono/command-permissions [get] +func (h *Handler) TonoCommandPermissions(c *gin.Context) { + req := request.TonoCommandPermissionsRequest{} + if err := c.ShouldBindQuery(&req); err != nil { + h.log.Error(err, "[handler.TonoCommandPermissions] ShouldBindQuery() failed") + c.JSON(http.StatusBadRequest, response.CreateResponse[any](nil, nil, err, nil)) + return + } + + data, err := h.entities.GetTonoCommandPermissions(req) + if err != nil { + h.log.Error(err, "[handler.TonoCommandPermissions] entities.GetTonoCommandPermissions() failed") + c.JSON(http.StatusInternalServerError, response.CreateResponse[any](nil, nil, err, nil)) + return + } + + c.JSON(http.StatusOK, response.CreateResponse[any](data, nil, nil, nil)) +} diff --git a/pkg/model/tono_command_permission.go b/pkg/model/tono_command_permission.go new file mode 100644 index 00000000..7d348bab --- /dev/null +++ b/pkg/model/tono_command_permission.go @@ -0,0 +1,13 @@ +package model + +import "time" + +type TonoCommandPermission struct { + ID int `json:"id"` + Code string `json:"code"` + DiscordPermissionFlag string `json:"discord_permission_flag"` + Description string `json:"description"` + NeedDm bool `json:"need_dm"` + CreatedAt time.Time `json:"created_at"` + UpdatedAt time.Time `json:"updated_at"` +} diff --git a/pkg/repo/pg/repo.go b/pkg/repo/pg/repo.go index 2fb15cc5..7d3350c5 100644 --- a/pkg/repo/pg/repo.go +++ b/pkg/repo/pg/repo.go @@ -72,6 +72,7 @@ import ( questuserreward "github.com/defipod/mochi/pkg/repo/quest_user_reward" "github.com/defipod/mochi/pkg/repo/token" tokeninfo "github.com/defipod/mochi/pkg/repo/token_info" + tonocommandpermission "github.com/defipod/mochi/pkg/repo/tono_command_permission" userfeedback "github.com/defipod/mochi/pkg/repo/user_feedback" usernftbalance "github.com/defipod/mochi/pkg/repo/user_nft_balance" usernftwatchlistitem "github.com/defipod/mochi/pkg/repo/user_nft_watchlist_items" @@ -178,5 +179,6 @@ func NewRepo(db *gorm.DB) *repo.Repo { FriendTechKeyWatchlistItem: friendfechkeywatchlistitem.NewPG(db), ProductHashtag: producthashtag.NewPG(db), ProfileCommandUsage: profilecommandusage.NewPG(db), + TonoCommandPermission: tonocommandpermission.NewPG(db), } } diff --git a/pkg/repo/repo.go b/pkg/repo/repo.go index daaddc12..20f67a5a 100644 --- a/pkg/repo/repo.go +++ b/pkg/repo/repo.go @@ -69,6 +69,7 @@ import ( questuserreward "github.com/defipod/mochi/pkg/repo/quest_user_reward" token "github.com/defipod/mochi/pkg/repo/token" tokeninfo "github.com/defipod/mochi/pkg/repo/token_info" + tonocommandpermission "github.com/defipod/mochi/pkg/repo/tono_command_permission" userfeedback "github.com/defipod/mochi/pkg/repo/user_feedback" usernftbalance "github.com/defipod/mochi/pkg/repo/user_nft_balance" usernftwatchlistitem "github.com/defipod/mochi/pkg/repo/user_nft_watchlist_items" @@ -173,4 +174,5 @@ type Repo struct { FriendTechKeyWatchlistItem friendtechkeywatchlistitem.Store ProductHashtag producthashtag.Store ProfileCommandUsage profilecommandusage.Store + TonoCommandPermission tonocommandpermission.Store } diff --git a/pkg/repo/tono_command_permission/pg.go b/pkg/repo/tono_command_permission/pg.go new file mode 100644 index 00000000..dc19dcb4 --- /dev/null +++ b/pkg/repo/tono_command_permission/pg.go @@ -0,0 +1,24 @@ +package tonocommandpermission + +import ( + "gorm.io/gorm" + + "github.com/defipod/mochi/pkg/model" +) + +type pg struct { + db *gorm.DB +} + +func NewPG(db *gorm.DB) Store { + return &pg{db: db} +} + +func (pg *pg) List(q ListQuery) (cmds []model.TonoCommandPermission, err error) { + db := pg.db + if q.Code != "" { + db = db.Where("lower(code) = lower(?)", q.Code) + } + + return cmds, db.Find(&cmds).Error +} diff --git a/pkg/repo/tono_command_permission/query.go b/pkg/repo/tono_command_permission/query.go new file mode 100644 index 00000000..bcaf1bf8 --- /dev/null +++ b/pkg/repo/tono_command_permission/query.go @@ -0,0 +1,5 @@ +package tonocommandpermission + +type ListQuery struct { + Code string +} diff --git a/pkg/repo/tono_command_permission/store.go b/pkg/repo/tono_command_permission/store.go new file mode 100644 index 00000000..d432fb42 --- /dev/null +++ b/pkg/repo/tono_command_permission/store.go @@ -0,0 +1,7 @@ +package tonocommandpermission + +import "github.com/defipod/mochi/pkg/model" + +type Store interface { + List(ListQuery) ([]model.TonoCommandPermission, error) +} diff --git a/pkg/request/tono_command_permission.go b/pkg/request/tono_command_permission.go new file mode 100644 index 00000000..40c06c43 --- /dev/null +++ b/pkg/request/tono_command_permission.go @@ -0,0 +1,5 @@ +package request + +type TonoCommandPermissionsRequest struct { + Code string `form:"code"` +} diff --git a/pkg/response/tono.go b/pkg/response/tono.go new file mode 100644 index 00000000..e0c1ce61 --- /dev/null +++ b/pkg/response/tono.go @@ -0,0 +1,7 @@ +package response + +import "github.com/defipod/mochi/pkg/model" + +type TonoCommandPermissions struct { + Data []model.TonoCommandPermission `json:"data"` +} diff --git a/pkg/routes/v1.go b/pkg/routes/v1.go index cc17df74..20c4116d 100644 --- a/pkg/routes/v1.go +++ b/pkg/routes/v1.go @@ -486,4 +486,10 @@ func NewRoutes(r *gin.Engine, h *handler.Handler, cfg config.Config) { { onboardingGroup.POST("/start", h.Onboarding.Start) } + + // Tono's api + tonoGroup := v1.Group("tono") + { + tonoGroup.GET("/command-permissions", h.Tono.TonoCommandPermissions) + } }