164 lines
3.8 KiB
Go
164 lines
3.8 KiB
Go
package admin
|
|
|
|
import (
|
|
"context"
|
|
"server/internal/db"
|
|
"server/internal/db/sqlc"
|
|
"server/internal/model/common"
|
|
"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 ApiService struct {
|
|
store *db.Store
|
|
cache *cache.Caches
|
|
}
|
|
|
|
func NewApiService(store *db.Store, cache *cache.Caches) *ApiService {
|
|
return &ApiService{
|
|
store: store,
|
|
cache: cache,
|
|
}
|
|
}
|
|
|
|
func (s *ApiService) List(ctx context.Context, p request.SearchApiParams) (*common.PageResult[sqlc.SysApi], error) {
|
|
params := sqlc.ListApisParams{
|
|
Limit: p.PageSize,
|
|
Offset: (p.Page - 1) * p.PageSize,
|
|
Name: p.Name,
|
|
GroupName: p.GroupName,
|
|
Method: p.Method,
|
|
}
|
|
|
|
total, err := s.store.CountApis(ctx, sqlc.CountApisParams{
|
|
Name: p.Name,
|
|
GroupName: p.GroupName,
|
|
Method: p.Method,
|
|
})
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
list, err := s.store.ListApis(ctx, params)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &common.PageResult[sqlc.SysApi]{
|
|
List: list,
|
|
Total: total,
|
|
}, nil
|
|
}
|
|
|
|
func (s *ApiService) ListAll(ctx context.Context) ([]sqlc.SysApi, error) {
|
|
return s.store.ListAllApis(ctx)
|
|
}
|
|
|
|
func (s *ApiService) ListApiGroups(ctx context.Context) ([]string, error) {
|
|
return s.store.ListApiGroups(ctx)
|
|
}
|
|
|
|
func (s *ApiService) Create(ctx context.Context, req request.CreateApiRequest) error {
|
|
|
|
return s.store.WithTx(ctx, func(q *sqlc.Queries) error {
|
|
api := sqlc.CreateApiParams{
|
|
Name: req.Name,
|
|
GroupName: req.GroupName,
|
|
Method: req.Method,
|
|
Path: req.Path,
|
|
Sort: req.Sort,
|
|
}
|
|
|
|
// 创建权限
|
|
permissionId, err := q.CreatePermission(ctx, sqlc.CreatePermissionParams{
|
|
Type: int16(enum.PermissionTypeApi),
|
|
})
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// 创建api
|
|
apiId, err := q.CreateApi(ctx, api)
|
|
if err != nil {
|
|
return dberr.MapUniqueViolation(err, dberr.SysApisMethodPathKey, errs.ErrApiMethodPathAlreadyExists)
|
|
}
|
|
|
|
// 关联权限
|
|
if err = q.CreateApiPermission(ctx, sqlc.CreateApiPermissionParams{
|
|
ApiID: apiId,
|
|
PermissionID: permissionId,
|
|
}); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
}
|
|
|
|
func (s *ApiService) Update(ctx context.Context, id int32, req request.UpdateApiRequest) error {
|
|
api := sqlc.UpdateApiParams{
|
|
ID: id,
|
|
Name: req.Name,
|
|
GroupName: req.GroupName,
|
|
Method: req.Method,
|
|
Path: req.Path,
|
|
Sort: req.Sort,
|
|
}
|
|
|
|
rows, err := s.store.UpdateApi(ctx, api)
|
|
if err = dberr.MapRowsAffected(rows, err, errs.ErrApiNotFound); err != nil {
|
|
return dberr.MapUniqueViolation(err, dberr.SysApisMethodPathKey, errs.ErrApiMethodPathAlreadyExists)
|
|
}
|
|
|
|
// 如果更新成功 则清理缓存
|
|
_ = s.cache.DelByPrefix(ctx, cachekey.UserApiPermissionsPattern)
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *ApiService) Delete(ctx context.Context, id int32) error {
|
|
err := s.store.WithTx(ctx, func(q *sqlc.Queries) error {
|
|
// 1. 找出权限id
|
|
permissionID, err := q.GetPermissionIDBySysApiID(ctx, id)
|
|
if err != nil {
|
|
return dberr.MapNoRows(err, errs.ErrApiNotFound)
|
|
}
|
|
// 2. 根据权限id,删除角色权限关联数据
|
|
if err = q.DeleteRolePermissionByPermissionID(ctx, permissionID); err != nil {
|
|
return err
|
|
}
|
|
// 3. 根据api id 删除sys_api_permission关联表数据
|
|
if err = q.DeleteApiPermission(ctx, id); err != nil {
|
|
return err
|
|
}
|
|
// 4. 根据权限id,删除权限表权限数据
|
|
if err = q.DeletePermission(ctx, permissionID); err != nil {
|
|
return err
|
|
}
|
|
// 5. 删除api
|
|
rows, err := q.DeleteApi(ctx, id)
|
|
if err = dberr.MapRowsAffected(rows, err, errs.ErrApiNotFound); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
})
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// 清理缓存 事务执行成功后才清理
|
|
_ = s.cache.DelByPrefix(ctx, cachekey.UserApiPermissionsPattern)
|
|
|
|
return nil
|
|
}
|