oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package library
2
3import (
4 "amadan.net/oficina/equipo/internal/equipo/kernel"
5 "context"
6 "gorm.io/gorm"
7 "slices"
8)
9
10type Application struct {
11 Write func(context.Context, func(*gorm.DB) error) error
12 Read func(context.Context, kernel.Actor, func(*gorm.DB) error) error
13 At func(*gorm.DB) *Store
14 Normalize func(string) string
15}
16
17func (s *Application) read(ctx context.Context, a kernel.Actor, fn func(*Store) error) error {
18 return s.Read(ctx, a, func(tx *gorm.DB) error { return fn(s.At(tx)) })
19}
20func (s *Application) libraryWrite(ctx context.Context, fn func(*Store) error) error {
21 return s.Write(ctx, func(tx *gorm.DB) error { return fn(s.At(tx)) })
22}
23func (s *Application) CreateSpecialist(ctx context.Context, a kernel.Actor, in SpecialistInput, m kernel.Mutation) (row Specialist, err error) {
24 a.Address = s.Normalize(a.Address)
25 err = s.libraryWrite(ctx, func(l *Store) error { row, err = l.Create(a, in, m); return err })
26 return
27}
28func (s *Application) UpdateSpecialist(ctx context.Context, a kernel.Actor, in SpecialistInput, m kernel.Mutation) (row Specialist, err error) {
29 a.Address = s.Normalize(a.Address)
30 err = s.libraryWrite(ctx, func(l *Store) error { row, err = l.Update(a, in, m); return err })
31 return
32}
33func (s *Application) DeleteSpecialist(ctx context.Context, a kernel.Actor, key string, m kernel.Mutation) error {
34 a.Address = s.Normalize(a.Address)
35 return s.libraryWrite(ctx, func(l *Store) error { return l.Delete(a, key, m) })
36}
37func (s *Application) Specialist(ctx context.Context, a kernel.Actor, key string) (row Specialist, err error) {
38 err = s.read(ctx, a, func(l *Store) error { row, err = l.Get(a, key); return err })
39 return
40}
41func (s *Application) SpecialistTemplates(ctx context.Context, a kernel.Actor) (rows []Template, err error) {
42 err = s.read(ctx, a, func(l *Store) error { rows, err = l.Templates(a); return err })
43 return
44}
45func (s *Application) InstallSpecialistTemplate(ctx context.Context, a kernel.Actor, ref TemplateRef, m kernel.Mutation) (row Specialist, err error) {
46 a.Address = s.Normalize(a.Address)
47 err = s.libraryWrite(ctx, func(l *Store) error { row, err = l.Install(a, ref, m); return err })
48 return
49}
50func (s *Application) SpecialistTemplateDiff(ctx context.Context, a kernel.Actor, key string, ref TemplateRef) (diff TemplateDiff, err error) {
51 err = s.read(ctx, a, func(l *Store) error { diff, err = l.Diff(a, key, ref); return err })
52 return
53}
54func (s *Application) ApplySpecialistTemplate(ctx context.Context, a kernel.Actor, key string, diff TemplateDiff, selected []string, m kernel.Mutation) (row Specialist, err error) {
55 a.Address = s.Normalize(a.Address)
56 err = s.libraryWrite(ctx, func(l *Store) error { row, err = l.Apply(a, key, diff, selected, m); return err })
57 return
58}
59
60func (s *Application) ListSpecialists(ctx context.Context, a kernel.Actor) (rows []Specialist, err error) {
61 err = s.read(ctx, a, func(l *Store) error { rows, err = l.List(a); return err })
62 return
63}
64
65func (s *Application) SpecialistFormProjects(ctx context.Context, a kernel.Actor, row Specialist) (out []ProjectOption, err error) {
66 err = s.read(ctx, a, func(l *Store) error {
67 access, e := l.deps.Authorize(l.tx, a)
68 if e != nil {
69 return e
70 }
71 if row.Key != "" && !access.Owner && row.Creator != a.Address {
72 return l.deps.Forbidden
73 }
74 var projects []kernel.Project
75 if e = l.tx.Order("name,id").Find(&projects).Error; e != nil {
76 return e
77 }
78 for _, p := range projects {
79 out = append(out, ProjectOption{ID: p.ID, Name: p.Name, Selected: slices.Contains(row.ProjectIDs, p.ID)})
80 }
81 return nil
82 })
83 return
84}
85