Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DmXxNULoSNPvjF7buKipsB
117 lines
2.5 KiB
Go
117 lines
2.5 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))
|
|
return n >= 1 && n <= 120 && len([]rune(p.Description)) <= 1000 &&
|
|
(p.Kind == ProjectKindProduct || p.Kind == ProjectKindTopic)
|
|
}
|
|
|
|
// 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()
|
|
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
|
|
}
|
|
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
|
|
}
|