本节内容,我们正式实现一个添加用户的接口,实现我们整个项目接口请求的层级。
接口代码层级
在根目录下,新创建 ./repositories 和 ./models 目录,接口请求的数据逻辑顺序如下:
route -> api -> services -> repositories -> models
接口实现
创建 ./models/user.go:
package models type User struct { Id int64 `json:"id" gorm:"primary_key;column:id"` Username string `json:"username" gorm:"column:username"` Password string `json:"password" gorm:"column:password"` Email string `json:"email" gorm:"column:email"` UpdateAt int64 `json:"update_at" gorm:"column:update_at"` CreateAt int64 `json:"create_at" gorm:"column:create_at"` } func (u User) DatabaseName() string { return "fusheng_admin" } func (u User) TableName() string { return "user" } func (u User) PrimaryKey() string { return "id" }
创建 ./repositories/user.go:
package repositories import ( "fusheng-admin/library/database" "fusheng-admin/models" "github.com/gin-gonic/gin" "time" ) type UserRepository struct { database.DB TableName string } func NewUserRepository(ctx *gin.Context) *UserRepository { return &UserRepository { database.NewDb(ctx, models.User{}.DatabaseName()), models.User{}.TableName(), } } func (r *UserRepository) Get(id int64) *models.User { ret := &models.User{} if err := r.GetConn().First(ret, "id = ?", id).Error; err != nil { return nil } return ret } func (r *UserRepository) Insert(u *models.User) (int64, error) { currTime := time.Now().Unix() u.UpdateAt = currTime u.CreateAt = currTime err := r.GetConn().Create(u).Error if err != nil { return 0, nil } return u.Id, err } func (r *UserRepository) Update(id int64, columns map[string]interface{}) error { columns["update_at"] = time.Now().Unix() return r.GetConn().Model(&models.User{}).Where("id = ?", id).Updates(columns).Error } func (r *UserRepository) Delete(id int64) error { return r.GetConn().Model(&models.User{}).Delete("id = ?", id).Error }
创建./services/user.go:
package services import ( "fusheng-admin/models" "fusheng-admin/repositories" "github.com/gin-gonic/gin" ) type UserService struct { userRepository *repositories.UserRepository } func NewUserService(ctx *gin.Context) *UserService { return &UserService{ userRepository: repositories.NewUserRepository(ctx), } } func (s *UserService) Get(id int64) *models.User { return s.userRepository.Get(id) } func (s *UserService) Insert(username, password, email string) (int64, error) { return s.userRepository.Insert(&models.User{ Username: username, Password: password, Email: email, }) } func (s *UserService) Update(id int64, columns map[string]interface{}) error { return s.userRepository.Update(id, columns) } func (s *UserService) Delete(id int64) error { return s.userRepository.Delete(id) }
增加路由,修改 ./router/router.go
package router import ( "fusheng-admin/api/v1" "github.com/gin-gonic/gin" ) func Http(router *gin.Engine) { apiRouter := router.Group("/api/v1/") { apiRouter.POST("/useradd", v1.UserAdd) } }
接口测试
curl -H "Content-Type: application/json;" -X POST "http://127.0.0.1:8088/api/v1/useradd" -d '{"username": "fushengwushi", "password":"123456", "email":"fushengwushi@126.com"}'
至此,一个添加用户的接口就实现了 ~