/
room_store.go
88 lines (66 loc) · 2.19 KB
/
room_store.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
package db
import (
"context"
"log"
customtypes "github.com/yannis94/hotel-reservation/custom_types"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
)
type RoomStore interface {
Dropper
GetRoomByID(context.Context, string) (*customtypes.Room, error)
GetRooms(context.Context, bson.M) ([]*customtypes.Room, error)
InsertRoom(context.Context, *customtypes.Room) (*customtypes.Room, error)
UpdateRoom(context.Context, bson.M, customtypes.UpdateRoomParams) error
DeleteRoom(context.Context, string) error
}
type MongoRoomStore struct {
client *mongo.Client
coll *mongo.Collection
HotelStore
}
func NewMongoRoomStore(client *mongo.Client, hotelStore HotelStore) *MongoRoomStore {
return &MongoRoomStore{
client: client,
coll: client.Database(DbName).Collection(roomColl),
HotelStore: hotelStore,
}
}
func (db *MongoRoomStore) InsertRoom(ctx context.Context, room *customtypes.Room) (*customtypes.Room, error) {
res, err := db.coll.InsertOne(ctx, room)
if err != nil {
return nil, err
}
room.ID = res.InsertedID.(primitive.ObjectID)
filter := bson.M{ "_id": room.HotelID }
update := bson.M{ "$push": bson.M{ "rooms": room.ID } }
if err := db.UpdateHotel(ctx, filter, update); err != nil {
log.Println(err)
return nil, err
}
return room, nil
}
func (db *MongoRoomStore) GetRoomByID(ctx context.Context, id string) (*customtypes.Room, error) {
return nil, nil
}
func (db *MongoRoomStore) GetRooms(ctx context.Context, filter bson.M) ([]*customtypes.Room, error) {
var rooms []*customtypes.Room
res, err := db.coll.Find(ctx, filter)
if err != nil {
return rooms, err
}
if err := res.All(ctx, &rooms); err != nil {
return rooms, err
}
return rooms, nil
}
func (db *MongoRoomStore) UpdateRoom(ctx context.Context, filter bson.M, values customtypes.UpdateRoomParams) error {
return nil
}
func (db *MongoRoomStore) DeleteRoom(ctx context.Context, id string) error {
return nil
}
func (db *MongoRoomStore) Drop(ctx context.Context) error {
return db.coll.Drop(ctx)
}