Commit 3e3895a5 authored by Max Richter's avatar Max Richter
Browse files

fix(): move disconnect function to main

parent ac105c32
Pipeline #1827 failed with stages
in 55 seconds
......@@ -18,46 +18,14 @@ func main() {
//sentry.CaptureMessage("Still works!")
database.Init()
defer database.Disconnect()
initSampleData()
database.CreateSampleData()
api.Init()
}
// Create sample objects in memory for playing around
func initSampleData() {
//"08:00", "10h", []string{"mon", "tue", "wed", "thu", "fri", "sat"}
// Add Building
// building1 := &model.Building{}
// building1.New("08:00", "10h", []string{"mon", "tue", "wed", "thu", "fri", "sat"})
// building, _ := services.BuildingService.CreateBuilding(building1)
// _, err := services.BuildingService.CreateRoom(building.ID.String(), &model.Room{
// Number: 320,
// Capacity: 10,
// })
// if err != nil {
// log.Fatal(err)
// }
// //model.Building.CreateRoom(520, 50)
// // Add sample Appointments
// a1, _ := model.Appointment{}.New("27-11-2016 10:00", "2h")
// _, err = services.AppointmentService.AddAppointment(a1)
// if err != nil {
// log.Fatal(err)
// }
// a2, _ := model.Appointment{}.New("27-11-2016 13:00", "2h")
// _, err = services.AppointmentService.AddAppointment(a2)
// if err != nil {
// log.Fatal(err)
// }
}
// Initialize sentry connection so you can track errors with `sentry.CaptureMessage(err)`
func initSentry() {
err := sentry.Init(sentry.ClientOptions{
......
package database
import "git.coco.study/fvitt/good2go/database/mongo"
import (
"context"
"log"
func Init() {
mongo.Init()
ownMongo "git.coco.study/fvitt/good2go/database/mongo"
"git.coco.study/fvitt/good2go/internal/model"
mongo "go.mongodb.org/mongo-driver/mongo"
)
func Init() (*mongo.Client, context.Context) {
return ownMongo.Init()
}
func Disconnect() {
ownMongo.Disconnect()
}
func CreateSampleData() {
//"08:00", "10h", []string{"mon", "tue", "wed", "thu", "fri", "sat"}
// Add Building
building := &model.Building{}
building.New("08:00", "10h", []string{"mon", "tue", "wed", "thu", "fri", "sat"})
building, err := ownMongo.BuildingRepo.CreateBuilding(building)
if err != nil {
log.Fatal(err)
}
log.Print("building", building)
// Add a room to the building
building.CreateRoom(320, 10)
// // Add a second room to the building
// _, err = services.BuildingService.CreateRoom(building.ID.String(), &model.Room{
// Number: 420,
// Capacity: 69,
// })
// if err != nil {
// log.Fatal(err)
// }
// // Add sample Appointments
// a1, _ := model.Appointment{}.New("27-11-2016 10:00", "2h")
// _, err = services.AppointmentService.AddAppointment(a1)
// if err != nil {
// log.Fatal(err)
// }
// a2, _ := model.Appointment{}.New("27-11-2016 13:00", "2h")
// _, err = services.AppointmentService.AddAppointment(a2)
// if err != nil {
// log.Fatal(err)
// }
}
package mongo
import (
"fmt"
"context"
"log"
"time"
"git.coco.study/fvitt/good2go/database/mongo/repositories"
"git.coco.study/fvitt/good2go/database/mongo/utils"
"go.mongodb.org/mongo-driver/mongo"
)
var (
......@@ -14,18 +16,15 @@ var (
AppointmentRepo *repositories.Repository
)
var client *mongo.Client
var ctx context.Context
// Init initializes connection to a database
// and connects all the individual repositories to their collections
func Init() {
func Init() (*mongo.Client, context.Context) {
// Create a mongodb client
client, ctx := utils.CreateDBClient(10 * time.Second)
defer func() {
err := client.Disconnect(ctx)
if err != nil {
fmt.Println(err)
}
}()
client, ctx = utils.CreateDBClient(10 * time.Second)
// Connect to a specified database
db := client.Database(utils.GetDBName())
......@@ -43,4 +42,13 @@ func Init() {
Col: db.Collection("appointments"),
}
log.Println("[MONGODB] initialized repositories")
return client, ctx
}
// Disconnect disconnects the database
func Disconnect() {
client.Disconnect(ctx)
}
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