task can be added
This commit is contained in:
parent
87e66f74bd
commit
f73f862bcf
4 changed files with 64 additions and 15 deletions
|
@ -34,6 +34,17 @@ func (db *Database) GetAllTasks() []Task {
|
||||||
return tasks
|
return tasks
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (db *Database) CreateTask(username string, description string) Task {
|
||||||
|
task := Task{
|
||||||
|
ID: 0,
|
||||||
|
Username: username,
|
||||||
|
Description: description,
|
||||||
|
Done: false,
|
||||||
|
}
|
||||||
|
db.ORM.Create(&task)
|
||||||
|
return task
|
||||||
|
}
|
||||||
|
|
||||||
func (db *Database) CreateUser(username string, password string) error {
|
func (db *Database) CreateUser(username string, password string) error {
|
||||||
user := User{Username: username, Password: password}
|
user := User{Username: username, Password: password}
|
||||||
result := db.ORM.Create(&user)
|
result := db.ORM.Create(&user)
|
||||||
|
|
|
@ -17,6 +17,7 @@ type User struct {
|
||||||
|
|
||||||
type Task struct {
|
type Task struct {
|
||||||
ID int `gorm:"primaryKey"`
|
ID int `gorm:"primaryKey"`
|
||||||
|
Username string
|
||||||
Description string
|
Description string
|
||||||
Done bool
|
Done bool
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,9 +30,37 @@
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{{template "scripts" .}}
|
{{template "scripts" .}}
|
||||||
|
{{template "formScripts" .}}
|
||||||
<script>
|
<script>
|
||||||
userLoggedIn().then((loggedIn) => !loggedIn ? redirect("/view/login") : getAllTasks());
|
userLoggedIn().then((loggedIn) => !loggedIn ? redirect("/view/login") : getAllTasks());
|
||||||
const tasksEl = document.getElementById("tasks");
|
const tasksEl = document.getElementById("tasks");
|
||||||
|
const descriptionInput = document.getElementById("description");
|
||||||
|
|
||||||
|
function submitForm(formData) {
|
||||||
|
axios.post("/tasks", formData, axiosConfig)
|
||||||
|
.then((response) => {
|
||||||
|
addTaskToTasks(response.data.task);
|
||||||
|
form.classList.remove('was-validated');
|
||||||
|
descriptionInput.value = "";
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
showErrorToast(err.response.data.message);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function addTaskToTasks(task) {
|
||||||
|
const newTask = document.createElement('div');
|
||||||
|
newTask.classList.add('row', 'g-0', 'align-items-center', 'mb-1');
|
||||||
|
newTask.innerHTML = `
|
||||||
|
<div class="col-1 text-center">${task.ID}</div>
|
||||||
|
<div class="col-8">${task.Description}</div>
|
||||||
|
<div class="col-2 form-check form-switch d-flex justify-content-center">
|
||||||
|
<input class="text-center form-check-input" type="checkbox" ${task.Done && "checked"} role="switch" id="flexSwitchCheckDefault">
|
||||||
|
</div>
|
||||||
|
<button class="col-1 btn btn-danger"><i class="bi bi-trash"></i></button>
|
||||||
|
`;
|
||||||
|
tasksEl.appendChild(newTask);
|
||||||
|
}
|
||||||
|
|
||||||
function getAllTasks() {
|
function getAllTasks() {
|
||||||
axios.get("/tasks", axiosConfig).then((response) => {
|
axios.get("/tasks", axiosConfig).then((response) => {
|
||||||
|
@ -49,17 +77,7 @@
|
||||||
const tasks = response.data.tasks;
|
const tasks = response.data.tasks;
|
||||||
[...tasks]
|
[...tasks]
|
||||||
.forEach((task) => {
|
.forEach((task) => {
|
||||||
const newTask = document.createElement('div');
|
addTaskToTasks(task);
|
||||||
newTask.classList.add('row', 'g-0', 'align-items-center', 'mb-1');
|
|
||||||
newTask.innerHTML = `
|
|
||||||
<div class="col-1 text-center">${task.ID}</div>
|
|
||||||
<div class="col-8">${task.Description}</div>
|
|
||||||
<div class="col-2 form-check form-switch d-flex justify-content-center">
|
|
||||||
<input class="text-center form-check-input" type="checkbox" ${task.Done && "checked"} role="switch" id="flexSwitchCheckDefault">
|
|
||||||
</div>
|
|
||||||
<button class="col-1 btn btn-danger"><i class="bi bi-trash"></i></button>
|
|
||||||
`;
|
|
||||||
tasksEl.appendChild(newTask);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,6 +11,16 @@ func (wp *Webpage) redirectHome(c *gin.Context) {
|
||||||
c.Redirect(http.StatusTemporaryRedirect, "/view/tasks")
|
c.Redirect(http.StatusTemporaryRedirect, "/view/tasks")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (wp *Webpage) isLoggedInMiddleware(c *gin.Context) bool {
|
||||||
|
username := c.Request.Header.Get("username")
|
||||||
|
if wp.Database.UserIsLoggedIn(username) {
|
||||||
|
return true
|
||||||
|
} else {
|
||||||
|
c.JSON(401, gin.H{"message": "unauthorized"})
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (wp *Webpage) defineRoutes() {
|
func (wp *Webpage) defineRoutes() {
|
||||||
|
|
||||||
view := wp.Router.Group("/view")
|
view := wp.Router.Group("/view")
|
||||||
|
@ -28,12 +38,21 @@ func (wp *Webpage) defineRoutes() {
|
||||||
tasks := wp.Router.Group("/tasks")
|
tasks := wp.Router.Group("/tasks")
|
||||||
{
|
{
|
||||||
tasks.GET("", func(c *gin.Context) {
|
tasks.GET("", func(c *gin.Context) {
|
||||||
username := c.Request.Header.Get("username")
|
if wp.isLoggedInMiddleware(c) {
|
||||||
if wp.Database.UserIsLoggedIn(username) {
|
|
||||||
tasks := wp.Database.GetAllTasks()
|
tasks := wp.Database.GetAllTasks()
|
||||||
c.JSON(200, gin.H{"tasks": tasks})
|
c.JSON(200, gin.H{"tasks": tasks})
|
||||||
} else {
|
}
|
||||||
c.JSON(401, gin.H{"message": "unauthorized"})
|
})
|
||||||
|
tasks.POST("", func(c *gin.Context) {
|
||||||
|
if wp.isLoggedInMiddleware(c) {
|
||||||
|
username := c.Request.Header.Get("username")
|
||||||
|
description, existing := c.GetPostForm("description")
|
||||||
|
if existing == false || description == "" {
|
||||||
|
c.JSON(400, gin.H{"message": "bad post form"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
task := wp.Database.CreateTask(username, description)
|
||||||
|
c.JSON(200, gin.H{"task": task})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue