cosmium/internal/repositories/collections.go

65 lines
2.0 KiB
Go
Raw Normal View History

2024-02-10 16:52:41 +00:00
package repositories
import (
2024-02-24 18:00:47 +00:00
"fmt"
"time"
"github.com/google/uuid"
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
structhidrators "github.com/pikami/cosmium/internal/struct_hidrators"
)
2024-02-24 18:00:47 +00:00
var collections = []repositorymodels.Collection{}
2024-02-10 16:52:41 +00:00
func GetAllCollections(databaseId string) ([]repositorymodels.Collection, repositorymodels.RepositoryStatus) {
dbCollections := make([]repositorymodels.Collection, 0)
2024-02-10 16:52:41 +00:00
for _, coll := range collections {
if coll.Internals.DatabaseId == databaseId {
2024-02-10 16:52:41 +00:00
dbCollections = append(dbCollections, coll)
}
}
return dbCollections, repositorymodels.StatusOk
2024-02-10 16:52:41 +00:00
}
func GetCollection(databaseId string, id string) (repositorymodels.Collection, repositorymodels.RepositoryStatus) {
2024-02-10 16:52:41 +00:00
for _, coll := range collections {
if coll.Internals.DatabaseId == databaseId && coll.ID == id {
return coll, repositorymodels.StatusOk
2024-02-10 16:52:41 +00:00
}
}
return repositorymodels.Collection{}, repositorymodels.StatusNotFound
2024-02-10 16:52:41 +00:00
}
func DeleteCollection(databaseId string, id string) repositorymodels.RepositoryStatus {
2024-02-10 16:52:41 +00:00
for index, coll := range collections {
if coll.Internals.DatabaseId == databaseId && coll.ID == id {
2024-02-10 16:52:41 +00:00
collections = append(collections[:index], collections[index+1:]...)
return repositorymodels.StatusOk
2024-02-10 16:52:41 +00:00
}
}
return repositorymodels.StatusNotFound
2024-02-10 16:52:41 +00:00
}
func CreateCollection(databaseId string, newCollection repositorymodels.Collection) (repositorymodels.Collection, repositorymodels.RepositoryStatus) {
2024-02-10 16:52:41 +00:00
for _, coll := range collections {
if coll.Internals.DatabaseId == databaseId && coll.ID == newCollection.ID {
return repositorymodels.Collection{}, repositorymodels.Conflict
2024-02-10 16:52:41 +00:00
}
}
newCollection = structhidrators.Hidrate(newCollection).(repositorymodels.Collection)
2024-02-24 18:00:47 +00:00
newCollection.TimeStamp = time.Now().Unix()
newCollection.UniqueID = uuid.New().String()
newCollection.ETag = fmt.Sprintf("\"%s\"", newCollection.UniqueID)
newCollection.Internals = struct{ DatabaseId string }{
DatabaseId: databaseId,
}
2024-02-10 16:52:41 +00:00
collections = append(collections, newCollection)
return newCollection, repositorymodels.StatusOk
2024-02-10 16:52:41 +00:00
}