-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
157 lines (135 loc) · 3.45 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"fmt"
"log"
"net/http"
"os"
"github.com/bharatkalra244/car-app/models"
"github.com/bharatkalra244/car-app/storage"
"github.com/gofiber/fiber/v2"
"github.com/joho/godotenv"
"gorm.io/gorm"
)
type Car struct {
CAR_NAME string `json:"carName"`
CAR_HP uint `json:"carHp"`
CAR_COMPANY string `json:"carCompany"`
CAR_ENGINE string `json:"carEngine"`
// CarID int64 `json:"carID"`
}
type Repository struct {
DB *gorm.DB
}
// Method created for Repository due to the presence of (r *Repository)
func (r *Repository) CreateCar(context *fiber.Ctx) error {
car := Car{}
//Work of body parser is to parse through the json body that we are getting in the request
err := context.BodyParser(&car)
if err != nil {
context.Status(http.StatusUnprocessableEntity).JSON(
&fiber.Map{"message": "request failed"})
return err
}
err = r.DB.Create(&car).Error
if err != nil {
context.Status(http.StatusBadRequest).JSON(
&fiber.Map{"message": "could not create car"})
return err
}
context.Status(http.StatusOK).JSON(
&fiber.Map{"message": "Car has been added"})
return nil
}
func (r *Repository) DeleteCar(context *fiber.Ctx) error {
carModel := models.Cars{}
id := context.Params("id")
if id == "" {
context.Status(http.StatusInternalServerError).JSON(&fiber.Map{
"message": "id cannot be empty",
})
return nil
}
err := r.DB.Delete(carModel, id)
if err.Error != nil {
context.Status(http.StatusBadRequest).JSON(&fiber.Map{
"message": "could not delete book",
})
return err.Error
}
context.Status(http.StatusOK).JSON(&fiber.Map{
"message": "car deleted sucessfully",
})
return nil
}
func (r *Repository) GetCars(context *fiber.Ctx) error {
carModels := &[]models.Cars{}
err := r.DB.Find(carModels).Error
if err != nil {
context.Status(http.StatusBadRequest).JSON(
&fiber.Map{"message": "could not get cars"})
return err
}
context.Status(http.StatusOK).JSON(
&fiber.Map{
"message": "cars fetched successfully",
"data": carModels,
})
return nil
}
func (r *Repository) GetCarsByID(context *fiber.Ctx) error {
id := context.Params("id")
carModel := &models.Cars{}
if id == "" {
context.Status(http.StatusInternalServerError).JSON(&fiber.Map{
"message": "id cannot be empty",
})
return nil
}
fmt.Println("The ID is ", id)
err := r.DB.Where("id = ?", id).First(carModel).Error
if err != nil {
context.Status(http.StatusBadRequest).JSON(
&fiber.Map{"message": "could not get the car"})
return err
}
context.Status(http.StatusOK).JSON(&fiber.Map{
"message": "Car ID fetched sucessfully",
"data": carModel,
})
return nil
}
func (r *Repository) SetupRoutes(app *fiber.App) {
api := app.Group("/api")
api.Post("/create_cars", r.CreateCar)
api.Delete("/delete_car/:id", r.DeleteCar)
api.Get("/get_cars/:id", r.GetCarsByID)
api.Get("/cars", r.GetCars)
}
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_PASS"),
User: os.Getenv("DB_USER"),
SSLMode: os.Getenv("DB_SSLMODE"),
DBName: os.Getenv("DB_NAME"),
}
db, err := storage.NewConnection(config)
if err != nil {
log.Fatal(err, "Could Not Load The Database !")
}
err = models.MigrateCars(db)
if err != nil {
log.Fatal("Could not migrate the database")
}
r := Repository{
DB: db,
}
app := fiber.New()
r.SetupRoutes(app)
app.Listen(":8080")
}