backend/internal/db/mongo/category_crud.go
package mongo
import (
"bar/internal/models"
"context"
"time"
"github.com/google/uuid"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/options"
)
func (b *Backend) CreateCategory(ctx context.Context, c *models.Category) error {
ctx, cancel := b.TimeoutContext(ctx)
defer cancel()
c.CreatedAt = time.Now().Unix()
_, err := b.db.Collection(CategoriesCollection).InsertOne(ctx, c)
if err != nil {
return err
}
return nil
}
func (b *Backend) GetCategory(ctx context.Context, id string) (*models.Category, error) {
ctx, cancel := b.TimeoutContext(ctx)
defer cancel()
var c models.Category
err := b.db.Collection(CategoriesCollection).FindOne(ctx,
bson.M{
"id": uuid.MustParse(id),
"$or": []bson.M{
{
"deleted_at": bson.M{
"$exists": false,
},
},
{
"deleted_at": nil,
},
},
},
).Decode(&c)
if err != nil {
return nil, err
}
return &c, nil
}
func (b *Backend) UpdateCategory(ctx context.Context, c *models.Category) error {
ctx, cancel := b.TimeoutContext(ctx)
defer cancel()
res := b.db.Collection(CategoriesCollection).FindOneAndUpdate(ctx,
bson.M{
"id": c.Id,
"$or": []bson.M{
{
"deleted_at": bson.M{
"$exists": false,
},
},
{
"deleted_at": nil,
},
},
},
bson.M{
"$set": c,
},
options.FindOneAndUpdate().SetUpsert(true))
if res.Err() != nil {
return res.Err()
}
return nil
}
func (b *Backend) MarkDeleteCategory(ctx context.Context, id, by string) error {
ctx, cancel := b.TimeoutContext(ctx)
defer cancel()
res := b.db.Collection(CategoriesCollection).FindOneAndUpdate(ctx,
bson.M{
"id": uuid.MustParse(id),
},
bson.M{
"$set": bson.M{
"deleted_at": time.Now().Unix(),
"deleted_by": uuid.MustParse(by),
},
},
options.FindOneAndUpdate().SetUpsert(false))
if res.Err() != nil {
return res.Err()
}
// Move all items from this category to the "Uncategorized" category
_, err := b.db.Collection(ItemsCollection).UpdateMany(ctx,
bson.M{
"category_id": uuid.MustParse(id),
},
bson.M{
"$set": bson.M{
"category_id": uuid.MustParse("00000000-0000-0000-0000-000000000000"),
},
},
)
if err != nil {
return err
}
return nil
}
func (b *Backend) UnMarkDeleteCategory(ctx context.Context, id string) error {
ctx, cancel := b.TimeoutContext(ctx)
defer cancel()
res := b.db.Collection(CategoriesCollection).FindOneAndUpdate(ctx,
bson.M{
"id": uuid.MustParse(id),
},
bson.M{
"$set": bson.M{
"deleted_at": nil,
"deleted_by": nil,
},
},
options.FindOneAndUpdate().SetUpsert(false))
if res.Err() != nil {
return res.Err()
}
return nil
}
func (b *Backend) DeleteCategory(ctx context.Context, id string) error {
ctx, cancel := b.TimeoutContext(ctx)
defer cancel()
res := b.db.Collection(CategoriesCollection).FindOneAndDelete(ctx,
bson.M{
"id": uuid.MustParse(id),
},
)
if res.Err() != nil {
return res.Err()
}
return nil
}
func (b *Backend) RestoreCategory(ctx context.Context, id string) error {
ctx, cancel := b.TimeoutContext(ctx)
defer cancel()
res := b.db.Collection(CategoriesCollection).FindOneAndUpdate(ctx,
bson.M{
"id": uuid.MustParse(id),
},
bson.M{
"$unset": bson.M{
"deleted_at": "",
"deleted_by": "",
},
},
)
if res.Err() != nil {
return res.Err()
}
return nil
}