From 7f91ff9dfa6d6ff8ec35dfea6d5195a20f631127 Mon Sep 17 00:00:00 2001 From: zhangqian <zhangqian@123.com> Date: 星期一, 13 五月 2024 13:58:16 +0800 Subject: [PATCH] 关注和取消关注接口 --- constvar/const.go | 8 + request/audio.go | 4 models/audio_text.go | 5 service/process.go | 6 service/audio.go | 24 ++++ models/audio.go | 1 docs/swagger.yaml | 47 ++++++++ controllers/audio.go | 35 ++++++- docs/docs.go | 73 +++++++++++++ docs/swagger.json | 73 +++++++++++++ response/audio.go | 7 + router/router.go | 1 12 files changed, 262 insertions(+), 22 deletions(-) diff --git a/constvar/const.go b/constvar/const.go index 8c1287c..5d5daca 100644 --- a/constvar/const.go +++ b/constvar/const.go @@ -17,3 +17,11 @@ BoolTypeTrue BoolType = 1 // true BoolTypeFalse BoolType = 2 // false ) + +func (slf BoolType) IsValid() bool { + return slf == BoolTypeTrue || slf == BoolTypeFalse +} + +func (slf BoolType) Bool() bool { + return slf == BoolTypeTrue +} diff --git a/controllers/audio.go b/controllers/audio.go index 9ea4502..04d3375 100644 --- a/controllers/audio.go +++ b/controllers/audio.go @@ -11,6 +11,7 @@ "speechAnalysis/models" "speechAnalysis/pkg/logx" "speechAnalysis/request" + "speechAnalysis/response" "speechAnalysis/service" "speechAnalysis/utils/upload" "strings" @@ -97,7 +98,7 @@ // @Tags 闊抽 // @Summary 闊抽鍒嗘瀽妫�绱� // @Produce application/json -// @Param object query request.GetAudioList true "鏌ヨ鍙傛暟" +// @Param object query request.GetAudioList true "鍙傛暟" // @Success 200 {object} util.ResponseList{data=[]models.Audio} "鎴愬姛" // @Router /api-sa/v1/audio/list [get] func (slf AudioCtl) List(c *gin.Context) { @@ -133,7 +134,7 @@ // @Tags 闊抽 // @Summary 澶勭悊闊抽 // @Produce application/json -// @Param object body request.ProcessAudio true "闊抽淇℃伅" +// @Param object body request.ProcessAudio true "鍙傛暟" // @Success 200 {object} util.Response "鎴愬姛" // @Router /api-sa/v1/audio/process [post] func (slf AudioCtl) Process(c *gin.Context) { @@ -156,7 +157,7 @@ // @Tags 闊抽 // @Summary 鎵归噺澶勭悊闊抽 // @Produce application/json -// @Param object body request.BatchProcessAudio true "闊抽淇℃伅" +// @Param object body request.BatchProcessAudio true "鍙傛暟" // @Success 200 {object} util.Response "鎴愬姛" // @Router /api-sa/v1/audio/batchProcess [post] func (slf AudioCtl) BatchProcess(c *gin.Context) { @@ -192,7 +193,7 @@ // @Tags 闊抽 // @Summary 鍒犻櫎闊抽 // @Produce application/json -// @Param object body request.ProcessAudio true "闊抽淇℃伅" +// @Param object body request.ProcessAudio true "鍙傛暟" // @Success 200 {object} util.Response "鎴愬姛" // @Router /api-sa/v1/audio/delete [post] func (slf AudioCtl) Delete(c *gin.Context) { @@ -215,7 +216,7 @@ // @Tags 闊抽 // @Summary 鎵归噺鍒犻櫎闊抽 // @Produce application/json -// @Param object body request.BatchProcessAudio true "闊抽淇℃伅" +// @Param object body request.BatchProcessAudio true "鍙傛暟" // @Success 200 {object} util.Response "鎴愬姛" // @Router /api-sa/v1/audio/batchDelete [post] func (slf AudioCtl) BatchDelete(c *gin.Context) { @@ -233,3 +234,27 @@ util.ResponseFormat(c, code.DeleteSuccess, "鎴愬姛") } + +// Follow +// @Tags 闊抽 +// @Summary 鍏虫敞/鍙栨秷鍏虫敞 +// @Produce application/json +// @Param object body request.FollowReq true "鍙傛暟" +// @Success 200 {object} util.Response{data=response.FollowResp} "鎴愬姛" +// @Router /api-sa/v1/audio/follow [post] +func (slf AudioCtl) Follow(c *gin.Context) { + var params request.ProcessAudio + if err := c.ShouldBind(¶ms); err != nil { + util.ResponseFormat(c, code.RequestParamError, err.Error()) + return + } + + followStatus, err := service.Follow(params.ID) + if err != nil { + util.ResponseFormat(c, code.InternalError, err.Error()) + return + } + resp := response.FollowResp{FollowStatus: followStatus} + + util.ResponseFormat(c, code.UpdateSuccess, resp) +} diff --git a/docs/docs.go b/docs/docs.go index d16d13e..0decc75 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -27,7 +27,7 @@ "summary": "鎵归噺鍒犻櫎闊抽", "parameters": [ { - "description": "闊抽淇℃伅", + "description": "鍙傛暟", "name": "object", "in": "body", "required": true, @@ -57,7 +57,7 @@ "summary": "鎵归噺澶勭悊闊抽", "parameters": [ { - "description": "闊抽淇℃伅", + "description": "鍙傛暟", "name": "object", "in": "body", "required": true, @@ -87,7 +87,7 @@ "summary": "鍒犻櫎闊抽", "parameters": [ { - "description": "闊抽淇℃伅", + "description": "鍙傛暟", "name": "object", "in": "body", "required": true, @@ -101,6 +101,48 @@ "description": "鎴愬姛", "schema": { "$ref": "#/definitions/util.Response" + } + } + } + } + }, + "/api-sa/v1/audio/follow": { + "post": { + "produces": [ + "application/json" + ], + "tags": [ + "闊抽" + ], + "summary": "鍏虫敞/鍙栨秷鍏虫敞", + "parameters": [ + { + "description": "鍙傛暟", + "name": "object", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/request.FollowReq" + } + } + ], + "responses": { + "200": { + "description": "鎴愬姛", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/util.Response" + }, + { + "type": "object", + "properties": { + "data": { + "$ref": "#/definitions/response.FollowResp" + } + } + } + ] } } } @@ -195,7 +237,7 @@ "summary": "澶勭悊闊抽", "parameters": [ { - "description": "闊抽淇℃伅", + "description": "鍙傛暟", "name": "object", "in": "body", "required": true, @@ -338,6 +380,10 @@ "occurrenceTime": { "type": "string" }, + "score": { + "description": "缃俊搴�", + "type": "number" + }, "size": { "description": "闊抽澶у皬", "type": "integer" @@ -369,6 +415,17 @@ } } }, + "request.FollowReq": { + "type": "object", + "required": [ + "id" + ], + "properties": { + "id": { + "type": "integer" + } + } + }, "request.ProcessAudio": { "type": "object", "required": [ @@ -380,6 +437,14 @@ } } }, + "response.FollowResp": { + "type": "object", + "properties": { + "followStatus": { + "$ref": "#/definitions/constvar.BoolType" + } + } + }, "util.Response": { "type": "object", "properties": { diff --git a/docs/swagger.json b/docs/swagger.json index 42ab712..ba47f87 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -15,7 +15,7 @@ "summary": "鎵归噺鍒犻櫎闊抽", "parameters": [ { - "description": "闊抽淇℃伅", + "description": "鍙傛暟", "name": "object", "in": "body", "required": true, @@ -45,7 +45,7 @@ "summary": "鎵归噺澶勭悊闊抽", "parameters": [ { - "description": "闊抽淇℃伅", + "description": "鍙傛暟", "name": "object", "in": "body", "required": true, @@ -75,7 +75,7 @@ "summary": "鍒犻櫎闊抽", "parameters": [ { - "description": "闊抽淇℃伅", + "description": "鍙傛暟", "name": "object", "in": "body", "required": true, @@ -89,6 +89,48 @@ "description": "鎴愬姛", "schema": { "$ref": "#/definitions/util.Response" + } + } + } + } + }, + "/api-sa/v1/audio/follow": { + "post": { + "produces": [ + "application/json" + ], + "tags": [ + "闊抽" + ], + "summary": "鍏虫敞/鍙栨秷鍏虫敞", + "parameters": [ + { + "description": "鍙傛暟", + "name": "object", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/request.FollowReq" + } + } + ], + "responses": { + "200": { + "description": "鎴愬姛", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/util.Response" + }, + { + "type": "object", + "properties": { + "data": { + "$ref": "#/definitions/response.FollowResp" + } + } + } + ] } } } @@ -183,7 +225,7 @@ "summary": "澶勭悊闊抽", "parameters": [ { - "description": "闊抽淇℃伅", + "description": "鍙傛暟", "name": "object", "in": "body", "required": true, @@ -326,6 +368,10 @@ "occurrenceTime": { "type": "string" }, + "score": { + "description": "缃俊搴�", + "type": "number" + }, "size": { "description": "闊抽澶у皬", "type": "integer" @@ -357,6 +403,17 @@ } } }, + "request.FollowReq": { + "type": "object", + "required": [ + "id" + ], + "properties": { + "id": { + "type": "integer" + } + } + }, "request.ProcessAudio": { "type": "object", "required": [ @@ -368,6 +425,14 @@ } } }, + "response.FollowResp": { + "type": "object", + "properties": { + "followStatus": { + "$ref": "#/definitions/constvar.BoolType" + } + } + }, "util.Response": { "type": "object", "properties": { diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 34715bd..92dd451 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -65,6 +65,9 @@ type: string occurrenceTime: type: string + score: + description: 缃俊搴� + type: number size: description: 闊抽澶у皬 type: integer @@ -86,12 +89,24 @@ required: - ids type: object + request.FollowReq: + properties: + id: + type: integer + required: + - id + type: object request.ProcessAudio: properties: id: type: integer required: - id + type: object + response.FollowResp: + properties: + followStatus: + $ref: '#/definitions/constvar.BoolType' type: object util.Response: properties: @@ -121,7 +136,7 @@ /api-sa/v1/audio/batchDelete: post: parameters: - - description: 闊抽淇℃伅 + - description: 鍙傛暟 in: body name: object required: true @@ -140,7 +155,7 @@ /api-sa/v1/audio/batchProcess: post: parameters: - - description: 闊抽淇℃伅 + - description: 鍙傛暟 in: body name: object required: true @@ -159,7 +174,7 @@ /api-sa/v1/audio/delete: post: parameters: - - description: 闊抽淇℃伅 + - description: 鍙傛暟 in: body name: object required: true @@ -173,6 +188,30 @@ schema: $ref: '#/definitions/util.Response' summary: 鍒犻櫎闊抽 + tags: + - 闊抽 + /api-sa/v1/audio/follow: + post: + parameters: + - description: 鍙傛暟 + in: body + name: object + required: true + schema: + $ref: '#/definitions/request.FollowReq' + produces: + - application/json + responses: + "200": + description: 鎴愬姛 + schema: + allOf: + - $ref: '#/definitions/util.Response' + - properties: + data: + $ref: '#/definitions/response.FollowResp' + type: object + summary: 鍏虫敞/鍙栨秷鍏虫敞 tags: - 闊抽 /api-sa/v1/audio/list: @@ -226,7 +265,7 @@ /api-sa/v1/audio/process: post: parameters: - - description: 闊抽淇℃伅 + - description: 鍙傛暟 in: body name: object required: true diff --git a/models/audio.go b/models/audio.go index 313f379..747d2d9 100644 --- a/models/audio.go +++ b/models/audio.go @@ -23,6 +23,7 @@ OccurrenceAt time.Time `json:"-"` OccurrenceTime string `json:"occurrenceTime" gorm:"-"` IsFollowed constvar.BoolType `gorm:"type:tinyint;not null;default:2;comment:鏄惁鍏虫敞"` //鏄惁鍏虫敞 1鍏虫敞 2鏈叧娉� + Score float64 `json:"score"` // 缃俊搴� } AudioSearch struct { diff --git a/models/audio_text.go b/models/audio_text.go index e5e1f36..3e5d4b2 100644 --- a/models/audio_text.go +++ b/models/audio_text.go @@ -10,9 +10,8 @@ // AudioText 闊抽鏂囨湰 AudioText struct { gorm.Model - AudioID uint `gorm:"unique;type:int;not null;default:0;comment:闊抽id" json:"audioID"` // 闊抽id - AudioText string `json:"audioText"` // 闊抽鏂囨湰 - Score float64 `json:"score"` // 缃俊搴� + AudioID uint `gorm:"unique;type:int;not null;default:0;comment:闊抽id" json:"audioID"` // 闊抽id + AudioText string `json:"audioText"` // 闊抽鏂囨湰 } AudioTextSearch struct { diff --git a/request/audio.go b/request/audio.go index 41cc415..1e5512a 100644 --- a/request/audio.go +++ b/request/audio.go @@ -16,3 +16,7 @@ type BatchProcessAudio struct { IDs []uint `json:"ids" binding:"required"` } + +type FollowReq struct { + ID uint `json:"id" binding:"required"` +} diff --git a/response/audio.go b/response/audio.go new file mode 100644 index 0000000..dc61f7a --- /dev/null +++ b/response/audio.go @@ -0,0 +1,7 @@ +package response + +import "speechAnalysis/constvar" + +type FollowResp struct { + FollowStatus constvar.BoolType `json:"followStatus"` +} diff --git a/router/router.go b/router/router.go index 6ab95fb..aba2789 100644 --- a/router/router.go +++ b/router/router.go @@ -31,6 +31,7 @@ organizeAPI.POST("batchProcess", AudioCtl.BatchProcess) // 闊抽鎵归噺澶勭悊 organizeAPI.POST("delete", AudioCtl.Delete) // 闊抽鍒犻櫎 organizeAPI.POST("batchDelete", AudioCtl.BatchDelete) // 闊抽鎵归噺鍒犻櫎 + organizeAPI.POST("follow", AudioCtl.Follow) // 鍏虫敞/鍙栨秷鍏虫敞 } return r diff --git a/service/audio.go b/service/audio.go index fa30810..677b695 100644 --- a/service/audio.go +++ b/service/audio.go @@ -3,6 +3,7 @@ import ( "errors" "gorm.io/gorm" + "speechAnalysis/constvar" "speechAnalysis/models" "speechAnalysis/pkg/logx" ) @@ -37,3 +38,26 @@ } return nil } + +func Follow(audioId uint) (followStatus constvar.BoolType, err error) { + if audioId == 0 { + return + } + audio, err := models.NewAudioSearch().SetID(audioId).First() + if err != nil { + return followStatus, errors.New("DB閿欒") + } + if audio.IsFollowed.Bool() { + followStatus = constvar.BoolTypeFalse + } else { + followStatus = constvar.BoolTypeTrue + } + err = models.NewAudioSearch().SetID(audioId).UpdateByMap(map[string]interface{}{ + "is_followed": followStatus, + }) + if err != nil { + logx.Errorf("follow audio err: %v", err) + return followStatus, errors.New("DB閿欒") + } + return followStatus, nil +} diff --git a/service/process.go b/service/process.go index 50bf7eb..1d1595a 100644 --- a/service/process.go +++ b/service/process.go @@ -111,14 +111,16 @@ } logx.Infof("AnalysisAudio result: %v", resp) err = models.WithTransaction(func(db *gorm.DB) error { - err = models.NewAudioSearch().SetID(audioId).UpdateByMap(map[string]interface{}{"audio_status": constvar.AudioStatusFinish}) + err = models.NewAudioSearch().SetID(audioId).UpdateByMap(map[string]interface{}{ + "audio_status": constvar.AudioStatusFinish, + "score": resp.Score, + }) if err != nil { return err } err = models.NewAudioTextSearch().Save(&models.AudioText{ AudioID: audio.ID, AudioText: resp.Result, - Score: resp.Score, }) return err }) -- Gitblit v1.8.0