69 lines
2.0 KiB
Go
69 lines
2.0 KiB
Go
|
|
package service
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
|
||
|
|
"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"
|
||
|
|
)
|
||
|
|
|
||
|
|
type AdminServiceServer struct {
|
||
|
|
pb.UnimplementedAdminServiceServer
|
||
|
|
authService *service.AuthService
|
||
|
|
orderRepo *repository.OrderRepository
|
||
|
|
printerRepo *repository.PrinterRepository
|
||
|
|
}
|
||
|
|
|
||
|
|
func NewAdminServiceServer(
|
||
|
|
authService *service.AuthService,
|
||
|
|
orderRepo *repository.OrderRepository,
|
||
|
|
printerRepo *repository.PrinterRepository,
|
||
|
|
) *AdminServiceServer {
|
||
|
|
return &AdminServiceServer{
|
||
|
|
authService: authService,
|
||
|
|
orderRepo: orderRepo,
|
||
|
|
printerRepo: printerRepo,
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *AdminServiceServer) Login(ctx context.Context, req *pb.AdminLoginRequest) (*pb.AdminLoginResponse, error) {
|
||
|
|
result, err := s.authService.AdminLogin(ctx, req.Username, req.Password)
|
||
|
|
if err != nil {
|
||
|
|
return nil, status.Errorf(codes.Unauthenticated, "login failed: %v", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
return &pb.AdminLoginResponse{
|
||
|
|
AccessToken: result.AccessToken,
|
||
|
|
Admin: &pb.Admin{
|
||
|
|
Id: result.Admin.ID,
|
||
|
|
Username: result.Admin.Username,
|
||
|
|
Nickname: result.Admin.Nickname,
|
||
|
|
Role: int32(result.Admin.Role),
|
||
|
|
},
|
||
|
|
}, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *AdminServiceServer) GetStatistics(ctx context.Context, req *pb.GetStatisticsRequest) (*pb.Statistics, error) {
|
||
|
|
totalUsers, totalOrders, todayOrders, todayRevenue, pendingOrders, err := s.orderRepo.GetStatistics(ctx)
|
||
|
|
if err != nil {
|
||
|
|
return nil, status.Errorf(codes.Internal, "get statistics failed: %v", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
activePrinters, err := s.printerRepo.CountActive(ctx)
|
||
|
|
if err != nil {
|
||
|
|
return nil, status.Errorf(codes.Internal, "get printer count failed: %v", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
return &pb.Statistics{
|
||
|
|
TotalUsers: totalUsers,
|
||
|
|
TotalOrders: totalOrders,
|
||
|
|
TodayOrders: todayOrders,
|
||
|
|
TodayRevenue: todayRevenue,
|
||
|
|
PendingOrders: pendingOrders,
|
||
|
|
ActivePrinters: int32(activePrinters),
|
||
|
|
}, nil
|
||
|
|
}
|