master
Lukas Vogl 1 year ago
commit 117e676cc7

@ -0,0 +1,3 @@
module todo
go 1.20

@ -0,0 +1,5 @@
package main
func main() {
}

@ -0,0 +1,72 @@
package config
import (
"database/sql"
"errors"
"fmt"
"log"
"os"
)
func Database() *sql.DB {
logger, _ := thoth.Init("log")
user, exist := os.LookupEnv("DB_USER")
if !exist {
logger.Log(errors.New("DB_USER not set in .env"))
log.Fatal("DB_USER not set in .env")
}
pass, exist := os.LookupEnv("DB_PASS")
if !exist {
logger.Log(errors.New("DB_PASS not set in .env"))
log.Fatal("DB_PASS not set in .env")
}
host, exist := os.LookupEnv("DB_HOST")
if !exist {
logger.Log(errors.New("DB_HOST not set in .env"))
log.Fatal("DB_HOST not set in .env")
}
credentials := fmt.Sprintf("%s:%s@(%s:3306)/?charset=utf8&parseTime=True", user, pass, host)
database, err := sql.Open("mysql", credentials)
if err != nil {
logger.Log(err)
log.Fatal(err)
} else {
fmt.Println("Database Connection Successful")
}
_, err = database.Exec(`CREATE DATABASE gotodo`)
if err != nil {
fmt.Println(err)
}
_, err = database.Exec(`USE gotodo`)
if err != nil {
fmt.Println(err)
}
_, err = database.Exec(`
CREATE TABLE todos (
id INT AUTO_INCREMENT,
item TEXT NOT NULL,
completed BOOLEAN DEFAULT FALSE,
PRIMARY KEY (id)
);
`)
if err != nil {
fmt.Println(err)
}
return database
}

@ -0,0 +1 @@
package controllers

@ -0,0 +1,69 @@
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.0/css/bootstrap.min.css"
integrity="sha384-SI27wrMjH3ZZ89r4o+fGIJtnzkAnFs3E4qz9DIYioCQ5l9Rd/7UAa8DHcaL8jkWt" crossorigin="anonymous">
<title>Todo</title>
</head>
<body>
<div style="text-align: center;"><h1>Todos</h1></div>
<div class="container">
<div class="card">
<div class="card-header">Add a task</div>
<div class="card-body">
<form action="/add" method="post">
<div class="form-group">
<label for="Item">Item</label>
<input type="text" class="form-control" name="item" placeholder="What do you want to do?">
<small class="form-text text-muted">Enter what you want to procastinate 🙂</small>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
</div>
&nbsp;
&nbsp;
<div class="container">
<div class="card">
<div class="card-header">Tasks</div>
<div class="card-body">
<table class="table table-borderless">
<thead>
<tr>
<th scope="col">Item</th>
<th scope="col">Status</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
{{range .Todos}}
{{if .Completed}}
<tr>
<td><strike>{{.Item}}</strike></td>
<td>Completed</td>
<td>
<button class="btn btn-primary" disabled>Complete</button>
<button class="btn btn-danger" disabled>Delete</button>
</td>
</tr>
{{else}}
<tr>
<td>{{.Item}}</td>
<td>Not Completed</td>
<td>
<a href="/complete/{{ .Id}}"><button class="btn btn-primary">Complete</button></a>
<a href="/delete/{{ .Id}}"><button class="btn btn-danger">Delete</button></a>
</td>
</tr>
{{end}}
{{end}}
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>
Loading…
Cancel
Save