room_repository.go 3.37 KB
Newer Older
1
2
3
package mongo

import (
4
	"errors"
5
	"log"
6
7
	"strconv"
	"time"
8

9
	utils "git.coco.study/fvitt/good2go/database/mongo/utils"
10
	"git.coco.study/fvitt/good2go/internal/model"
11
12
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/bson/primitive"
13
14
15
	"go.mongodb.org/mongo-driver/mongo"
)

16
17
type RoomRepository struct {
	Col *mongo.Collection
18
19
}

20
func (repo *RoomRepository) findByRoomNumber(roomNumber int) (*model.Room, error) {
21

22
	ctx, cancel := utils.CreateTimeoutCTX(5 * time.Second)
23
24
25
	defer cancel()

	// Find
26
	result, err := repo.Col.Find(ctx, bson.M{"number": roomNumber})
27
28
29
30
31
32
33
34
35
36
37
38
39
	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

40
41
}

42
func (repo *RoomRepository) findByID(id primitive.ObjectID) (*model.Room, error) {
43

44
	ctx, cancel := utils.CreateTimeoutCTX(5 * time.Second)
45
46
47
	defer cancel()

	// Find
48
	result, err := repo.Col.Find(ctx, bson.M{"_id": id})
49
50
51
52
53
54
55
56
57
58
59
60
	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
61
62
}

63
func (repo *RoomRepository) insertOne(document interface{}) (*model.Room, error) {
64

65
	ctx, cancel := utils.CreateTimeoutCTX(5 * time.Second)
66
67
68
	defer cancel()

	// Insert
69
	result, err := repo.Col.InsertOne(ctx, document)
70
71
72
73
74
75
	if err != nil {
		return &model.Room{}, err
	}

	// Return created entity
	return repo.findByID(result.InsertedID.(primitive.ObjectID))
76
77
}

78
func (repo *RoomRepository) updateOne(document interface{}) {
79

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

83
	repo.Col.UpdateOne(ctx, document, document)
84
85
}

86
func (repo *RoomRepository) findOne(document interface{}) (*model.Room, error) {
87

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

91
	result := repo.Col.FindOne(ctx, document)
92
93
94
95
96
	var room model.Room
	err := result.Decode(&room)
	return &room, err
}

97
func (repo *RoomRepository) deleteByID(id primitive.ObjectID) error {
98

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

102
	_, error := repo.Col.DeleteOne(ctx, bson.M{"_id": id})
103
104
105
106

	return error
}

107
func (repo *RoomRepository) CreateRoom(r *model.Room) (*model.Room, error) {
108
109
110
111
112
113
114
115
116
117
118
119
120
121

	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,
	})
122
123
124

	if err != nil {
		log.Fatal(err)
125
126
127
128
129
130
131
132
		return &model.Room{}, err
	}

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

	return room, err
}

133
func (repo *RoomRepository) UpdateRoom(r *model.Room) {
134
135
136
	repo.updateOne(r)
}

137
func (repo *RoomRepository) GetRoom(document interface{}) (room *model.Room, err error) {
138
139
140
141
	log.Print("Finding room by ", document)
	return repo.findOne(document)
}

142
func (repo *RoomRepository) GetAllRooms() []model.Room {
143

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

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

	var rooms []model.Room
153
	if err = cursor.All(ctx, &rooms); err != nil {
154
155
156
157
158
159
		log.Fatal(err)
	}

	return rooms
}

160
161
func (repo RoomRepository) ConnectTo(c *mongo.Collection) *RoomRepository {
	repo.Col = c
162
	return &repo
163
}