Commit f4e7774a authored by Max Richter's avatar Max Richter
Browse files

Merge branch 'dev' into 'master'

Dev

See merge request !26
parents 4d58cbef 86218e52
Pipeline #1775 passed with stages
in 3 minutes and 4 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{})
}
......
......@@ -2,9 +2,7 @@ package utils
import (
"context"
"flag"
"log"
"os"
"strings"
"time"
......@@ -12,37 +10,8 @@ import (
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
// Autoloads .env file
godotenv "github.com/joho/godotenv"
)
// GetEnv gets a env variable, and when not present returns the default value
func GetEnv(variableName string, defaultValue string) string {
godotenv.Load(".env")
envValue := os.Getenv(variableName)
if envValue == "" {
return defaultValue
}
return envValue
}
// GetDBName gets the db name from the environment
func GetDBName() string {
dbName := GetEnv("MONGO_DB", "good2go")
if flag.Lookup("test.v") == nil {
return dbName
}
return dbName + "_test"
}
// CreateDBClient creates a mongodb client and connects to the specified database
func CreateDBClient(timeout time.Duration) (client *mongo.Client, ctx context.Context) {
......@@ -53,7 +22,7 @@ func CreateDBClient(timeout time.Duration) (client *mongo.Client, ctx context.Co
log.Fatal(err)
}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
ctx, cancel := CreateTimeoutCTX(timeout)
defer cancel()
err = client.Connect(ctx)
if err != nil {
......@@ -79,6 +48,7 @@ func CreateDBClient(timeout time.Duration) (client *mongo.Client, ctx context.Co
return client, ctx
}
func CreateTimoutCTX(timeout time.Duration) (context.Context, context.CancelFunc) {
// CreateTimeoutCTX creates a timeout context.
func CreateTimeoutCTX(timeout time.Duration) (context.Context, context.CancelFunc) {
return context.WithTimeout(context.Background(), timeout)
}
package utils
import (
"flag"
"os"
godotenv "github.com/joho/godotenv"
)
// GetEnv gets a env variable, and when not present returns the default value
func GetEnv(variableName string, defaultValue string) string {
godotenv.Load(".env")
envValue := os.Getenv(variableName)
if envValue == "" {
return defaultValue
}
return envValue
}
// GetDBName gets the db name from the environment
func GetDBName() string {
dbName := GetEnv("MONGO_DB", "good2go")
if flag.Lookup("test.v") == nil {
return dbName
}
return dbName + "_test"
}
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