From 89f547a3bfd65f4650c49ef02a9f70e5369c187b Mon Sep 17 00:00:00 2001 From: Shikong <919411476@qq.com> Date: Sun, 13 Nov 2022 18:14:50 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=93=E6=9E=84=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/auth/auth.go | 14 ++-- controller/test/test.go | 11 ++- controller/user/user.go | 10 ++- docs/docs.go | 101 +++++++++++------------ docs/swagger.json | 101 +++++++++++------------ docs/swagger.yaml | 80 +++++++++--------- model/{dto/auth.go => auth/dto.go} | 6 +- model/{vo/auth.go => auth/vo.go} | 4 +- model/{dto/captcha.go => captcha/dto.go} | 2 +- model/{vo/captcha.go => captcha/vo.go} | 2 +- model/generic/generate/config.yml | 2 + model/generic/models/reply.go | 21 +++++ model/generic/models/topical.go | 20 +++++ model/topical/dto.go | 1 + services/auth/auth.go | 11 ++- services/services.go | 2 + services/topical/topical.go | 39 +++++++++ 17 files changed, 244 insertions(+), 183 deletions(-) rename model/{dto/auth.go => auth/dto.go} (90%) rename model/{vo/auth.go => auth/vo.go} (84%) rename model/{dto/captcha.go => captcha/dto.go} (93%) rename model/{vo/captcha.go => captcha/vo.go} (94%) create mode 100644 model/generic/models/reply.go create mode 100644 model/generic/models/topical.go create mode 100644 model/topical/dto.go create mode 100644 services/topical/topical.go diff --git a/controller/auth/auth.go b/controller/auth/auth.go index 39133c9..3623946 100644 --- a/controller/auth/auth.go +++ b/controller/auth/auth.go @@ -6,7 +6,7 @@ import ( "gofiber.study.skcks.cn/common/response" "gofiber.study.skcks.cn/controller/types" "gofiber.study.skcks.cn/global" - "gofiber.study.skcks.cn/model/dto" + auth2 "gofiber.study.skcks.cn/model/auth" "gofiber.study.skcks.cn/services/auth" ) @@ -31,13 +31,13 @@ func NewController(app *fiber.App) *Controller { // @Tags Auth // @Accept json // @Produce json -// @Param vo body dto.Login true "用户登录" -// @Success 200 {object} response.Response{data=vo.Login} +// @Param vo body auth.LoginDTO true "用户登录" +// @Success 200 {object} response.Response{data=auth.LoginVO} // @Failure default {object} errorx.CodeErrorResponse // @Router /auth/login [post] func (c *Controller) Login() { c.Router.Post("login", func(ctx *fiber.Ctx) error { - login := &dto.Login{} + login := &auth2.LoginDTO{} err := ctx.BodyParser(login) if err = errorx.ParseError(err); err != nil { return ctx.JSON(err) @@ -59,13 +59,13 @@ func (c *Controller) Login() { // @Tags Auth // @Accept json // @Produce json -// @Param vo body dto.RefreshToken true "刷新令牌" -// @Success 200 {object} response.Response{data=vo.Login} +// @Param vo body auth.RefreshToken true "刷新令牌" +// @Success 200 {object} response.Response{data=auth.LoginVO} // @Failure default {object} errorx.CodeErrorResponse // @Router /auth/refreshToken [post] func (c *Controller) RefreshToken() { c.Router.Post("refreshToken", func(ctx *fiber.Ctx) error { - refresh := &dto.RefreshToken{} + refresh := &auth2.RefreshToken{} err := ctx.BodyParser(refresh) if err = errorx.ParseErrorWithCode(fiber.StatusInternalServerError, err); err != nil { return ctx.JSON(err) diff --git a/controller/test/test.go b/controller/test/test.go index 8b4eabf..cb27e28 100644 --- a/controller/test/test.go +++ b/controller/test/test.go @@ -8,8 +8,7 @@ import ( "gofiber.study.skcks.cn/common/response" "gofiber.study.skcks.cn/controller/types" "gofiber.study.skcks.cn/global" - "gofiber.study.skcks.cn/model/dto" - "gofiber.study.skcks.cn/model/vo" + "gofiber.study.skcks.cn/model/captcha" "gofiber.study.skcks.cn/services/waf" "gofiber.study.skcks.cn/services/wol" "strconv" @@ -135,7 +134,7 @@ func (c *Controller) NanoIdTest() { // @Tags Test // @Accept json // @Produce json -// @Success 200 {object} response.Response{data=vo.Captcha} +// @Success 200 {object} response.Response{data=captcha.Captcha} // @Failure default {object} errorx.CodeErrorResponse // @Router /test/captcha/get [get] func (c *Controller) GetCaptchaTest() { @@ -147,7 +146,7 @@ func (c *Controller) GetCaptchaTest() { return ctx.JSON(errorx.NewDefaultError("验证码生成失败")) } - return ctx.JSON(response.NewResponse(vo.Captcha{ + return ctx.JSON(response.NewResponse(captcha.Captcha{ Id: id, Base64: item.EncodeB64string(), Expire: time.Now().Add(time.Duration(global.Config.Captcha.Expire) * time.Second).Unix(), @@ -162,13 +161,13 @@ func (c *Controller) GetCaptchaTest() { // @Tags Test // @Accept json // @Produce json -// @Param vo body dto.VerifyCaptcha true "验证码验证" +// @Param vo body captcha.VerifyCaptcha true "验证码验证" // @Success 200 {object} response.Response{data=string} // @Failure default {object} errorx.CodeErrorResponse // @Router /test/captcha/verify [post] func (c *Controller) VerifyCaptchaTest() { c.Router.Add(fiber.MethodPost, "/captcha/verify", func(ctx *fiber.Ctx) error { - verifyCaptcha := &dto.VerifyCaptcha{} + verifyCaptcha := &captcha.VerifyCaptcha{} if err := errorx.ParseError(ctx.BodyParser(verifyCaptcha)); err != nil { return ctx.JSON(err) diff --git a/controller/user/user.go b/controller/user/user.go index b8a5a79..20d074c 100644 --- a/controller/user/user.go +++ b/controller/user/user.go @@ -5,6 +5,7 @@ import ( "gofiber.study.skcks.cn/common/errorx" "gofiber.study.skcks.cn/common/response" "gofiber.study.skcks.cn/controller/types" + "gofiber.study.skcks.cn/global" "gofiber.study.skcks.cn/middleware" "gofiber.study.skcks.cn/services/user" ) @@ -25,19 +26,20 @@ func NewController(app *fiber.App) *Controller { // GetByAccount 根据 账号 获取用户信息 // -// @Summary 根据 账号 获取用户信息 -// @Description 根据 账号 获取用户信息 +// @Summary 获取当前用户信息 +// @Description 获取当前用户信息 // @Tags User // @Accept json // @Produce json -// @Param account query string true "账号名称" // @Success 200 {object} response.Response{data=models.User} // @Failure default {object} errorx.CodeErrorResponse // @Router /user/account [get] // @Security Token func (c *Controller) GetByAccount() { c.Router.Get("/account", func(ctx *fiber.Ctx) error { - account := ctx.Query("account") + token := ctx.GetReqHeaders()["Token"] + claim, _ := global.ParseToken(token) + account := claim.Account u, err := user.Services.GetUserByAccount(account) if err != nil { diff --git a/docs/docs.go b/docs/docs.go index 47e2cf6..347ae43 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -44,7 +44,7 @@ const docTemplate = `{ "in": "body", "required": true, "schema": { - "$ref": "#/definitions/dto.Login" + "$ref": "#/definitions/auth.LoginDTO" } } ], @@ -60,7 +60,7 @@ const docTemplate = `{ "type": "object", "properties": { "data": { - "$ref": "#/definitions/vo.Login" + "$ref": "#/definitions/auth.LoginVO" } } } @@ -96,7 +96,7 @@ const docTemplate = `{ "in": "body", "required": true, "schema": { - "$ref": "#/definitions/dto.RefreshToken" + "$ref": "#/definitions/auth.RefreshToken" } } ], @@ -112,7 +112,7 @@ const docTemplate = `{ "type": "object", "properties": { "data": { - "$ref": "#/definitions/vo.Login" + "$ref": "#/definitions/auth.LoginVO" } } } @@ -467,7 +467,7 @@ const docTemplate = `{ "type": "object", "properties": { "data": { - "$ref": "#/definitions/vo.Captcha" + "$ref": "#/definitions/captcha.Captcha" } } } @@ -503,7 +503,7 @@ const docTemplate = `{ "in": "body", "required": true, "schema": { - "$ref": "#/definitions/dto.VerifyCaptcha" + "$ref": "#/definitions/captcha.VerifyCaptcha" } } ], @@ -815,7 +815,7 @@ const docTemplate = `{ "Token": [] } ], - "description": "根据 账号 获取用户信息", + "description": "获取当前用户信息", "consumes": [ "application/json" ], @@ -825,16 +825,7 @@ const docTemplate = `{ "tags": [ "User" ], - "summary": "根据 账号 获取用户信息", - "parameters": [ - { - "type": "string", - "description": "账号名称", - "name": "account", - "in": "query", - "required": true - } - ], + "summary": "获取当前用户信息", "responses": { "200": { "description": "OK", @@ -865,7 +856,7 @@ const docTemplate = `{ } }, "definitions": { - "dto.Login": { + "auth.LoginDTO": { "type": "object", "required": [ "account", @@ -882,7 +873,22 @@ const docTemplate = `{ } } }, - "dto.RefreshToken": { + "auth.LoginVO": { + "type": "object", + "properties": { + "refreshToken": { + "description": "refreshToken 刷新令牌", + "type": "string", + "example": "0123456789ABCDEFG" + }, + "token": { + "description": "token 用户令牌", + "type": "string", + "example": "0123456789ABCDEFG" + } + } + }, + "auth.RefreshToken": { "type": "object", "required": [ "refreshToken" @@ -894,7 +900,27 @@ const docTemplate = `{ } } }, - "dto.VerifyCaptcha": { + "captcha.Captcha": { + "type": "object", + "properties": { + "base64": { + "description": "验证码图片 base64", + "type": "string", + "example": "data:image/png;base64,iVBO..." + }, + "expire": { + "description": "过期时间 unix", + "type": "integer", + "example": 10000000 + }, + "id": { + "description": "验证码 id", + "type": "string", + "example": "abcdefg123456" + } + } + }, + "captcha.VerifyCaptcha": { "type": "object", "properties": { "captcha": { @@ -1010,41 +1036,6 @@ const docTemplate = `{ "example": "OK" } } - }, - "vo.Captcha": { - "type": "object", - "properties": { - "base64": { - "description": "验证码图片 base64", - "type": "string", - "example": "data:image/png;base64,iVBO..." - }, - "expire": { - "description": "过期时间 unix", - "type": "integer", - "example": 10000000 - }, - "id": { - "description": "验证码 id", - "type": "string", - "example": "abcdefg123456" - } - } - }, - "vo.Login": { - "type": "object", - "properties": { - "refreshToken": { - "description": "refreshToken 刷新令牌", - "type": "string", - "example": "0123456789ABCDEFG" - }, - "token": { - "description": "token 用户令牌", - "type": "string", - "example": "0123456789ABCDEFG" - } - } } }, "securityDefinitions": { diff --git a/docs/swagger.json b/docs/swagger.json index 7a8546a..2335376 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -36,7 +36,7 @@ "in": "body", "required": true, "schema": { - "$ref": "#/definitions/dto.Login" + "$ref": "#/definitions/auth.LoginDTO" } } ], @@ -52,7 +52,7 @@ "type": "object", "properties": { "data": { - "$ref": "#/definitions/vo.Login" + "$ref": "#/definitions/auth.LoginVO" } } } @@ -88,7 +88,7 @@ "in": "body", "required": true, "schema": { - "$ref": "#/definitions/dto.RefreshToken" + "$ref": "#/definitions/auth.RefreshToken" } } ], @@ -104,7 +104,7 @@ "type": "object", "properties": { "data": { - "$ref": "#/definitions/vo.Login" + "$ref": "#/definitions/auth.LoginVO" } } } @@ -459,7 +459,7 @@ "type": "object", "properties": { "data": { - "$ref": "#/definitions/vo.Captcha" + "$ref": "#/definitions/captcha.Captcha" } } } @@ -495,7 +495,7 @@ "in": "body", "required": true, "schema": { - "$ref": "#/definitions/dto.VerifyCaptcha" + "$ref": "#/definitions/captcha.VerifyCaptcha" } } ], @@ -807,7 +807,7 @@ "Token": [] } ], - "description": "根据 账号 获取用户信息", + "description": "获取当前用户信息", "consumes": [ "application/json" ], @@ -817,16 +817,7 @@ "tags": [ "User" ], - "summary": "根据 账号 获取用户信息", - "parameters": [ - { - "type": "string", - "description": "账号名称", - "name": "account", - "in": "query", - "required": true - } - ], + "summary": "获取当前用户信息", "responses": { "200": { "description": "OK", @@ -857,7 +848,7 @@ } }, "definitions": { - "dto.Login": { + "auth.LoginDTO": { "type": "object", "required": [ "account", @@ -874,7 +865,22 @@ } } }, - "dto.RefreshToken": { + "auth.LoginVO": { + "type": "object", + "properties": { + "refreshToken": { + "description": "refreshToken 刷新令牌", + "type": "string", + "example": "0123456789ABCDEFG" + }, + "token": { + "description": "token 用户令牌", + "type": "string", + "example": "0123456789ABCDEFG" + } + } + }, + "auth.RefreshToken": { "type": "object", "required": [ "refreshToken" @@ -886,7 +892,27 @@ } } }, - "dto.VerifyCaptcha": { + "captcha.Captcha": { + "type": "object", + "properties": { + "base64": { + "description": "验证码图片 base64", + "type": "string", + "example": "data:image/png;base64,iVBO..." + }, + "expire": { + "description": "过期时间 unix", + "type": "integer", + "example": 10000000 + }, + "id": { + "description": "验证码 id", + "type": "string", + "example": "abcdefg123456" + } + } + }, + "captcha.VerifyCaptcha": { "type": "object", "properties": { "captcha": { @@ -1002,41 +1028,6 @@ "example": "OK" } } - }, - "vo.Captcha": { - "type": "object", - "properties": { - "base64": { - "description": "验证码图片 base64", - "type": "string", - "example": "data:image/png;base64,iVBO..." - }, - "expire": { - "description": "过期时间 unix", - "type": "integer", - "example": 10000000 - }, - "id": { - "description": "验证码 id", - "type": "string", - "example": "abcdefg123456" - } - } - }, - "vo.Login": { - "type": "object", - "properties": { - "refreshToken": { - "description": "refreshToken 刷新令牌", - "type": "string", - "example": "0123456789ABCDEFG" - }, - "token": { - "description": "token 用户令牌", - "type": "string", - "example": "0123456789ABCDEFG" - } - } } }, "securityDefinitions": { diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 5be0134..2d64ad4 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -1,6 +1,6 @@ basePath: / definitions: - dto.Login: + auth.LoginDTO: properties: account: example: root @@ -12,7 +12,18 @@ definitions: - account - password type: object - dto.RefreshToken: + auth.LoginVO: + properties: + refreshToken: + description: refreshToken 刷新令牌 + example: 0123456789ABCDEFG + type: string + token: + description: token 用户令牌 + example: 0123456789ABCDEFG + type: string + type: object + auth.RefreshToken: properties: refreshToken: example: 0123456789ABCDEFG @@ -20,7 +31,22 @@ definitions: required: - refreshToken type: object - dto.VerifyCaptcha: + captcha.Captcha: + properties: + base64: + description: 验证码图片 base64 + example: data:image/png;base64,iVBO... + type: string + expire: + description: 过期时间 unix + example: 10000000 + type: integer + id: + description: 验证码 id + example: abcdefg123456 + type: string + type: object + captcha.VerifyCaptcha: properties: captcha: example: abcde @@ -100,32 +126,6 @@ definitions: example: OK type: string type: object - vo.Captcha: - properties: - base64: - description: 验证码图片 base64 - example: data:image/png;base64,iVBO... - type: string - expire: - description: 过期时间 unix - example: 10000000 - type: integer - id: - description: 验证码 id - example: abcdefg123456 - type: string - type: object - vo.Login: - properties: - refreshToken: - description: refreshToken 刷新令牌 - example: 0123456789ABCDEFG - type: string - token: - description: token 用户令牌 - example: 0123456789ABCDEFG - type: string - type: object info: contact: email: 919411476@qq.com @@ -149,7 +149,7 @@ paths: name: vo required: true schema: - $ref: '#/definitions/dto.Login' + $ref: '#/definitions/auth.LoginDTO' produces: - application/json responses: @@ -160,7 +160,7 @@ paths: - $ref: '#/definitions/response.Response' - properties: data: - $ref: '#/definitions/vo.Login' + $ref: '#/definitions/auth.LoginVO' type: object default: description: "" @@ -180,7 +180,7 @@ paths: name: vo required: true schema: - $ref: '#/definitions/dto.RefreshToken' + $ref: '#/definitions/auth.RefreshToken' produces: - application/json responses: @@ -191,7 +191,7 @@ paths: - $ref: '#/definitions/response.Response' - properties: data: - $ref: '#/definitions/vo.Login' + $ref: '#/definitions/auth.LoginVO' type: object default: description: "" @@ -412,7 +412,7 @@ paths: - $ref: '#/definitions/response.Response' - properties: data: - $ref: '#/definitions/vo.Captcha' + $ref: '#/definitions/captcha.Captcha' type: object default: description: "" @@ -432,7 +432,7 @@ paths: name: vo required: true schema: - $ref: '#/definitions/dto.VerifyCaptcha' + $ref: '#/definitions/captcha.VerifyCaptcha' produces: - application/json responses: @@ -618,13 +618,7 @@ paths: get: consumes: - application/json - description: 根据 账号 获取用户信息 - parameters: - - description: 账号名称 - in: query - name: account - required: true - type: string + description: 获取当前用户信息 produces: - application/json responses: @@ -643,7 +637,7 @@ paths: $ref: '#/definitions/errorx.CodeErrorResponse' security: - Token: [] - summary: 根据 账号 获取用户信息 + summary: 获取当前用户信息 tags: - User securityDefinitions: diff --git a/model/dto/auth.go b/model/auth/dto.go similarity index 90% rename from model/dto/auth.go rename to model/auth/dto.go index b536cac..3148eba 100644 --- a/model/dto/auth.go +++ b/model/auth/dto.go @@ -1,9 +1,9 @@ -package dto +package auth -// Login +// LoginDTO // @Param account body string true "用户账号(account)" // @Param password body string true "用户密码" -type Login struct { +type LoginDTO struct { Account string `json:"account" example:"root" validate:"required"` Password string `json:"password" example:"12341234" validate:"required"` } diff --git a/model/vo/auth.go b/model/auth/vo.go similarity index 84% rename from model/vo/auth.go rename to model/auth/vo.go index 6995bc3..bb617f3 100644 --- a/model/vo/auth.go +++ b/model/auth/vo.go @@ -1,6 +1,6 @@ -package vo +package auth -type Login struct { +type LoginVO struct { // token 用户令牌 Token string `json:"token" example:"0123456789ABCDEFG"` // refreshToken 刷新令牌 diff --git a/model/dto/captcha.go b/model/captcha/dto.go similarity index 93% rename from model/dto/captcha.go rename to model/captcha/dto.go index b14b399..b9423c1 100644 --- a/model/dto/captcha.go +++ b/model/captcha/dto.go @@ -1,4 +1,4 @@ -package dto +package captcha // VerifyCaptcha // @Param id body string true "验证码 id" diff --git a/model/vo/captcha.go b/model/captcha/vo.go similarity index 94% rename from model/vo/captcha.go rename to model/captcha/vo.go index 6653f06..8a284e3 100644 --- a/model/vo/captcha.go +++ b/model/captcha/vo.go @@ -1,4 +1,4 @@ -package vo +package captcha type Captcha struct { // 验证码 id diff --git a/model/generic/generate/config.yml b/model/generic/generate/config.yml index 49a84e9..20dd47f 100644 --- a/model/generic/generate/config.yml +++ b/model/generic/generate/config.yml @@ -12,6 +12,8 @@ targets: include_tables: # tables included, you can use ** - user - waf + - topical + - reply table_prefix: "" multiple_files: true # generate multiple files or one template: | # template for code file, it has higher perior than template_path diff --git a/model/generic/models/reply.go b/model/generic/models/reply.go new file mode 100644 index 0000000..02d4552 --- /dev/null +++ b/model/generic/models/reply.go @@ -0,0 +1,21 @@ +package models + +import ( + "gofiber.study.skcks.cn/common/time" +) + +type Reply struct { + Id string `xorm:"not null pk VARCHAR(32)" json:"id"` + Active bool `xorm:"not null index BIT(1)" json:"active"` + Content string `xorm:"not null index LONGTEXT(4294967295)" json:"content"` + CreateTime time.Time `xorm:"not null index DATETIME(6) created" json:"CreateTime"` + ModifyTime time.Time `xorm:"not null index DATETIME(6) updated" json:"modifyTime"` + ModifyTimes int64 `xorm:"not null index BIGINT" json:"modifyTimes"` + Top int `xorm:"not null index BIT(1)" json:"top"` + UserId string `xorm:"not null index VARCHAR(32)" json:"userId"` + TopicalId string `xorm:"not null index VARCHAR(32)" json:"topicalId"` +} + +func (m *Reply) TableName() string { + return "reply" +} diff --git a/model/generic/models/topical.go b/model/generic/models/topical.go new file mode 100644 index 0000000..c78eb10 --- /dev/null +++ b/model/generic/models/topical.go @@ -0,0 +1,20 @@ +package models + +import ( + "gofiber.study.skcks.cn/common/time" +) + +type Topical struct { + Id string `xorm:"not null pk VARCHAR(32)" json:"id"` + Active bool `xorm:"not null index BIT(1)" json:"active"` + Content string `xorm:"not null index LONGTEXT(4294967295)" json:"content"` + CreateTime time.Time `xorm:"not null index DATETIME(6) created" json:"createTime"` + ModifyTime time.Time `xorm:"not null index DATETIME(6) updated" json:"modifyTime"` + ModifyTimes int64 `xorm:"not null index BIGINT" json:"modifyTimes"` + Title string `xorm:"not null VARCHAR(255)" json:"title"` + UserId string `xorm:"not null index VARCHAR(32)" json:"userId"` +} + +func (m *Topical) TableName() string { + return "topical" +} diff --git a/model/topical/dto.go b/model/topical/dto.go new file mode 100644 index 0000000..44f434e --- /dev/null +++ b/model/topical/dto.go @@ -0,0 +1 @@ +package topical diff --git a/services/auth/auth.go b/services/auth/auth.go index 0b5c06e..4480fb4 100644 --- a/services/auth/auth.go +++ b/services/auth/auth.go @@ -9,9 +9,8 @@ import ( "gofiber.study.skcks.cn/common/utils" "gofiber.study.skcks.cn/global" "gofiber.study.skcks.cn/middleware" - "gofiber.study.skcks.cn/model/dto" + "gofiber.study.skcks.cn/model/auth" "gofiber.study.skcks.cn/model/generic/models" - "gofiber.study.skcks.cn/model/vo" "time" ) @@ -91,7 +90,7 @@ func (s *Service) getUserClaims(user *models.User) global.UserClaims { } } -func (s *Service) Login(login *dto.Login) (result *vo.Login, err error) { +func (s *Service) Login(login *auth.LoginDTO) (result *auth.LoginVO, err error) { err = global.ValidateStruct(login) if err != nil { return nil, err @@ -117,13 +116,13 @@ func (s *Service) Login(login *dto.Login) (result *vo.Login, err error) { _, _ = global.DataSources.Update(user) - return &vo.Login{ + return &auth.LoginVO{ Token: token, RefreshToken: refreshToken, }, err } -func (s *Service) RefreshToken(refreshTokenB64 string) (result *vo.Login, err error) { +func (s *Service) RefreshToken(refreshTokenB64 string) (result *auth.LoginVO, err error) { user, err := s.fromRefreshTokenGetUserCache(refreshTokenB64) if err != nil { return @@ -137,7 +136,7 @@ func (s *Service) RefreshToken(refreshTokenB64 string) (result *vo.Login, err er refreshToken, err := s.generateAndSaveRefreshToken(user) - return &vo.Login{ + return &auth.LoginVO{ Token: token, RefreshToken: refreshToken, }, err diff --git a/services/services.go b/services/services.go index c46dc3f..3f3a2bd 100644 --- a/services/services.go +++ b/services/services.go @@ -2,6 +2,7 @@ package services import ( "gofiber.study.skcks.cn/services/auth" + "gofiber.study.skcks.cn/services/topical" "gofiber.study.skcks.cn/services/user" "gofiber.study.skcks.cn/services/waf" "gofiber.study.skcks.cn/services/wol" @@ -12,4 +13,5 @@ func Init() { user.InitService() waf.InitService() wol.InitService() + topical.InitService() } diff --git a/services/topical/topical.go b/services/topical/topical.go new file mode 100644 index 0000000..9d7414f --- /dev/null +++ b/services/topical/topical.go @@ -0,0 +1,39 @@ +package topical + +import ( + "gofiber.study.skcks.cn/global" + "gofiber.study.skcks.cn/model/generic/models" + "strconv" +) + +type Service struct { +} + +var Services *Service + +func InitService() { + Services = &Service{} +} + +type CreateTopicalDTO struct { + Title string + Content string +} + +func (s *Service) CreateTopical(dto CreateTopicalDTO, userId string) error { + id, _ := global.SonyFlake.NextID() + _, err := global.DataSources.Insert(&models.Topical{ + Id: strconv.FormatUint(id, 10), + Active: true, + Content: dto.Content, + ModifyTimes: 0, + Title: dto.Title, + UserId: userId, + }) + + if err != nil { + return err + } + + return nil +}