Skip to content

Commit 03e6fe5

Browse files
committed
first push
0 parents  commit 03e6fe5

23 files changed

+1310
-0
lines changed

.DS_Store

6 KB
Binary file not shown.

.gitignore

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
server/.env
2+
3+
# Binaries for programs and plugins
4+
*.exe
5+
*.exe~
6+
*.dll
7+
*.so
8+
*.dylib
9+
10+
# Test binary, built with `go test -c`
11+
*.test
12+
13+
# Output of the go coverage tool, specifically when used with LiteIDE
14+
*.out
15+
16+
# Dependency directories (remove the comment below to include it)
17+
# vendor/

server/.gitignore

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
*.exe
2+
*.exe~
3+
*.dll
4+
*.so
5+
*.dylib
6+
7+
# Test binary, built with `go test -c`
8+
*.test
9+
10+
# Output of the go coverage tool, specifically when used with LiteIDE
11+
*.out
12+
13+
# Dependency directories (remove the comment below to include it)
14+
# vendor/

server/config/config.go

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package config
2+
3+
import (
4+
"os"
5+
6+
"github.com/hashicorp/go-hclog"
7+
"github.com/joho/godotenv"
8+
)
9+
10+
var l = hclog.Default().Named("Config")
11+
12+
func verifyAllEnvVars() {
13+
envVars := []string{"MONGO_URL", "JWT_SECRET", "SENDGRID_API_KEY", "DOMAIN_NAME"}
14+
for _, envVar := range envVars {
15+
if os.Getenv(envVar) == "" {
16+
l.Error(("Missing env var: " + envVar))
17+
}
18+
}
19+
}
20+
21+
func init() {
22+
godotenv.Load(".env")
23+
verifyAllEnvVars()
24+
}
25+
26+
type Config struct {
27+
MongoUrl string
28+
JwtSecret []byte
29+
SmtpUsername string
30+
SmtpPassword string
31+
DomainName string
32+
}
33+
34+
func getConfig() *Config {
35+
jwtSecret := []byte("secret")
36+
mongoUrl := "mongodb://localhost:27017"
37+
38+
if os.Getenv("TESTING") != "" {
39+
goto ReturnConfig
40+
}
41+
42+
if os.Getenv("JWT_SECRET") != "" {
43+
jwtSecret = []byte(os.Getenv("JWT_SECRET"))
44+
}
45+
46+
if os.Getenv("MONGO_URL") != "" {
47+
mongoUrl = os.Getenv("MONGO_URL")
48+
}
49+
50+
ReturnConfig:
51+
52+
return &Config{
53+
MongoUrl: mongoUrl,
54+
JwtSecret: jwtSecret,
55+
SmtpUsername: os.Getenv("SMTP_USERNAME"),
56+
SmtpPassword: os.Getenv("SMTP_PASSWORD"),
57+
DomainName: os.Getenv("DOMAIN_NAME"),
58+
}
59+
}
60+
61+
func New() *Config {
62+
return getConfig()
63+
}
+154
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
package controllers
2+
3+
import (
4+
"context"
5+
"strconv"
6+
"time"
7+
8+
"github.com/TudorEsan/QPerior-Hackhaton/database"
9+
"github.com/TudorEsan/QPerior-Hackhaton/helpers"
10+
"github.com/TudorEsan/QPerior-Hackhaton/models"
11+
"github.com/gin-gonic/gin"
12+
"github.com/hashicorp/go-hclog"
13+
"go.mongodb.org/mongo-driver/bson"
14+
"go.mongodb.org/mongo-driver/bson/primitive"
15+
"go.mongodb.org/mongo-driver/mongo"
16+
)
17+
18+
type ReservationController struct {
19+
l hclog.Logger
20+
userCollection *mongo.Collection
21+
reservationCollection *mongo.Collection
22+
roomsCollection *mongo.Collection
23+
}
24+
25+
func NewReservationController(l hclog.Logger, mongoClient *mongo.Client) *ReservationController {
26+
userCollection := database.OpenCollection(mongoClient, "users")
27+
reservationCollection := database.OpenCollection(mongoClient, "reservations")
28+
roomCollection := database.OpenCollection(mongoClient, "rooms")
29+
return &ReservationController{l, userCollection, reservationCollection, roomCollection}
30+
}
31+
32+
func (controller *ReservationController) AddReservation() gin.HandlerFunc {
33+
return func(c *gin.Context) {
34+
var reservationForm models.ReservationForm
35+
if err := c.Bind(&reservationForm); err != nil {
36+
controller.l.Error("Could not bind reservation", err)
37+
c.JSON(400, gin.H{"message": err.Error()})
38+
return
39+
}
40+
41+
reservation := models.NewReservation(reservationForm)
42+
userId, _ := primitive.ObjectIDFromHex(c.MustGet("claims").(*models.SignedDetails).Id)
43+
reservation.UserId = userId
44+
45+
// validate room
46+
ctx, cancel := context.WithTimeout(context.Background(), time.Second*15)
47+
defer cancel()
48+
49+
var room models.Room
50+
err := controller.roomsCollection.FindOne(ctx, bson.M{"_id": reservation.RoomId}).Decode(&room)
51+
if err != nil {
52+
controller.l.Error("Could not get room", err)
53+
c.JSON(400, gin.H{"message": err.Error()})
54+
return
55+
}
56+
57+
reservation.Id = primitive.NewObjectID()
58+
59+
reservationFromThatDay, err := helpers.GetReservationsFromDate(controller.reservationCollection, reservation.From.Year(), int(reservation.From.Month()), reservation.From.Day())
60+
if err != nil {
61+
controller.l.Error("Could not get reservations from that day", err)
62+
c.JSON(400, gin.H{"message": err.Error()})
63+
return
64+
}
65+
66+
reservationAvalabile := helpers.IsReservationValid(reservation, reservationFromThatDay)
67+
if !reservationAvalabile {
68+
controller.l.Error("Reservation is not valid", err)
69+
c.JSON(400, gin.H{"message": "Reservation is not valid"})
70+
return
71+
}
72+
73+
_, err = controller.reservationCollection.InsertOne(ctx, reservation)
74+
if err != nil {
75+
controller.l.Error("Could not add reservation", err)
76+
c.JSON(400, gin.H{"message": err.Error()})
77+
return
78+
}
79+
80+
c.JSON(200, gin.H{"message": "Reservation added", "reservation": reservation})
81+
// get all reservations
82+
// return reservations
83+
}
84+
}
85+
86+
func (controller *ReservationController) GetReservations() gin.HandlerFunc {
87+
return func(c *gin.Context) {
88+
ctx, cancel := context.WithTimeout(context.Background(), time.Second*15)
89+
defer cancel()
90+
91+
var reservations []models.Reservation
92+
cursor, err := controller.reservationCollection.Find(ctx, bson.M{})
93+
if err != nil {
94+
controller.l.Error("Could not get reservations", err)
95+
c.JSON(400, gin.H{"message": err.Error()})
96+
return
97+
}
98+
99+
defer cursor.Close(ctx)
100+
for cursor.Next(ctx) {
101+
var reservation models.Reservation
102+
cursor.Decode(&reservation)
103+
reservations = append(reservations, reservation)
104+
}
105+
106+
c.JSON(200, reservations)
107+
}
108+
}
109+
110+
func (controller *ReservationController) FilterReservations() gin.HandlerFunc {
111+
return func(c *gin.Context) {
112+
var day, month, year int
113+
// get date from query
114+
day, _ = strconv.Atoi(c.Query("day"))
115+
month, _ = strconv.Atoi(c.Query("month"))
116+
year, _ = strconv.Atoi(c.Query("year"))
117+
118+
reservations, err := helpers.GetReservationsFromDate(controller.reservationCollection, year, month, day)
119+
if err != nil {
120+
controller.l.Error("Could not get reservations", err)
121+
c.JSON(400, gin.H{"message": err.Error()})
122+
return
123+
}
124+
125+
c.JSON(200, reservations)
126+
}
127+
}
128+
129+
func (controller *ReservationController) GetUserReservations() gin.HandlerFunc {
130+
return func(c *gin.Context) {
131+
ctx, cancel := context.WithTimeout(context.Background(), time.Second*15)
132+
defer cancel()
133+
134+
claims := c.MustGet("claims").(models.SignedDetails)
135+
idS := claims.Id
136+
137+
userId, err := primitive.ObjectIDFromHex(idS)
138+
if err != nil {
139+
controller.l.Error("Could not get user id", err)
140+
c.JSON(400, gin.H{"message": err.Error()})
141+
return
142+
}
143+
144+
var reservation models.Reservation
145+
err = controller.reservationCollection.FindOne(ctx, bson.M{"userId": userId}).Decode(&reservation)
146+
if err != nil {
147+
controller.l.Error("Could not get reservation", err)
148+
c.JSON(400, gin.H{"message": err.Error()})
149+
return
150+
}
151+
152+
c.JSON(200, reservation)
153+
}
154+
}

0 commit comments

Comments
 (0)