task can be added

This commit is contained in:
Florian Hoss 2022-04-06 20:59:10 +02:00
parent 87e66f74bd
commit f73f862bcf
4 changed files with 64 additions and 15 deletions

View file

@ -34,6 +34,17 @@ func (db *Database) GetAllTasks() []Task {
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 {
user := User{Username: username, Password: password}
result := db.ORM.Create(&user)

View file

@ -17,6 +17,7 @@ type User struct {
type Task struct {
ID int `gorm:"primaryKey"`
Username string
Description string
Done bool
}

View file

@ -30,9 +30,37 @@
</div>
{{template "scripts" .}}
{{template "formScripts" .}}
<script>
userLoggedIn().then((loggedIn) => !loggedIn ? redirect("/view/login") : getAllTasks());
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() {
axios.get("/tasks", axiosConfig).then((response) => {
@ -49,17 +77,7 @@
const tasks = response.data.tasks;
[...tasks]
.forEach((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);
addTaskToTasks(task);
});
});
}

View file

@ -11,6 +11,16 @@ func (wp *Webpage) redirectHome(c *gin.Context) {
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() {
view := wp.Router.Group("/view")
@ -28,12 +38,21 @@ func (wp *Webpage) defineRoutes() {
tasks := wp.Router.Group("/tasks")
{
tasks.GET("", func(c *gin.Context) {
username := c.Request.Header.Get("username")
if wp.Database.UserIsLoggedIn(username) {
if wp.isLoggedInMiddleware(c) {
tasks := wp.Database.GetAllTasks()
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})
}
})
}