oficina / fichas Public

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

Plain git — no account needed to clone.

Download

Download this file

1package fichas
2
3// A card's own pages: the full page, the scriptless edit form with its
4// all-or-nothing conflict page (level-up §5.2, §5.7), and History (§5.5).
5
6import (
7 "errors"
8 "net/http"
9 "strconv"
10
11 "gorm.io/gorm"
12)
13
14// showCard is a card on its own page: the same card the pane shows.
15func (a *app) showCard(w http.ResponseWriter, r *http.Request) {
16 id, ok := pathID(w, r)
17 if !ok {
18 return
19 }
20 card, err := LoadCard(r.Context(), a.db.G, a.sealer, id)
21 if err != nil {
22 a.notFoundOrFail(w, r, err)
23 return
24 }
25 entryFields, err := EntryFields(r.Context(), a.db.G, a.sealer, TypeCallLog)
26 if err != nil {
27 a.fail(w, r, err)
28 return
29 }
30 pane := cardPane{Card: card, CloseHref: typeHref(card.TypeKey)}
31 pane.EditedBy, pane.EditedAt = a.editedBy(r, card)
32 a.page(w, r, "card_show", map[string]any{
33 "TypeKey": card.TypeKey, "Card": card, "Pane": pane,
34 "EntryFields": entryFields, "Notice": a.noticeOf(r),
35 })
36}
37
38// editCard is the scriptless edit form, with ?field= focused (§5.7).
39func (a *app) editCard(w http.ResponseWriter, r *http.Request) {
40 id, ok := pathID(w, r)
41 if !ok {
42 return
43 }
44 card, err := LoadCard(r.Context(), a.db.G, a.sealer, id)
45 if err != nil {
46 a.notFoundOrFail(w, r, err)
47 return
48 }
49 a.page(w, r, "card_edit", map[string]any{
50 "TypeKey": card.TypeKey, "Card": card, "Was": wasOf(card.Fields),
51 "Focus": r.URL.Query().Get("field"), "Typed": false,
52 "Conflicts": map[string]*conflictChoice{},
53 "ReturnTo": localReturn(r.URL.Query().Get("return_to"), "/cards/"+strconv.FormatInt(id, 10)),
54 })
55}
56
57func wasOf(fields []FieldView) map[string]string {
58 out := make(map[string]string, len(fields))
59 for _, f := range fields {
60 out[f.Def.Key] = f.Value
61 }
62 return out
63}
64
65// updateCard is the edit form's POST. It sends was for every field it
66// shows and changes only the fields whose value differs from its was;
67// it is all or nothing (§5.2). A field posted with no was (a client
68// older than this form) is checked against the value now.
69func (a *app) updateCard(w http.ResponseWriter, r *http.Request) {
70 id, ok := pathID(w, r)
71 if !ok {
72 return
73 }
74 actor, err := a.actor(r)
75 if err != nil {
76 a.fail(w, r, err)
77 return
78 }
79 card, err := LoadCard(r.Context(), a.db.G, a.sealer, id)
80 if err != nil {
81 a.notFoundOrFail(w, r, err)
82 return
83 }
84 _ = r.ParseForm()
85 values, was, typed, problems := formEdit(r, card)
86 if len(problems) > 0 {
87 a.editAgain(w, r, http.StatusUnprocessableEntity, card, typed, was, problems, nil)
88 return
89 }
90 res, err := EditFields(r.Context(), a.db.G, a.sealer, id, actor, values, was)
91 if fe, ok := IsFormError(err); ok {
92 a.editAgain(w, r, http.StatusUnprocessableEntity, card, typed, was, fe, nil)
93 return
94 }
95 if c, ok := IsConflict(err); ok {
96 a.editAgain(w, r, http.StatusConflict, card, typed, was, nil, c)
97 return
98 }
99 if errors.Is(err, gorm.ErrRecordNotFound) {
100 notFound(w, r)
101 return
102 }
103 if err != nil {
104 a.fail(w, r, err)
105 return
106 }
107 if res.EventID == 0 {
108 a.backWith(w, r, id, "", "")
109 return
110 }
111 a.backWith(w, r, id, "saved", strconv.FormatInt(res.EventID, 10))
112}
113
114// formEdit reads the edit form: the canonical values that differ from
115// their was, every field's was, what was typed (to draw it again), and
116// what a person must fix.
117func formEdit(r *http.Request, card CardView) (values, was, typed map[string]string, problems FormError) {
118 values, was, typed, problems = map[string]string{}, map[string]string{}, map[string]string{}, FormError{}
119 for _, f := range card.Fields {
120 key := f.Def.Key
121 if _, posted := r.PostForm[key]; !posted {
122 continue
123 }
124 w, sent := r.PostForm["was."+key]
125 was[key] = f.Value
126 if sent {
127 was[key] = w[0] // compared newline-blind by EditFields
128 }
129 typed[key] = r.PostForm.Get(key)
130 v, problem := postedValue(r, f.Def, was[key])
131 if problem != "" {
132 problems[key] = problem
133 continue
134 }
135 if !sameText(normaliseValue(f.Def, v), was[key]) {
136 values[key] = v
137 }
138 }
139 return values, was, typed, problems
140}
141
142// editAgain draws the edit form again: with what was typed and the
143// problems beside it (422), or, after a conflict (409), each
144// conflicting field as two choices, theirs and mine, with theirs as its
145// new was, and the other fields as typed.
146func (a *app) editAgain(w http.ResponseWriter, r *http.Request, status int, card CardView, typed, was map[string]string, problems FormError, c *ConflictError) {
147 L := a.readerL(r)
148 conflicts := map[string]*conflictChoice{}
149 if c != nil {
150 for key, theirs := range c.Fields {
151 f := card.Field(key)
152 was[key] = theirs.Theirs
153 mine := typed[key]
154 conflicts[key] = &conflictChoice{
155 Theirs: editorSeed(L, f.Def, theirs.Theirs), Mine: mine,
156 TheirsText: orText(showValue(L, f.Def.Kind, theirs.Theirs), "nothing"),
157 MineText: orText(mine, "nothing"), By: personName(theirs.By),
158 }
159 }
160 }
161 shown := card
162 shown.Fields = withValues(card.Fields, typed)
163 a.pageStatus(w, r, status, "card_edit", map[string]any{
164 "TypeKey": card.TypeKey, "Card": shown, "Was": was, "Errors": problems,
165 "Conflicts": conflicts, "Typed": true,
166 "ReturnTo": localReturn(r.PostForm.Get("return_to"), "/cards/"+strconv.FormatInt(card.Card.ID, 10)),
167 })
168}
169
170// conflictChoice is one field's two values on the conflict page.
171type conflictChoice struct {
172 Theirs, Mine string // as they post: the editor's seed form
173 TheirsText, MineText string // as a person reads them
174 By string
175}
176
177// cardHistory is a card's History page (§5.5).
178func (a *app) cardHistory(w http.ResponseWriter, r *http.Request) {
179 id, ok := pathID(w, r)
180 if !ok {
181 return
182 }
183 card, err := LoadCard(r.Context(), a.db.G, a.sealer, id)
184 if err != nil {
185 a.notFoundOrFail(w, r, err)
186 return
187 }
188 items, err := CardHistory(r.Context(), a.db.G, a.sealer, id)
189 if err != nil {
190 a.notFoundOrFail(w, r, err)
191 return
192 }
193 a.page(w, r, "history", map[string]any{
194 "TypeKey": card.TypeKey, "Card": card, "Items": items, "Now": wasOf(card.Fields),
195 "Notice": a.noticeOf(r),
196 })
197}
198