update 04.03.24
This commit is contained in:
parent
740323333f
commit
fa863cbd12
5 changed files with 267 additions and 29 deletions
25
util/structs.go
Normal file
25
util/structs.go
Normal file
|
@ -0,0 +1,25 @@
|
|||
package util
|
||||
|
||||
import (
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Person struct {
|
||||
ID primitive.ObjectID `bson:"_id,omitempty"`
|
||||
Name string `bson:"name"`
|
||||
Age int `bson:"age,omitempty" json:"age,omitempty"`
|
||||
Email string `bson:"email,omitempty" json:"email,omitempty"`
|
||||
}
|
||||
|
||||
type Debt struct {
|
||||
ID primitive.ObjectID `bson:"_id,omitempty"`
|
||||
PersonID primitive.ObjectID `bson:"fk_pid"`
|
||||
Amount int `bson:"amount"`
|
||||
Description string `bson:"description,omitempty"`
|
||||
Datetime time.Time `bson:"datetime"`
|
||||
}
|
||||
type PersonWithDebts struct {
|
||||
Person Person `json:"person"`
|
||||
Debts []Debt `json:"debts"`
|
||||
}
|
36
util/util.go
Normal file
36
util/util.go
Normal file
|
@ -0,0 +1,36 @@
|
|||
package util
|
||||
|
||||
import (
|
||||
"context"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"log"
|
||||
"time"
|
||||
)
|
||||
|
||||
func InsertRandomData(client *mongo.Client) {
|
||||
persons := []string{"Max", "Moritz", "Hans", "Peter", "Paul", "Klaus", "Karl", "Kai", "Kurt", "Karl-Heinz"}
|
||||
debts := []Debt{}
|
||||
for i := 0; i < 10; i++ {
|
||||
person := Person{Name: persons[i]}
|
||||
collection := client.Database("debtlist").Collection("persons")
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
result, err := collection.InsertOne(ctx, person)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
for j := 0; j < 3; j++ {
|
||||
debts = append(debts, Debt{PersonID: result.InsertedID.(primitive.ObjectID), Amount: j * 100, Description: "Test", Datetime: time.Now()})
|
||||
}
|
||||
}
|
||||
collection := client.Database("debtlist").Collection("debts")
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
for _, debt := range debts {
|
||||
_, err := collection.InsertOne(ctx, debt)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue