room_repository.go 3.46 KB
Newer Older
1
package repositories
2
3

import (
4
	"errors"
Enrico Bollen's avatar
Enrico Bollen committed
5
	"fmt"
6
	"log"
7
8
	"strconv"
	"time"
9

Enrico Bollen's avatar
Enrico Bollen committed
10
	"git.coco.study/fvitt/good2go/database/mongo/utils"
11
	"git.coco.study/fvitt/good2go/internal/model"
12
13
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/bson/primitive"
14
15
16
	"go.mongodb.org/mongo-driver/mongo"
)

17
18
19
20
21
22
type RoomRepository interface {
	CreateRoom(r *model.Room) (*model.Room, error)
	UpdateRoom(r *model.Room)
	GetRoom(document interface{}) (room *model.Room, err error)
	GetAllRooms() []model.Room
	ConnectTo(c *mongo.Collection) *Repository
23
24
}

25
func (repo *Repository) findByRoomNumber(roomNumber int) (*model.Room, error) {
26

27
	ctx, cancel := utils.CreateTimeoutCTX(5 * time.Second)
28
29
30
	defer cancel()

	// Find
31
	result, err := repo.Col.Find(ctx, bson.M{"number": roomNumber})
32
33
34
35
36
37
38
39
40
41
42
43
44
	if err != nil {
		return &model.Room{}, err
	}

	// Decode
	var r model.Room
	err = result.Decode(&r)
	if err != nil {
		return &model.Room{}, err
	}

	return &r, nil

45
46
}

47
func (repo *Repository) findByID(id primitive.ObjectID) (*model.Room, error) {
48

49
	ctx, cancel := utils.CreateTimeoutCTX(5 * time.Second)
50
51
52
	defer cancel()

	// Find
53
	result, err := repo.Col.Find(ctx, bson.M{"_id": id})
54
55
56
57
58
59
60
61
62
63
64
65
	if err != nil {
		return &model.Room{}, err
	}

	// Decode
	var r model.Room
	err = result.Decode(&r)
	if err != nil {
		return &model.Room{}, err
	}

	return &r, nil
66
67
}

68
func (repo *Repository) insertOne(document interface{}) (*model.Room, error) {
69

70
	ctx, cancel := utils.CreateTimeoutCTX(5 * time.Second)
71
72
73
	defer cancel()

	// Insert
74
	result, err := repo.Col.InsertOne(ctx, document)
75
76
77
78
79
80
	if err != nil {
		return &model.Room{}, err
	}

	// Return created entity
	return repo.findByID(result.InsertedID.(primitive.ObjectID))
81
82
}

83
func (repo *Repository) updateOne(document interface{}) {
84

85
	ctx, cancel := utils.CreateTimeoutCTX(5 * time.Second)
86
87
	defer cancel()

Enrico Bollen's avatar
Enrico Bollen committed
88
89
90
91
	_, err := repo.Col.UpdateOne(ctx, document, document)
	if err != nil {
		fmt.Println(err)
	}
92
93
}

94
func (repo *Repository) findOne(document interface{}) (*model.Room, error) {
95

96
	ctx, cancel := utils.CreateTimeoutCTX(5 * time.Second)
97
98
	defer cancel()

99
	result := repo.Col.FindOne(ctx, document)
100
101
102
103
104
	var room model.Room
	err := result.Decode(&room)
	return &room, err
}

105
func (repo *Repository) deleteByID(id primitive.ObjectID) error {
106

107
	ctx, cancel := utils.CreateTimeoutCTX(5 * time.Second)
108
109
	defer cancel()

Enrico Bollen's avatar
Enrico Bollen committed
110
	_, err := repo.Col.DeleteOne(ctx, bson.M{"_id": id})
111

Enrico Bollen's avatar
Enrico Bollen committed
112
	return err
113
114
}

115
func (repo *Repository) CreateRoom(r *model.Room) (*model.Room, error) {
116
117
118
119
120
121
122
123
124
125
126
127
128
129

	log.Print("Create room ", r.Number)

	//Check if room with same room number exists
	existingRoom, err := repo.GetRoom(bson.M{"number": r.Number})
	if err == nil {
		t := strconv.Itoa(r.Number)
		return existingRoom, errors.New("Room with number " + t + " already exists")
	}

	room, err := repo.insertOne(bson.M{
		"number":   r.Number,
		"capacity": r.Capacity,
	})
130
131
132

	if err != nil {
		log.Fatal(err)
133
134
135
136
137
138
139
140
		return &model.Room{}, err
	}

	log.Print("created new room", room.Number)

	return room, err
}

141
func (repo *Repository) UpdateRoom(r *model.Room) {
142
143
144
	repo.updateOne(r)
}

145
func (repo *Repository) GetRoom(document interface{}) (room *model.Room, err error) {
146
147
148
149
	log.Print("Finding room by ", document)
	return repo.findOne(document)
}

150
func (repo *Repository) GetAllRooms() []model.Room {
151

152
	ctx, cancel := utils.CreateTimeoutCTX(5 * time.Second)
153
154
	defer cancel()

155
	cursor, err := repo.Col.Find(ctx, bson.M{})
156
157
	if err != nil {
		log.Fatal("[MONGODB] roomRepository", err)
158
159
160
	}

	var rooms []model.Room
161
	if err = cursor.All(ctx, &rooms); err != nil {
162
163
164
165
166
		log.Fatal(err)
	}

	return rooms
}