This repository has been archived by the owner on Jan 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
action_delete.go
87 lines (66 loc) · 2.63 KB
/
action_delete.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package nebula
import (
"github.com/benpate/derp"
"github.com/benpate/html"
"github.com/benpate/rosetta/convert"
)
// Delete action removes a single item from a container
type DeleteItem struct {
ItemID int `json:"itemId" form:"itemId"`
Check string `json:"check" form:"check"`
}
func (txn DeleteItem) Get(library *Library, container *Container, endpoint string) string {
b := html.New()
b.H1().InnerHTML("Remove This Content Item?")
b.Div()
b.Form("", "").Data("hx-post", endpoint).Data("hx-swap", "innerHTML")
b.Input("hidden", "action").Value("delete-item")
b.Input("hidden", "itemId").Value(convert.String(txn.ItemID))
b.Input("hidden", "check").Value(txn.Check)
b.Button().Class("warning").Type("submit").InnerHTML("Delete")
b.Button().Type("button").Script("on click send closeModal").InnerHTML("Cancel")
return b.String()
}
// Execute removes a single itme from a container
func (txn DeleteItem) Post(library *Library, container *Container) (int, error) {
// Find parent index and record
parentID := container.GetParentID(txn.ItemID)
if container.IsNil(parentID) {
return -1, derp.NewBadRequestError("nebula.DeleteItem.Post", "Invalid item", txn)
}
parent := container.GetItem(parentID)
if err := parent.Validate(txn.Check); err != nil {
return -1, derp.Wrap(err, "nebula.DeleteItem.Post", "Invalid checksum")
}
// Remove parent's reference to this item
container.DeleteReference(parentID, txn.ItemID)
container.NewChecksum(parentID)
// TODO: If a delete results in an empty layout, then remove the layout
// TODO: If a delete results in a layout with a single item, then remove the layout and promote the item
// Recursively delete this item and all of its children
return 0, deleteItem(container, parentID, txn.ItemID)
}
// DeleteReference removes an item from a parent.
// This separate function is used to make recursive calls efficiently
func deleteItem(container *Container, parentID int, deleteID int) error {
// TODO: perhaps these could just fail gracefully? Then, we allow cleanup by the "compact" function
// to handle items that are still bad
// Bounds check
if container.IsNil(parentID) {
return derp.New(500, "nebula.deleteItem", "Parent index out of bounds", container, parentID)
}
// Bounds check
if container.IsNil(deleteID) {
return derp.New(500, "nebula.deleteItem", "Child index out of bounds", container, deleteID)
}
// Remove all children from the content
if len((*container)[deleteID].Refs) > 0 {
for _, childID := range (*container)[deleteID].Refs {
deleteItem(container, deleteID, childID)
}
}
// Remove the deleted item
(*container)[deleteID] = Item{}
// Success!
return nil
}