更新管理员相关接口
This commit is contained in:
@@ -3,11 +3,13 @@ package service
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/cloudprint/cloudprint-backend/internal/model"
|
||||
"github.com/cloudprint/cloudprint-backend/internal/repository"
|
||||
"github.com/cloudprint/cloudprint-backend/internal/service"
|
||||
pb "github.com/cloudprint/cloudprint-backend/proto/generated"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
"google.golang.org/protobuf/types/known/timestamppb"
|
||||
)
|
||||
|
||||
type AdminServiceServer struct {
|
||||
@@ -66,3 +68,68 @@ func (s *AdminServiceServer) GetStatistics(ctx context.Context, req *pb.GetStati
|
||||
ActivePrinters: int32(activePrinters),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *AdminServiceServer) CreateAdmin(ctx context.Context, req *pb.CreateAdminRequest) (*pb.CreateAdminResponse, error) {
|
||||
admin, err := s.authService.CreateAdmin(ctx, req.Username, req.Password, req.Nickname, int8(req.Role))
|
||||
if err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "create admin failed: %v", err)
|
||||
}
|
||||
|
||||
return &pb.CreateAdminResponse{
|
||||
Admin: convertAdminToProto(admin),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *AdminServiceServer) GetAdminList(ctx context.Context, req *pb.GetAdminListRequest) (*pb.AdminListResponse, error) {
|
||||
page := req.Page
|
||||
if page <= 0 {
|
||||
page = 1
|
||||
}
|
||||
pageSize := req.PageSize
|
||||
if pageSize <= 0 {
|
||||
pageSize = 10
|
||||
}
|
||||
|
||||
admins, total, err := s.authService.GetAdminList(ctx, int(page), int(pageSize))
|
||||
if err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "get admin list failed: %v", err)
|
||||
}
|
||||
|
||||
pbAdmins := make([]*pb.Admin, len(admins))
|
||||
for i, admin := range admins {
|
||||
pbAdmins[i] = convertAdminToProto(&admin)
|
||||
}
|
||||
|
||||
return &pb.AdminListResponse{
|
||||
Admins: pbAdmins,
|
||||
Total: int32(total),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *AdminServiceServer) UpdateAdmin(ctx context.Context, req *pb.UpdateAdminRequest) (*pb.Admin, error) {
|
||||
admin, err := s.authService.UpdateAdmin(ctx, req.Id, req.Nickname, int8(req.Role))
|
||||
if err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "update admin failed: %v", err)
|
||||
}
|
||||
|
||||
return convertAdminToProto(admin), nil
|
||||
}
|
||||
|
||||
func (s *AdminServiceServer) DeleteAdmin(ctx context.Context, req *pb.DeleteAdminRequest) (*pb.Empty, error) {
|
||||
if err := s.authService.DeleteAdmin(ctx, req.Id); err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "delete admin failed: %v", err)
|
||||
}
|
||||
|
||||
return &pb.Empty{}, nil
|
||||
}
|
||||
|
||||
// convertAdminToProto 将model.Admin转换为pb.Admin
|
||||
func convertAdminToProto(admin *model.Admin) *pb.Admin {
|
||||
return &pb.Admin{
|
||||
Id: admin.ID,
|
||||
Username: admin.Username,
|
||||
Nickname: admin.Nickname,
|
||||
Role: int32(admin.Role),
|
||||
CreatedAt: timestamppb.New(admin.CreatedAt),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user