2024-02-10 16:52:41 +00:00
|
|
|
package repositories
|
|
|
|
|
2024-02-12 19:38:03 +00:00
|
|
|
import (
|
2024-02-24 18:00:47 +00:00
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
2024-02-12 19:38:03 +00:00
|
|
|
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
2024-02-26 19:03:47 +00:00
|
|
|
"github.com/pikami/cosmium/internal/resourceid"
|
2024-02-12 19:38:03 +00:00
|
|
|
structhidrators "github.com/pikami/cosmium/internal/struct_hidrators"
|
2024-02-25 20:13:04 +00:00
|
|
|
"golang.org/x/exp/maps"
|
2024-02-12 19:38:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func GetAllCollections(databaseId string) ([]repositorymodels.Collection, repositorymodels.RepositoryStatus) {
|
2024-02-25 20:13:04 +00:00
|
|
|
if _, ok := storeState.Databases[databaseId]; !ok {
|
|
|
|
return make([]repositorymodels.Collection, 0), repositorymodels.StatusNotFound
|
2024-02-10 16:52:41 +00:00
|
|
|
}
|
|
|
|
|
2024-02-25 20:13:04 +00:00
|
|
|
return maps.Values(storeState.Collections[databaseId]), repositorymodels.StatusOk
|
2024-02-10 16:52:41 +00:00
|
|
|
}
|
|
|
|
|
2024-02-25 20:13:04 +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
|
|
|
}
|
|
|
|
|
2024-02-25 20:13:04 +00:00
|
|
|
return storeState.Collections[databaseId][collectionId], repositorymodels.StatusOk
|
2024-02-10 16:52:41 +00:00
|
|
|
}
|
|
|
|
|
2024-02-25 20:13:04 +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
|
|
|
}
|
|
|
|
|
2024-02-25 20:13:04 +00:00
|
|
|
delete(storeState.Collections[databaseId], collectionId)
|
|
|
|
|
|
|
|
return repositorymodels.StatusOk
|
2024-02-10 16:52:41 +00:00
|
|
|
}
|
|
|
|
|
2024-02-12 19:38:03 +00:00
|
|
|
func CreateCollection(databaseId string, newCollection repositorymodels.Collection) (repositorymodels.Collection, repositorymodels.RepositoryStatus) {
|
2024-02-26 19:03:47 +00:00
|
|
|
var ok bool
|
|
|
|
var database repositorymodels.Database
|
|
|
|
if database, ok = storeState.Databases[databaseId]; !ok {
|
2024-02-25 20:13:04 +00:00
|
|
|
return repositorymodels.Collection{}, repositorymodels.StatusNotFound
|
|
|
|
}
|
|
|
|
|
2024-02-26 19:03:47 +00:00
|
|
|
if _, ok = storeState.Collections[databaseId][newCollection.ID]; ok {
|
2024-02-25 20:13:04 +00:00
|
|
|
return repositorymodels.Collection{}, repositorymodels.Conflict
|
2024-02-10 16:52:41 +00:00
|
|
|
}
|
|
|
|
|
2024-02-12 19:38:03 +00:00
|
|
|
newCollection = structhidrators.Hidrate(newCollection).(repositorymodels.Collection)
|
|
|
|
|
2024-02-24 18:00:47 +00:00
|
|
|
newCollection.TimeStamp = time.Now().Unix()
|
2024-02-26 19:03:47 +00:00
|
|
|
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)
|
2024-02-25 20:13:04 +00:00
|
|
|
|
|
|
|
storeState.Collections[databaseId][newCollection.ID] = newCollection
|
|
|
|
storeState.Documents[databaseId][newCollection.ID] = make(map[string]repositorymodels.Document)
|
|
|
|
|
2024-02-12 19:38:03 +00:00
|
|
|
return newCollection, repositorymodels.StatusOk
|
2024-02-10 16:52:41 +00:00
|
|
|
}
|