Commit 995281a1 authored by Max Richter's avatar Max Richter
Browse files

feat(mongo): add a lot more crud funcs to room repo

parent 6b367cb9
package mongo
import (
"context"
"errors"
"log"
"strconv"
"time"
utils "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/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"gopkg.in/mgo.v2/bson"
)
type roomRepository struct {
c *mongo.Collection
ctx context.Context
c *mongo.Collection
}
var (
// RoomRepository CRID
RoomRepository = &roomRepository{}
)
func (repo *roomRepository) insertOne(document interface{}) {
repo.c.InsertOne(repo.ctx, document)
func (repo roomRepository) findByRoomNumber(roomNumber int) (*model.Room, error) {
ctx, cancel := utils.CreateTimoutCTX(5 * time.Second)
defer cancel()
// Find
result, err := repo.c.Find(ctx, bson.M{"number": roomNumber})
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
}
func (repo *roomRepository) updateOne(document interface{}) {
repo.c.UpdateOne(repo.ctx, document, document)
func (repo roomRepository) findByID(id primitive.ObjectID) (*model.Room, error) {
ctx, cancel := utils.CreateTimoutCTX(5 * time.Second)
defer cancel()
// Find
result, err := repo.c.Find(ctx, bson.M{"_id": id})
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
}
func (repo *roomRepository) CreateRoom(r *model.Room) {
repo.insertOne(r)
func (repo roomRepository) insertOne(document interface{}) (*model.Room, error) {
ctx, cancel := utils.CreateTimoutCTX(5 * time.Second)
defer cancel()
// Insert
result, err := repo.c.InsertOne(ctx, document)
if err != nil {
return &model.Room{}, err
}
// Return created entity
return repo.findByID(result.InsertedID.(primitive.ObjectID))
}
func (repo *roomRepository) UpdateRoom(r *model.Room) {
repo.updateOne(r)
func (repo roomRepository) updateOne(document interface{}) {
ctx, cancel := utils.CreateTimoutCTX(5 * time.Second)
defer cancel()
repo.c.UpdateOne(ctx, document, document)
}
func (repo *roomRepository) GetAllRooms() []model.Room {
func (repo roomRepository) findOne(document interface{}) (*model.Room, error) {
ctx, cancel := utils.CreateTimoutCTX(5 * time.Second)
defer cancel()
result := repo.c.FindOne(ctx, document)
var room model.Room
err := result.Decode(&room)
return &room, err
}
func (repo roomRepository) deleteByID(id primitive.ObjectID) error {
ctx, cancel := utils.CreateTimoutCTX(5 * time.Second)
defer cancel()
_, error := repo.c.DeleteOne(ctx, bson.M{"_id": id})
return error
}
func (repo roomRepository) CreateRoom(r *model.Room) (*model.Room, error) {
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,
})
cursor, err := repo.c.Find(repo.ctx, bson.M{})
if err != nil {
log.Fatal(err)
return &model.Room{}, err
}
log.Print("created new room", room.Number)
return room, err
}
func (repo roomRepository) UpdateRoom(r *model.Room) {
repo.updateOne(r)
}
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 {
ctx, cancel := utils.CreateTimoutCTX(5 * time.Second)
defer cancel()
cursor, err := repo.c.Find(ctx, bson.M{})
if err != nil {
log.Fatal("[MONGODB] roomRepository", err)
}
var rooms []model.Room
if err = cursor.All(repo.ctx, &rooms); err != nil {
if err = cursor.All(ctx, &rooms); err != nil {
log.Fatal(err)
}
return rooms
}
func (repo *roomRepository) ConnectTo(ctx context.Context, c *mongo.Collection) {
func (repo roomRepository) ConnectTo(c *mongo.Collection) *roomRepository {
repo.c = c
repo.ctx = ctx
return &repo
}
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