157 lines
3.8 KiB
Go
157 lines
3.8 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"server/internal/db"
|
|
"server/internal/db/sqlc"
|
|
"server/internal/model/enum"
|
|
"server/internal/model/request"
|
|
"server/internal/pkg/cache"
|
|
"server/internal/pkg/cache/cachekey"
|
|
"server/internal/pkg/dberr"
|
|
"server/internal/pkg/errs"
|
|
)
|
|
|
|
type SysApiService struct {
|
|
store *db.Store
|
|
cache *cache.Caches
|
|
}
|
|
|
|
func NewSysApiService(store *db.Store, cache *cache.Caches) *SysApiService {
|
|
return &SysApiService{
|
|
store: store,
|
|
cache: cache,
|
|
}
|
|
}
|
|
|
|
func (s *SysApiService) ListPage(ctx context.Context, p request.SearchSysApiParams) ([]sqlc.SysApi, int64, error) {
|
|
params := sqlc.GetSysApisParams{
|
|
Limit: p.PageSize,
|
|
Offset: (p.Page - 1) * p.PageSize,
|
|
GroupName: p.GroupName,
|
|
Method: p.Method,
|
|
}
|
|
|
|
total, err := s.store.CountSysApis(ctx, sqlc.CountSysApisParams{
|
|
GroupName: p.GroupName,
|
|
Method: p.Method,
|
|
})
|
|
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
list, err := s.store.GetSysApis(ctx, params)
|
|
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
return list, total, nil
|
|
}
|
|
|
|
func (s *SysApiService) GetAllSysApis(ctx context.Context) ([]sqlc.SysApi, error) {
|
|
return s.store.GetAllSysApis(ctx)
|
|
}
|
|
|
|
func (s *SysApiService) GetApiGroupNames(ctx context.Context) ([]string, error) {
|
|
return s.store.GetSysApiGroupNames(ctx)
|
|
}
|
|
|
|
func (s *SysApiService) Create(ctx context.Context, req request.CreateSysApiRequest) error {
|
|
return s.store.WithTx(ctx, func(q *sqlc.Queries) error {
|
|
api := sqlc.CreateSysApiParams{
|
|
Name: req.Name,
|
|
GroupName: req.GroupName,
|
|
Method: req.Method,
|
|
Path: req.Path,
|
|
Sort: req.Sort,
|
|
}
|
|
|
|
// 创建权限
|
|
permissionId, err := q.CreateSysPermission(ctx, sqlc.CreateSysPermissionParams{
|
|
Type: int16(enum.PermissionTypeApi),
|
|
})
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// 创建api
|
|
apiId, err := q.CreateSysApi(ctx, api)
|
|
if err != nil {
|
|
return dberr.MapUniqueViolation(err, dberr.SysApisMethodPathKey, errs.ErrSysApiMethodPathAlreadyExists)
|
|
}
|
|
|
|
// 关联权限
|
|
if err = q.CreateSysApiPermission(ctx, sqlc.CreateSysApiPermissionParams{
|
|
ApiID: apiId,
|
|
PermissionID: permissionId,
|
|
}); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
}
|
|
|
|
func (s *SysApiService) Update(ctx context.Context, id int32, req request.UpdateSysApiRequest) error {
|
|
api := sqlc.UpdateSysApiParams{
|
|
ID: id,
|
|
Name: req.Name,
|
|
GroupName: req.GroupName,
|
|
Method: req.Method,
|
|
Path: req.Path,
|
|
Sort: req.Sort,
|
|
}
|
|
|
|
rows, err := s.store.UpdateSysApi(ctx, api)
|
|
if err = dberr.MapRowsAffected(rows, err, errs.ErrSysApiNotFound); err != nil {
|
|
return dberr.MapUniqueViolation(err, dberr.SysApisMethodPathKey, errs.ErrSysApiMethodPathAlreadyExists)
|
|
}
|
|
|
|
// 如果更新成功 则清理缓存
|
|
_ = s.cache.DelByPrefix(ctx, cachekey.UserApiPermissionsPattern)
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *SysApiService) Delete(ctx context.Context, id int32) error {
|
|
err := s.store.WithTx(ctx, func(q *sqlc.Queries) error {
|
|
// 1. 找出权限id
|
|
permissionID, err := q.GetSysPermissionIDBySysApiID(ctx, id)
|
|
if err != nil {
|
|
return dberr.MapNoRows(err, errs.ErrSysApiNotFound)
|
|
}
|
|
// 2. 根据权限id,删除角色权限关联数据
|
|
if err = q.DeleteSysRolePermissionByPermissionID(ctx, permissionID); err != nil {
|
|
return err
|
|
}
|
|
// 3. 根据api id 删除sys_api_permission关联表数据
|
|
if err = q.DeleteSysApiPermission(ctx, id); err != nil {
|
|
return err
|
|
}
|
|
// 4. 根据权限id,删除权限表权限数据
|
|
if err = q.DeleteSysPermission(ctx, permissionID); err != nil {
|
|
return err
|
|
}
|
|
// 5. 删除api
|
|
rows, err := q.DeleteSysApi(ctx, id)
|
|
if err = dberr.MapRowsAffected(rows, err, errs.ErrSysApiNotFound); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
})
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// 清理缓存 事务执行成功后才清理
|
|
_ = s.cache.DelByPrefix(ctx, cachekey.UserApiPermissionsPattern)
|
|
|
|
return nil
|
|
}
|