diff --git a/Lab01/app/.idea/app.iml b/Lab01/app/.idea/app.iml index 3f092d9..d946bc7 100644 --- a/Lab01/app/.idea/app.iml +++ b/Lab01/app/.idea/app.iml @@ -8,5 +8,6 @@ + \ No newline at end of file diff --git a/Lab01/app/.idea/jsLibraryMappings.xml b/Lab01/app/.idea/jsLibraryMappings.xml index a9dd937..f12a793 100644 --- a/Lab01/app/.idea/jsLibraryMappings.xml +++ b/Lab01/app/.idea/jsLibraryMappings.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/Lab01/app/database/database.go b/Lab01/app/database/database.go index c6d3218..df27a29 100644 --- a/Lab01/app/database/database.go +++ b/Lab01/app/database/database.go @@ -50,7 +50,7 @@ func (db *Database) DeleteTask(id string) { } func (db *Database) CreateUser(username string, password string) error { - user := User{Username: username, Password: password} + user := User{Username: username, Password: password, LoggedIn: true} result := db.ORM.Create(&user) return result.Error } diff --git a/Lab01/app/templates/register.tmpl b/Lab01/app/templates/register.tmpl index 0c7ece4..c1e8c29 100644 --- a/Lab01/app/templates/register.tmpl +++ b/Lab01/app/templates/register.tmpl @@ -22,8 +22,8 @@ function submitForm(formData) { axios({method: 'POST', url: '/auth/register', data: formData}) .then((response) => { - showSuccessToast(response.data.message); - redirect("/login"); + setCookie("username", response.data.username, 1); + redirect("/"); }) .catch((err) => { showErrorToast(err.response.data.message); diff --git a/Lab01/app/templates/tasks.tmpl b/Lab01/app/templates/tasks.tmpl index 7a1608a..971c200 100644 --- a/Lab01/app/templates/tasks.tmpl +++ b/Lab01/app/templates/tasks.tmpl @@ -35,11 +35,12 @@ userLoggedIn().then((loggedIn) => !loggedIn ? redirect("/view/login") : getAllTasks()); const tasksEl = document.getElementById("tasks"); const descriptionInput = document.getElementById("description"); + let tasks = []; function submitForm(formData) { axios.post("/tasks", formData, axiosConfig) .then((response) => { - addTaskToTasks(response.data.task); + addTaskToTasks(response.data.task, tasks.length + 1); form.classList.remove('was-validated'); descriptionInput.value = ""; }) @@ -50,19 +51,21 @@ function deleteTask(id) { axios.delete("/tasks/" + id, axiosConfig) - .then((response) => { - console.log(response); + .then(() => { + getAllTasks(); }) .catch((err) => { showErrorToast(err.response.data.message); }); } - function addTaskToTasks(task) { + function addTaskToTasks(task, number) { + tasks.push(task); const newTask = document.createElement('div'); newTask.classList.add('row', 'g-0', 'align-items-center', 'mb-1'); + newTask.id = task.ID; newTask.innerHTML = ` -
${task.ID}
+
${number}
${task.Description}
@@ -72,23 +75,27 @@ tasksEl.appendChild(newTask); } - function getAllTasks() { - axios.get("/tasks", axiosConfig).then((response) => { - const taskHeader = document.createElement("div"); - taskHeader.classList.add('row', 'g-0', 'align-items-center', 'mb-1'); - taskHeader.innerHTML = ` + function addTaskHeader() { + const taskHeader = document.createElement("div"); + taskHeader.classList.add('row', 'g-0', 'align-items-center', 'mb-1'); + taskHeader.innerHTML = `
ID
Description
Done
Delete

`; - tasksEl.appendChild(taskHeader); - const tasks = response.data.tasks; - [...tasks] - .forEach((task) => { - addTaskToTasks(task); - }); + tasksEl.appendChild(taskHeader); + } + + function getAllTasks() { + tasksEl.innerHTML = ""; + addTaskHeader(); + axios.get("/tasks", axiosConfig).then((response) => { + tasks = response.data.tasks; + tasks.forEach((task, index) => { + addTaskToTasks(task, index + 1); + }); }); } diff --git a/Lab01/app/webpage/webpage.go b/Lab01/app/webpage/webpage.go index 061e2da..d47e0ff 100644 --- a/Lab01/app/webpage/webpage.go +++ b/Lab01/app/webpage/webpage.go @@ -59,7 +59,7 @@ func (wp *Webpage) defineRoutes() { if wp.isLoggedInMiddleware(c) { id := c.Param("id") wp.Database.DeleteTask(id) - c.JSON(200, gin.H{"message": "success", "id": id}) + c.JSON(200, gin.H{"message": "success"}) } }) } @@ -110,7 +110,7 @@ func (wp *Webpage) defineRoutes() { c.JSON(500, gin.H{"message": err.Error()}) return } - c.JSON(200, gin.H{"message": "user registered"}) + c.JSON(200, gin.H{"message": "user registered", "username": username}) }) } wp.Router.NoRoute(func(c *gin.Context) {