From 89ff3763c7eaa2db032b965596566a870dcb5030 Mon Sep 17 00:00:00 2001 From: zhangqian <zhangqian@123.com> Date: 星期二, 14 五月 2024 11:14:25 +0800 Subject: [PATCH] 添加文字和文字列表接口 --- controllers/text.go | 89 ++++++ request/text.go | 11 models/db.go | 1 docs/swagger.yaml | 84 ++++++ controllers/audio.go | 6 docs/docs.go | 130 +++++++++ docs/swagger.json | 130 +++++++++ models/text.go | 272 ++++++++++++++++++++ router/router.go | 28 + 9 files changed, 733 insertions(+), 18 deletions(-) diff --git a/controllers/audio.go b/controllers/audio.go index 04d3375..94b0117 100644 --- a/controllers/audio.go +++ b/controllers/audio.go @@ -127,7 +127,7 @@ return } - util.ResponseFormatList(c, code.Success, list, int(total)) + util.ResponseFormatList(c, code.Success, list, total) } // Process @@ -195,7 +195,7 @@ // @Produce application/json // @Param object body request.ProcessAudio true "鍙傛暟" // @Success 200 {object} util.Response "鎴愬姛" -// @Router /api-sa/v1/audio/delete [post] +// @Router /api-sa/v1/audio/delete [delete] func (slf AudioCtl) Delete(c *gin.Context) { var params request.ProcessAudio if err := c.ShouldBind(¶ms); err != nil { @@ -218,7 +218,7 @@ // @Produce application/json // @Param object body request.BatchProcessAudio true "鍙傛暟" // @Success 200 {object} util.Response "鎴愬姛" -// @Router /api-sa/v1/audio/batchDelete [post] +// @Router /api-sa/v1/audio/batchDelete [delete] func (slf AudioCtl) BatchDelete(c *gin.Context) { var params request.BatchProcessAudio if err := c.ShouldBind(¶ms); err != nil { diff --git a/controllers/text.go b/controllers/text.go new file mode 100644 index 0000000..625e367 --- /dev/null +++ b/controllers/text.go @@ -0,0 +1,89 @@ +package controllers + +import ( + "errors" + "github.com/gin-gonic/gin" + "speechAnalysis/extend/code" + "speechAnalysis/extend/util" + "speechAnalysis/models" + "speechAnalysis/pkg/logx" + "speechAnalysis/request" +) + +type TextCtl struct{} + +// AddText +// @Tags 鏂囧瓧搴� +// @Summary 鏂板鏂囧瓧 +// @Produce application/json +// @Param object body request.AddTextReq true "鍙傛暟" +// @Success 200 {object} util.Response "鎴愬姛" +// @Router /api-sa/v1/text/add [post] +func (slf TextCtl) AddText(c *gin.Context) { + var req request.AddTextReq + if err := c.BindJSON(&req); err != nil { + logx.Errorf("add text params err:%v", err) + util.ResponseFormat(c, code.RequestParamError, err.Error()) + return + } + + text := models.Text{ + Content: req.Content, + LocomotiveNumber: req.LocomotiveNumber, + } + + if err := slf.paramsCheck(text); err != nil { + util.ResponseFormat(c, code.RequestParamError, err.Error()) + return + } + + if err := models.NewTextSearch().Create(&text); err != nil { + util.ResponseFormat(c, code.SaveFail, "娣诲姞澶辫触锛岃妫�鏌ユ槸鍚﹂噸澶�") + return + } + + util.ResponseFormat(c, code.Success, "娣诲姞鎴愬姛") +} + +func (slf TextCtl) paramsCheck(text models.Text) (err error) { + if text.Content == "" || text.LocomotiveNumber == "" { + return errors.New("鍙傛暟缂哄け") + } + _, err = models.NewTextSearch().SetLocomotiveNumber(text.LocomotiveNumber).SetContent(text.Content).First() + if err == nil { + return errors.New("鏂囧瓧閲嶅") + } + return nil +} + +// List +// @Tags 鏂囧瓧搴� +// @Summary 鏂囧瓧搴撳垪琛� +// @Produce application/json +// @Param object query request.GetTextList true "鍙傛暟" +// @Success 200 {object} util.ResponseList{data=[]models.Text} "鎴愬姛" +// @Router /api-sa/v1/text/list [get] +func (slf TextCtl) List(c *gin.Context) { + var params request.GetTextList + if err := c.ShouldBindQuery(¶ms); err != nil { + util.ResponseFormat(c, code.RequestParamError, err.Error()) + return + } + + if !params.PageInfo.Check() { + util.ResponseFormat(c, code.RequestParamError, "鍒嗛〉鍙傛暟閿欒") + return + } + + list, total, err := models.NewTextSearch(). + SetPage(params.Page, params.PageSize). + SetKeyword(params.Keyword). + Find() + + if err != nil { + util.ResponseFormat(c, code.RequestParamError, "鏌ユ壘澶辫触") + return + } + + util.ResponseFormatList(c, code.Success, list, total) +} diff --git a/docs/docs.go b/docs/docs.go index 0decc75..c21ad56 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -17,7 +17,7 @@ "basePath": "{{.BasePath}}", "paths": { "/api-sa/v1/audio/batchDelete": { - "post": { + "delete": { "produces": [ "application/json" ], @@ -77,7 +77,7 @@ } }, "/api-sa/v1/audio/delete": { - "post": { + "delete": { "produces": [ "application/json" ], @@ -283,6 +283,90 @@ } } } + }, + "/api-sa/v1/text/add": { + "post": { + "produces": [ + "application/json" + ], + "tags": [ + "鏂囧瓧搴�" + ], + "summary": "鏂板鏂囧瓧", + "parameters": [ + { + "description": "鍙傛暟", + "name": "object", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/request.AddTextReq" + } + } + ], + "responses": { + "200": { + "description": "鎴愬姛", + "schema": { + "$ref": "#/definitions/util.Response" + } + } + } + } + }, + "/api-sa/v1/text/list": { + "get": { + "produces": [ + "application/json" + ], + "tags": [ + "鏂囧瓧搴�" + ], + "summary": "鏂囧瓧搴撳垪琛�", + "parameters": [ + { + "type": "string", + "description": "鍏抽敭瀛�", + "name": "keyword", + "in": "query" + }, + { + "type": "integer", + "description": "椤电爜", + "name": "page", + "in": "query" + }, + { + "type": "integer", + "description": "姣忛〉澶у皬", + "name": "pageSize", + "in": "query" + } + ], + "responses": { + "200": { + "description": "鎴愬姛", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/util.ResponseList" + }, + { + "type": "object", + "properties": { + "data": { + "type": "array", + "items": { + "$ref": "#/definitions/models.Text" + } + } + } + } + ] + } + } + } + } } }, "definitions": { @@ -401,6 +485,48 @@ } } }, + "models.Text": { + "type": "object", + "properties": { + "content": { + "description": "闊抽鍚嶇О", + "type": "string" + }, + "createdAt": { + "type": "string" + }, + "deletedAt": { + "$ref": "#/definitions/gorm.DeletedAt" + }, + "id": { + "type": "integer" + }, + "locomotiveNumber": { + "description": "鏈鸿溅鍙�", + "type": "string" + }, + "updatedAt": { + "type": "string" + } + } + }, + "request.AddTextReq": { + "type": "object", + "required": [ + "content", + "locomotiveNumber" + ], + "properties": { + "content": { + "description": "闊抽鍚嶇О", + "type": "string" + }, + "locomotiveNumber": { + "description": "鏈鸿溅鍙�", + "type": "string" + } + } + }, "request.BatchProcessAudio": { "type": "object", "required": [ diff --git a/docs/swagger.json b/docs/swagger.json index ba47f87..fd84112 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -5,7 +5,7 @@ }, "paths": { "/api-sa/v1/audio/batchDelete": { - "post": { + "delete": { "produces": [ "application/json" ], @@ -65,7 +65,7 @@ } }, "/api-sa/v1/audio/delete": { - "post": { + "delete": { "produces": [ "application/json" ], @@ -271,6 +271,90 @@ } } } + }, + "/api-sa/v1/text/add": { + "post": { + "produces": [ + "application/json" + ], + "tags": [ + "鏂囧瓧搴�" + ], + "summary": "鏂板鏂囧瓧", + "parameters": [ + { + "description": "鍙傛暟", + "name": "object", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/request.AddTextReq" + } + } + ], + "responses": { + "200": { + "description": "鎴愬姛", + "schema": { + "$ref": "#/definitions/util.Response" + } + } + } + } + }, + "/api-sa/v1/text/list": { + "get": { + "produces": [ + "application/json" + ], + "tags": [ + "鏂囧瓧搴�" + ], + "summary": "鏂囧瓧搴撳垪琛�", + "parameters": [ + { + "type": "string", + "description": "鍏抽敭瀛�", + "name": "keyword", + "in": "query" + }, + { + "type": "integer", + "description": "椤电爜", + "name": "page", + "in": "query" + }, + { + "type": "integer", + "description": "姣忛〉澶у皬", + "name": "pageSize", + "in": "query" + } + ], + "responses": { + "200": { + "description": "鎴愬姛", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/util.ResponseList" + }, + { + "type": "object", + "properties": { + "data": { + "type": "array", + "items": { + "$ref": "#/definitions/models.Text" + } + } + } + } + ] + } + } + } + } } }, "definitions": { @@ -389,6 +473,48 @@ } } }, + "models.Text": { + "type": "object", + "properties": { + "content": { + "description": "闊抽鍚嶇О", + "type": "string" + }, + "createdAt": { + "type": "string" + }, + "deletedAt": { + "$ref": "#/definitions/gorm.DeletedAt" + }, + "id": { + "type": "integer" + }, + "locomotiveNumber": { + "description": "鏈鸿溅鍙�", + "type": "string" + }, + "updatedAt": { + "type": "string" + } + } + }, + "request.AddTextReq": { + "type": "object", + "required": [ + "content", + "locomotiveNumber" + ], + "properties": { + "content": { + "description": "闊抽鍚嶇О", + "type": "string" + }, + "locomotiveNumber": { + "description": "鏈鸿溅鍙�", + "type": "string" + } + } + }, "request.BatchProcessAudio": { "type": "object", "required": [ diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 92dd451..53b00ca 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -80,6 +80,35 @@ updatedAt: type: string type: object + models.Text: + properties: + content: + description: 闊抽鍚嶇О + type: string + createdAt: + type: string + deletedAt: + $ref: '#/definitions/gorm.DeletedAt' + id: + type: integer + locomotiveNumber: + description: 鏈鸿溅鍙� + type: string + updatedAt: + type: string + type: object + request.AddTextReq: + properties: + content: + description: 闊抽鍚嶇О + type: string + locomotiveNumber: + description: 鏈鸿溅鍙� + type: string + required: + - content + - locomotiveNumber + type: object request.BatchProcessAudio: properties: ids: @@ -134,7 +163,7 @@ contact: {} paths: /api-sa/v1/audio/batchDelete: - post: + delete: parameters: - description: 鍙傛暟 in: body @@ -172,7 +201,7 @@ tags: - 闊抽 /api-sa/v1/audio/delete: - post: + delete: parameters: - description: 鍙傛暟 in: body @@ -299,4 +328,55 @@ summary: 涓婁紶闊抽 tags: - 闊抽 + /api-sa/v1/text/add: + post: + parameters: + - description: 鍙傛暟 + in: body + name: object + required: true + schema: + $ref: '#/definitions/request.AddTextReq' + produces: + - application/json + responses: + "200": + description: 鎴愬姛 + schema: + $ref: '#/definitions/util.Response' + summary: 鏂板鏂囧瓧 + tags: + - 鏂囧瓧搴� + /api-sa/v1/text/list: + get: + parameters: + - description: 鍏抽敭瀛� + in: query + name: keyword + type: string + - description: 椤电爜 + in: query + name: page + type: integer + - description: 姣忛〉澶у皬 + in: query + name: pageSize + type: integer + produces: + - application/json + responses: + "200": + description: 鎴愬姛 + schema: + allOf: + - $ref: '#/definitions/util.ResponseList' + - properties: + data: + items: + $ref: '#/definitions/models.Text' + type: array + type: object + summary: 鏂囧瓧搴撳垪琛� + tags: + - 鏂囧瓧搴� swagger: "2.0" diff --git a/models/db.go b/models/db.go index 8f977c0..0459d6a 100644 --- a/models/db.go +++ b/models/db.go @@ -75,6 +75,7 @@ err := db.AutoMigrate( Audio{}, AudioText{}, + Text{}, ) return err } diff --git a/models/text.go b/models/text.go new file mode 100644 index 0000000..4b4d2b9 --- /dev/null +++ b/models/text.go @@ -0,0 +1,272 @@ +package models + +import ( + "fmt" + "gorm.io/gorm" + "speechAnalysis/pkg/mysqlx" +) + +type ( + // Text 鏂囧瓧 + Text struct { + gorm.Model + Content string `gorm:"uniqueIndex:locomotive_number_text;type:varchar(255);not null;default:'';comment:闊抽鍚嶇О" json:"content"` // 闊抽鍚嶇О + LocomotiveNumber string `gorm:"uniqueIndex:locomotive_number_text;type:varchar(255);not null;default:'';comment:鏈鸿溅鍙�" json:"locomotiveNumber"` // 鏈鸿溅鍙� + } + + TextSearch struct { + Text + Order string + PageNum int + PageSize int + Orm *gorm.DB + Keyword string + IDs []uint + } +) + +func (slf *Text) TableName() string { + return "text" +} + +func NewTextSearch() *TextSearch { + return &TextSearch{Orm: mysqlx.GetDB()} +} + +func (slf *TextSearch) SetOrm(tx *gorm.DB) *TextSearch { + slf.Orm = tx + return slf +} + +func (slf *TextSearch) SetPage(page, size int) *TextSearch { + slf.PageNum, slf.PageSize = page, size + return slf +} + +func (slf *TextSearch) SetOrder(order string) *TextSearch { + slf.Order = order + return slf +} + +func (slf *TextSearch) SetID(id uint) *TextSearch { + slf.ID = id + return slf +} + +func (slf *TextSearch) SetIDs(ids []uint) *TextSearch { + slf.IDs = ids + return slf +} + +func (slf *TextSearch) SetKeyword(kw string) *TextSearch { + slf.Keyword = kw + return slf +} + +func (slf *TextSearch) SetLocomotiveNumber(number string) *TextSearch { + slf.LocomotiveNumber = number + return slf +} + +func (slf *TextSearch) SetContent(content string) *TextSearch { + slf.Content = content + return slf +} + +func (slf *TextSearch) build() *gorm.DB { + var db = slf.Orm.Table(slf.TableName()) + + if slf.ID != 0 { + db = db.Where("id = ?", slf.ID) + } + + if slf.Order != "" { + db = db.Order(slf.Order) + } + + if slf.Keyword != "" { + kw := "%" + slf.Keyword + "%" + db = db.Where("locomotive_number like ? or content like ?", kw, kw) + } + + if slf.LocomotiveNumber != "" { + db = db.Where("locomotive_number = ?", slf.LocomotiveNumber) + } + + if len(slf.IDs) > 0 { + db = db.Where("id in ?", slf.IDs) + } + + if slf.Content != "" { + db = db.Where("content = ?", slf.Content) + } + + return db +} + +// Create 鍗曟潯鎻掑叆 +func (slf *TextSearch) Create(record *Text) error { + var db = slf.build() + + if err := db.Create(record).Error; err != nil { + return fmt.Errorf("create err: %v, record: %+v", err, record) + } + + return nil +} + +// CreateBatch 鎵归噺鎻掑叆 +func (slf *TextSearch) CreateBatch(records []*Text) error { + var db = slf.build() + + if err := db.Create(&records).Error; err != nil { + return fmt.Errorf("create batch err: %v, records: %+v", err, records) + } + + return nil +} + +func (slf *TextSearch) Save(record *Text) error { + var db = slf.build() + + if err := db.Omit("CreatedAt").Save(record).Error; err != nil { + return fmt.Errorf("save err: %v, record: %+v", err, record) + } + + return nil +} + +func (slf *TextSearch) UpdateByMap(upMap map[string]interface{}) error { + var ( + db = slf.build() + ) + + if err := db.Updates(upMap).Error; err != nil { + return fmt.Errorf("update by map err: %v, upMap: %+v", err, upMap) + } + + return nil +} + +func (slf *TextSearch) UpdateByQuery(query string, args []interface{}, upMap map[string]interface{}) error { + var ( + db = slf.Orm.Table(slf.TableName()).Where(query, args...) + ) + + if err := db.Updates(upMap).Error; err != nil { + return fmt.Errorf("update by query err: %v, query: %s, args: %+v, upMap: %+v", err, query, args, upMap) + } + + return nil +} + +func (slf *TextSearch) Delete() error { + var db = slf.build() + + if err := db.Delete(&Text{}).Error; err != nil { + return err + } + + return nil +} + +func (slf *TextSearch) First() (*Text, error) { + var ( + record = new(Text) + db = slf.build() + ) + + if err := db.First(record).Error; err != nil { + return record, err + } + + return record, nil +} + +func (slf *TextSearch) Find() ([]*Text, int, error) { + var ( + records = make([]*Text, 0) + total int64 + db = slf.build() + ) + + if err := db.Count(&total).Error; err != nil { + return records, int(total), fmt.Errorf("find count err: %v", err) + } + if slf.PageNum*slf.PageSize > 0 { + db = db.Offset((slf.PageNum - 1) * slf.PageSize).Limit(slf.PageSize) + } + if err := db.Find(&records).Error; err != nil { + return records, int(total), fmt.Errorf("find records err: %v", err) + } + + return records, int(total), nil +} + +func (slf *TextSearch) FindNotTotal() ([]*Text, error) { + var ( + records = make([]*Text, 0) + db = slf.build() + ) + + if slf.PageNum*slf.PageSize > 0 { + db = db.Offset((slf.PageNum - 1) * slf.PageSize).Limit(slf.PageSize) + } + if err := db.Find(&records).Error; err != nil { + return records, fmt.Errorf("find records err: %v", err) + } + + return records, nil +} + +// FindByQuery 鎸囧畾鏉′欢鏌ヨ. +func (slf *TextSearch) FindByQuery(query string, args []interface{}) ([]*Text, int64, error) { + var ( + records = make([]*Text, 0) + total int64 + db = slf.Orm.Table(slf.TableName()).Where(query, args...) + ) + + if err := db.Count(&total).Error; err != nil { + return records, total, fmt.Errorf("find by query count err: %v", err) + } + if slf.PageNum*slf.PageSize > 0 { + db = db.Offset((slf.PageNum - 1) * slf.PageSize).Limit(slf.PageSize) + } + if err := db.Find(&records).Error; err != nil { + return records, total, fmt.Errorf("find by query records err: %v, query: %s, args: %+v", err, query, args) + } + + return records, total, nil +} + +// FindAll 鎸囧畾鏉′欢鏌ヨ&涓嶅垎椤�. +func (slf *TextSearch) FindAll(query string, args []interface{}) ([]*Text, error) { + var ( + records = make([]*Text, 0) + db = slf.Orm.Table(slf.TableName()).Where(query, args...) + ) + + if err := db.Find(&records).Error; err != nil { + return records, fmt.Errorf("find by query records err: %v, query: %s, args: %+v", err, query, args) + } + + return records, nil +} + +//// InitDefaultData 鍒濆鍖栨暟鎹� +//func (slf *TextSearch) InitDefaultData() error { +// var ( +// db = slf.Orm.Table(slf.TableName()) +// total int64 = 0 +// ) +// if err := db.Count(&total).Error; err != nil { +// return err +// } +// if total != 0 { +// return nil +// } +// Texts := make([]*Text, 0, 3) +// Texts = append(Texts, &Text{Name: "璐㈠姟閮�", Number: "cwb"}) +// return slf.CreateBatch(Texts) +//} diff --git a/request/text.go b/request/text.go new file mode 100644 index 0000000..6759eab --- /dev/null +++ b/request/text.go @@ -0,0 +1,11 @@ +package request + +type AddTextReq struct { + Content string `gorm:"type:varchar(255);not null;default:'';comment:闊抽鍚嶇О" json:"content" binding:"required"` // 闊抽鍚嶇О + LocomotiveNumber string `gorm:"index;type:varchar(255);not null;default:'';comment:鏈鸿溅鍙�" json:"locomotiveNumber" binding:"required"` // 鏈鸿溅鍙� +} + +type GetTextList struct { + PageInfo + Keyword string `form:"keyword"` // 鍏抽敭瀛� +} diff --git a/router/router.go b/router/router.go index aba2789..86b1549 100644 --- a/router/router.go +++ b/router/router.go @@ -22,16 +22,26 @@ urlPrefix := "/api-sa/v1" // 闊抽绠$悊 - AudioCtl := new(controllers.AudioCtl) - organizeAPI := r.Group(urlPrefix + "/audio") + audioCtl := new(controllers.AudioCtl) + audioAPi := r.Group(urlPrefix + "/audio") { - organizeAPI.POST("upload", AudioCtl.Upload) // 涓婁紶闊抽 - organizeAPI.GET("list", AudioCtl.List) // 闊抽妫�绱� - organizeAPI.POST("process", AudioCtl.Process) // 闊抽澶勭悊 - organizeAPI.POST("batchProcess", AudioCtl.BatchProcess) // 闊抽鎵归噺澶勭悊 - organizeAPI.POST("delete", AudioCtl.Delete) // 闊抽鍒犻櫎 - organizeAPI.POST("batchDelete", AudioCtl.BatchDelete) // 闊抽鎵归噺鍒犻櫎 - organizeAPI.POST("follow", AudioCtl.Follow) // 鍏虫敞/鍙栨秷鍏虫敞 + audioAPi.POST("upload", audioCtl.Upload) // 涓婁紶闊抽 + audioAPi.GET("list", audioCtl.List) // 闊抽妫�绱� + audioAPi.POST("process", audioCtl.Process) // 闊抽澶勭悊 + audioAPi.POST("batchProcess", audioCtl.BatchProcess) // 闊抽鎵归噺澶勭悊 + audioAPi.DELETE("delete", audioCtl.Delete) // 闊抽鍒犻櫎 + audioAPi.DELETE("batchDelete", audioCtl.BatchDelete) // 闊抽鎵归噺鍒犻櫎 + audioAPi.POST("follow", audioCtl.Follow) // 鍏虫敞/鍙栨秷鍏虫敞 + + } + + // 鏂囧瓧搴撶鐞� + textCtl := new(controllers.TextCtl) + textApi := r.Group(urlPrefix + "/text") + { + textApi.POST("add", textCtl.AddText) // 娣诲姞鏂囧瓧 + textApi.GET("list", textCtl.List) // 鏂囧瓧鍒楄〃 + } return r -- Gitblit v1.8.0