2024-02-10 15:17:34 +00:00
|
|
|
package repositories
|
|
|
|
|
2024-02-24 18:00:47 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
2024-02-26 19:03:47 +00:00
|
|
|
"github.com/pikami/cosmium/internal/resourceid"
|
2024-02-25 20:13:04 +00:00
|
|
|
"golang.org/x/exp/maps"
|
2024-02-24 18:00:47 +00:00
|
|
|
)
|
2024-02-12 19:38:03 +00:00
|
|
|
|
|
|
|
func GetAllDatabases() ([]repositorymodels.Database, repositorymodels.RepositoryStatus) {
|
2024-02-25 20:13:04 +00:00
|
|
|
return maps.Values(storeState.Databases), repositorymodels.StatusOk
|
2024-02-10 15:17:34 +00:00
|
|
|
}
|
|
|
|
|
2024-02-12 19:38:03 +00:00
|
|
|
func GetDatabase(id string) (repositorymodels.Database, repositorymodels.RepositoryStatus) {
|
2024-02-25 20:13:04 +00:00
|
|
|
if database, ok := storeState.Databases[id]; ok {
|
|
|
|
return database, repositorymodels.StatusOk
|
2024-02-10 15:17:34 +00:00
|
|
|
}
|
|
|
|
|
2024-02-12 19:38:03 +00:00
|
|
|
return repositorymodels.Database{}, repositorymodels.StatusNotFound
|
2024-02-10 15:17:34 +00:00
|
|
|
}
|
|
|
|
|
2024-02-12 19:38:03 +00:00
|
|
|
func DeleteDatabase(id string) repositorymodels.RepositoryStatus {
|
2024-02-25 20:13:04 +00:00
|
|
|
if _, ok := storeState.Databases[id]; !ok {
|
|
|
|
return repositorymodels.StatusNotFound
|
2024-02-10 15:17:34 +00:00
|
|
|
}
|
|
|
|
|
2024-02-25 20:13:04 +00:00
|
|
|
delete(storeState.Databases, id)
|
|
|
|
|
|
|
|
return repositorymodels.StatusOk
|
2024-02-10 15:17:34 +00:00
|
|
|
}
|
|
|
|
|
2024-02-25 20:13:04 +00:00
|
|
|
func CreateDatabase(newDatabase repositorymodels.Database) (repositorymodels.Database, repositorymodels.RepositoryStatus) {
|
|
|
|
if _, ok := storeState.Databases[newDatabase.ID]; ok {
|
|
|
|
return repositorymodels.Database{}, repositorymodels.Conflict
|
2024-02-10 15:17:34 +00:00
|
|
|
}
|
|
|
|
|
2024-02-24 18:00:47 +00:00
|
|
|
newDatabase.TimeStamp = time.Now().Unix()
|
2024-02-26 19:03:47 +00:00
|
|
|
newDatabase.ResourceID = resourceid.New()
|
|
|
|
newDatabase.ETag = fmt.Sprintf("\"%s\"", uuid.New())
|
|
|
|
newDatabase.Self = fmt.Sprintf("dbs/%s/", newDatabase.ResourceID)
|
|
|
|
|
2024-02-25 20:13:04 +00:00
|
|
|
storeState.Databases[newDatabase.ID] = newDatabase
|
|
|
|
storeState.Collections[newDatabase.ID] = make(map[string]repositorymodels.Collection)
|
|
|
|
storeState.Documents[newDatabase.ID] = make(map[string]map[string]repositorymodels.Document)
|
|
|
|
|
|
|
|
return newDatabase, repositorymodels.StatusOk
|
2024-02-10 15:17:34 +00:00
|
|
|
}
|