room_repository.go 4.25 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
}

Max Richter's avatar
Max Richter committed
47
48
49
50
51
52
func (repo *Repository) findByID(roomID string) (*model.Room, error) {

	id, err := primitive.ObjectIDFromHex(roomID)
	if err != nil {
		return nil, err
	}
53

54
	ctx, cancel := utils.CreateTimeoutCTX(5 * time.Second)
55
56
57
	defer cancel()

	// Find
58
	result, err := repo.Col.Find(ctx, bson.M{"_id": id})
59
60
61
62
63
64
65
66
67
68
69
70
	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
71
72
}

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

75
	ctx, cancel := utils.CreateTimeoutCTX(5 * time.Second)
76
77
78
	defer cancel()

	// Insert
Max Richter's avatar
Max Richter committed
79
80
81
82
83
84
85
	insertResult, err := repo.Col.InsertOne(ctx, document)
	if err != nil {
		return &model.Room{}, err
	}

	// Find
	result, err := repo.Col.Find(ctx, bson.M{"_id": insertResult.InsertedID})
86
87
88
89
	if err != nil {
		return &model.Room{}, err
	}

Max Richter's avatar
Max Richter committed
90
91
92
93
94
95
96
	// Decode
	var r model.Room
	err = result.Decode(&r)
	if err != nil {
		return nil, err
	}

97
	// Return created entity
Max Richter's avatar
Max Richter committed
98
	return &r, err
99
100
}

Max Richter's avatar
Max Richter committed
101
func (repo *Repository) updateOne(document interface{}) (*model.Room, error) {
102

103
	ctx, cancel := utils.CreateTimeoutCTX(5 * time.Second)
104
105
	defer cancel()

Max Richter's avatar
Max Richter committed
106
	insertedResult, err := repo.Col.UpdateOne(ctx, document, document)
Enrico Bollen's avatar
Enrico Bollen committed
107
108
109
	if err != nil {
		fmt.Println(err)
	}
Max Richter's avatar
Max Richter committed
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126

	// Find
	result, err := repo.Col.Find(ctx, bson.M{"_id": insertedResult.UpsertedID})
	if err != nil {
		return &model.Room{}, err
	}

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

	// Return created entity
	return &r, err

127
128
}

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

131
	ctx, cancel := utils.CreateTimeoutCTX(5 * time.Second)
132
133
	defer cancel()

134
	result := repo.Col.FindOne(ctx, document)
135
136
137
138
139
	var room model.Room
	err := result.Decode(&room)
	return &room, err
}

140
func (repo *Repository) deleteByID(id primitive.ObjectID) error {
141

142
	ctx, cancel := utils.CreateTimeoutCTX(5 * time.Second)
143
144
	defer cancel()

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

Enrico Bollen's avatar
Enrico Bollen committed
147
	return err
148
149
}

Max Richter's avatar
Max Richter committed
150
151
152
153
func (repo *Repository) GetRoomByID(id string) (*model.Room, error) {
	return repo.findByID(id)
}

154
func (repo *Repository) CreateRoom(r *model.Room) (*model.Room, error) {
155
156
157
158
159
160
161
162
163
164
165
166
167
168

	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,
	})
169
170
171

	if err != nil {
		log.Fatal(err)
172
173
174
175
176
177
178
179
		return &model.Room{}, err
	}

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

	return room, err
}

Max Richter's avatar
Max Richter committed
180
181
func (repo *Repository) UpdateRoom(r *model.Room) (*model.Room, error) {
	return repo.updateOne(r)
182
183
}

184
func (repo *Repository) GetRoom(document interface{}) (room *model.Room, err error) {
185
186
187
188
	log.Print("Finding room by ", document)
	return repo.findOne(document)
}

Max Richter's avatar
Max Richter committed
189
190
191
192
func (repo *Repository) DeleteRoom(roomID string) (room *model.Room, err error) {
	return repo.findByID(roomID)
}

Can Maihofer's avatar
Can Maihofer committed
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
func (repo *Repository) GetAllRooms() []*model.Room {

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

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

	var rooms []*model.Room
	if err = cursor.All(ctx, &rooms); err != nil {
		log.Fatal(err)
	}

	return rooms
}