cosmium/internal/repositories/collections.go

71 lines
2.5 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"
"github.com/pikami/cosmium/internal/resourceid"
structhidrators "github.com/pikami/cosmium/internal/struct_hidrators"
"golang.org/x/exp/maps"
)
func GetAllCollections(databaseId string) ([]repositorymodels.Collection, repositorymodels.RepositoryStatus) {
if _, ok := storeState.Databases[databaseId]; !ok {
return make([]repositorymodels.Collection, 0), repositorymodels.StatusNotFound
2024-02-10 16:52:41 +00:00
}
return maps.Values(storeState.Collections[databaseId]), repositorymodels.StatusOk
2024-02-10 16:52:41 +00:00
}
func GetCollection(databaseId string, collectionId string) (repositorymodels.Collection, repositorymodels.RepositoryStatus) {
if _, ok := storeState.Databases[databaseId]; !ok {
return repositorymodels.Collection{}, repositorymodels.StatusNotFound
}
if _, ok := storeState.Collections[databaseId][collectionId]; !ok {
return repositorymodels.Collection{}, repositorymodels.StatusNotFound
2024-02-10 16:52:41 +00:00
}
return storeState.Collections[databaseId][collectionId], repositorymodels.StatusOk
2024-02-10 16:52:41 +00:00
}
func DeleteCollection(databaseId string, collectionId string) repositorymodels.RepositoryStatus {
if _, ok := storeState.Databases[databaseId]; !ok {
return repositorymodels.StatusNotFound
}
if _, ok := storeState.Collections[databaseId][collectionId]; !ok {
return repositorymodels.StatusNotFound
2024-02-10 16:52:41 +00:00
}
delete(storeState.Collections[databaseId], collectionId)
return repositorymodels.StatusOk
2024-02-10 16:52:41 +00:00
}
func CreateCollection(databaseId string, newCollection repositorymodels.Collection) (repositorymodels.Collection, repositorymodels.RepositoryStatus) {
var ok bool
var database repositorymodels.Database
if database, ok = storeState.Databases[databaseId]; !ok {
return repositorymodels.Collection{}, repositorymodels.StatusNotFound
}
if _, ok = storeState.Collections[databaseId][newCollection.ID]; ok {
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.ResourceID = resourceid.NewCombined(database.ResourceID, resourceid.New())
newCollection.ETag = fmt.Sprintf("\"%s\"", uuid.New())
newCollection.Self = fmt.Sprintf("dbs/%s/colls/%s/", database.ResourceID, newCollection.ResourceID)
storeState.Collections[databaseId][newCollection.ID] = newCollection
storeState.Documents[databaseId][newCollection.ID] = make(map[string]repositorymodels.Document)
return newCollection, repositorymodels.StatusOk
2024-02-10 16:52:41 +00:00
}