-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
133 lines (112 loc) · 2.9 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
131
132
133
package main
import (
"github.com/gofiber/fiber/v2"
"github.com/joho/godotenv"
"github.com/pianisimo/go-postgresql/models"
"github.com/pianisimo/go-postgresql/storage"
"gorm.io/gorm"
"log"
"net/http"
"os"
)
type Book struct {
Author string `json:"author"`
Title string `json:"title"`
Publisher string `json:"publisher"`
}
type Repository struct {
Db *gorm.DB
}
func (r *Repository) SetupRoutes(app *fiber.App) {
api := app.Group("/api")
api.Post("/create_books", r.CreateBook)
api.Delete("/delete_book/:id", r.DeleteBook)
api.Get("/get_book/:id", r.GetBookById)
api.Get("/books/", r.GetBooks)
}
func (r *Repository) CreateBook(ctx *fiber.Ctx) error {
book := &Book{}
err := ctx.BodyParser(book)
if err != nil {
ctx.Status(http.StatusUnprocessableEntity).JSON(&fiber.Map{"message": "request failed"})
return err
}
err = r.Db.Create(book).Error
if err != nil {
ctx.Status(http.StatusInternalServerError).JSON(&fiber.Map{"message": "could not save book"})
return err
}
ctx.Status(http.StatusOK).JSON(&fiber.Map{"message": "book created"})
return nil
}
func (r *Repository) DeleteBook(ctx *fiber.Ctx) error {
book := &models.Book{}
id := ctx.Params("id")
if id == "" {
ctx.Status(http.StatusInternalServerError).JSON(&fiber.Map{"message": "id can not be empty"})
return nil
}
tx := r.Db.Delete(book, id)
if tx.Error != nil {
ctx.Status(http.StatusInternalServerError).JSON(&fiber.Map{"message": "could not delete book by id"})
return tx.Error
}
return nil
}
func (r *Repository) GetBookById(ctx *fiber.Ctx) error {
book := &models.Book{}
id := ctx.Params("id")
if id == "" {
ctx.Status(http.StatusInternalServerError).JSON(&fiber.Map{"message": "id can not be empty"})
return nil
}
err := r.Db.Where("id = ?", id).First(book).Error
if err != nil {
ctx.Status(http.StatusNotFound).JSON(&fiber.Map{"message": "book not found"})
return err
}
ctx.Status(http.StatusNotFound).JSON(&fiber.Map{"data": book})
return nil
}
func (r *Repository) GetBooks(ctx *fiber.Ctx) error {
books := &[]models.Book{}
err := r.Db.Find(books).Error
if err != nil {
ctx.Status(http.StatusBadRequest).JSON(&fiber.Map{"message": "could not get all books"})
return err
}
ctx.Status(http.StatusOK).JSON(&fiber.Map{"data": books})
return nil
}
func main() {
err := godotenv.Load(".env")
if err != nil {
log.Fatal(err)
}
config := storage.Config{
Host: os.Getenv("DB_HOST"),
Port: os.Getenv("DB_PORT"),
Password: os.Getenv("DB_PASSWORD"),
User: os.Getenv("DB_USER"),
DbName: os.Getenv("DB_NAME"),
SSLMode: os.Getenv("DB_SSL"),
}
db, err := storage.NewConnection(config)
if err != nil {
log.Fatal("could not load the database")
}
err = models.MigrateBooks(db)
if err != nil {
log.Fatal("could not migrate db")
}
r := Repository{
Db: db,
}
app := fiber.New()
r.SetupRoutes(app)
err = app.Listen(":8080")
if err != nil {
log.Fatal(err)
return
}
}