diff --git a/docs/docs.go b/docs/docs.go index 15daedc..0c45f89 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -59,14 +59,20 @@ var doc = `{ }, "/admin/v1/employees": { "get": { + "consumes": [ + "application/json" + ], "produces": [ "application/json" ], + "tags": [ + "employees" + ], "summary": "获取员工列表", "parameters": [ { "type": "string", - "description": "auth by /auth", + "description": "auth by /admin/login", "name": "Authorization", "in": "header", "required": true @@ -98,18 +104,81 @@ var doc = `{ } } } + }, + "post": { + "consumes": [ + "multipart/form-data" + ], + "produces": [ + "application/json" + ], + "tags": [ + "employees" + ], + "summary": "新增员工", + "parameters": [ + { + "type": "string", + "description": "auth by /admin/login", + "name": "Authorization", + "in": "header", + "required": true + }, + { + "type": "string", + "description": "员工用户名", + "name": "username", + "in": "formData", + "required": true + }, + { + "type": "string", + "description": "员工密码", + "name": "password", + "in": "formData", + "required": true + }, + { + "type": "string", + "description": "员工部门", + "name": "department", + "in": "formData", + "required": true + }, + { + "type": "string", + "description": "员工职位", + "name": "position", + "in": "formData", + "required": true + } + ], + "responses": { + "200": { + "description": "{\"code\": 200, \"data\":{}, \"msg\":\"ok\"}", + "schema": { + "type": "string" + } + } + } } }, "/admin/v1/employees/{id}": { "get": { + "consumes": [ + "application/json" + ], "produces": [ "application/json" ], + "tags": [ + "employees" + ], "summary": "获取单个员工", "parameters": [ { "type": "string", - "description": "auth by /auth", + "description": "auth by /admin/login", "name": "Authorization", "in": "header", "required": true @@ -130,6 +199,72 @@ var doc = `{ } } } + }, + "put": { + "consumes": [ + "multipart/form-data" + ], + "produces": [ + "application/json" + ], + "tags": [ + "employees" + ], + "summary": "更新员工", + "parameters": [ + { + "type": "string", + "description": "auth by /admin/login", + "name": "Authorization", + "in": "header", + "required": true + }, + { + "type": "integer", + "description": "ID", + "name": "id", + "in": "path", + "required": true + }, + { + "type": "string", + "description": "员工用户名", + "name": "username", + "in": "formData" + }, + { + "type": "string", + "description": "员工密码", + "name": "password", + "in": "formData" + }, + { + "type": "string", + "description": "员工部门", + "name": "department", + "in": "formData" + }, + { + "type": "string", + "description": "员工职位", + "name": "position", + "in": "formData" + }, + { + "type": "string", + "description": "员工状态", + "name": "state", + "in": "formData" + } + ], + "responses": { + "200": { + "description": "{\"code\": 200, \"data\":{}, \"msg\":\"ok\"}", + "schema": { + "type": "string" + } + } + } } } } diff --git a/docs/swagger.json b/docs/swagger.json index 39fe2a7..7b3fc45 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -39,14 +39,20 @@ }, "/admin/v1/employees": { "get": { + "consumes": [ + "application/json" + ], "produces": [ "application/json" ], + "tags": [ + "employees" + ], "summary": "获取员工列表", "parameters": [ { "type": "string", - "description": "auth by /auth", + "description": "auth by /admin/login", "name": "Authorization", "in": "header", "required": true @@ -78,18 +84,81 @@ } } } + }, + "post": { + "consumes": [ + "multipart/form-data" + ], + "produces": [ + "application/json" + ], + "tags": [ + "employees" + ], + "summary": "新增员工", + "parameters": [ + { + "type": "string", + "description": "auth by /admin/login", + "name": "Authorization", + "in": "header", + "required": true + }, + { + "type": "string", + "description": "员工用户名", + "name": "username", + "in": "formData", + "required": true + }, + { + "type": "string", + "description": "员工密码", + "name": "password", + "in": "formData", + "required": true + }, + { + "type": "string", + "description": "员工部门", + "name": "department", + "in": "formData", + "required": true + }, + { + "type": "string", + "description": "员工职位", + "name": "position", + "in": "formData", + "required": true + } + ], + "responses": { + "200": { + "description": "{\"code\": 200, \"data\":{}, \"msg\":\"ok\"}", + "schema": { + "type": "string" + } + } + } } }, "/admin/v1/employees/{id}": { "get": { + "consumes": [ + "application/json" + ], "produces": [ "application/json" ], + "tags": [ + "employees" + ], "summary": "获取单个员工", "parameters": [ { "type": "string", - "description": "auth by /auth", + "description": "auth by /admin/login", "name": "Authorization", "in": "header", "required": true @@ -110,6 +179,72 @@ } } } + }, + "put": { + "consumes": [ + "multipart/form-data" + ], + "produces": [ + "application/json" + ], + "tags": [ + "employees" + ], + "summary": "更新员工", + "parameters": [ + { + "type": "string", + "description": "auth by /admin/login", + "name": "Authorization", + "in": "header", + "required": true + }, + { + "type": "integer", + "description": "ID", + "name": "id", + "in": "path", + "required": true + }, + { + "type": "string", + "description": "员工用户名", + "name": "username", + "in": "formData" + }, + { + "type": "string", + "description": "员工密码", + "name": "password", + "in": "formData" + }, + { + "type": "string", + "description": "员工部门", + "name": "department", + "in": "formData" + }, + { + "type": "string", + "description": "员工职位", + "name": "position", + "in": "formData" + }, + { + "type": "string", + "description": "员工状态", + "name": "state", + "in": "formData" + } + ], + "responses": { + "200": { + "description": "{\"code\": 200, \"data\":{}, \"msg\":\"ok\"}", + "schema": { + "type": "string" + } + } + } } } } diff --git a/docs/swagger.yaml b/docs/swagger.yaml index a4b0e45..5ab2b00 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -25,8 +25,10 @@ paths: summary: 后台登录 /admin/v1/employees: get: + consumes: + - application/json parameters: - - description: auth by /auth + - description: auth by /admin/login in: header name: Authorization required: true @@ -51,10 +53,53 @@ paths: schema: type: string summary: 获取员工列表 + tags: + - employees + post: + consumes: + - multipart/form-data + parameters: + - description: auth by /admin/login + in: header + name: Authorization + required: true + type: string + - description: 员工用户名 + in: formData + name: username + required: true + type: string + - description: 员工密码 + in: formData + name: password + required: true + type: string + - description: 员工部门 + in: formData + name: department + required: true + type: string + - description: 员工职位 + in: formData + name: position + required: true + type: string + produces: + - application/json + responses: + "200": + description: '{"code": 200, "data":{}, "msg":"ok"}' + schema: + type: string + summary: 新增员工 + tags: + - employees /admin/v1/employees/{id}: get: + consumes: + - application/json parameters: - - description: auth by /auth + - description: auth by /admin/login in: header name: Authorization required: true @@ -72,4 +117,50 @@ paths: schema: type: string summary: 获取单个员工 + tags: + - employees + put: + consumes: + - multipart/form-data + parameters: + - description: auth by /admin/login + in: header + name: Authorization + required: true + type: string + - description: ID + in: path + name: id + required: true + type: integer + - description: 员工用户名 + in: formData + name: username + type: string + - description: 员工密码 + in: formData + name: password + type: string + - description: 员工部门 + in: formData + name: department + type: string + - description: 员工职位 + in: formData + name: position + type: string + - description: 员工状态 + in: formData + name: state + type: string + produces: + - application/json + responses: + "200": + description: '{"code": 200, "data":{}, "msg":"ok"}' + schema: + type: string + summary: 更新员工 + tags: + - employees swagger: "2.0" diff --git a/models/employee.go b/models/employee.go index 433f3dd..2e2f116 100644 --- a/models/employee.go +++ b/models/employee.go @@ -37,6 +37,15 @@ func ExistEmployeeByID(id int) bool { return false } +func ExistEmployeeByUsername(username string) bool { + var employee Employee + db.Select("username").Where("username = ?", username).First(&employee) + if employee.ID > 0 { + return true + } + return false +} + func GetEmployeeTotal(maps interface{}) (count int) { db.Model(&Employee{}).Where(maps).Count(&count) @@ -56,7 +65,6 @@ func GetEmployee(id int) (employee Employee) { func EditEmployee(id int, data interface{}) bool { db.Model(&Employee{}).Where("id = ?", id).Updates(data) - return true } @@ -66,11 +74,16 @@ func AddArticle(data map[string]interface{}) bool { Password: data["password"].(string), Department: data["department"].(string), Position: data["position"].(string), - State: data["state"].(int), }) return true } +func (employee *Employee) BeforeCreate(scope *gorm.Scope) error { + scope.SetColumn("CreateOn", time.Now().Unix()) + + return nil +} + func (employee *Employee) BeforeUpdate(scope *gorm.Scope) error { scope.SetColumn("ModifiedOn", time.Now().Unix()) diff --git a/routers/admin.go b/routers/admin.go index 1b1db8c..d31b396 100644 --- a/routers/admin.go +++ b/routers/admin.go @@ -18,10 +18,10 @@ func InitAdmin(r *gin.Engine) *gin.Engine { adminv1.GET("/employees", v1.GetEmployees) // 获取指定员工列表 adminv1.GET("/employees/:id", v1.GetEmployee) - //// 新建员工 - //adminv1.POST("/employees", v1.AddEmployee) - //// 更新指定员工 - //adminv1.PUT("/employees/:id", v1.EditArticle) + // 新建员工 + adminv1.POST("/employees", v1.AddEmployee) + // 更新指定员工 + adminv1.PUT("/employees/:id", v1.EditEmployee) } return r diff --git a/routers/admin/v1/employee.go b/routers/admin/v1/employee.go index bd18125..c1b3cbe 100644 --- a/routers/admin/v1/employee.go +++ b/routers/admin/v1/employee.go @@ -13,8 +13,11 @@ import ( ) // @Summary 获取单个员工 +// @Tags employees +// @Description +// @Accept json // @Produce json -// @Param Authorization header string true "auth by /auth" +// @Param Authorization header string true "auth by /admin/login" // @Param id path int true "ID" // @Success 200 {string} json "{"code": 200, "data": {}, "msg": "ok"}" // @Router /admin/v1/employees/{id} [GET] @@ -47,8 +50,11 @@ func GetEmployee(c *gin.Context) { } // @Summary 获取员工列表 +// @Tags employees +// @Description +// @Accept json // @Produce json -// @Param Authorization header string true "auth by /auth" +// @Param Authorization header string true "auth by /admin/login" // @Param department query string false "Department" // @Param position query string false "Position" // @Param state query int false "State" @@ -84,3 +90,71 @@ func GetEmployees(c *gin.Context) { }) } +// @Summary 新增员工 +// @Tags employees +// @Description +// @Accept mpfd +// @Produce json +// @Param Authorization header string true "auth by /admin/login" +// @Param username formData string true "员工用户名" +// @Param password formData string true "员工密码" +// @Param department formData string true "员工部门" +// @Param position formData string true "员工职位" +// @Success 200 {string} json "{"code": 200, "data":{}, "msg":"ok"}" +// @Router /admin/v1/employees [post] +func AddEmployee(c *gin.Context) { + username := c.PostForm("username") + password := c.PostForm("password") + department := c.PostForm("department") + position := c.PostForm("position") + + valid := validation.Validation{} + valid.Required(username, "username").Message("用户名不能为空") + valid.Required(password, "password").Message("密码不能为空") + valid.Required(department, "department").Message("部门不能为空") + valid.Required(position, "position").Message("职位不能为空") + + data := make(map[string]interface{}) + + code := e.InvalidParams + + if !valid.HasErrors() { + if !models.ExistEmployeeByUsername(username) { + data["username"] = username + data["password"] = password + data["department"] = department + data["position"] = position + code = e.SUCCESS + } else { + code = e.ErrorExistEmployee + } + } + + if code == 200 { + models.AddArticle(data) + } + + c.JSON(http.StatusOK, gin.H{ + "code" : code, + "msg" : e.GetMsg(code), + "data" : data, + }) +} + +// @Summary 更新员工 +// @Tags employees +// @Description +// @Accept mpfd +// @Produce json +// @Param Authorization header string true "auth by /admin/login" +// @Param id path int true "ID" +// @Param username formData string false "员工用户名" +// @Param password formData string false "员工密码" +// @Param department formData string false "员工部门" +// @Param position formData string false "员工职位" +// @Param state formData string false "员工状态" +// @Success 200 {string} json "{"code": 200, "data":{}, "msg":"ok"}" +// @Router /admin/v1/employees/{id} [put] +func EditEmployee(c *gin.Context) { + +} \ No newline at end of file