-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
130 lines (108 loc) · 2.91 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"database/sql"
"net/http"
"os"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
_ "github.com/mattn/go-sqlite3"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
var db *sql.DB
type Todo struct {
ID int `json:"id"`
UserID int `json:"user_id"`
Title string `json:"title"`
Completed bool `json:"completed"`
}
func main() {
// Initialize zerolog
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
// Initialize Echo
e := echo.New()
e.Use(middleware.Logger())
e.Use(middleware.Recover())
// Connect to SQLite database
var err error
db, err = sql.Open("sqlite3", "./todos.db")
if err != nil {
log.Fatal().Err(err).Msg("failed to connect to database")
}
defer db.Close()
// Create table if not exists
createTable()
// Routes
e.POST("/todos", createTodo)
e.GET("/todos", getTodos)
e.PUT("/todos/:id", updateTodo)
e.DELETE("/todos/:id", deleteTodo)
// Start server
e.Logger.Fatal(e.Start(":1323"))
}
func createTable() {
query := `
CREATE TABLE IF NOT EXISTS todos (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER,
title TEXT,
completed BOOLEAN
);
`
_, err := db.Exec(query)
if err != nil {
log.Fatal().Err(err).Msg("failed to create table")
}
}
func createTodo(c echo.Context) error {
todo := new(Todo)
if err := c.Bind(todo); err != nil {
return c.JSON(http.StatusBadRequest, err.Error())
}
query := "INSERT INTO todos (user_id, title, completed) VALUES (?, ?, ?)"
result, err := db.Exec(query, todo.UserID, todo.Title, todo.Completed)
if err != nil {
return c.JSON(http.StatusInternalServerError, err.Error())
}
id, _ := result.LastInsertId()
todo.ID = int(id)
return c.JSON(http.StatusCreated, todo)
}
func getTodos(c echo.Context) error {
rows, err := db.Query("SELECT id, user_id, title, completed FROM todos")
if err != nil {
return c.JSON(http.StatusInternalServerError, err.Error())
}
defer rows.Close()
todos := []Todo{}
for rows.Next() {
var todo Todo
if err := rows.Scan(&todo.ID, &todo.UserID, &todo.Title, &todo.Completed); err != nil {
return c.JSON(http.StatusInternalServerError, err.Error())
}
todos = append(todos, todo)
}
return c.JSON(http.StatusOK, todos)
}
func updateTodo(c echo.Context) error {
id := c.Param("id")
todo := new(Todo)
if err := c.Bind(todo); err != nil {
return c.JSON(http.StatusBadRequest, err.Error())
}
query := "UPDATE todos SET title = ?, completed = ? WHERE id = ?"
_, err := db.Exec(query, todo.Title, todo.Completed, id)
if err != nil {
return c.JSON(http.StatusInternalServerError, err.Error())
}
return c.JSON(http.StatusOK, todo)
}
func deleteTodo(c echo.Context) error {
id := c.Param("id")
query := "DELETE FROM todos WHERE id = ?"
_, err := db.Exec(query, id)
if err != nil {
return c.JSON(http.StatusInternalServerError, err.Error())
}
return c.NoContent(http.StatusNoContent)
}