53 lines
1.4 KiB
Go
53 lines
1.4 KiB
Go
package libraryService
|
|
|
|
import (
|
|
"context"
|
|
"github.com/mitchellh/mapstructure"
|
|
libraryApi "leafdev.top/leaf/rag/api/library"
|
|
"leafdev.top/leaf/rag/internal/logic"
|
|
)
|
|
|
|
type LibraryService struct {
|
|
libraryApi.UnimplementedLibraryServiceServer
|
|
}
|
|
|
|
var libraryLogic = logic.NewLibraryLogic()
|
|
|
|
func (LibraryService) ListLibrary(ctx context.Context, _ *libraryApi.ListLibraryRequest) (*libraryApi.ListLibraryResponse, error) {
|
|
libraries, err := libraryLogic.ListLibrary(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
libraryResponse := &libraryApi.ListLibraryResponse{}
|
|
|
|
// convert
|
|
for _, library := range libraries {
|
|
var libraryResponseItem = libraryApi.Library{
|
|
Id: int64(library.ID),
|
|
Name: library.Name,
|
|
UserId: library.UserID,
|
|
CreatedAt: library.CreatedAt.String(),
|
|
UpdatedAt: library.UpdatedAt.String(),
|
|
}
|
|
libraryResponse.Libraries = append(libraryResponse.Libraries, &libraryResponseItem)
|
|
}
|
|
|
|
return libraryResponse, err
|
|
}
|
|
|
|
func (LibraryService) CreateLibrary(ctx context.Context, req *libraryApi.CreateLibraryRequest) (*libraryApi.CreateLibraryResponse, error) {
|
|
library, err := libraryLogic.CreateLibrary(ctx, req.Name)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var libraryApiResponse libraryApi.CreateLibraryResponse
|
|
err = mapstructure.Decode(library, &libraryApiResponse)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &libraryApiResponse, nil
|
|
}
|