feat: 初始提交云印享付费打印服务后端
This commit is contained in:
91
cloudprint-backend/internal/grpc/service/library_service.go
Normal file
91
cloudprint-backend/internal/grpc/service/library_service.go
Normal file
@@ -0,0 +1,91 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/cloudprint/cloudprint-backend/internal/repository"
|
||||
pb "github.com/cloudprint/cloudprint-backend/proto/generated"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
type LibraryServiceServer struct {
|
||||
pb.UnimplementedLibraryServiceServer
|
||||
libraryRepo *repository.LibraryRepository
|
||||
fileRepo *repository.FileRepository
|
||||
}
|
||||
|
||||
func NewLibraryServiceServer(libraryRepo *repository.LibraryRepository, fileRepo *repository.FileRepository) *LibraryServiceServer {
|
||||
return &LibraryServiceServer{
|
||||
libraryRepo: libraryRepo,
|
||||
fileRepo: fileRepo,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *LibraryServiceServer) GetCategories(ctx context.Context, req *pb.GetCategoriesRequest) (*pb.CategoryListResponse, error) {
|
||||
categories, err := s.libraryRepo.GetAllCategories(ctx)
|
||||
if err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "get categories failed: %v", err)
|
||||
}
|
||||
|
||||
pbCategories := make([]*pb.LibraryCategory, 0, len(categories))
|
||||
for _, cat := range categories {
|
||||
pbCategories = append(pbCategories, &pb.LibraryCategory{
|
||||
Id: cat.ID,
|
||||
Name: cat.Name,
|
||||
Icon: cat.Icon,
|
||||
SortOrder: int32(cat.SortOrder),
|
||||
})
|
||||
}
|
||||
|
||||
return &pb.CategoryListResponse{Categories: pbCategories}, nil
|
||||
}
|
||||
|
||||
func (s *LibraryServiceServer) GetFiles(ctx context.Context, req *pb.GetLibraryFilesRequest) (*pb.LibraryFileListResponse, error) {
|
||||
files, total, err := s.libraryRepo.GetFilesByCategory(ctx, req.CategoryId, int(req.Page), int(req.PageSize))
|
||||
if err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "get files failed: %v", err)
|
||||
}
|
||||
|
||||
pbFiles := make([]*pb.LibraryFile, 0, len(files))
|
||||
for _, file := range files {
|
||||
pbFile := &pb.LibraryFile{
|
||||
Id: file.ID,
|
||||
CategoryId: file.CategoryID,
|
||||
FileId: file.FileID,
|
||||
Title: file.Title,
|
||||
Description: file.Description,
|
||||
PageCount: int32(file.PageCount),
|
||||
DownloadCount: int32(file.DownloadCount),
|
||||
}
|
||||
if file.File != nil {
|
||||
pbFile.File = &pb.FileInfo{
|
||||
Id: file.File.ID,
|
||||
FileName: file.File.FileName,
|
||||
FilePath: file.File.FilePath,
|
||||
FileType: file.File.FileType,
|
||||
FileSize: file.File.FileSize,
|
||||
}
|
||||
}
|
||||
pbFiles = append(pbFiles, pbFile)
|
||||
}
|
||||
|
||||
return &pb.LibraryFileListResponse{
|
||||
Files: pbFiles,
|
||||
Total: int32(total),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *LibraryServiceServer) UploadFile(stream pb.LibraryService_UploadFileServer) error {
|
||||
// Placeholder for streaming upload
|
||||
return status.Errorf(codes.Unimplemented, "streaming upload not implemented")
|
||||
}
|
||||
|
||||
func (s *LibraryServiceServer) DeleteFile(ctx context.Context, req *pb.DeleteLibraryFileRequest) (*pb.Empty, error) {
|
||||
err := s.libraryRepo.DeleteFile(ctx, req.LibraryFileId)
|
||||
if err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "delete file failed: %v", err)
|
||||
}
|
||||
|
||||
return &pb.Empty{}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user