chore: initial commit
This commit is contained in:
158
internal/service/sys_api.go
Normal file
158
internal/service/sys_api.go
Normal file
@@ -0,0 +1,158 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
db "server/internal/db/sqlc"
|
||||
"server/internal/model/common"
|
||||
"server/internal/model/enum"
|
||||
"server/internal/model/request"
|
||||
"server/internal/pkg/cache"
|
||||
"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
|
||||
}
|
||||
|
||||
func NewSysApiService(queries *db.Queries, pool *pgxpool.Pool, cache *cache.Caches) *SysApiService {
|
||||
return &SysApiService{queries: queries, pool: pool, 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,
|
||||
}
|
||||
|
||||
total, err := s.queries.CountSysApis(ctx)
|
||||
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
list, err := s.queries.GetSysApis(ctx, params)
|
||||
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return list, total, nil
|
||||
}
|
||||
|
||||
func (s *SysApiService) GetAllSysApis(ctx context.Context) ([]db.SysApi, error) {
|
||||
return s.queries.GetAllSysApis(ctx)
|
||||
}
|
||||
|
||||
func (s *SysApiService) GetApiGroupNames(ctx context.Context) ([]string, error) {
|
||||
return s.queries.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
|
||||
}
|
||||
|
||||
defer func(ctx context.Context) {
|
||||
_ = tx.Rollback(ctx)
|
||||
}(ctx)
|
||||
|
||||
q := db.New(tx)
|
||||
|
||||
api := db.CreateSysApiParams{
|
||||
Name: req.Name,
|
||||
GroupName: req.GroupName,
|
||||
Method: req.Method,
|
||||
Path: req.Path,
|
||||
Sort: req.Sort,
|
||||
}
|
||||
|
||||
// 创建权限
|
||||
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{
|
||||
ID: id,
|
||||
Name: req.Name,
|
||||
GroupName: req.GroupName,
|
||||
Method: req.Method,
|
||||
Path: req.Path,
|
||||
Sort: req.Sort,
|
||||
}
|
||||
|
||||
// 清理缓存
|
||||
s.cache.ClearAllSysUserCache()
|
||||
|
||||
rows, err := s.queries.UpdateSysApi(ctx, api)
|
||||
if err = dberr.MapRowsAffected(rows, err, errs.ErrSysApiNotFound); err != nil {
|
||||
return dberr.MapUniqueViolation(err, dberr.SysApisMethodPathKey, errs.ErrSysApiMethodPathAlreadyExists)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SysApiService) Delete(ctx context.Context, id int32) error {
|
||||
tx, err := s.pool.BeginTx(ctx, pgx.TxOptions{})
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer tx.Rollback(ctx)
|
||||
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user