cosmium/internal/repositories/databases.go

51 lines
1.6 KiB
Go
Raw Normal View History

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"
"github.com/pikami/cosmium/internal/resourceid"
"golang.org/x/exp/maps"
2024-02-24 18:00:47 +00:00
)
func GetAllDatabases() ([]repositorymodels.Database, repositorymodels.RepositoryStatus) {
return maps.Values(storeState.Databases), repositorymodels.StatusOk
2024-02-10 15:17:34 +00:00
}
func GetDatabase(id string) (repositorymodels.Database, repositorymodels.RepositoryStatus) {
if database, ok := storeState.Databases[id]; ok {
return database, repositorymodels.StatusOk
2024-02-10 15:17:34 +00:00
}
return repositorymodels.Database{}, repositorymodels.StatusNotFound
2024-02-10 15:17:34 +00:00
}
func DeleteDatabase(id string) repositorymodels.RepositoryStatus {
if _, ok := storeState.Databases[id]; !ok {
return repositorymodels.StatusNotFound
2024-02-10 15:17:34 +00:00
}
delete(storeState.Databases, id)
return repositorymodels.StatusOk
2024-02-10 15:17:34 +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()
newDatabase.ResourceID = resourceid.New()
newDatabase.ETag = fmt.Sprintf("\"%s\"", uuid.New())
newDatabase.Self = fmt.Sprintf("dbs/%s/", newDatabase.ResourceID)
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
}