update 04.03.24
This commit is contained in:
parent
eab7fea353
commit
5b26df3e4c
10 changed files with 35 additions and 6 deletions
25
src/util/structs.go
Normal file
25
src/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"`
|
||||
}
|
83
src/util/util.go
Normal file
83
src/util/util.go
Normal file
|
@ -0,0 +1,83 @@
|
|||
package util
|
||||
|
||||
import (
|
||||
"context"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
var Client *mongo.Client
|
||||
|
||||
func GetDebtsForPerson(client *mongo.Client, personID string) ([]Debt, error) {
|
||||
var results []Debt
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
// Umwandlung der personID von string in primitive.ObjectID
|
||||
objID, err := primitive.ObjectIDFromHex(personID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Verbindung zur "debts" Sammlung
|
||||
debtsCollection := client.Database("debtlist").Collection("debts")
|
||||
|
||||
// Abfrage erstellen, die alle Schulden findet, deren fk_pid mit der personID übereinstimmt
|
||||
filter := bson.M{"fk_pid": objID}
|
||||
cursor, err := debtsCollection.Find(ctx, filter)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer cursor.Close(ctx)
|
||||
|
||||
// Durchlaufen des Cursors und Hinzufügen der Ergebnisse zur results-Slice
|
||||
for cursor.Next(ctx) {
|
||||
var debt Debt
|
||||
if err := cursor.Decode(&debt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
results = append(results, debt)
|
||||
}
|
||||
|
||||
if err := cursor.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return results, nil
|
||||
}
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func Init(w http.ResponseWriter, r *http.Request) {
|
||||
InsertRandomData(Client)
|
||||
w.Write([]byte("Init"))
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue