Commit 8b20d21c authored by Max Richter's avatar Max Richter
Browse files

fix(mongo): finally fix nil reference error in room repository

parent 73fdb3b7
Pipeline #1773 passed with stages
in 1 minute and 30 seconds
package mongo
import (
"log"
"time"
repos "git.coco.study/fvitt/good2go/database/mongo/repositories"
......@@ -10,7 +9,7 @@ import (
var (
// RoomRepository does crud stuff
roomRepo = repos.RoomRepository
RoomRepo *repos.RoomRepository
)
// Init initializes connection to a database
......@@ -24,9 +23,9 @@ func Init() {
// Connect to a specified database
db := client.Database(utils.GetDBName())
// Connect the repositories
repos.RoomRepository.ConnectTo(db.Collection("rooms"))
log.Print("All rooms", roomRepo.GetAllRooms())
// Create the repositories
RoomRepo = &repos.RoomRepository{
Col: db.Collection("rooms"),
}
}
......@@ -13,22 +13,17 @@ import (
"go.mongodb.org/mongo-driver/mongo"
)
type roomRepository struct {
c *mongo.Collection
type RoomRepository struct {
Col *mongo.Collection
}
var (
// RoomRepository CRID
RoomRepository = &roomRepository{}
)
func (repo roomRepository) findByRoomNumber(roomNumber int) (*model.Room, error) {
func (repo *RoomRepository) findByRoomNumber(roomNumber int) (*model.Room, error) {
ctx, cancel := utils.CreateTimoutCTX(5 * time.Second)
ctx, cancel := utils.CreateTimeoutCTX(5 * time.Second)
defer cancel()
// Find
result, err := repo.c.Find(ctx, bson.M{"number": roomNumber})
result, err := repo.Col.Find(ctx, bson.M{"number": roomNumber})
if err != nil {
return &model.Room{}, err
}
......@@ -44,13 +39,13 @@ func (repo roomRepository) findByRoomNumber(roomNumber int) (*model.Room, error)
}
func (repo roomRepository) findByID(id primitive.ObjectID) (*model.Room, error) {
func (repo *RoomRepository) findByID(id primitive.ObjectID) (*model.Room, error) {
ctx, cancel := utils.CreateTimoutCTX(5 * time.Second)
ctx, cancel := utils.CreateTimeoutCTX(5 * time.Second)
defer cancel()
// Find
result, err := repo.c.Find(ctx, bson.M{"_id": id})
result, err := repo.Col.Find(ctx, bson.M{"_id": id})
if err != nil {
return &model.Room{}, err
}
......@@ -65,13 +60,13 @@ func (repo roomRepository) findByID(id primitive.ObjectID) (*model.Room, error)
return &r, nil
}
func (repo roomRepository) insertOne(document interface{}) (*model.Room, error) {
func (repo *RoomRepository) insertOne(document interface{}) (*model.Room, error) {
ctx, cancel := utils.CreateTimoutCTX(5 * time.Second)
ctx, cancel := utils.CreateTimeoutCTX(5 * time.Second)
defer cancel()
// Insert
result, err := repo.c.InsertOne(ctx, document)
result, err := repo.Col.InsertOne(ctx, document)
if err != nil {
return &model.Room{}, err
}
......@@ -80,36 +75,36 @@ func (repo roomRepository) insertOne(document interface{}) (*model.Room, error)
return repo.findByID(result.InsertedID.(primitive.ObjectID))
}
func (repo roomRepository) updateOne(document interface{}) {
func (repo *RoomRepository) updateOne(document interface{}) {
ctx, cancel := utils.CreateTimoutCTX(5 * time.Second)
ctx, cancel := utils.CreateTimeoutCTX(5 * time.Second)
defer cancel()
repo.c.UpdateOne(ctx, document, document)
repo.Col.UpdateOne(ctx, document, document)
}
func (repo roomRepository) findOne(document interface{}) (*model.Room, error) {
func (repo *RoomRepository) findOne(document interface{}) (*model.Room, error) {
ctx, cancel := utils.CreateTimoutCTX(5 * time.Second)
ctx, cancel := utils.CreateTimeoutCTX(5 * time.Second)
defer cancel()
result := repo.c.FindOne(ctx, document)
result := repo.Col.FindOne(ctx, document)
var room model.Room
err := result.Decode(&room)
return &room, err
}
func (repo roomRepository) deleteByID(id primitive.ObjectID) error {
func (repo *RoomRepository) deleteByID(id primitive.ObjectID) error {
ctx, cancel := utils.CreateTimoutCTX(5 * time.Second)
ctx, cancel := utils.CreateTimeoutCTX(5 * time.Second)
defer cancel()
_, error := repo.c.DeleteOne(ctx, bson.M{"_id": id})
_, error := repo.Col.DeleteOne(ctx, bson.M{"_id": id})
return error
}
func (repo roomRepository) CreateRoom(r *model.Room) (*model.Room, error) {
func (repo *RoomRepository) CreateRoom(r *model.Room) (*model.Room, error) {
log.Print("Create room ", r.Number)
......@@ -135,21 +130,21 @@ func (repo roomRepository) CreateRoom(r *model.Room) (*model.Room, error) {
return room, err
}
func (repo roomRepository) UpdateRoom(r *model.Room) {
func (repo *RoomRepository) UpdateRoom(r *model.Room) {
repo.updateOne(r)
}
func (repo roomRepository) GetRoom(document interface{}) (room *model.Room, err error) {
func (repo *RoomRepository) GetRoom(document interface{}) (room *model.Room, err error) {
log.Print("Finding room by ", document)
return repo.findOne(document)
}
func (repo roomRepository) GetAllRooms() []model.Room {
func (repo *RoomRepository) GetAllRooms() []model.Room {
ctx, cancel := utils.CreateTimoutCTX(5 * time.Second)
ctx, cancel := utils.CreateTimeoutCTX(5 * time.Second)
defer cancel()
cursor, err := repo.c.Find(ctx, bson.M{})
cursor, err := repo.Col.Find(ctx, bson.M{})
if err != nil {
log.Fatal("[MONGODB] roomRepository", err)
}
......@@ -162,7 +157,7 @@ func (repo roomRepository) GetAllRooms() []model.Room {
return rooms
}
func (repo roomRepository) ConnectTo(c *mongo.Collection) *roomRepository {
repo.c = c
func (repo RoomRepository) ConnectTo(c *mongo.Collection) *RoomRepository {
repo.Col = c
return &repo
}
......@@ -8,12 +8,12 @@ import (
"go.mongodb.org/mongo-driver/mongo"
)
var repo *roomRepository
var repo *RoomRepository
func TestMain(m *testing.M) {
// Mock the collection
repo = roomRepository{}.ConnectTo(&mongo.Collection{})
repo = RoomRepository{}.ConnectTo(&mongo.Collection{})
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment