feat: release v1.0.0
This commit is contained in:
@@ -2,41 +2,46 @@ package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
db "server/internal/db/sqlc"
|
||||
"server/internal/model/common"
|
||||
"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"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
type SysApiService struct {
|
||||
queries *db.Queries
|
||||
pool *pgxpool.Pool
|
||||
cache *cache.Caches
|
||||
store *db.Store
|
||||
cache *cache.Caches
|
||||
}
|
||||
|
||||
func NewSysApiService(queries *db.Queries, pool *pgxpool.Pool, cache *cache.Caches) *SysApiService {
|
||||
return &SysApiService{queries: queries, pool: pool, cache: cache}
|
||||
func NewSysApiService(store *db.Store, cache *cache.Caches) *SysApiService {
|
||||
return &SysApiService{
|
||||
store: store,
|
||||
cache: cache,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *SysApiService) ListPage(ctx context.Context, p *common.Pagination) ([]db.SysApi, int64, error) {
|
||||
params := db.GetSysApisParams{
|
||||
Limit: p.PageSize,
|
||||
Offset: (p.Page - 1) * p.PageSize,
|
||||
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.queries.CountSysApis(ctx)
|
||||
total, err := s.store.CountSysApis(ctx, sqlc.CountSysApisParams{
|
||||
GroupName: p.GroupName,
|
||||
Method: p.Method,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
list, err := s.queries.GetSysApis(ctx, params)
|
||||
list, err := s.store.GetSysApis(ctx, params)
|
||||
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
@@ -45,69 +50,54 @@ func (s *SysApiService) ListPage(ctx context.Context, p *common.Pagination) ([]d
|
||||
return list, total, nil
|
||||
}
|
||||
|
||||
func (s *SysApiService) GetAllSysApis(ctx context.Context) ([]db.SysApi, error) {
|
||||
return s.queries.GetAllSysApis(ctx)
|
||||
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.queries.GetSysApiGroupNames(ctx)
|
||||
return s.store.GetSysApiGroupNames(ctx)
|
||||
}
|
||||
|
||||
func (s *SysApiService) Create(ctx context.Context, req request.CreateSysApiRequest) error {
|
||||
tx, err := s.pool.BeginTx(ctx, pgx.TxOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
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,
|
||||
}
|
||||
|
||||
defer func(ctx context.Context) {
|
||||
_ = tx.Rollback(ctx)
|
||||
}(ctx)
|
||||
// 创建权限
|
||||
permissionId, err := q.CreateSysPermission(ctx, sqlc.CreateSysPermissionParams{
|
||||
Type: int16(enum.PermissionTypeApi),
|
||||
})
|
||||
|
||||
q := db.New(tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
api := db.CreateSysApiParams{
|
||||
Name: req.Name,
|
||||
GroupName: req.GroupName,
|
||||
Method: req.Method,
|
||||
Path: req.Path,
|
||||
Sort: req.Sort,
|
||||
}
|
||||
// 创建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
|
||||
|
||||
// 创建权限
|
||||
permissionId, err := q.CreateSysPermission(ctx, db.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, db.CreateSysApiPermissionParams{
|
||||
ApiID: apiId,
|
||||
PermissionID: permissionId,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = tx.Commit(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 清理缓存
|
||||
s.cache.ClearAllSysUserCache()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SysApiService) Update(ctx context.Context, id int32, req request.UpdateSysApiRequest) error {
|
||||
api := db.UpdateSysApiParams{
|
||||
api := sqlc.UpdateSysApiParams{
|
||||
ID: id,
|
||||
Name: req.Name,
|
||||
GroupName: req.GroupName,
|
||||
@@ -116,43 +106,51 @@ func (s *SysApiService) Update(ctx context.Context, id int32, req request.Update
|
||||
Sort: req.Sort,
|
||||
}
|
||||
|
||||
// 清理缓存
|
||||
s.cache.ClearAllSysUserCache()
|
||||
|
||||
rows, err := s.queries.UpdateSysApi(ctx, api)
|
||||
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 {
|
||||
tx, err := s.pool.BeginTx(ctx, pgx.TxOptions{})
|
||||
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
|
||||
}
|
||||
|
||||
defer tx.Rollback(ctx)
|
||||
// 清理缓存 事务执行成功后才清理
|
||||
_ = s.cache.DelByPrefix(ctx, cachekey.UserApiPermissionsPattern)
|
||||
|
||||
q := db.New(tx)
|
||||
|
||||
rows, err := q.DeleteSysApi(ctx, id)
|
||||
if err = dberr.MapRowsAffected(rows, err, errs.ErrSysApiNotFound); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = q.DeleteSysPermissionBySysApiID(ctx, id); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = q.DeleteSysApiPermission(ctx, id); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 清理缓存
|
||||
s.cache.ClearAllSysUserCache()
|
||||
|
||||
return tx.Commit(ctx)
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user