-
Notifications
You must be signed in to change notification settings - Fork 3
/
todo.handlers.go
155 lines (122 loc) · 3.76 KB
/
todo.handlers.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package handlers
import (
"fmt"
"strconv"
"strings"
"github.com/emarifer/gofiber-htmx-todolist/models"
"github.com/gofiber/fiber/v2"
"github.com/sujit-baniya/flash"
)
/********** Handlers for Todo Views **********/
// Render List Page with success/error messages
func HandleViewList(c *fiber.Ctx) error {
todo := new(models.Todo)
todo.CreatedBy = c.Locals("userId").(uint64)
fm := fiber.Map{
"type": "alert-error",
}
todosSlice, err := todo.GetAllTodos()
if err != nil {
fm["message"] = fmt.Sprintf("something went wrong: %s", err)
return flash.WithError(c, fm).Redirect("/todo/list")
}
return c.Render("todo/index", fiber.Map{
"Page": "Tasks List",
"FromProtected": fromProtected,
"Todos": todosSlice,
"UserID": c.Locals("userId").(uint64),
"Username": c.Locals("username").(string),
"Message": flash.Get(c),
})
}
// Render Create Todo Page with success/error messages
func HandleViewCreatePage(c *fiber.Ctx) error {
if c.Method() == "POST" {
fm := fiber.Map{
"type": "alert-error",
}
newTodo := new(models.Todo)
newTodo.CreatedBy = c.Locals("userId").(uint64)
newTodo.Title = strings.Trim(c.FormValue("title"), " ")
newTodo.Description = strings.Trim(c.FormValue("description"), " ")
if _, err := newTodo.CreateTodo(); err != nil {
fm["message"] = fmt.Sprintf("something went wrong: %s", err)
return flash.WithError(c, fm).Redirect("/todo/list")
}
return c.Redirect("/todo/list")
}
return c.Render("todo/create", fiber.Map{
"Page": "Create Todo",
"FromProtected": fromProtected,
"UserID": c.Locals("userId").(uint64),
"Username": c.Locals("username").(string),
})
}
// Render Edit Todo Page with success/error messages
func HandleViewEditPage(c *fiber.Ctx) error {
idParams, _ := strconv.Atoi(c.Params("id"))
todoId := uint64(idParams)
todo := new(models.Todo)
todo.ID = todoId
todo.CreatedBy = c.Locals("userId").(uint64)
fm := fiber.Map{
"type": "alert-error",
}
recoveredTodo, err := todo.GetNoteById()
if err != nil {
fm["message"] = fmt.Sprintf("something went wrong: %s", err)
return flash.WithError(c, fm).Redirect("/todo/list")
}
if c.Method() == "POST" {
todo.Title = strings.Trim(c.FormValue("title"), " ")
todo.Description = strings.Trim(c.FormValue("description"), " ")
if c.FormValue("status") == "on" {
todo.Status = true
} else {
todo.Status = false
}
_, err := todo.UpdateTodo()
if err != nil {
fm["message"] = fmt.Sprintf("something went wrong: %s", err)
return flash.WithError(c, fm).Redirect("/todo/list")
}
fm = fiber.Map{
"type": "alert-success",
"message": "Task successfully updated!!",
}
return flash.WithSuccess(c, fm).Redirect("/todo/list")
}
return c.Render("todo/update", fiber.Map{
"Page": fmt.Sprintf("Edit Todo #%d", recoveredTodo.ID),
"FromProtected": fromProtected,
"ID": recoveredTodo.ID,
"Title": recoveredTodo.Title,
"Description": recoveredTodo.Description,
"Status": recoveredTodo.Status,
"UserID": c.Locals("userId").(uint64),
"Username": c.Locals("username").(string),
})
}
// Handler Remove Todo
func HandleDeleteTodo(c *fiber.Ctx) error {
idParams, _ := strconv.Atoi(c.Params("id"))
todoId := uint64(idParams)
todo := new(models.Todo)
todo.ID = todoId
todo.CreatedBy = c.Locals("userId").(uint64)
fm := fiber.Map{
"type": "alert-error",
}
if err := todo.DeleteTodo(); err != nil {
fm["message"] = fmt.Sprintf("something went wrong: %s", err)
return flash.WithError(c, fm).Redirect(
"/todo/list",
fiber.StatusSeeOther,
)
}
fm = fiber.Map{
"type": "alert-success",
"message": "Task successfully deleted!!",
}
return flash.WithSuccess(c, fm).Redirect("/todo/list", fiber.StatusSeeOther)
}