memos/server/router/api/v1/memo_resource_service.go

87 lines
2.5 KiB
Go
Raw Normal View History

2024-04-28 00:44:29 +08:00
package v1
import (
"context"
2024-01-06 09:23:20 +08:00
"slices"
"time"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
2024-04-27 22:02:15 +08:00
"google.golang.org/protobuf/types/known/emptypb"
2024-04-28 00:44:29 +08:00
v1pb "github.com/usememos/memos/proto/gen/api/v1"
"github.com/usememos/memos/store"
)
2024-04-28 00:44:29 +08:00
func (s *APIV1Service) SetMemoResources(ctx context.Context, request *v1pb.SetMemoResourcesRequest) (*emptypb.Empty, error) {
2024-03-20 21:17:04 +08:00
memoID, err := ExtractMemoIDFromName(request.Name)
2024-03-18 23:23:53 +08:00
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "invalid memo name: %v", err)
}
2024-01-06 09:23:20 +08:00
resources, err := s.Store.ListResources(ctx, &store.FindResource{
2024-03-20 21:17:04 +08:00
MemoID: &memoID,
2024-01-06 09:23:20 +08:00
})
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to list resources")
}
// Delete resources that are not in the request.
for _, resource := range resources {
found := false
for _, requestResource := range request.Resources {
2024-03-20 21:17:04 +08:00
if resource.UID == requestResource.Uid {
found = true
break
}
}
if !found {
if err = s.Store.DeleteResource(ctx, &store.DeleteResource{
ID: int32(resource.ID),
2024-03-20 21:17:04 +08:00
MemoID: &memoID,
}); err != nil {
return nil, status.Errorf(codes.Internal, "failed to delete resource")
}
}
}
2024-01-06 09:23:20 +08:00
slices.Reverse(request.Resources)
// Update resources' memo_id in the request.
2024-01-06 09:23:20 +08:00
for index, resource := range request.Resources {
2024-03-24 22:35:10 +08:00
id, err := ExtractResourceIDFromName(resource.Name)
2024-03-20 21:17:04 +08:00
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "invalid resource name: %v", err)
}
2024-01-06 09:23:20 +08:00
updatedTs := time.Now().Unix() + int64(index)
2024-05-02 21:28:06 +08:00
if err := s.Store.UpdateResource(ctx, &store.UpdateResource{
2024-03-20 21:17:04 +08:00
ID: id,
MemoID: &memoID,
2024-01-06 09:23:20 +08:00
UpdatedTs: &updatedTs,
}); err != nil {
2024-01-15 08:13:06 +08:00
return nil, status.Errorf(codes.Internal, "failed to update resource: %v", err)
}
}
2024-04-27 22:02:15 +08:00
return &emptypb.Empty{}, nil
}
2024-04-28 00:44:29 +08:00
func (s *APIV1Service) ListMemoResources(ctx context.Context, request *v1pb.ListMemoResourcesRequest) (*v1pb.ListMemoResourcesResponse, error) {
2024-03-18 23:23:53 +08:00
id, err := ExtractMemoIDFromName(request.Name)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "invalid memo name: %v", err)
}
resources, err := s.Store.ListResources(ctx, &store.FindResource{
2024-03-18 23:23:53 +08:00
MemoID: &id,
})
if err != nil {
2024-05-07 22:07:44 +08:00
return nil, status.Errorf(codes.Internal, "failed to list resources: %v", err)
}
2024-04-28 00:44:29 +08:00
response := &v1pb.ListMemoResourcesResponse{
Resources: []*v1pb.Resource{},
}
for _, resource := range resources {
response.Resources = append(response.Resources, s.convertResourceFromStore(ctx, resource))
}
return response, nil
}