cosmium/internal/repositories/collections.go

61 lines
1.9 KiB
Go
Raw Normal View History

2024-02-10 16:52:41 +00:00
package repositories
import (
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
structhidrators "github.com/pikami/cosmium/internal/struct_hidrators"
)
var collections = []repositorymodels.Collection{
2024-02-10 16:52:41 +00:00
{ID: "db1"},
{ID: "db2"},
}
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)
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
}