144 lines
3.1 KiB
Go
144 lines
3.1 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"sort"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
func (m *Memory) validProject(p Project) bool {
|
|
n := len([]rune(p.Name))
|
|
if n < 1 || n > 120 || len([]rune(p.Description)) > 1000 {
|
|
return false
|
|
}
|
|
switch p.Kind {
|
|
case ProjectKindProduct:
|
|
if p.Level != ProjectLevelFamily && p.Level != ProjectLevelProduct && p.Level != ProjectLevelComponent {
|
|
return false
|
|
}
|
|
case ProjectKindTopic:
|
|
if p.Level != "" || p.ParentID != "" {
|
|
return false
|
|
}
|
|
default:
|
|
return false
|
|
}
|
|
if p.ParentID != "" {
|
|
// Like the database: the parent exists in the same tenant and is not the project itself.
|
|
parent, ok := m.projects[p.ParentID]
|
|
if !ok || parent.TenantID != p.TenantID || p.ParentID == p.ID {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// nameTaken reports whether another project of the tenant has the name
|
|
// (case-insensitively). Caller holds the lock.
|
|
func (m *Memory) nameTaken(p Project) bool {
|
|
for _, x := range m.projects {
|
|
if x.TenantID == p.TenantID && x.ID != p.ID && strings.EqualFold(x.Name, p.Name) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (m *Memory) ListProjects(_ context.Context, tenantID string) ([]Project, error) {
|
|
m.mu.RLock()
|
|
defer m.mu.RUnlock()
|
|
out := []Project{}
|
|
for _, p := range m.projects {
|
|
if p.TenantID == tenantID {
|
|
out = append(out, *p)
|
|
}
|
|
}
|
|
sort.Slice(out, func(i, j int) bool {
|
|
a, b := strings.ToLower(out[i].Name), strings.ToLower(out[j].Name)
|
|
if a == b {
|
|
return out[i].ID < out[j].ID
|
|
}
|
|
return a < b
|
|
})
|
|
return out, nil
|
|
}
|
|
|
|
func (m *Memory) GetProject(_ context.Context, id string) (*Project, error) {
|
|
m.mu.RLock()
|
|
defer m.mu.RUnlock()
|
|
p, ok := m.projects[id]
|
|
if !ok {
|
|
return nil, ErrNotFound
|
|
}
|
|
out := *p
|
|
return &out, nil
|
|
}
|
|
|
|
func (m *Memory) CreateProject(_ context.Context, p Project) (*Project, error) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
if _, ok := m.tenants[p.TenantID]; !ok {
|
|
return nil, ErrNotFound
|
|
}
|
|
if !m.validProject(p) {
|
|
return nil, ErrInvalidInput
|
|
}
|
|
p.ID = uuid.NewString()
|
|
if m.nameTaken(p) {
|
|
return nil, ErrConflict
|
|
}
|
|
now := time.Now().UTC()
|
|
p.CreatedAt, p.UpdatedAt = now, now
|
|
row := p
|
|
m.projects[p.ID] = &row
|
|
out := row
|
|
return &out, nil
|
|
}
|
|
|
|
func (m *Memory) UpdateProject(_ context.Context, p Project) (*Project, error) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
cur, ok := m.projects[p.ID]
|
|
if !ok {
|
|
return nil, ErrNotFound
|
|
}
|
|
p.TenantID = cur.TenantID
|
|
if !m.validProject(p) {
|
|
return nil, ErrInvalidInput
|
|
}
|
|
if m.nameTaken(p) {
|
|
return nil, ErrConflict
|
|
}
|
|
cur.Name, cur.Kind, cur.Description, cur.UpdatedAt = p.Name, p.Kind, p.Description, time.Now().UTC()
|
|
cur.Level, cur.ParentID = p.Level, p.ParentID
|
|
out := *cur
|
|
return &out, nil
|
|
}
|
|
|
|
func (m *Memory) DeleteProject(_ context.Context, id string) error {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
if _, ok := m.projects[id]; !ok {
|
|
return ErrNotFound
|
|
}
|
|
for _, x := range m.projects {
|
|
if x.ParentID == id {
|
|
return ErrHasChildren
|
|
}
|
|
}
|
|
delete(m.projects, id)
|
|
for _, d := range m.documents {
|
|
kept := d.ProjectIDs[:0:0]
|
|
for _, pid := range d.ProjectIDs {
|
|
if pid != id {
|
|
kept = append(kept, pid)
|
|
}
|
|
}
|
|
d.ProjectIDs = kept
|
|
}
|
|
return nil
|
|
}
|