diff --git a/pages/base.html b/pages/base.html index 77a3b62..7153e26 100644 --- a/pages/base.html +++ b/pages/base.html @@ -44,12 +44,13 @@ + diff --git a/scripts/api.js b/scripts/api.js new file mode 100644 index 0000000..9221728 --- /dev/null +++ b/scripts/api.js @@ -0,0 +1,59 @@ +/* + Copyright (c) 2023 Kasyanov Nikolay Alexeyevich (Unbewohnte) +*/ + + +async function post_new_todo(username, password, new_todo) { + return fetch("/api/todo", { + method: "POST", + headers: { + "EncryptedBase64": "false", + "Auth": username + "<-->" + password, + "Content-Type": "application/json", + }, + body: JSON.stringify(new_todo), + }); +} + + +async function get_todos(username, password) { + return fetch("/api/todo", { + method: "GET", + headers: { + "EncryptedBase64": "false", + "Auth": username + "<-->" + password + }, + }); +} + + +async function get_todo_groups(username, password) { + return fetch("/api/group", { + method: "GET", + headers: { + "EncryptedBase64": "false", + "Auth": username + "<-->" + password + }, + }); +} + +async function delete_todo(username, password, todo) { + return fetch("/api/todo", { + method: "DELETE", + headers: { + "EnctyptedBase64": "false", + "Auth": username + "<-->" + password, + body: JSON.stringify(todo), + }, + }); +} + +async function get_user(username, password) { + return fetch("/api/user", { + method: "GET", + headers: { + "EncryptedBase64": "false", + "Auth": username + "<-->" + password + }, + }); +} \ No newline at end of file diff --git a/src/db/db.go b/src/db/db.go index e02d842..8913865 100644 --- a/src/db/db.go +++ b/src/db/db.go @@ -40,6 +40,7 @@ func setUpTables(db *DB) error { id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE, group_id INTEGER NOT NULL, text TEXT NOT NULL, + time_created_unix INTEGER, due_unix INTEGER, owner_username TEXT NOT NULL, FOREIGN KEY(group_id) REFERENCES todo_groups(id), diff --git a/src/db/todo.go b/src/db/todo.go index 4b43d40..493825c 100644 --- a/src/db/todo.go +++ b/src/db/todo.go @@ -4,20 +4,20 @@ import "database/sql" // Todo group structure type TodoGroup struct { - ID uint64 `json: "id"` - Name string `json: "name"` - TimeCreatedUnix uint64 `json: "timeCreatedUnix` - OwnerUsername string `json: "ownerUsername` + ID uint64 `json:"id"` + Name string `json:"name"` + TimeCreatedUnix uint64 `json:"timeCreatedUnix"` + OwnerUsername string `json:"ownerUsername"` } // Todo structure type Todo struct { - ID uint64 `json: "id"` - GroupID uint64 `json: "groupId"` - Text string `json: "text"` - TimeCreatedUnix uint64 `json: "timeCreatedUnix"` - DueUnix uint64 `json: "dueUnix"` - OwnerUsername string `json: "ownerUsername"` + ID uint64 `json:"id"` + GroupID uint64 `json:"groupId"` + Text string `json:"text"` + TimeCreatedUnix uint64 `json:"timeCreatedUnix"` + DueUnix uint64 `json:"dueUnix"` + OwnerUsername string `json:"ownerUsername"` } // Creates a new TODO group in the database @@ -115,6 +115,7 @@ func scanTodo(rows *sql.Rows) (*Todo, error) { &newTodo.ID, &newTodo.GroupID, &newTodo.Text, + &newTodo.TimeCreatedUnix, &newTodo.DueUnix, &newTodo.OwnerUsername, ) @@ -169,9 +170,10 @@ func (db *DB) GetTodos() ([]*Todo, error) { // Creates a new TODO in the database func (db *DB) CreateTodo(todo Todo) error { _, err := db.Exec( - "INSERT INTO todos(group_id, text, due_unix, owner_username) VALUES(?, ?, ?, ?)", + "INSERT INTO todos(group_id, text, time_created_unix, due_unix, owner_username) VALUES(?, ?, ?, ?, ?)", todo.GroupID, todo.Text, + todo.TimeCreatedUnix, todo.DueUnix, todo.OwnerUsername, ) diff --git a/src/db/user.go b/src/db/user.go index 95ae1c0..5e3a22a 100644 --- a/src/db/user.go +++ b/src/db/user.go @@ -4,9 +4,9 @@ import "database/sql" // User structure type User struct { - Username string `json: "username"` - Password string `json: "password"` - TimeCreatedUnix uint64 `json: "timeCreatedUnix"` + Username string `json:"username"` + Password string `json:"password"` + TimeCreatedUnix uint64 `json:"timeCreatedUnix"` } func scanUser(rows *sql.Rows) (*User, error) { diff --git a/src/server/api.go b/src/server/api.go index eaa5193..58d0781 100644 --- a/src/server/api.go +++ b/src/server/api.go @@ -198,6 +198,7 @@ func (s *Server) TodoEndpoint(w http.ResponseWriter, req *http.Request) { err = s.db.CreateTodo(newTodo) if err != nil { http.Error(w, "Failed to create TODO", http.StatusInternalServerError) + logger.Error("[Server] Failed to put a new todo (%+v) into the db: %s", newTodo, err) return }