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"
|
|
|
|
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
|
|
|
|
2024-02-12 19:38:03 +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 {
|
2024-02-12 19:38:03 +00:00
|
|
|
if coll.Internals.DatabaseId == databaseId {
|
2024-02-10 16:52:41 +00:00
|
|
|
dbCollections = append(dbCollections, coll)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-12 19:38:03 +00:00
|
|
|
return dbCollections, repositorymodels.StatusOk
|
2024-02-10 16:52:41 +00:00
|
|
|
}
|
|
|
|
|
2024-02-12 19:38:03 +00:00
|
|
|
func GetCollection(databaseId string, id string) (repositorymodels.Collection, repositorymodels.RepositoryStatus) {
|
2024-02-10 16:52:41 +00:00
|
|
|
for _, coll := range collections {
|
2024-02-12 19:38:03 +00:00
|
|
|
if coll.Internals.DatabaseId == databaseId && coll.ID == id {
|
|
|
|
return coll, repositorymodels.StatusOk
|
2024-02-10 16:52:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-12 19:38:03 +00:00
|
|
|
return repositorymodels.Collection{}, repositorymodels.StatusNotFound
|
2024-02-10 16:52:41 +00:00
|
|
|
}
|
|
|
|
|
2024-02-12 19:38:03 +00:00
|
|
|
func DeleteCollection(databaseId string, id string) repositorymodels.RepositoryStatus {
|
2024-02-10 16:52:41 +00:00
|
|
|
for index, coll := range collections {
|
2024-02-12 19:38:03 +00:00
|
|
|
if coll.Internals.DatabaseId == databaseId && coll.ID == id {
|
2024-02-10 16:52:41 +00:00
|
|
|
collections = append(collections[:index], collections[index+1:]...)
|
2024-02-12 19:38:03 +00:00
|
|
|
return repositorymodels.StatusOk
|
2024-02-10 16:52:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-12 19:38:03 +00:00
|
|
|
return repositorymodels.StatusNotFound
|
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-10 16:52:41 +00:00
|
|
|
for _, coll := range collections {
|
2024-02-12 19:38:03 +00:00
|
|
|
if coll.Internals.DatabaseId == databaseId && coll.ID == newCollection.ID {
|
|
|
|
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()
|
|
|
|
newCollection.UniqueID = uuid.New().String()
|
|
|
|
newCollection.ETag = fmt.Sprintf("\"%s\"", newCollection.UniqueID)
|
2024-02-12 19:38:03 +00:00
|
|
|
newCollection.Internals = struct{ DatabaseId string }{
|
|
|
|
DatabaseId: databaseId,
|
2024-02-10 18:17:33 +00:00
|
|
|
}
|
2024-02-10 16:52:41 +00:00
|
|
|
collections = append(collections, newCollection)
|
2024-02-12 19:38:03 +00:00
|
|
|
return newCollection, repositorymodels.StatusOk
|
2024-02-10 16:52:41 +00:00
|
|
|
}
|