✨ List attachments api
This commit is contained in:
parent
9ceb9925dd
commit
01e395b2bf
@ -3,12 +3,14 @@ package grpc
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/database"
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/proto"
|
||||
"google.golang.org/protobuf/types/known/emptypb"
|
||||
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/models"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
func (v *Server) GetAttachment(ctx context.Context, request *proto.AttachmentLookupRequest) (*proto.Attachment, error) {
|
||||
@ -31,6 +33,10 @@ func (v *Server) GetAttachment(ctx context.Context, request *proto.AttachmentLoo
|
||||
|
||||
rawMetadata, _ := jsoniter.Marshal(attachment.Metadata)
|
||||
|
||||
if attachment.AccountID == nil {
|
||||
attachment.AccountID = lo.ToPtr[uint](0)
|
||||
}
|
||||
|
||||
return &proto.Attachment{
|
||||
Id: uint64(attachment.ID),
|
||||
Uuid: attachment.Uuid,
|
||||
@ -43,7 +49,7 @@ func (v *Server) GetAttachment(ctx context.Context, request *proto.AttachmentLoo
|
||||
Destination: attachment.Destination,
|
||||
Metadata: rawMetadata,
|
||||
IsMature: attachment.IsMature,
|
||||
AccountId: uint64(attachment.AccountID),
|
||||
AccountId: uint64(*attachment.AccountID),
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
@ -1 +1,42 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/database"
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/models"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func listAttachment(c *fiber.Ctx) error {
|
||||
take := c.QueryInt("take", 0)
|
||||
offset := c.QueryInt("offset", 0)
|
||||
|
||||
if take > 100 {
|
||||
take = 100
|
||||
}
|
||||
|
||||
tx := database.C
|
||||
|
||||
if author := c.QueryInt("authorId", 0); author > 0 {
|
||||
tx = tx.Where("account_id = ?", author)
|
||||
}
|
||||
if usage := strings.Split(c.Query("usage"), " "); len(usage) > 0 {
|
||||
tx = tx.Where("usage IN ?", usage)
|
||||
}
|
||||
|
||||
var count int64
|
||||
countTx := tx
|
||||
if err := countTx.Model(&models.Attachment{}).Count(&count).Error; err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
var attachments []models.Attachment
|
||||
if err := tx.Offset(offset).Limit(take).Find(&attachments).Error; err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(fiber.Map{
|
||||
"count": count,
|
||||
"data": attachments,
|
||||
})
|
||||
}
|
||||
|
@ -7,7 +7,7 @@ func MapAPIs(app *fiber.App, baseURL string) {
|
||||
|
||||
api := app.Group(baseURL).Name("API")
|
||||
{
|
||||
api.Get("/attachments", list)
|
||||
api.Get("/attachments", listAttachment)
|
||||
api.Get("/attachments/:id/meta", getAttachmentMeta)
|
||||
api.Get("/attachments/:id", openAttachment)
|
||||
api.Post("/attachments", createAttachment)
|
||||
|
Loading…
Reference in New Issue
Block a user