bucket crud

This commit is contained in:
2024-11-17 22:08:06 +01:00
parent abd805b9cd
commit 8cf61c68bf
10 changed files with 143 additions and 80 deletions

View File

@ -6,6 +6,7 @@ import (
"git.schreifuchs.ch/schreifuchs/warehouse/api"
"git.schreifuchs.ch/schreifuchs/warehouse/model"
"git.schreifuchs.ch/schreifuchs/warehouse/utils"
"gorm.io/gorm"
)
@ -13,7 +14,7 @@ func (c *Controller) GetBuckets(ctx context.Context, request api.GetBucketsReque
buckets, err := c.db.FindApiBuckets(*request.Params.Limit, *request.Params.Offset)
if err != nil {
return api.GetBuckets500Response{}, nil
return nil, err
}
t := len(buckets)
@ -38,17 +39,49 @@ func (c *Controller) DeleteBucketsBucketName(ctx context.Context, request api.De
if err := c.db.DeleteBucketByName(request.BucketName); errors.Is(err, gorm.ErrRecordNotFound) {
return api.DeleteBucketsBucketName404Response{}, nil
} else if err != nil {
return api.DeleteBucketsBucketName500Response{}, err
return nil, err
}
return api.DeleteBucketsBucketName204Response{}, nil
}
func (c *Controller) GetBucketsBucketNameObjects(ctx context.Context, request api.GetBucketsBucketNameObjectsRequestObject) (api.GetBucketsBucketNameObjectsResponseObject, error) {
return api.GetBucketsBucketNameObjects404Response{}, nil
func (c *Controller) GetBucketsBucketNameObjects(ctx context.Context, request api.GetBucketsBucketNameObjectsRequestObject) (response api.GetBucketsBucketNameObjectsResponseObject, err error) {
if bucket, err := c.db.FindBucketByName(request.BucketName, *request.Params.Limit, *request.Params.Offset); err == nil {
objects := make([]api.Object, 0, len(bucket.Objects))
for _, o := range utils.Paginate(bucket.Objects, *request.Params.Offset, *request.Params.Limit) {
s := int(o.Size)
objects = append(objects, api.Object{
Key: &o.Key,
LastModified: &o.UpdatedAt,
Size: &s,
})
}
total := len(bucket.Objects)
return api.GetBucketsBucketNameObjects200JSONResponse{
Items: &objects,
Total: &total,
}, nil
} else if errors.Is(err, gorm.ErrRecordNotFound) {
return api.GetBucketsBucketNameObjects404Response{}, nil
}
return
}
func (c *Controller) DeleteBucketsBucketNameObjectsObjectKey(ctx context.Context, request api.DeleteBucketsBucketNameObjectsObjectKeyRequestObject) (api.DeleteBucketsBucketNameObjectsObjectKeyResponseObject, error) {
return api.DeleteBucketsBucketNameObjectsObjectKey500Response{}, nil
err := c.db.DeleteObjectByKey(request.BucketName, request.ObjectKey)
if errors.Is(err, gorm.ErrRecordNotFound) {
return api.DeleteBucketsBucketNameObjectsObjectKey404Response{}, nil
}
return api.DeleteBucketsBucketNameObjectsObjectKey204Response{}, err
}
func (c *Controller) GetBucketsBucketNameObjectsObjectKey(ctx context.Context, request api.GetBucketsBucketNameObjectsObjectKeyRequestObject) (api.GetBucketsBucketNameObjectsObjectKeyResponseObject, error) {
@ -56,5 +89,5 @@ func (c *Controller) GetBucketsBucketNameObjectsObjectKey(ctx context.Context, r
}
func (c *Controller) PutBucketsBucketNameObjectsObjectKey(ctx context.Context, request api.PutBucketsBucketNameObjectsObjectKeyRequestObject) (api.PutBucketsBucketNameObjectsObjectKeyResponseObject, error) {
return api.PutBucketsBucketNameObjectsObjectKey500Response{}, nil
return api.PutBucketsBucketNameObjectsObjectKey400Response{}, nil
}

View File

@ -19,6 +19,9 @@ func New(db database) *Controller {
type database interface {
InsertBucket(bucket *model.Bucket) error
FindApiBuckets(limit, offset int) (buckets []api.Bucket, err error)
FindBucketByName(name string, limit, offset int) (buckets model.Bucket, err error)
FindBucketIdByName(name string) (id uint, err error)
DeleteBucketByName(name string) error
FindObjectByKey(bucketName, key string) (object *model.Object, err error)
DeleteObjectByKey(bucketName, key string) error
}