diff --git a/api/endpotins.go b/api/endpotins.go index ea304a3..6be5bd9 100644 --- a/api/endpotins.go +++ b/api/endpotins.go @@ -24,7 +24,7 @@ func Listen(address string, path string) { apiAuth := api.Group("auth") { apiAuth.POST("register", auth.Register) - apiAuth.GET("login", auth.Login) + apiAuth.POST("login", auth.Login) } apiAdmin := api.Group("admin").Use(auth.LoggedIn).Use(auth.IsAdmin) diff --git a/default.conf.template b/default.conf.template index 3bd28db..b4913a3 100644 --- a/default.conf.template +++ b/default.conf.template @@ -1,17 +1,13 @@ server { listen ${NGINX_PORT}; + root /usr/share/nginx/html; + location / { - root /usr/share/nginx/html; - index index.html index.htm; + index index.html; } - error_page 404 /404.html; - - error_page 500 502 503 504 /50x.html; - location = /50x.html { - root /usr/share/nginx/html; - } + error_page 404 /404.html; location ~ api\/?.* { proxy_pass http://backend:5000; diff --git a/model/model.go b/model/model.go index 1c8f8c4..14dd512 100644 --- a/model/model.go +++ b/model/model.go @@ -9,7 +9,7 @@ type User struct { } type Task struct { - ID int `db:"id"` + ID int `db:"id" json:"id"` Description string `db:"description" form:"description" json:"description"` Points int `db:"points" form:"points" json:"points" validate:"required"` Recipient string `db:"recipient" form:"recipient" json:"recipient" validate:"required,len=6"` diff --git a/static/admin/add/index.html b/static/admin/add/index.html new file mode 100644 index 0000000..1d8c71c --- /dev/null +++ b/static/admin/add/index.html @@ -0,0 +1,28 @@ + + + + + + + Add task + + + + + + +
+ + +
+ + +
+ + +
+ +
+ + + \ No newline at end of file diff --git a/static/admin/index.html b/static/admin/index.html new file mode 100644 index 0000000..d6aab1f --- /dev/null +++ b/static/admin/index.html @@ -0,0 +1,12 @@ + + + + + + Admin + + + Add task + List tasks + + \ No newline at end of file diff --git a/static/admin/list/index.html b/static/admin/list/index.html new file mode 100644 index 0000000..ebfe3fb --- /dev/null +++ b/static/admin/list/index.html @@ -0,0 +1,37 @@ + + + + + + + List tasks by Neptun + + + + + + + + +
+ + +
Sum:
+ + + + + + + + + + + + + +
DescriptionIssuerPoints
+ + + + \ No newline at end of file diff --git a/static/index.html b/static/index.html index e055dad..f712d4a 100644 --- a/static/index.html +++ b/static/index.html @@ -1,11 +1,29 @@ - + + - SZOE pontok + Authentication + + + + -

SZOE pontok

+
+ + +
+ + +
+ + +
+ + +
+ \ No newline at end of file diff --git a/static/js/add.js b/static/js/add.js new file mode 100644 index 0000000..b85f260 --- /dev/null +++ b/static/js/add.js @@ -0,0 +1,22 @@ +var add_form = document.getElementById("add_form"); + +function add() { + fetch(`${config.apiBase}/admin/task/add`, + { + method: "POST", + credentials: "same-origin", + body: new FormData(add_form) + }) + .then(response => response.json()) + .then(data => { + if (data.status !== 200) { + alert(data.error); + console.log(data); + } else { + alert("task added"); + } + }) + .catch(error => { + console.log(error); + }); +} \ No newline at end of file diff --git a/static/js/admin_list.js b/static/js/admin_list.js new file mode 100644 index 0000000..2ddacf9 --- /dev/null +++ b/static/js/admin_list.js @@ -0,0 +1,54 @@ +function list_tasks() { + fetch(`${config.apiBase}/admin/task/list/${document.getElementById("neptun").value}`, + { + method: "GET", + credentials: "same-origin", + }) + .then(response => response.json()) + .then(data => { + if (data.status !== 200) { + alert(data.error); + console.log(data); + } else { + let sum = 0; + + let table = document.getElementById("list"); + table.innerHTML = ""; + + data.tasks.forEach(task => { + let row = table.insertRow(); + row.insertCell().innerText = task.description; + row.insertCell().innerText = task.issuer; + row.insertCell().innerText = task.points; + row.insertCell().innerHTML = ``; + sum += task.points; + }); + + document.getElementById("sum").innerText = sum; + } + }) + .catch(error => { + console.log(error); + }); +} + +function remove(id) { + fetch(`${config.apiBase}/admin/task/remove/${id}`, + { + method: "POST", + credentials: "same-origin", + }) + .then(response => response.json()) + .then(data => { + if (data.status !== 200) { + alert(data.error); + console.log(data); + } else { + alert("removed"); + list_tasks(); + } + }) + .catch(error => { + console.log(error); + }); +} \ No newline at end of file diff --git a/static/js/auth.js b/static/js/auth.js new file mode 100644 index 0000000..e48f20e --- /dev/null +++ b/static/js/auth.js @@ -0,0 +1,43 @@ +var auth_form = document.getElementById("auth_form"); + +function login() { + fetch(`${config.apiBase}/auth/login`, + { + method: "POST", + credentials: "same-origin", + body: new FormData(auth_form) + }) + .then(response => response.json()) + .then(data => { + if (data.status !== 200) { + alert(data.error); + console.log(data); + } else { + redirect("/list"); + } + }) + .catch(error => { + console.log(error); + }); +} + +function register() { + fetch(`${config.apiBase}/auth/register`, + { + method: "POST", + credentials: "same-origin", + body: new FormData(auth_form) + }) + .then(response => response.json()) + .then(data => { + if (data.status !== 200) { + alert(data.error); + console.log(data); + } else { + alert("succesful registration"); + } + }) + .catch(error => { + console.log(error); + }); +} \ No newline at end of file diff --git a/static/js/config.js b/static/js/config.js new file mode 100644 index 0000000..e004d40 --- /dev/null +++ b/static/js/config.js @@ -0,0 +1,3 @@ +const config = { + apiBase: "/api" +}; \ No newline at end of file diff --git a/static/js/list.js b/static/js/list.js new file mode 100644 index 0000000..9643514 --- /dev/null +++ b/static/js/list.js @@ -0,0 +1,30 @@ +function list() { + fetch(`${config.apiBase}/user/list`, + { + method: "GET", + credentials: "same-origin", + }) + .then(response => response.json()) + .then(data => { + if (data.status !== 200) { + alert(data.error); + console.log(data); + } else { + let sum = 0; + data.tasks.forEach(task => { + let row = document.getElementById("list").insertRow(); + row.insertCell().innerText = task.description; + row.insertCell().innerText = task.issuer; + row.insertCell().innerText = task.points; + sum += task.points; + }); + + document.getElementById("sum").innerText = sum; + } + }) + .catch(error => { + console.log(error); + }); +} + +list(); \ No newline at end of file diff --git a/static/js/utils.js b/static/js/utils.js new file mode 100644 index 0000000..007a6de --- /dev/null +++ b/static/js/utils.js @@ -0,0 +1,3 @@ +function redirect(path) { + window.location.href = path; +} \ No newline at end of file diff --git a/static/list/index.html b/static/list/index.html new file mode 100644 index 0000000..fc78005 --- /dev/null +++ b/static/list/index.html @@ -0,0 +1,34 @@ + + + + + + + List tasks + + + + + + + + Admin + +
Sum:
+ + + + + + + + + + + + +
DescriptionIssuerPoints
+ + + + \ No newline at end of file