Commit 642fb79d authored by Timo Bechtel's avatar Timo Bechtel
Browse files

feat(repositories): add building repository

parent 7560d55a
Pipeline #1805 failed with stages
in 55 seconds
......@@ -4,13 +4,13 @@ import (
"fmt"
"time"
repos "git.coco.study/fvitt/good2go/database/mongo/repositories"
"git.coco.study/fvitt/good2go/database/mongo/repositories"
"git.coco.study/fvitt/good2go/database/mongo/utils"
)
var (
// RoomRepository does crud stuff
RoomRepo *repos.RoomRepository
RoomRepo *repositories.Repository
)
// Init initializes connection to a database
......@@ -30,7 +30,7 @@ func Init() {
db := client.Database(utils.GetDBName())
// Create the repositories
RoomRepo = &repos.RoomRepository{
RoomRepo = &repositories.Repository{
Col: db.Collection("rooms"),
}
......
package repositories
import (
"time"
"git.coco.study/fvitt/good2go/database/mongo/utils"
"git.coco.study/fvitt/good2go/internal/model"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)
type BuildingRepository interface {
CreateBuilding(building *model.Building) (*mongo.InsertOneResult, error)
GetBuildingById(id interface{}) (*model.Building, error)
GetBuilding(filter *bson.M) (*model.Building, error)
UpdateBuilding(id interface{}, building *model.Building) (*mongo.UpdateResult, error)
}
func (repo *Repository) CreateBuilding(building *model.Building) (*mongo.InsertOneResult, error) {
ctx, cancel := utils.CreateTimeoutCTX(time.Second * 10)
defer cancel()
res, err := repo.Col.InsertOne(ctx, &building)
if err != nil {
return nil, err
}
return res, nil
}
func (repo *Repository) UpdateBuilding(id interface{}, building *model.Building) (*mongo.UpdateResult, error) {
ctx, cancel := utils.CreateTimeoutCTX(time.Second * 10)
defer cancel()
res, err := repo.Col.ReplaceOne(ctx, bson.M{"_id": id}, &building)
if err != nil {
return nil, err
}
return res, nil
}
func (repo *Repository) GetBuildingById(id interface{}) (*model.Building, error) {
return repo.GetBuilding(&bson.M{"_id": id})
}
func (repo *Repository) GetBuilding(filter *bson.M) (*model.Building, error) {
ctx, cancel := utils.CreateTimeoutCTX(time.Second * 10)
defer cancel()
res := repo.Col.FindOne(ctx, filter)
building := model.Building{}
err := res.Decode(&building)
if err != nil {
return nil, err
}
return &building, nil
}
package repositories
import "go.mongodb.org/mongo-driver/mongo"
type Repository struct {
Col *mongo.Collection
}
......@@ -14,11 +14,15 @@ import (
"go.mongodb.org/mongo-driver/mongo"
)
type RoomRepository struct {
Col *mongo.Collection
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
}
func (repo *RoomRepository) findByRoomNumber(roomNumber int) (*model.Room, error) {
func (repo *Repository) findByRoomNumber(roomNumber int) (*model.Room, error) {
ctx, cancel := utils.CreateTimeoutCTX(5 * time.Second)
defer cancel()
......@@ -40,7 +44,7 @@ func (repo *RoomRepository) findByRoomNumber(roomNumber int) (*model.Room, error
}
func (repo *RoomRepository) findByID(id primitive.ObjectID) (*model.Room, error) {
func (repo *Repository) findByID(id primitive.ObjectID) (*model.Room, error) {
ctx, cancel := utils.CreateTimeoutCTX(5 * time.Second)
defer cancel()
......@@ -61,7 +65,7 @@ func (repo *RoomRepository) findByID(id primitive.ObjectID) (*model.Room, error)
return &r, nil
}
func (repo *RoomRepository) insertOne(document interface{}) (*model.Room, error) {
func (repo *Repository) insertOne(document interface{}) (*model.Room, error) {
ctx, cancel := utils.CreateTimeoutCTX(5 * time.Second)
defer cancel()
......@@ -76,7 +80,7 @@ func (repo *RoomRepository) insertOne(document interface{}) (*model.Room, error)
return repo.findByID(result.InsertedID.(primitive.ObjectID))
}
func (repo *RoomRepository) updateOne(document interface{}) {
func (repo *Repository) updateOne(document interface{}) {
ctx, cancel := utils.CreateTimeoutCTX(5 * time.Second)
defer cancel()
......@@ -87,7 +91,7 @@ func (repo *RoomRepository) updateOne(document interface{}) {
}
}
func (repo *RoomRepository) findOne(document interface{}) (*model.Room, error) {
func (repo *Repository) findOne(document interface{}) (*model.Room, error) {
ctx, cancel := utils.CreateTimeoutCTX(5 * time.Second)
defer cancel()
......@@ -98,7 +102,7 @@ func (repo *RoomRepository) findOne(document interface{}) (*model.Room, error) {
return &room, err
}
func (repo *RoomRepository) deleteByID(id primitive.ObjectID) error {
func (repo *Repository) deleteByID(id primitive.ObjectID) error {
ctx, cancel := utils.CreateTimeoutCTX(5 * time.Second)
defer cancel()
......@@ -108,7 +112,7 @@ func (repo *RoomRepository) deleteByID(id primitive.ObjectID) error {
return err
}
func (repo *RoomRepository) CreateRoom(r *model.Room) (*model.Room, error) {
func (repo *Repository) CreateRoom(r *model.Room) (*model.Room, error) {
log.Print("Create room ", r.Number)
......@@ -134,16 +138,16 @@ func (repo *RoomRepository) CreateRoom(r *model.Room) (*model.Room, error) {
return room, err
}
func (repo *RoomRepository) UpdateRoom(r *model.Room) {
func (repo *Repository) UpdateRoom(r *model.Room) {
repo.updateOne(r)
}
func (repo *RoomRepository) GetRoom(document interface{}) (room *model.Room, err error) {
func (repo *Repository) 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 *Repository) GetAllRooms() []model.Room {
ctx, cancel := utils.CreateTimeoutCTX(5 * time.Second)
defer cancel()
......@@ -160,8 +164,3 @@ func (repo *RoomRepository) GetAllRooms() []model.Room {
return rooms
}
func (repo RoomRepository) ConnectTo(c *mongo.Collection) *RoomRepository {
repo.Col = c
return &repo
}
......@@ -9,12 +9,12 @@ import (
"go.mongodb.org/mongo-driver/mongo"
)
var repo *RoomRepository
var repo Repository
func TestMain(m *testing.M) {
// Mock the collection
repo = RoomRepository{}.ConnectTo(&mongo.Collection{})
repo = Repository{Col: &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