-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
96 lines (88 loc) · 2.61 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
package main
import (
"encoding/json"
"os"
"coworkingapp/handlers"
"coworkingapp/middlewares"
"coworkingapp/models"
"coworkingapp/utils"
"github.com/gin-gonic/gin"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
var config models.CoworkingConfig
func init() {
data, err := os.ReadFile("config/config.json")
if err != nil {
panic(err)
}
if err := json.Unmarshal(data, &config); err != nil {
panic(err)
}
}
func main() {
gin.SetMode(gin.DebugMode)
db, err := gorm.Open(postgres.Open(config.Dsn))
if err != nil {
panic(err)
}
db.AutoMigrate(&models.User{})
db.AutoMigrate(&models.Room{})
db.AutoMigrate(&models.Photo{})
db.AutoMigrate(&models.Booking{})
seedData(db)
r := gin.Default()
r.Static("/imgs", "./imgs")
r.Use(middlewares.EarlyExitOnPreflightRequests())
r.Use(middlewares.SetCorsPolicy(config.AllowedOrigins))
r.Use(func(c *gin.Context) {
c.Set("DbKey", db)
c.Set("ConfigKey", config)
c.Next()
})
r.POST("/auth/login", handlers.Login)
r.POST("/auth/signup", handlers.Signup)
r.GET("/rooms", handlers.GetAllRooms)
r.GET("/rooms/:id", handlers.GetRoomById)
r.GET("/rooms/:id/photos", handlers.GetRoomPhotos)
r.GET("/bookings", middlewares.AuthorizeUser(), handlers.GetBookingsByUserId)
r.GET("/bookings/:id", middlewares.AuthorizeUser(), handlers.GetBookingById)
r.POST("/bookings", middlewares.AuthorizeUser(), handlers.AddBooking)
r.DELETE("/bookings/:id", middlewares.AuthorizeUser(), handlers.DeleteBooking)
if err := r.Run(":8080"); err != nil {
panic(err)
}
}
func seedData(db *gorm.DB) {
db.Delete(&models.Booking{}, "1 = 1")
db.Delete(&models.User{}, "1 = 1")
db.Delete(&models.Photo{}, "1 = 1")
db.Delete(&models.Room{}, "1 = 1")
userId := utils.GetUuid()
db.Create(&models.User{
ID: userId,
Email: "ipesenti@sorint.com",
Username: "ipesenti",
Password: "abcd1234!!",
})
db.Create([]*models.Room{
{
ID: utils.GetUuid(), Name: "Green", Cost: 12.50, NumberOfSeats: 4, Category: "Open Space", MainPhoto: "/green_0001.jpg", Photos: []models.Photo{
{Url: "/green_0002.jpg"},
{Url: "/green_0003.jpg"},
},
},
{
ID: utils.GetUuid(), Name: "Red", Cost: 100.00, NumberOfSeats: 50, Category: "Conference Hall", MainPhoto: "/red_0001.jpg", Photos: []models.Photo{
{Url: "/red_0002.jpg"},
},
},
{
ID: utils.GetUuid(), Name: "Yellow", Cost: 4.50, NumberOfSeats: 1, Category: "Shared Desk", MainPhoto: "/yellow_0001.jpg", Photos: []models.Photo{
{Url: "/yellow_0002.jpg"},
{Url: "/yellow_0003.jpg"},
{Url: "/yellow_0004.jpg"},
},
},
})
}