remove interfaces
This commit is contained in:
11
config.toml
11
config.toml
@@ -88,12 +88,12 @@ ShowLine = true
|
|||||||
LogInConsole = true
|
LogInConsole = true
|
||||||
|
|
||||||
[Database]
|
[Database]
|
||||||
Driver = "sqlite"
|
Driver = "mysql"
|
||||||
|
|
||||||
[Database.MySQL]
|
[Database.MySQL]
|
||||||
Host = "localhost"
|
Host = "10.47.119.226"
|
||||||
Port = 3306
|
Port = 3306
|
||||||
Database = "demos11"
|
Database = "atom"
|
||||||
Username = "root"
|
Username = "root"
|
||||||
Password = "root"
|
Password = "root"
|
||||||
Prefix = ""
|
Prefix = ""
|
||||||
@@ -106,8 +106,7 @@ Engine = "InnoDB"
|
|||||||
File = "sqlite.db"
|
File = "sqlite.db"
|
||||||
|
|
||||||
[Database.Redis]
|
[Database.Redis]
|
||||||
Host = "localhost"
|
Host = "10.47.119.226"
|
||||||
Port = 3306
|
Port = 6379
|
||||||
Database = 0
|
Database = 0
|
||||||
Username = ""
|
|
||||||
Password = ""
|
Password = ""
|
||||||
|
|||||||
@@ -10,11 +10,7 @@ import (
|
|||||||
"github.com/rogeecn/gen"
|
"github.com/rogeecn/gen"
|
||||||
)
|
)
|
||||||
|
|
||||||
type PermissionController interface {
|
type PermissionController struct {
|
||||||
Get(ctx *gin.Context) (string, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
type permissionControllerImpl struct {
|
|
||||||
jwt *jwt.JWT
|
jwt *jwt.JWT
|
||||||
rbac rbac.IRbac
|
rbac rbac.IRbac
|
||||||
}
|
}
|
||||||
@@ -22,11 +18,11 @@ type permissionControllerImpl struct {
|
|||||||
func NewPermissionController(
|
func NewPermissionController(
|
||||||
jwt *jwt.JWT,
|
jwt *jwt.JWT,
|
||||||
rbac rbac.IRbac,
|
rbac rbac.IRbac,
|
||||||
) PermissionController {
|
) *PermissionController {
|
||||||
return &permissionControllerImpl{rbac: rbac, jwt: jwt}
|
return &PermissionController{rbac: rbac, jwt: jwt}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *permissionControllerImpl) Get(ctx *gin.Context) (string, error) {
|
func (c *PermissionController) Get(ctx *gin.Context) (string, error) {
|
||||||
claimsCtx, exists := ctx.Get(jwt.CtxKey)
|
claimsCtx, exists := ctx.Get(jwt.CtxKey)
|
||||||
if !exists {
|
if !exists {
|
||||||
return "", gen.NewBusError(http.StatusBadRequest, http.StatusBadRequest, "Token 获取失败")
|
return "", gen.NewBusError(http.StatusBadRequest, http.StatusBadRequest, "Token 获取失败")
|
||||||
|
|||||||
@@ -10,45 +10,37 @@ import (
|
|||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
type RoleController interface {
|
type RoleController struct {
|
||||||
GetByFilter(*gin.Context, dto.RoleRequestFilter, request.PageFilter) (*response.PageResponse[*models.SysRole], error)
|
|
||||||
Tree(*gin.Context) ([]*dto.RoleTree, error)
|
|
||||||
Create(*gin.Context, dto.RoleRequestForm) error
|
|
||||||
Delete(*gin.Context, int) error
|
|
||||||
UpdateByID(*gin.Context, int, dto.RoleRequestForm) error
|
|
||||||
}
|
|
||||||
|
|
||||||
type roleControllerImpl struct {
|
|
||||||
roleSvc service.RoleService
|
roleSvc service.RoleService
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRoleController(
|
func NewRoleController(
|
||||||
roleSvc service.RoleService,
|
roleSvc service.RoleService,
|
||||||
) RoleController {
|
) *RoleController {
|
||||||
return &roleControllerImpl{
|
return &RoleController{
|
||||||
roleSvc: roleSvc,
|
roleSvc: roleSvc,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *roleControllerImpl) GetByFilter(
|
func (c *RoleController) GetByFilter(
|
||||||
ctx *gin.Context,
|
ctx *gin.Context,
|
||||||
filter dto.RoleRequestFilter,
|
filter dto.RoleRequestFilter,
|
||||||
page request.PageFilter,
|
page request.PageFilter,
|
||||||
) (*response.PageResponse[*models.SysRole], error) {
|
) (*response.PageResponse[*models.SysRole], error) {
|
||||||
return c.roleSvc.GetByFilter(ctx, filter, page)
|
return c.roleSvc.GetByFilter(ctx, filter, page)
|
||||||
}
|
}
|
||||||
func (c *roleControllerImpl) Tree(ctx *gin.Context) ([]*dto.RoleTree, error) {
|
func (c *RoleController) Tree(ctx *gin.Context) ([]*dto.RoleTree, error) {
|
||||||
return c.roleSvc.Tree(ctx)
|
return c.roleSvc.Tree(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *roleControllerImpl) Create(ctx *gin.Context, req dto.RoleRequestForm) error {
|
func (c *RoleController) Create(ctx *gin.Context, req dto.RoleRequestForm) error {
|
||||||
_, err := c.roleSvc.Create(ctx, req)
|
_, err := c.roleSvc.Create(ctx, req)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
func (c *roleControllerImpl) UpdateByID(ctx *gin.Context, id int, req dto.RoleRequestForm) error {
|
func (c *RoleController) UpdateByID(ctx *gin.Context, id int, req dto.RoleRequestForm) error {
|
||||||
_, err := c.roleSvc.UpdateByID(ctx, uint64(id), req)
|
_, err := c.roleSvc.UpdateByID(ctx, uint64(id), req)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
func (c *roleControllerImpl) Delete(ctx *gin.Context, id int) error {
|
func (c *RoleController) Delete(ctx *gin.Context, id int) error {
|
||||||
return c.roleSvc.DeleteByID(ctx, uint64(id))
|
return c.roleSvc.DeleteByID(ctx, uint64(id))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,11 +9,7 @@ import (
|
|||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
type UserController interface {
|
type UserController struct {
|
||||||
Login(*gin.Context, dto.LoginRequestForm) (*dto.LoginResponse, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
type userControllerImpl struct {
|
|
||||||
conf *config.Config
|
conf *config.Config
|
||||||
user service.UserService
|
user service.UserService
|
||||||
jwt *jwt.JWT
|
jwt *jwt.JWT
|
||||||
@@ -23,15 +19,15 @@ func NewUserController(
|
|||||||
conf *config.Config,
|
conf *config.Config,
|
||||||
user service.UserService,
|
user service.UserService,
|
||||||
jwt *jwt.JWT,
|
jwt *jwt.JWT,
|
||||||
) UserController {
|
) *UserController {
|
||||||
return &userControllerImpl{
|
return &UserController{
|
||||||
conf: conf,
|
conf: conf,
|
||||||
user: user,
|
user: user,
|
||||||
jwt: jwt,
|
jwt: jwt,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *userControllerImpl) Login(ctx *gin.Context, req dto.LoginRequestForm) (*dto.LoginResponse, error) {
|
func (c *UserController) Login(ctx *gin.Context, req dto.LoginRequestForm) (*dto.LoginResponse, error) {
|
||||||
user, err := c.user.AuthMatchPassword(ctx, &req)
|
user, err := c.user.AuthMatchPassword(ctx, &req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
@@ -8,25 +8,15 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
)
|
)
|
||||||
|
|
||||||
type RoleDao interface {
|
type RoleDao struct {
|
||||||
GetByFilter(context.Context, dto.RoleRequestFilter, request.PageFilter) ([]*models.SysRole, uint64, error)
|
|
||||||
FindByID(context.Context, uint64) (*models.SysRole, error)
|
|
||||||
Create(context.Context, *models.SysRole) (*models.SysRole, error)
|
|
||||||
UpdateByID(context.Context, *models.SysRole) (*models.SysRole, error)
|
|
||||||
DeleteByID(context.Context, uint64) error
|
|
||||||
DeletePermanentlyByID(context.Context, uint64) error
|
|
||||||
All(context.Context) ([]*models.SysRole, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
type roleDaoImpl struct {
|
|
||||||
query *query.Query
|
query *query.Query
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRoleDao(query *query.Query) RoleDao {
|
func NewRoleDao(query *query.Query) *RoleDao {
|
||||||
return &roleDaoImpl{query: query}
|
return &RoleDao{query: query}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dao *roleDaoImpl) GetByFilter(ctx context.Context, filter dto.RoleRequestFilter, page request.PageFilter) ([]*models.SysRole, uint64, error) {
|
func (dao *RoleDao) GetByFilter(ctx context.Context, filter dto.RoleRequestFilter, page request.PageFilter) ([]*models.SysRole, uint64, error) {
|
||||||
role := dao.query.SysRole
|
role := dao.query.SysRole
|
||||||
query := role.WithContext(ctx)
|
query := role.WithContext(ctx)
|
||||||
|
|
||||||
@@ -55,17 +45,17 @@ func (dao *roleDaoImpl) GetByFilter(ctx context.Context, filter dto.RoleRequestF
|
|||||||
return items, uint64(total), nil
|
return items, uint64(total), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dao *roleDaoImpl) All(ctx context.Context) ([]*models.SysRole, error) {
|
func (dao *RoleDao) All(ctx context.Context) ([]*models.SysRole, error) {
|
||||||
role := dao.query.SysRole
|
role := dao.query.SysRole
|
||||||
return role.WithContext(ctx).Find()
|
return role.WithContext(ctx).Find()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dao *roleDaoImpl) FindByID(ctx context.Context, id uint64) (*models.SysRole, error) {
|
func (dao *RoleDao) FindByID(ctx context.Context, id uint64) (*models.SysRole, error) {
|
||||||
role := dao.query.SysRole
|
role := dao.query.SysRole
|
||||||
return role.WithContext(ctx).Where(role.ID.Eq(id)).First()
|
return role.WithContext(ctx).Where(role.ID.Eq(id)).First()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dao *roleDaoImpl) Create(ctx context.Context, model *models.SysRole) (*models.SysRole, error) {
|
func (dao *RoleDao) Create(ctx context.Context, model *models.SysRole) (*models.SysRole, error) {
|
||||||
role := dao.query.SysRole
|
role := dao.query.SysRole
|
||||||
if err := role.WithContext(ctx).Create(model); err != nil {
|
if err := role.WithContext(ctx).Create(model); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -73,7 +63,7 @@ func (dao *roleDaoImpl) Create(ctx context.Context, model *models.SysRole) (*mod
|
|||||||
return model, nil
|
return model, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dao *roleDaoImpl) UpdateByID(ctx context.Context, model *models.SysRole) (*models.SysRole, error) {
|
func (dao *RoleDao) UpdateByID(ctx context.Context, model *models.SysRole) (*models.SysRole, error) {
|
||||||
role := dao.query.SysRole
|
role := dao.query.SysRole
|
||||||
_, err := role.WithContext(ctx).Where(role.ID.Eq(model.ID)).Updates(model)
|
_, err := role.WithContext(ctx).Where(role.ID.Eq(model.ID)).Updates(model)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -82,13 +72,13 @@ func (dao *roleDaoImpl) UpdateByID(ctx context.Context, model *models.SysRole) (
|
|||||||
return model, nil
|
return model, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dao *roleDaoImpl) DeleteByID(ctx context.Context, id uint64) error {
|
func (dao *RoleDao) DeleteByID(ctx context.Context, id uint64) error {
|
||||||
role := dao.query.SysRole
|
role := dao.query.SysRole
|
||||||
_, err := role.WithContext(ctx).Where(role.ID.Eq(id)).Delete()
|
_, err := role.WithContext(ctx).Where(role.ID.Eq(id)).Delete()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dao *roleDaoImpl) DeletePermanentlyByID(ctx context.Context, id uint64) error {
|
func (dao *RoleDao) DeletePermanentlyByID(ctx context.Context, id uint64) error {
|
||||||
role := dao.query.SysRole
|
role := dao.query.SysRole
|
||||||
_, err := role.WithContext(ctx).Unscoped().Where(role.ID.Eq(id)).Delete()
|
_, err := role.WithContext(ctx).Unscoped().Where(role.ID.Eq(id)).Delete()
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ import (
|
|||||||
type RoleInjectParams struct {
|
type RoleInjectParams struct {
|
||||||
dig.In
|
dig.In
|
||||||
DB *gorm.DB
|
DB *gorm.DB
|
||||||
Dao RoleDao
|
Dao *RoleDao
|
||||||
Query *query.Query
|
Query *query.Query
|
||||||
Faker *gofakeit.Faker
|
Faker *gofakeit.Faker
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,24 +6,20 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
)
|
)
|
||||||
|
|
||||||
type UserDao interface {
|
type UserDao struct {
|
||||||
Create(context.Context, *models.User) (*models.User, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
type userDaoImpl struct {
|
|
||||||
query *query.Query
|
query *query.Query
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewUserDao(query *query.Query) UserDao {
|
func NewUserDao(query *query.Query) *UserDao {
|
||||||
return &userDaoImpl{query: query}
|
return &UserDao{query: query}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dao *userDaoImpl) FindByID(ctx context.Context, id uint64) (*models.User, error) {
|
func (dao *UserDao) FindByID(ctx context.Context, id uint64) (*models.User, error) {
|
||||||
user := dao.query.User
|
user := dao.query.User
|
||||||
return user.WithContext(ctx).Where(user.ID.Eq(id)).First()
|
return user.WithContext(ctx).Where(user.ID.Eq(id)).First()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dao *userDaoImpl) Create(ctx context.Context, model *models.User) (*models.User, error) {
|
func (dao *UserDao) Create(ctx context.Context, model *models.User) (*models.User, error) {
|
||||||
user := dao.query.User
|
user := dao.query.User
|
||||||
if err := user.WithContext(ctx).Create(model); err != nil {
|
if err := user.WithContext(ctx).Create(model); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
@@ -6,28 +6,21 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
)
|
)
|
||||||
|
|
||||||
type UserRoleDao interface {
|
type UserRoleDao struct {
|
||||||
Exists(context.Context, int) bool
|
|
||||||
Create(context.Context, int, int) error
|
|
||||||
Update(context.Context, int, int) error
|
|
||||||
Delete(context.Context, int, int) error
|
|
||||||
}
|
|
||||||
|
|
||||||
type userRoleDaoImpl struct {
|
|
||||||
query *query.Query
|
query *query.Query
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewUserRoleDao(query *query.Query) UserRoleDao {
|
func NewUserRoleDao(query *query.Query) *UserRoleDao {
|
||||||
return &userRoleDaoImpl{query: query}
|
return &UserRoleDao{query: query}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dao *userRoleDaoImpl) Exists(ctx context.Context, userID int) bool {
|
func (dao *UserRoleDao) Exists(ctx context.Context, userID int) bool {
|
||||||
userRole := dao.query.UserRole
|
userRole := dao.query.UserRole
|
||||||
count, _ := userRole.WithContext(ctx).Where(userRole.UserID.Eq(uint64(userID))).Count()
|
count, _ := userRole.WithContext(ctx).Where(userRole.UserID.Eq(uint64(userID))).Count()
|
||||||
return count > 0
|
return count > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dao *userRoleDaoImpl) Create(ctx context.Context, userID, roleID int) error {
|
func (dao *UserRoleDao) Create(ctx context.Context, userID, roleID int) error {
|
||||||
userRole := dao.query.UserRole
|
userRole := dao.query.UserRole
|
||||||
return userRole.WithContext(ctx).Create(&models.UserRole{
|
return userRole.WithContext(ctx).Create(&models.UserRole{
|
||||||
UserID: uint64(userID),
|
UserID: uint64(userID),
|
||||||
@@ -35,13 +28,13 @@ func (dao *userRoleDaoImpl) Create(ctx context.Context, userID, roleID int) erro
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dao *userRoleDaoImpl) Update(ctx context.Context, userID, roleID int) error {
|
func (dao *UserRoleDao) Update(ctx context.Context, userID, roleID int) error {
|
||||||
userRole := dao.query.UserRole
|
userRole := dao.query.UserRole
|
||||||
_, err := userRole.WithContext(ctx).Where(userRole.UserID.Eq(uint64(userID))).Update(userRole.RoleID, roleID)
|
_, err := userRole.WithContext(ctx).Where(userRole.UserID.Eq(uint64(userID))).Update(userRole.RoleID, roleID)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dao *userRoleDaoImpl) Delete(ctx context.Context, userID, roleID int) error {
|
func (dao *UserRoleDao) Delete(ctx context.Context, userID, roleID int) error {
|
||||||
userRole := dao.query.UserRole
|
userRole := dao.query.UserRole
|
||||||
_, err := userRole.WithContext(ctx).
|
_, err := userRole.WithContext(ctx).
|
||||||
Where(userRole.UserID.Eq(uint64(userID))).
|
Where(userRole.UserID.Eq(uint64(userID))).
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ type UserRoleInjectParams struct {
|
|||||||
dig.In
|
dig.In
|
||||||
|
|
||||||
DB *gorm.DB
|
DB *gorm.DB
|
||||||
Dao UserRoleDao
|
Dao *UserRoleDao
|
||||||
Query *query.Query
|
Query *query.Query
|
||||||
Faker *gofakeit.Faker
|
Faker *gofakeit.Faker
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ type UserInjectParams struct {
|
|||||||
dig.In
|
dig.In
|
||||||
|
|
||||||
DB *gorm.DB
|
DB *gorm.DB
|
||||||
Dao UserDao
|
Dao *UserDao
|
||||||
Query *query.Query
|
Query *query.Query
|
||||||
Faker *gofakeit.Faker
|
Faker *gofakeit.Faker
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,15 +10,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
)
|
)
|
||||||
|
|
||||||
type RoleService interface {
|
type RoleService struct {
|
||||||
GetByFilter(context.Context, dto.RoleRequestFilter, request.PageFilter) (*response.PageResponse[*models.SysRole], error)
|
|
||||||
Create(context.Context, dto.RoleRequestForm) (*models.SysRole, error)
|
|
||||||
Tree(context.Context) ([]*dto.RoleTree, error)
|
|
||||||
UpdateByID(context.Context, uint64, dto.RoleRequestForm) (*models.SysRole, error)
|
|
||||||
DeleteByID(context.Context, uint64) error
|
|
||||||
}
|
|
||||||
|
|
||||||
type roleService struct {
|
|
||||||
dao dao.RoleDao
|
dao dao.RoleDao
|
||||||
uuid *uuid.Generator
|
uuid *uuid.Generator
|
||||||
}
|
}
|
||||||
@@ -26,14 +18,14 @@ type roleService struct {
|
|||||||
func NewRoleService(
|
func NewRoleService(
|
||||||
dao dao.RoleDao,
|
dao dao.RoleDao,
|
||||||
uuid *uuid.Generator,
|
uuid *uuid.Generator,
|
||||||
) RoleService {
|
) *RoleService {
|
||||||
return &roleService{
|
return &RoleService{
|
||||||
dao: dao,
|
dao: dao,
|
||||||
uuid: uuid,
|
uuid: uuid,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (svc *roleService) GetByFilter(
|
func (svc *RoleService) GetByFilter(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
filter dto.RoleRequestFilter,
|
filter dto.RoleRequestFilter,
|
||||||
page request.PageFilter,
|
page request.PageFilter,
|
||||||
@@ -49,7 +41,7 @@ func (svc *roleService) GetByFilter(
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (svc *roleService) treeMaker(ctx context.Context, models []*models.SysRole, pid uint64) []*dto.RoleTree {
|
func (svc *RoleService) treeMaker(ctx context.Context, models []*models.SysRole, pid uint64) []*dto.RoleTree {
|
||||||
items := []*dto.RoleTree{}
|
items := []*dto.RoleTree{}
|
||||||
for _, model := range models {
|
for _, model := range models {
|
||||||
if model.ParentID == pid {
|
if model.ParentID == pid {
|
||||||
@@ -66,7 +58,7 @@ func (svc *roleService) treeMaker(ctx context.Context, models []*models.SysRole,
|
|||||||
return items
|
return items
|
||||||
}
|
}
|
||||||
|
|
||||||
func (svc *roleService) Tree(ctx context.Context) ([]*dto.RoleTree, error) {
|
func (svc *RoleService) Tree(ctx context.Context) ([]*dto.RoleTree, error) {
|
||||||
models, err := svc.dao.All(ctx)
|
models, err := svc.dao.All(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -75,7 +67,7 @@ func (svc *roleService) Tree(ctx context.Context) ([]*dto.RoleTree, error) {
|
|||||||
return svc.treeMaker(ctx, models, 0), nil
|
return svc.treeMaker(ctx, models, 0), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (svc *roleService) Create(ctx context.Context, req dto.RoleRequestForm) (*models.SysRole, error) {
|
func (svc *RoleService) Create(ctx context.Context, req dto.RoleRequestForm) (*models.SysRole, error) {
|
||||||
model := models.SysRole{
|
model := models.SysRole{
|
||||||
UUID: svc.uuid.MustGenerate(),
|
UUID: svc.uuid.MustGenerate(),
|
||||||
Name: req.Name,
|
Name: req.Name,
|
||||||
@@ -84,7 +76,7 @@ func (svc *roleService) Create(ctx context.Context, req dto.RoleRequestForm) (*m
|
|||||||
}
|
}
|
||||||
return svc.dao.Create(ctx, &model)
|
return svc.dao.Create(ctx, &model)
|
||||||
}
|
}
|
||||||
func (svc *roleService) UpdateByID(ctx context.Context, id uint64, req dto.RoleRequestForm) (*models.SysRole, error) {
|
func (svc *RoleService) UpdateByID(ctx context.Context, id uint64, req dto.RoleRequestForm) (*models.SysRole, error) {
|
||||||
model, err := svc.dao.FindByID(ctx, id)
|
model, err := svc.dao.FindByID(ctx, id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -97,6 +89,6 @@ func (svc *roleService) UpdateByID(ctx context.Context, id uint64, req dto.RoleR
|
|||||||
return svc.dao.UpdateByID(ctx, model)
|
return svc.dao.UpdateByID(ctx, model)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (svc *roleService) DeleteByID(ctx context.Context, id uint64) error {
|
func (svc *RoleService) DeleteByID(ctx context.Context, id uint64) error {
|
||||||
return svc.dao.DeleteByID(ctx, id)
|
return svc.dao.DeleteByID(ctx, id)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,13 +8,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
)
|
)
|
||||||
|
|
||||||
type UserService interface {
|
type UserService struct {
|
||||||
AttachRole(context.Context, int, int) error
|
|
||||||
AuthMatchPassword(context.Context, *dto.LoginRequestForm) (*models.User, error)
|
|
||||||
GenerateJWTTokenFromUser(context.Context, *models.User) (string, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
type userService struct {
|
|
||||||
userRoleDao dao.UserRoleDao
|
userRoleDao dao.UserRoleDao
|
||||||
userDao dao.UserDao
|
userDao dao.UserDao
|
||||||
jwt *jwt.JWT
|
jwt *jwt.JWT
|
||||||
@@ -24,28 +18,28 @@ func NewUserService(
|
|||||||
userRoleDao dao.UserRoleDao,
|
userRoleDao dao.UserRoleDao,
|
||||||
userDao dao.UserDao,
|
userDao dao.UserDao,
|
||||||
jwt *jwt.JWT,
|
jwt *jwt.JWT,
|
||||||
) UserService {
|
) *UserService {
|
||||||
return &userService{
|
return &UserService{
|
||||||
userRoleDao: userRoleDao,
|
userRoleDao: userRoleDao,
|
||||||
userDao: userDao,
|
userDao: userDao,
|
||||||
jwt: jwt,
|
jwt: jwt,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (svc *userService) AttachRole(ctx context.Context, userID, roleID int) error {
|
func (svc *UserService) AttachRole(ctx context.Context, userID, roleID int) error {
|
||||||
if svc.userRoleDao.Exists(ctx, userID) {
|
if svc.userRoleDao.Exists(ctx, userID) {
|
||||||
return svc.userRoleDao.Update(ctx, userID, roleID)
|
return svc.userRoleDao.Update(ctx, userID, roleID)
|
||||||
}
|
}
|
||||||
return svc.userRoleDao.Create(ctx, userID, roleID)
|
return svc.userRoleDao.Create(ctx, userID, roleID)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (svc *userService) DetachRole(ctx context.Context, userID, roleID int) error {
|
func (svc *UserService) DetachRole(ctx context.Context, userID, roleID int) error {
|
||||||
if !svc.userRoleDao.Exists(ctx, userID) {
|
if !svc.userRoleDao.Exists(ctx, userID) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return svc.userRoleDao.Delete(ctx, userID, roleID)
|
return svc.userRoleDao.Delete(ctx, userID, roleID)
|
||||||
}
|
}
|
||||||
func (svc *userService) AuthMatchPassword(ctx context.Context, req *dto.LoginRequestForm) (*models.User, error) {
|
func (svc *UserService) AuthMatchPassword(ctx context.Context, req *dto.LoginRequestForm) (*models.User, error) {
|
||||||
return &models.User{
|
return &models.User{
|
||||||
ID: 10,
|
ID: 10,
|
||||||
UUID: "1",
|
UUID: "1",
|
||||||
@@ -57,7 +51,7 @@ func (svc *userService) AuthMatchPassword(ctx context.Context, req *dto.LoginReq
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (svc *userService) GenerateJWTTokenFromUser(ctx context.Context, user *models.User) (string, error) {
|
func (svc *UserService) GenerateJWTTokenFromUser(ctx context.Context, user *models.User) (string, error) {
|
||||||
return svc.jwt.CreateToken(svc.jwt.CreateClaims(jwt.BaseClaims{
|
return svc.jwt.CreateToken(svc.jwt.CreateClaims(jwt.BaseClaims{
|
||||||
UID: user.ID,
|
UID: user.ID,
|
||||||
Role: user.RoleID,
|
Role: user.RoleID,
|
||||||
|
|||||||
@@ -7,11 +7,7 @@ import (
|
|||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
type CaptchaController interface {
|
type CaptchaController struct {
|
||||||
Show(*gin.Context) (*captcha.CaptchaResponse, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
type captchaControllerImpl struct {
|
|
||||||
conf *config.Config
|
conf *config.Config
|
||||||
captcha *captcha.Captcha
|
captcha *captcha.Captcha
|
||||||
}
|
}
|
||||||
@@ -19,13 +15,13 @@ type captchaControllerImpl struct {
|
|||||||
func NewCaptchaController(
|
func NewCaptchaController(
|
||||||
conf *config.Config,
|
conf *config.Config,
|
||||||
captcha *captcha.Captcha,
|
captcha *captcha.Captcha,
|
||||||
) CaptchaController {
|
) *CaptchaController {
|
||||||
return &captchaControllerImpl{
|
return &CaptchaController{
|
||||||
conf: conf,
|
conf: conf,
|
||||||
captcha: captcha,
|
captcha: captcha,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *captchaControllerImpl) Show(ctx *gin.Context) (*captcha.CaptchaResponse, error) {
|
func (c *CaptchaController) Show(ctx *gin.Context) (*captcha.CaptchaResponse, error) {
|
||||||
return c.captcha.Generate()
|
return c.captcha.Generate()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,20 +8,16 @@ import (
|
|||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Dao interface {
|
type Dao struct {
|
||||||
Release(context.Context, int, string) error
|
|
||||||
}
|
|
||||||
|
|
||||||
type DaoImpl struct {
|
|
||||||
Conf *config.Config
|
Conf *config.Config
|
||||||
DB *gorm.DB
|
DB *gorm.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDao(DB *gorm.DB) Dao {
|
func NewDao(DB *gorm.DB) *Dao {
|
||||||
return &DaoImpl{DB: DB}
|
return &Dao{DB: DB}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *DaoImpl) Release(ctx context.Context, a int, b string) error {
|
func (c *Dao) Release(ctx context.Context, a int, b string) error {
|
||||||
if a == 20 {
|
if a == 20 {
|
||||||
return errors.New("A cant't be 20 ")
|
return errors.New("A cant't be 20 ")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ func TestDaoImpl_Release(t *testing.T) {
|
|||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
c := &DaoImpl{
|
c := &Dao{
|
||||||
Conf: tt.fields.Conf,
|
Conf: tt.fields.Conf,
|
||||||
DB: tt.fields.DB,
|
DB: tt.fields.DB,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,13 +4,10 @@ import (
|
|||||||
"atom/modules/system/dao"
|
"atom/modules/system/dao"
|
||||||
)
|
)
|
||||||
|
|
||||||
type SystemService interface {
|
type SystemService struct {
|
||||||
}
|
|
||||||
|
|
||||||
type systemService struct {
|
|
||||||
dao dao.Dao
|
dao dao.Dao
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSystemService(dao dao.Dao) SystemService {
|
func NewSystemService(dao dao.Dao) *SystemService {
|
||||||
return &systemService{dao: dao}
|
return &SystemService{dao: dao}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user