From 88bac7ba59ef68f81a91e5551ae6b264f3e529d4 Mon Sep 17 00:00:00 2001 From: yangchnet <1048887414@qq.com> Date: Mon, 14 Feb 2022 17:03:04 +0800 Subject: [PATCH] feat(sample): post create --- apis/post/handle_post.go | 438 ++++++++++++++--------------- apis/system/handle_post.go | 44 +++ db/query/post/post.go | 116 ++++---- db/schema/000008_post_table.up.sql | 16 +- docs/docs.go | 423 ++++------------------------ models/post/post.go | 40 --- models/system/post.go | 39 +++ pkg/form/post/form_post.go | 90 +++--- routers/init.go | 4 +- routers/post/post_router.go | 30 +- routers/system/sys_router.go | 7 + 11 files changed, 498 insertions(+), 749 deletions(-) create mode 100644 apis/system/handle_post.go delete mode 100644 models/post/post.go create mode 100644 models/system/post.go diff --git a/apis/post/handle_post.go b/apis/post/handle_post.go index 1a87e59..8b6e209 100644 --- a/apis/post/handle_post.go +++ b/apis/post/handle_post.go @@ -1,221 +1,221 @@ package post -import ( - "errors" - "time" - - "github.com/CodeHanHan/ferry-backend/db" - post "github.com/CodeHanHan/ferry-backend/db/query/post" - modelPost "github.com/CodeHanHan/ferry-backend/models/post" - "github.com/CodeHanHan/ferry-backend/pkg/app" - formPost "github.com/CodeHanHan/ferry-backend/pkg/form/post" - "github.com/CodeHanHan/ferry-backend/pkg/logger" - "github.com/CodeHanHan/ferry-backend/pkg/sender" - "github.com/gin-gonic/gin" - "github.com/gin-gonic/gin/binding" - "gorm.io/gorm" -) - -// CreatePost godoc -// @Summary 创建岗位 -// @Description 根据PostName和PostCode创建岗位信息 -// @Tags post -// @ID post-create -// @Param post body formPost.CreatePostRequest true "post名称和post等级" -// @Success 200 {object} formPost.CreatePostResponse -// @Failure 500 {object} app.ErrResponse -// @Failure 400 {object} app.ErrResponse -// @Produce json -// @Router /post [post] -// @Security BearerAuth -func CreatePost(c *gin.Context) { - var req formPost.CreatePostRequest - if err := c.ShouldBindWith(&req, binding.JSON); err != nil { - logger.ErrorParams(c, err) - app.ErrorParams(c, err) - return - } - - creator, _, err := sender.GetSender(c) - if err != nil { - logger.Error(c, err.Error()) - app.InternalServerError(c) - return - } - - newPost := modelPost.NewPost(req.PostName, req.PostCode, creator) - if err := post.CreatePost(c, newPost); err != nil { - if errors.Is(db.ErrDuplicateValue, err) { - app.Error(c, app.Err_Invalid_Argument, "PostName already exists") - return - } - app.InternalServerError(c) - return - } - - resp := formPost.CreatePostResponse{ - PostID: newPost.PostID, - PostName: newPost.PostName, - } - - app.OK(c, resp) -} - -// DeletePost godoc -// @Summary 删除岗位 -// @Description 根据PostID删除岗位 -// @Tags post -// @ID post-delete -// @Param post_id path string true "岗位唯一id" -// @Success 200 {object} formPost.DeletePostResponse -// @Failure 500 {object} app.ErrResponse -// @Failure 400 {object} app.ErrResponse -// @Produce json -// @Router /post/{post_id} [delete] -// @Security BearerAuth -func DeletePost(c *gin.Context) { - var req formPost.DeletePostRequest - if err := c.ShouldBindUri(&req); err != nil { - logger.ErrorParams(c, err) - app.ErrorParams(c, err) - return - } - - if err := post.DeletePostById(c, req.PostID); err != nil { - app.InternalServerError(c) - return - } - - app.OK(c, formPost.DeletePostResponse{ - Result: "success", - }) -} - -// ListPost godoc -// @Summary 查询岗位列表 -// @Description 根据offset和limit查询岗位列表 -// @Tags post -// @ID post-list -// @Param offset query int true "偏移" -// @Param limit query int true "限制" -// @Success 200 {object} formPost.ListPostResponse -// @Failure 500 {object} app.ErrResponse -// @Failure 400 {object} app.ErrResponse -// @Produce json -// @Router /post [get] -// @Security BearerAuth -func ListPost(c *gin.Context) { - var req formPost.ListPostRequest - if err := c.ShouldBind(&req); err != nil { - logger.ErrorParams(c, err) - app.ErrorParams(c, err) - return - } - - list, err := post.SearchPost(c, *req.Offset, req.Limit) - if err != nil { - app.InternalServerError(c) - return - } - - resp := formPost.ListPostResponse{ - Post: list, - Length: len(list), - } - - app.OK(c, resp) -} - -// GetPost godoc -// @Summary 查询岗位 -// @Description 根据岗位id查询岗位信息 -// @Tags post -// @ID post-get -// @Param post_id path string true "部门id" -// @Success 200 {object} formPost.GetPostResponse -// @Failure 500 {object} app.ErrResponse -// @Failure 400 {object} app.ErrResponse -// @Produce json -// @Router /post/{post_id} [get] -// @Security BearerAuth -func GetPost(c *gin.Context) { - var req formPost.GetPostRequest - if err := c.ShouldBindUri(&req); err != nil { - logger.ErrorParams(c, err) - app.ErrorParams(c, err) - return - } - - f := db.NewFilter().Set("post_id", req.PostID) - post, err := post.GetPost(c, f) - if err != nil { - if errors.Is(err, gorm.ErrRecordNotFound) { - app.Errorf(c, app.Err_Not_found, "查询失败,未找到该记录值: %s", req.PostID) - return - } - app.InternalServerError(c) - return - } - - resp := formPost.GetPostResponse{ - Post: post, - } - - app.OK(c, resp) -} - -// UpdatePost godoc -// @Summary 更新岗位 -// @Description 更新岗位信息 -// @Tags post -// @ID post-update -// @Param post body formPost.UpdatePostRequest true "包含postid、post名称、post等级等相关信息" -// @Success 200 {object} formPost.UpdatePostResponse -// @Failure 500 {object} app.ErrResponse -// @Failure 400 {object} app.ErrResponse -// @Accept application/json -// @Produce json -// @Router /post [put] -// @Security BearerAuth -func UpdatePost(c *gin.Context) { - var req formPost.UpdatePostRequest - if err := c.ShouldBindBodyWith(&req, binding.JSON); err != nil { - logger.ErrorParams(c, err) - app.ErrorParams(c, err) - return - } - - post_ := modelPost.Post(req) - - f := db.NewFilter().Set("post_id", req.PostID) - _, err := post.GetPost(c, f) - if err != nil { - if errors.Is(err, gorm.ErrRecordNotFound) { - app.Errorf(c, app.Err_Not_found, "查询失败,未找到该记录值: %s", req.PostID) - return - } - app.InternalServerError(c) - return - } - - sender, _, err := sender.GetSender(c) - if err != nil { - logger.Error(c, err.Error()) - app.Error(c, app.Err_Unauthenticated, err) - return - } - - var TimeNow = time.Now() - post_.UpdateBy = sender - post_.UpdateTime = &TimeNow - if err := post.UpdatePost(c, &post_); err != nil { - app.InternalServerError(c) - return - } - - resp := formPost.UpdatePostResponse{ - Result: "success", - } - - app.OK(c, resp) -} +// import ( +// "errors" +// "time" + +// "github.com/CodeHanHan/ferry-backend/db" +// post "github.com/CodeHanHan/ferry-backend/db/query/post" +// modelPost "github.com/CodeHanHan/ferry-backend/models/post" +// "github.com/CodeHanHan/ferry-backend/pkg/app" +// formPost "github.com/CodeHanHan/ferry-backend/pkg/form/post" +// "github.com/CodeHanHan/ferry-backend/pkg/logger" +// "github.com/CodeHanHan/ferry-backend/pkg/sender" +// "github.com/gin-gonic/gin" +// "github.com/gin-gonic/gin/binding" +// "gorm.io/gorm" +// ) + +// // CreatePost godoc +// // @Summary 创建岗位 +// // @Description 根据PostName和PostCode创建岗位信息 +// // @Tags post +// // @ID post-create +// // @Param post body formPost.CreatePostRequest true "post名称和post等级" +// // @Success 200 {object} formPost.CreatePostResponse +// // @Failure 500 {object} app.ErrResponse +// // @Failure 400 {object} app.ErrResponse +// // @Produce json +// // @Router /post [post] +// // @Security BearerAuth +// func CreatePost(c *gin.Context) { +// var req formPost.CreatePostRequest +// if err := c.ShouldBindWith(&req, binding.JSON); err != nil { +// logger.ErrorParams(c, err) +// app.ErrorParams(c, err) +// return +// } + +// creator, _, err := sender.GetSender(c) +// if err != nil { +// logger.Error(c, err.Error()) +// app.InternalServerError(c) +// return +// } + +// newPost := modelPost.NewPost(req.PostName, req.PostCode, creator) +// if err := post.CreatePost(c, newPost); err != nil { +// if errors.Is(db.ErrDuplicateValue, err) { +// app.Error(c, app.Err_Invalid_Argument, "PostName already exists") +// return +// } +// app.InternalServerError(c) +// return +// } + +// resp := formPost.CreatePostResponse{ +// PostID: newPost.PostID, +// PostName: newPost.PostName, +// } + +// app.OK(c, resp) +// } + +// // DeletePost godoc +// // @Summary 删除岗位 +// // @Description 根据PostID删除岗位 +// // @Tags post +// // @ID post-delete +// // @Param post_id path string true "岗位唯一id" +// // @Success 200 {object} formPost.DeletePostResponse +// // @Failure 500 {object} app.ErrResponse +// // @Failure 400 {object} app.ErrResponse +// // @Produce json +// // @Router /post/{post_id} [delete] +// // @Security BearerAuth +// func DeletePost(c *gin.Context) { +// var req formPost.DeletePostRequest +// if err := c.ShouldBindUri(&req); err != nil { +// logger.ErrorParams(c, err) +// app.ErrorParams(c, err) +// return +// } + +// if err := post.DeletePostById(c, req.PostID); err != nil { +// app.InternalServerError(c) +// return +// } + +// app.OK(c, formPost.DeletePostResponse{ +// Result: "success", +// }) +// } + +// // ListPost godoc +// // @Summary 查询岗位列表 +// // @Description 根据offset和limit查询岗位列表 +// // @Tags post +// // @ID post-list +// // @Param offset query int true "偏移" +// // @Param limit query int true "限制" +// // @Success 200 {object} formPost.ListPostResponse +// // @Failure 500 {object} app.ErrResponse +// // @Failure 400 {object} app.ErrResponse +// // @Produce json +// // @Router /post [get] +// // @Security BearerAuth +// func ListPost(c *gin.Context) { +// var req formPost.ListPostRequest +// if err := c.ShouldBind(&req); err != nil { +// logger.ErrorParams(c, err) +// app.ErrorParams(c, err) +// return +// } + +// list, err := post.SearchPost(c, *req.Offset, req.Limit) +// if err != nil { +// app.InternalServerError(c) +// return +// } + +// resp := formPost.ListPostResponse{ +// Post: list, +// Length: len(list), +// } + +// app.OK(c, resp) +// } + +// // GetPost godoc +// // @Summary 查询岗位 +// // @Description 根据岗位id查询岗位信息 +// // @Tags post +// // @ID post-get +// // @Param post_id path string true "部门id" +// // @Success 200 {object} formPost.GetPostResponse +// // @Failure 500 {object} app.ErrResponse +// // @Failure 400 {object} app.ErrResponse +// // @Produce json +// // @Router /post/{post_id} [get] +// // @Security BearerAuth +// func GetPost(c *gin.Context) { +// var req formPost.GetPostRequest +// if err := c.ShouldBindUri(&req); err != nil { +// logger.ErrorParams(c, err) +// app.ErrorParams(c, err) +// return +// } + +// f := db.NewFilter().Set("post_id", req.PostID) +// post, err := post.GetPost(c, f) +// if err != nil { +// if errors.Is(err, gorm.ErrRecordNotFound) { +// app.Errorf(c, app.Err_Not_found, "查询失败,未找到该记录值: %s", req.PostID) +// return +// } +// app.InternalServerError(c) +// return +// } + +// resp := formPost.GetPostResponse{ +// Post: post, +// } + +// app.OK(c, resp) +// } + +// // UpdatePost godoc +// // @Summary 更新岗位 +// // @Description 更新岗位信息 +// // @Tags post +// // @ID post-update +// // @Param post body formPost.UpdatePostRequest true "包含postid、post名称、post等级等相关信息" +// // @Success 200 {object} formPost.UpdatePostResponse +// // @Failure 500 {object} app.ErrResponse +// // @Failure 400 {object} app.ErrResponse +// // @Accept application/json +// // @Produce json +// // @Router /post [put] +// // @Security BearerAuth +// func UpdatePost(c *gin.Context) { +// var req formPost.UpdatePostRequest +// if err := c.ShouldBindBodyWith(&req, binding.JSON); err != nil { +// logger.ErrorParams(c, err) +// app.ErrorParams(c, err) +// return +// } + +// post_ := modelPost.Post(req) + +// f := db.NewFilter().Set("post_id", req.PostID) +// _, err := post.GetPost(c, f) +// if err != nil { +// if errors.Is(err, gorm.ErrRecordNotFound) { +// app.Errorf(c, app.Err_Not_found, "查询失败,未找到该记录值: %s", req.PostID) +// return +// } +// app.InternalServerError(c) +// return +// } + +// sender, _, err := sender.GetSender(c) +// if err != nil { +// logger.Error(c, err.Error()) +// app.Error(c, app.Err_Unauthenticated, err) +// return +// } + +// var TimeNow = time.Now() +// post_.UpdateBy = sender +// post_.UpdateTime = &TimeNow +// if err := post.UpdatePost(c, &post_); err != nil { +// app.InternalServerError(c) +// return +// } + +// resp := formPost.UpdatePostResponse{ +// Result: "success", +// } + +// app.OK(c, resp) +// } diff --git a/apis/system/handle_post.go b/apis/system/handle_post.go new file mode 100644 index 0000000..4f1b92b --- /dev/null +++ b/apis/system/handle_post.go @@ -0,0 +1,44 @@ +package system + +import ( + "github.com/CodeHanHan/ferry-backend/models/system" + "github.com/CodeHanHan/ferry-backend/pkg/app" + "github.com/CodeHanHan/ferry-backend/pkg/logger" + "github.com/CodeHanHan/ferry-backend/pkg/sender" + "github.com/gin-gonic/gin" +) + +// @Summary 添加职位 +// @Description 获取JSON +// @Tags 职位 +// @Accept application/json +// @Product application/json +// @Param data body system.Post true "data" +// @Success 200 {string} string "{"code": 200, "message": "添加成功"}" +// @Success 200 {string} string "{"code": -1, "message": "添加失败"}" +// @Router /post [post] +// @Security Bearer +func InsertPost(c *gin.Context) { + var post system.Post + if err := c.Bind(&post); err != nil { + logger.Error(c, err.Error()) + app.ErrorParams(c, err) + return + } + + sender, _, err := sender.GetSender(c) + if err != nil { + app.InternalServerError(c) + return + } + + post.CreateBy = sender + + post_, err := post.Create(c) + if err != nil { + app.InternalServerError(c) + return + } + + app.AdaptOK(c, post_, "") +} diff --git a/db/query/post/post.go b/db/query/post/post.go index 98e72a3..e3e6e3b 100644 --- a/db/query/post/post.go +++ b/db/query/post/post.go @@ -1,60 +1,60 @@ package post -import ( - "context" - - "github.com/CodeHanHan/ferry-backend/db" - modelPost "github.com/CodeHanHan/ferry-backend/models/post" - "github.com/CodeHanHan/ferry-backend/pkg/logger" - "github.com/go-sql-driver/mysql" -) - -func CreatePost(ctx context.Context, post *modelPost.Post) error { - if err := db.Store.Table(modelPost.PostTableName).Create(post).Error; err != nil { - logger.Error(ctx, err.Error()) - err, ok := err.(*mysql.MySQLError) - if ok && err.Number == 1062 { - return db.ErrDuplicateValue - } - - return err - } - return nil -} - -func DeletePostById(ctx context.Context, postId string) error { - var post modelPost.Post - if err := db.Store.Table(modelPost.PostTableName).Where(map[string]interface{}{"post_id": postId}).Delete(&post).Error; err != nil { - logger.Error(ctx, err.Error()) - return err - } - return nil -} - -func SearchPost(ctx context.Context, offset, limit int) (list []*modelPost.Post, err error) { - if err := db.Store.Table(modelPost.PostTableName).Offset(offset).Limit(limit).Find(&list).Error; err != nil { - logger.Error(ctx, err.Error()) - return nil, err - } - - return -} - -func GetPost(ctx context.Context, f *db.Filter) (post *modelPost.Post, err error) { - if err := db.Store.Table(modelPost.PostTableName).Where(f.Params).Take(&post).Error; err != nil { - logger.Error(ctx, err.Error()) - return nil, err - } - - return -} - -func UpdatePost(ctx context.Context, post *modelPost.Post) error { - if err := db.Store.Table(modelPost.PostTableName).Updates(post).Error; err != nil { - logger.Error(ctx, err.Error()) - return err - } - - return nil - -} \ No newline at end of file +// import ( +// "context" + +// "github.com/CodeHanHan/ferry-backend/db" +// modelPost "github.com/CodeHanHan/ferry-backend/models/post" +// "github.com/CodeHanHan/ferry-backend/pkg/logger" +// "github.com/go-sql-driver/mysql" +// ) + +// func CreatePost(ctx context.Context, post *modelPost.Post) error { +// if err := db.Store.Table(modelPost.PostTableName).Create(post).Error; err != nil { +// logger.Error(ctx, err.Error()) +// err, ok := err.(*mysql.MySQLError) +// if ok && err.Number == 1062 { +// return db.ErrDuplicateValue +// } + +// return err +// } +// return nil +// } + +// func DeletePostById(ctx context.Context, postId string) error { +// var post modelPost.Post +// if err := db.Store.Table(modelPost.PostTableName).Where(map[string]interface{}{"post_id": postId}).Delete(&post).Error; err != nil { +// logger.Error(ctx, err.Error()) +// return err +// } +// return nil +// } + +// func SearchPost(ctx context.Context, offset, limit int) (list []*modelPost.Post, err error) { +// if err := db.Store.Table(modelPost.PostTableName).Offset(offset).Limit(limit).Find(&list).Error; err != nil { +// logger.Error(ctx, err.Error()) +// return nil, err +// } + +// return +// } + +// func GetPost(ctx context.Context, f *db.Filter) (post *modelPost.Post, err error) { +// if err := db.Store.Table(modelPost.PostTableName).Where(f.Params).Take(&post).Error; err != nil { +// logger.Error(ctx, err.Error()) +// return nil, err +// } + +// return +// } + +// func UpdatePost(ctx context.Context, post *modelPost.Post) error { +// if err := db.Store.Table(modelPost.PostTableName).Updates(post).Error; err != nil { +// logger.Error(ctx, err.Error()) +// return err +// } + +// return nil + +// } diff --git a/db/schema/000008_post_table.up.sql b/db/schema/000008_post_table.up.sql index 9cfb55c..0060892 100644 --- a/db/schema/000008_post_table.up.sql +++ b/db/schema/000008_post_table.up.sql @@ -1,13 +1,15 @@ CREATE TABLE `post` ( - `post_id` varchar(255) PRIMARY KEY NOT NULL, + `post_id` int(11) NOT NULL AUTO_INCREMENT, `post_name` varchar(128) DEFAULT NULL, `post_code` varchar(128) DEFAULT NULL, - `sort` int DEFAULT NULL, - `status` int DEFAULT NULL, + `sort` int(4) DEFAULT NULL, + `status` int(1) DEFAULT NULL, `remark` varchar(255) DEFAULT NULL, `create_by` varchar(128) DEFAULT NULL, `update_by` varchar(128) DEFAULT NULL, - `create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP, - `update_time` timestamp NULL DEFAULT '2000-01-01 00:00:00', - `delete_time` timestamp NULL DEFAULT '2000-01-01 00:00:00' -); \ No newline at end of file + `create_time` timestamp NULL DEFAULT NULL, + `update_time` timestamp NULL DEFAULT NULL, + `delete_time` timestamp NULL DEFAULT NULL, + PRIMARY KEY (`post_id`), + KEY `idx_sys_post_delete_time` (`delete_time`) +) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4; \ No newline at end of file diff --git a/docs/docs.go b/docs/docs.go index d68a58c..caec6e9 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -911,243 +911,36 @@ var doc = `{ } }, "/post": { - "get": { - "security": [ - { - "BearerAuth": [] - } - ], - "description": "根据offset和limit查询岗位列表", - "produces": [ - "application/json" - ], - "tags": [ - "post" - ], - "summary": "查询岗位列表", - "operationId": "post-list", - "parameters": [ - { - "type": "integer", - "description": "偏移", - "name": "offset", - "in": "query", - "required": true - }, - { - "type": "integer", - "description": "限制", - "name": "limit", - "in": "query", - "required": true - } - ], - "responses": { - "200": { - "description": "OK", - "schema": { - "$ref": "#/definitions/post.ListPostResponse" - } - }, - "400": { - "description": "Bad Request", - "schema": { - "$ref": "#/definitions/app.ErrResponse" - } - }, - "500": { - "description": "Internal Server Error", - "schema": { - "$ref": "#/definitions/app.ErrResponse" - } - } - } - }, - "put": { - "security": [ - { - "BearerAuth": [] - } - ], - "description": "更新岗位信息", - "consumes": [ - "application/json" - ], - "produces": [ - "application/json" - ], - "tags": [ - "post" - ], - "summary": "更新岗位", - "operationId": "post-update", - "parameters": [ - { - "description": "包含postid、post名称、post等级等相关信息", - "name": "post", - "in": "body", - "required": true, - "schema": { - "$ref": "#/definitions/post.UpdatePostRequest" - } - } - ], - "responses": { - "200": { - "description": "OK", - "schema": { - "$ref": "#/definitions/post.UpdatePostResponse" - } - }, - "400": { - "description": "Bad Request", - "schema": { - "$ref": "#/definitions/app.ErrResponse" - } - }, - "500": { - "description": "Internal Server Error", - "schema": { - "$ref": "#/definitions/app.ErrResponse" - } - } - } - }, "post": { "security": [ { - "BearerAuth": [] + "Bearer": [] } ], - "description": "根据PostName和PostCode创建岗位信息", - "produces": [ + "description": "获取JSON", + "consumes": [ "application/json" ], "tags": [ - "post" + "职位" ], - "summary": "创建岗位", - "operationId": "post-create", + "summary": "添加职位", "parameters": [ { - "description": "post名称和post等级", - "name": "post", + "description": "data", + "name": "data", "in": "body", "required": true, "schema": { - "$ref": "#/definitions/post.CreatePostRequest" + "$ref": "#/definitions/system.Post" } } ], "responses": { "200": { - "description": "OK", - "schema": { - "$ref": "#/definitions/post.CreatePostResponse" - } - }, - "400": { - "description": "Bad Request", - "schema": { - "$ref": "#/definitions/app.ErrResponse" - } - }, - "500": { - "description": "Internal Server Error", - "schema": { - "$ref": "#/definitions/app.ErrResponse" - } - } - } - } - }, - "/post/{post_id}": { - "get": { - "security": [ - { - "BearerAuth": [] - } - ], - "description": "根据岗位id查询岗位信息", - "produces": [ - "application/json" - ], - "tags": [ - "post" - ], - "summary": "查询岗位", - "operationId": "post-get", - "parameters": [ - { - "type": "string", - "description": "部门id", - "name": "post_id", - "in": "path", - "required": true - } - ], - "responses": { - "200": { - "description": "OK", - "schema": { - "$ref": "#/definitions/post.GetPostResponse" - } - }, - "400": { - "description": "Bad Request", - "schema": { - "$ref": "#/definitions/app.ErrResponse" - } - }, - "500": { - "description": "Internal Server Error", - "schema": { - "$ref": "#/definitions/app.ErrResponse" - } - } - } - }, - "delete": { - "security": [ - { - "BearerAuth": [] - } - ], - "description": "根据PostID删除岗位", - "produces": [ - "application/json" - ], - "tags": [ - "post" - ], - "summary": "删除岗位", - "operationId": "post-delete", - "parameters": [ - { - "type": "string", - "description": "岗位唯一id", - "name": "post_id", - "in": "path", - "required": true - } - ], - "responses": { - "200": { - "description": "OK", - "schema": { - "$ref": "#/definitions/post.DeletePostResponse" - } - }, - "400": { - "description": "Bad Request", - "schema": { - "$ref": "#/definitions/app.ErrResponse" - } - }, - "500": { - "description": "Internal Server Error", + "description": "{\"code\": -1, \"message\": \"添加失败\"}", "schema": { - "$ref": "#/definitions/app.ErrResponse" + "type": "string" } } } @@ -2034,152 +1827,6 @@ var doc = `{ } } }, - "post.CreatePostRequest": { - "type": "object", - "required": [ - "post_code", - "post_name" - ], - "properties": { - "post_code": { - "type": "string" - }, - "post_name": { - "type": "string" - } - } - }, - "post.CreatePostResponse": { - "type": "object", - "properties": { - "post_id": { - "type": "string" - }, - "post_name": { - "type": "string" - } - } - }, - "post.DeletePostResponse": { - "type": "object", - "properties": { - "result": { - "type": "string" - } - } - }, - "post.GetPostResponse": { - "type": "object", - "properties": { - "post": { - "$ref": "#/definitions/post.Post" - } - } - }, - "post.ListPostResponse": { - "type": "object", - "properties": { - "length": { - "type": "integer" - }, - "post": { - "type": "array", - "items": { - "$ref": "#/definitions/post.Post" - } - } - } - }, - "post.Post": { - "type": "object", - "properties": { - "create_by": { - "type": "string" - }, - "create_time": { - "type": "string", - "default": "2000-01-01 00:00:00" - }, - "delete_time": { - "type": "string", - "default": "2000-01-01 00:00:00" - }, - "post_code": { - "type": "string" - }, - "post_id": { - "type": "string" - }, - "post_name": { - "type": "string" - }, - "remark": { - "type": "string" - }, - "sort": { - "type": "integer" - }, - "status": { - "type": "integer" - }, - "update_by": { - "type": "string" - }, - "update_time": { - "type": "string", - "default": "2000-01-01 00:00:00" - } - } - }, - "post.UpdatePostRequest": { - "type": "object", - "properties": { - "create_by": { - "type": "string" - }, - "create_time": { - "type": "string", - "default": "2000-01-01 00:00:00" - }, - "delete_time": { - "type": "string", - "default": "2000-01-01 00:00:00" - }, - "post_code": { - "type": "string" - }, - "post_id": { - "type": "string" - }, - "post_name": { - "type": "string" - }, - "remark": { - "type": "string" - }, - "sort": { - "type": "integer" - }, - "status": { - "type": "integer" - }, - "update_by": { - "type": "string" - }, - "update_time": { - "type": "string", - "default": "2000-01-01 00:00:00" - } - } - }, - "post.UpdatePostResponse": { - "type": "object", - "properties": { - "result": { - "type": "string" - } - } - }, "role.CreateRoleRequest": { "type": "object", "required": [ @@ -2394,6 +2041,56 @@ var doc = `{ } } }, + "system.Post": { + "type": "object", + "properties": { + "createBy": { + "type": "string" + }, + "create_time": { + "type": "string", + "default": "2000-01-01 00:00:00" + }, + "delete_time": { + "type": "string", + "default": "2000-01-01 00:00:00" + }, + "params": { + "type": "string" + }, + "postCode": { + "description": "岗位代码", + "type": "string" + }, + "postId": { + "description": "岗位编号", + "type": "integer" + }, + "postName": { + "description": "岗位名称", + "type": "string" + }, + "remark": { + "description": "描述", + "type": "string" + }, + "sort": { + "description": "岗位排序", + "type": "integer" + }, + "status": { + "description": "状态", + "type": "string" + }, + "updateBy": { + "type": "string" + }, + "update_time": { + "type": "string", + "default": "2000-01-01 00:00:00" + } + } + }, "system.Role": { "type": "object", "properties": { diff --git a/models/post/post.go b/models/post/post.go deleted file mode 100644 index dda5240..0000000 --- a/models/post/post.go +++ /dev/null @@ -1,40 +0,0 @@ -package post - -import ( - "time" - - "github.com/CodeHanHan/ferry-backend/utils/idutil" -) - -const PostTableName = "post" - -type PostStatus int - -const ( - PostDeactivated PostStatus = 0 - PostActive PostStatus = 1 -) - -type Post struct { - PostID string `gorm:"column:post_id;primary_key" json:"post_id"` - PostName string `gorm:"column:post_name" json:"post_name"` - PostCode string `gorm:"column:post_code" json:"post_code"` - Sort int `gorm:"column:sort" json:"sort"` - Status int `gorm:"column:status" json:"status"` - Remark string `gorm:"column:remark" json:"remark"` - CreateBy string `gorm:"column:create_by" json:"create_by"` - UpdateBy string `gorm:"column:update_by" json:"update_by"` - CreateTime *time.Time `gorm:"column:create_time" json:"create_time" default:"2000-01-01 00:00:00"` - UpdateTime *time.Time `gorm:"column:update_time" json:"update_time" default:"2000-01-01 00:00:00"` - DeleteTime *time.Time `gorm:"column:delete_time" json:"delete_time" default:"2000-01-01 00:00:00"` -} - -func NewPost(postname string, postcode string, create_by string) *Post { - return &Post{ - PostID: idutil.GetId("post"), - PostName: postname, - PostCode: postcode, - Status: int(PostActive), - CreateBy: create_by, - } -} diff --git a/models/system/post.go b/models/system/post.go new file mode 100644 index 0000000..3af4c37 --- /dev/null +++ b/models/system/post.go @@ -0,0 +1,39 @@ +package system + +import ( + "context" + "time" + + "github.com/CodeHanHan/ferry-backend/db" + "github.com/CodeHanHan/ferry-backend/pkg/logger" +) + +const PostTableName = "post" + +type PostStatus int + +type Post struct { + PostId int `gorm:"primary_key;AUTO_INCREMENT" json:"postId"` //岗位编号 + PostName string `gorm:"type:varchar(128);" json:"postName"` //岗位名称 + PostCode string `gorm:"type:varchar(128);" json:"postCode"` //岗位代码 + Sort int `gorm:"type:int(4);" json:"sort"` //岗位排序 + Status string `gorm:"type:int(1);" json:"status"` //状态 + Remark string `gorm:"type:varchar(255);" json:"remark"` //描述 + CreateBy string `gorm:"type:varchar(128);" json:"createBy"` + UpdateBy string `gorm:"type:varchar(128);" json:"updateBy"` + Params string `gorm:"-" json:"params"` + CreateTime *time.Time `gorm:"column:create_time" json:"create_time" default:"2000-01-01 00:00:00"` + UpdateTime *time.Time `gorm:"column:update_time" json:"update_time" default:"2000-01-01 00:00:00"` + DeleteTime *time.Time `gorm:"column:delete_time" json:"delete_time" default:"2000-01-01 00:00:00"` +} + +func (p *Post) Create(ctx context.Context) (*Post, error) { + var post Post + if err := db.Store.Table(PostTableName).Create(p).Error; err != nil { + logger.Error(ctx, err.Error()) + return nil, err + } + + post = *p + return &post, nil +} diff --git a/pkg/form/post/form_post.go b/pkg/form/post/form_post.go index 124bf0c..70fe72a 100644 --- a/pkg/form/post/form_post.go +++ b/pkg/form/post/form_post.go @@ -1,47 +1,47 @@ package post -import ( - "github.com/CodeHanHan/ferry-backend/models/post" -) - -type CreatePostRequest struct { - PostCode string `json:"post_code" form:"post_code" binding:"required"` - PostName string `json:"post_name" form:"post_name" binding:"required"` -} - -type CreatePostResponse struct { - PostID string `json:"post_id"` - PostName string `json:"post_name"` -} - -type DeletePostRequest struct { - PostID string `json:"post_id" uri:"post_id" binding:"required"` -} - -type DeletePostResponse struct { - Result string `json:"result"` -} - -type ListPostRequest struct { - Offset *int `json:"offset" form:"offset" binding:"required"` - Limit int `json:"limit" form:"limit" binding:"required"` -} - -type ListPostResponse struct { - Post []*post.Post - Length int -} - -type GetPostRequest struct { - PostID string `json:"post_id" uri:"post_id" binding:"required"` -} - -type GetPostResponse struct { - Post *post.Post -} - -type UpdatePostRequest post.Post - -type UpdatePostResponse struct { - Result string `json:"result"` -} \ No newline at end of file +// import ( +// "github.com/CodeHanHan/ferry-backend/models/post" +// ) + +// type CreatePostRequest struct { +// PostCode string `json:"post_code" form:"post_code" binding:"required"` +// PostName string `json:"post_name" form:"post_name" binding:"required"` +// } + +// type CreatePostResponse struct { +// PostID string `json:"post_id"` +// PostName string `json:"post_name"` +// } + +// type DeletePostRequest struct { +// PostID string `json:"post_id" uri:"post_id" binding:"required"` +// } + +// type DeletePostResponse struct { +// Result string `json:"result"` +// } + +// type ListPostRequest struct { +// Offset *int `json:"offset" form:"offset" binding:"required"` +// Limit int `json:"limit" form:"limit" binding:"required"` +// } + +// type ListPostResponse struct { +// Post []*post.Post +// Length int +// } + +// type GetPostRequest struct { +// PostID string `json:"post_id" uri:"post_id" binding:"required"` +// } + +// type GetPostResponse struct { +// Post *post.Post +// } + +// type UpdatePostRequest post.Post + +// type UpdatePostResponse struct { +// Result string `json:"result"` +// } diff --git a/routers/init.go b/routers/init.go index 630f280..fa32f78 100644 --- a/routers/init.go +++ b/routers/init.go @@ -11,7 +11,6 @@ import ( "github.com/CodeHanHan/ferry-backend/pkg/pi" "github.com/CodeHanHan/ferry-backend/routers/dept" "github.com/CodeHanHan/ferry-backend/routers/ping" - "github.com/CodeHanHan/ferry-backend/routers/post" "github.com/CodeHanHan/ferry-backend/routers/system" "github.com/CodeHanHan/ferry-backend/routers/user" ) @@ -54,12 +53,13 @@ func InitAuthSysRouter(r *gin.RouterGroup, mdw ...gin.HandlerFunc) *gin.RouterGr user.RegisterUserRouter(g, mdw...) dept.RegisterDeptRouter(g, mdw...) - post.RegisterPostRouter(g, mdw...) + // post.RegisterPostRouter(g, mdw...) system.RegisterMenuRouter(g, mdw...) system.RegisterRoleMenuRouter(g, mdw...) system.RegisterPageRouter(g, mdw...) system.RegisterRoleRouter(g, mdw...) + system.RegisterPostRouter(g, mdw...) return g } diff --git a/routers/post/post_router.go b/routers/post/post_router.go index f3c5bf8..523f361 100644 --- a/routers/post/post_router.go +++ b/routers/post/post_router.go @@ -1,18 +1,18 @@ package post -import ( - "github.com/CodeHanHan/ferry-backend/apis/post" - "github.com/gin-gonic/gin" -) +// import ( +// "github.com/CodeHanHan/ferry-backend/apis/post" +// "github.com/gin-gonic/gin" +// ) -func RegisterPostRouter(g *gin.RouterGroup, mdw ...gin.HandlerFunc) { - postGroup := g.Group("/post", mdw...) - { - postGroup.POST("", post.CreatePost) - postGroup.DELETE("/:post_id", post.DeletePost) - postGroup.GET("", post.ListPost) - postGroup.GET("/:post_id", post.GetPost) - postGroup.PUT("", post.UpdatePost) - - } -} +// func RegisterPostRouter(g *gin.RouterGroup, mdw ...gin.HandlerFunc) { +// postGroup := g.Group("/post", mdw...) +// { +// postGroup.POST("", post.CreatePost) +// postGroup.DELETE("/:post_id", post.DeletePost) +// postGroup.GET("", post.ListPost) +// postGroup.GET("/:post_id", post.GetPost) +// postGroup.PUT("", post.UpdatePost) + +// } +// } diff --git a/routers/system/sys_router.go b/routers/system/sys_router.go index 130b98f..08b1ca4 100644 --- a/routers/system/sys_router.go +++ b/routers/system/sys_router.go @@ -41,3 +41,10 @@ func RegisterRoleRouter(g *gin.RouterGroup, mdw ...gin.HandlerFunc) { roleGroup.PUT("", system.UpdateRole) } } + +func RegisterPostRouter(g *gin.RouterGroup, mdw ...gin.HandlerFunc) { + postGroup := g.Group("/post", mdw...) + { + postGroup.POST("", system.InsertPost) + } +}