update 04.03.24
This commit is contained in:
parent
fa863cbd12
commit
eab7fea353
5 changed files with 355 additions and 334 deletions
337
main.go
337
main.go
|
@ -2,359 +2,28 @@ package main
|
|||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"gibb165lb2/util"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
var client *mongo.Client
|
||||
|
||||
// func to insert 10 random persons with real names into the database and create 30 random debts for them
|
||||
|
||||
func getDebtsForPerson(client *mongo.Client, personID string) ([]util.Debt, error) {
|
||||
var results []util.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 util.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 main() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
var err error
|
||||
client, err = mongo.Connect(ctx, options.Client().ApplyURI("mongodb://root:root12345@localhost:27017"))
|
||||
util.Client, err = mongo.Connect(ctx, options.Client().ApplyURI("mongodb://root:root12345@localhost:27017"))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
//util.InsertRandomData(client)
|
||||
// Router initialisieren
|
||||
r := mux.NewRouter()
|
||||
r.HandleFunc("/persons", createPerson).Methods("POST")
|
||||
r.HandleFunc("/persons/{id}", getPerson).Methods("GET")
|
||||
r.HandleFunc("/persons", getAllPersons).Methods("GET")
|
||||
r.HandleFunc("/personsWithDebts", getPersonsWithDebts).Methods("GET")
|
||||
r.HandleFunc("/persons/{id}", updatePerson).Methods("PUT")
|
||||
r.HandleFunc("/persons/{id}", deletePerson).Methods("DELETE")
|
||||
r.HandleFunc("/persons/{id}/debts", getDebts).Methods("GET")
|
||||
r.HandleFunc("/persons/{id}/debts", createDebt).Methods("POST")
|
||||
r.HandleFunc("/debts/{id}", deleteDebt).Methods("DELETE")
|
||||
r.HandleFunc("/debts", getAllDebts).Methods("GET")
|
||||
r.HandleFunc("/debts/{id}", getDebt).Methods("GET")
|
||||
r.HandleFunc("/debts/{id}", updateDebt).Methods("PUT")
|
||||
r := GetRouter()
|
||||
|
||||
err = http.ListenAndServe(":3333", r)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func createPerson(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
var person util.Person
|
||||
json.NewDecoder(r.Body).Decode(&person)
|
||||
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 {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(`{"message": "` + err.Error() + `"}`))
|
||||
return
|
||||
|
||||
}
|
||||
json.NewEncoder(w).Encode(result)
|
||||
}
|
||||
func updatePerson(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
var person util.Person
|
||||
vars := mux.Vars(r)
|
||||
personID := vars["id"]
|
||||
person.ID, _ = primitive.ObjectIDFromHex(personID)
|
||||
json.NewDecoder(r.Body).Decode(&person)
|
||||
fmt.Println(person)
|
||||
|
||||
collection := client.Database("debtlist").Collection("persons")
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
filter := bson.M{"_id": person.ID}
|
||||
update := bson.M{
|
||||
"$set": bson.M{
|
||||
"name": person.Name,
|
||||
"age": person.Age,
|
||||
"email": person.Email,
|
||||
},
|
||||
}
|
||||
result, err := collection.UpdateOne(ctx, filter, update)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(`{"message": "` + err.Error() + `"}`))
|
||||
return
|
||||
|
||||
}
|
||||
json.NewEncoder(w).Encode(result)
|
||||
}
|
||||
func deletePerson(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
vars := mux.Vars(r)
|
||||
personID := vars["id"]
|
||||
collection := client.Database("debtlist").Collection("persons")
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
newPersonID, err := primitive.ObjectIDFromHex(personID)
|
||||
filter := bson.M{"_id": newPersonID}
|
||||
result, err := collection.DeleteOne(ctx, filter)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(`{"message": "` + err.Error() + `"}`))
|
||||
return
|
||||
}
|
||||
json.NewEncoder(w).Encode(result)
|
||||
}
|
||||
func getPerson(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
personID := vars["id"]
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
var person util.Person
|
||||
collection := client.Database("debtlist").Collection("persons")
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
pid, err := primitive.ObjectIDFromHex(personID)
|
||||
filter := bson.M{"_id": pid}
|
||||
err = collection.FindOne(ctx, filter).Decode(&person)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(`{"message": "` + err.Error() + `"}`))
|
||||
return
|
||||
}
|
||||
json.NewEncoder(w).Encode(person)
|
||||
}
|
||||
func getAllPersons(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
var persons []util.Person
|
||||
collection := client.Database("debtlist").Collection("persons")
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
cursor, err := collection.Find(ctx, bson.M{})
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(`{"message": "` + err.Error() + `"}`))
|
||||
return
|
||||
}
|
||||
defer cursor.Close(ctx)
|
||||
for cursor.Next(ctx) {
|
||||
var person util.Person
|
||||
cursor.Decode(&person)
|
||||
persons = append(persons, person)
|
||||
}
|
||||
if err := cursor.Err(); err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(`{"message": "` + err.Error() + `"}`))
|
||||
return
|
||||
}
|
||||
json.NewEncoder(w).Encode(persons)
|
||||
}
|
||||
|
||||
func getDebts(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
personID := vars["id"]
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
debts, err := getDebtsForPerson(client, personID)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(`{"message": "` + err.Error() + `"}`))
|
||||
return
|
||||
}
|
||||
json.NewEncoder(w).Encode(debts)
|
||||
}
|
||||
|
||||
func createDebt(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
var debt util.Debt
|
||||
personID := vars["id"]
|
||||
json.NewDecoder(r.Body).Decode(&debt)
|
||||
debt.PersonID, _ = primitive.ObjectIDFromHex(personID)
|
||||
collection := client.Database("debtlist").Collection("debts")
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
result, err := collection.InsertOne(ctx, debt)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(`{"message": "` + err.Error() + `"}`))
|
||||
return
|
||||
}
|
||||
json.NewEncoder(w).Encode(result)
|
||||
}
|
||||
|
||||
func deleteDebt(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
var debt util.Debt
|
||||
debtId := vars["id"]
|
||||
json.NewDecoder(r.Body).Decode(&debt)
|
||||
debt.ID, _ = primitive.ObjectIDFromHex(debtId)
|
||||
collection := client.Database("debtlist").Collection("debts")
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
filter := bson.M{"_id": debt.ID}
|
||||
result, err := collection.DeleteOne(ctx, filter)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(`{"message": "` + err.Error() + `"}`))
|
||||
return
|
||||
}
|
||||
json.NewEncoder(w).Encode(result)
|
||||
}
|
||||
|
||||
func getAllDebts(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
var debts []util.Debt
|
||||
collection := client.Database("debtlist").Collection("debts")
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
cursor, err := collection.Find(ctx, bson.M{})
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(`{"message": "` + err.Error() + `"}`))
|
||||
return
|
||||
}
|
||||
defer cursor.Close(ctx)
|
||||
for cursor.Next(ctx) {
|
||||
var debt util.Debt
|
||||
cursor.Decode(&debt)
|
||||
debts = append(debts, debt)
|
||||
}
|
||||
if err := cursor.Err(); err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(`{"message": "` + err.Error() + `"}`))
|
||||
return
|
||||
}
|
||||
json.NewEncoder(w).Encode(debts)
|
||||
}
|
||||
|
||||
// eine route welche alle personen zurückgibt welche ein array aus debts haben.
|
||||
func getPersonsWithDebts(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
var personsWithDebts []util.PersonWithDebts
|
||||
var persons []util.Person
|
||||
collection := client.Database("debtlist").Collection("persons")
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
cursor, err := collection.Find(ctx, bson.M{})
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(`{"message": "` + err.Error() + `"}`))
|
||||
return
|
||||
}
|
||||
defer cursor.Close(ctx)
|
||||
for cursor.Next(ctx) {
|
||||
var person util.Person
|
||||
cursor.Decode(&person)
|
||||
persons = append(persons, person)
|
||||
}
|
||||
if err := cursor.Err(); err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(`{"message": "` + err.Error() + `"}`))
|
||||
return
|
||||
}
|
||||
for _, person := range persons {
|
||||
debts, err := getDebtsForPerson(client, person.ID.Hex())
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(`{"message": "` + err.Error() + `"}`))
|
||||
return
|
||||
}
|
||||
personsWithDebts = append(personsWithDebts, util.PersonWithDebts{Person: person, Debts: debts})
|
||||
}
|
||||
json.NewEncoder(w).Encode(personsWithDebts)
|
||||
}
|
||||
|
||||
// neue route welche eine einzelne debt zurückgibt
|
||||
func getDebt(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
debtID := vars["id"]
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
var debt util.Debt
|
||||
collection := client.Database("debtlist").Collection("debts")
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
did, err := primitive.ObjectIDFromHex(debtID)
|
||||
filter := bson.M{"_id": did}
|
||||
err = collection.FindOne(ctx, filter).Decode(&debt)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(`{"message": "` + err.Error() + `"}`))
|
||||
return
|
||||
}
|
||||
json.NewEncoder(w).Encode(debt)
|
||||
}
|
||||
|
||||
// neue route welche eine einzelne debt aktualisiert
|
||||
func updateDebt(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
var debt util.Debt
|
||||
vars := mux.Vars(r)
|
||||
debtID := vars["id"]
|
||||
debt.ID, _ = primitive.ObjectIDFromHex(debtID)
|
||||
json.NewDecoder(r.Body).Decode(&debt)
|
||||
collection := client.Database("debtlist").Collection("debts")
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
filter := bson.M{"_id": debt.ID}
|
||||
update := bson.M{
|
||||
"$set": bson.M{
|
||||
"fk_pid": debt.PersonID,
|
||||
"amount": debt.Amount,
|
||||
"description": debt.Description,
|
||||
"datetime": debt.Datetime,
|
||||
},
|
||||
}
|
||||
result, err := collection.UpdateOne(ctx, filter, update)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(`{"message": "` + err.Error() + `"}`))
|
||||
return
|
||||
}
|
||||
json.NewEncoder(w).Encode(result)
|
||||
}
|
||||
|
|
23
router.go
Normal file
23
router.go
Normal file
|
@ -0,0 +1,23 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"gibb165lb2/router"
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
func GetRouter() *mux.Router {
|
||||
r := mux.NewRouter()
|
||||
r.HandleFunc("/persons", router.CreatePerson).Methods("POST")
|
||||
r.HandleFunc("/persons/{id}", router.GetPerson).Methods("GET")
|
||||
r.HandleFunc("/persons", router.GetAllPersons).Methods("GET")
|
||||
r.HandleFunc("/personsWithDebts", router.GetPersonsWithDebts).Methods("GET")
|
||||
r.HandleFunc("/persons/{id}", router.UpdatePerson).Methods("PUT")
|
||||
r.HandleFunc("/persons/{id}", router.DeletePerson).Methods("DELETE")
|
||||
r.HandleFunc("/persons/{id}/debts", router.GetDebts).Methods("GET")
|
||||
r.HandleFunc("/persons/{id}/debts", router.CreateDebt).Methods("POST")
|
||||
r.HandleFunc("/debts/{id}", router.DeleteDebt).Methods("DELETE")
|
||||
r.HandleFunc("/debts", router.GetAllDebts).Methods("GET")
|
||||
r.HandleFunc("/debts/{id}", router.GetDebt).Methods("GET")
|
||||
r.HandleFunc("/debts/{id}", router.UpdateDebt).Methods("PUT")
|
||||
return r
|
||||
}
|
134
router/debt.go
Normal file
134
router/debt.go
Normal file
|
@ -0,0 +1,134 @@
|
|||
package router
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"gibb165lb2/util"
|
||||
"github.com/gorilla/mux"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
func GetDebts(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
personID := vars["id"]
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
debts, err := util.GetDebtsForPerson(util.Client, personID)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(`{"message": "` + err.Error() + `"}`))
|
||||
return
|
||||
}
|
||||
json.NewEncoder(w).Encode(debts)
|
||||
}
|
||||
func CreateDebt(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
var debt util.Debt
|
||||
personID := vars["id"]
|
||||
json.NewDecoder(r.Body).Decode(&debt)
|
||||
debt.PersonID, _ = primitive.ObjectIDFromHex(personID)
|
||||
collection := util.Client.Database("debtlist").Collection("debts")
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
result, err := collection.InsertOne(ctx, debt)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(`{"message": "` + err.Error() + `"}`))
|
||||
return
|
||||
}
|
||||
json.NewEncoder(w).Encode(result)
|
||||
}
|
||||
func DeleteDebt(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
var debt util.Debt
|
||||
debtId := vars["id"]
|
||||
json.NewDecoder(r.Body).Decode(&debt)
|
||||
debt.ID, _ = primitive.ObjectIDFromHex(debtId)
|
||||
collection := util.Client.Database("debtlist").Collection("debts")
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
filter := bson.M{"_id": debt.ID}
|
||||
result, err := collection.DeleteOne(ctx, filter)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(`{"message": "` + err.Error() + `"}`))
|
||||
return
|
||||
}
|
||||
json.NewEncoder(w).Encode(result)
|
||||
}
|
||||
func GetAllDebts(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
var debts []util.Debt
|
||||
collection := util.Client.Database("debtlist").Collection("debts")
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
cursor, err := collection.Find(ctx, bson.M{})
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(`{"message": "` + err.Error() + `"}`))
|
||||
return
|
||||
}
|
||||
defer cursor.Close(ctx)
|
||||
for cursor.Next(ctx) {
|
||||
var debt util.Debt
|
||||
cursor.Decode(&debt)
|
||||
debts = append(debts, debt)
|
||||
}
|
||||
if err := cursor.Err(); err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(`{"message": "` + err.Error() + `"}`))
|
||||
return
|
||||
}
|
||||
json.NewEncoder(w).Encode(debts)
|
||||
}
|
||||
|
||||
func GetDebt(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
debtID := vars["id"]
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
var debt util.Debt
|
||||
collection := util.Client.Database("debtlist").Collection("debts")
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
did, err := primitive.ObjectIDFromHex(debtID)
|
||||
filter := bson.M{"_id": did}
|
||||
err = collection.FindOne(ctx, filter).Decode(&debt)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(`{"message": "` + err.Error() + `"}`))
|
||||
return
|
||||
}
|
||||
json.NewEncoder(w).Encode(debt)
|
||||
}
|
||||
func UpdateDebt(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
var debt util.Debt
|
||||
vars := mux.Vars(r)
|
||||
debtID := vars["id"]
|
||||
debt.ID, _ = primitive.ObjectIDFromHex(debtID)
|
||||
json.NewDecoder(r.Body).Decode(&debt)
|
||||
collection := util.Client.Database("debtlist").Collection("debts")
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
filter := bson.M{"_id": debt.ID}
|
||||
update := bson.M{
|
||||
"$set": bson.M{
|
||||
"fk_pid": debt.PersonID,
|
||||
"amount": debt.Amount,
|
||||
"description": debt.Description,
|
||||
"datetime": debt.Datetime,
|
||||
},
|
||||
}
|
||||
result, err := collection.UpdateOne(ctx, filter, update)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(`{"message": "` + err.Error() + `"}`))
|
||||
return
|
||||
}
|
||||
json.NewEncoder(w).Encode(result)
|
||||
}
|
154
router/person.go
Normal file
154
router/person.go
Normal file
|
@ -0,0 +1,154 @@
|
|||
package router
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"gibb165lb2/util"
|
||||
"github.com/gorilla/mux"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
func CreatePerson(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
var person util.Person
|
||||
json.NewDecoder(r.Body).Decode(&person)
|
||||
collection := util.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 {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(`{"message": "` + err.Error() + `"}`))
|
||||
return
|
||||
|
||||
}
|
||||
json.NewEncoder(w).Encode(result)
|
||||
}
|
||||
func UpdatePerson(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
var person util.Person
|
||||
vars := mux.Vars(r)
|
||||
personID := vars["id"]
|
||||
person.ID, _ = primitive.ObjectIDFromHex(personID)
|
||||
json.NewDecoder(r.Body).Decode(&person)
|
||||
fmt.Println(person)
|
||||
|
||||
collection := util.Client.Database("debtlist").Collection("persons")
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
filter := bson.M{"_id": person.ID}
|
||||
update := bson.M{
|
||||
"$set": bson.M{
|
||||
"name": person.Name,
|
||||
"age": person.Age,
|
||||
"email": person.Email,
|
||||
},
|
||||
}
|
||||
result, err := collection.UpdateOne(ctx, filter, update)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(`{"message": "` + err.Error() + `"}`))
|
||||
return
|
||||
|
||||
}
|
||||
json.NewEncoder(w).Encode(result)
|
||||
}
|
||||
func DeletePerson(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
vars := mux.Vars(r)
|
||||
personID := vars["id"]
|
||||
collection := util.Client.Database("debtlist").Collection("persons")
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
newPersonID, err := primitive.ObjectIDFromHex(personID)
|
||||
filter := bson.M{"_id": newPersonID}
|
||||
result, err := collection.DeleteOne(ctx, filter)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(`{"message": "` + err.Error() + `"}`))
|
||||
return
|
||||
}
|
||||
json.NewEncoder(w).Encode(result)
|
||||
}
|
||||
func GetPerson(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
personID := vars["id"]
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
var person util.Person
|
||||
collection := util.Client.Database("debtlist").Collection("persons")
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
pid, err := primitive.ObjectIDFromHex(personID)
|
||||
filter := bson.M{"_id": pid}
|
||||
err = collection.FindOne(ctx, filter).Decode(&person)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(`{"message": "` + err.Error() + `"}`))
|
||||
return
|
||||
}
|
||||
json.NewEncoder(w).Encode(person)
|
||||
}
|
||||
func GetAllPersons(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
var persons []util.Person
|
||||
collection := util.Client.Database("debtlist").Collection("persons")
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
cursor, err := collection.Find(ctx, bson.M{})
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(`{"message": "` + err.Error() + `"}`))
|
||||
return
|
||||
}
|
||||
defer cursor.Close(ctx)
|
||||
for cursor.Next(ctx) {
|
||||
var person util.Person
|
||||
cursor.Decode(&person)
|
||||
persons = append(persons, person)
|
||||
}
|
||||
if err := cursor.Err(); err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(`{"message": "` + err.Error() + `"}`))
|
||||
return
|
||||
}
|
||||
json.NewEncoder(w).Encode(persons)
|
||||
}
|
||||
func GetPersonsWithDebts(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
var personsWithDebts []util.PersonWithDebts
|
||||
var persons []util.Person
|
||||
collection := util.Client.Database("debtlist").Collection("persons")
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
cursor, err := collection.Find(ctx, bson.M{})
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(`{"message": "` + err.Error() + `"}`))
|
||||
return
|
||||
}
|
||||
defer cursor.Close(ctx)
|
||||
for cursor.Next(ctx) {
|
||||
var person util.Person
|
||||
cursor.Decode(&person)
|
||||
persons = append(persons, person)
|
||||
}
|
||||
if err := cursor.Err(); err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(`{"message": "` + err.Error() + `"}`))
|
||||
return
|
||||
}
|
||||
for _, person := range persons {
|
||||
debts, err := util.GetDebtsForPerson(util.Client, person.ID.Hex())
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(`{"message": "` + err.Error() + `"}`))
|
||||
return
|
||||
}
|
||||
personsWithDebts = append(personsWithDebts, util.PersonWithDebts{Person: person, Debts: debts})
|
||||
}
|
||||
json.NewEncoder(w).Encode(personsWithDebts)
|
||||
}
|
41
util/util.go
41
util/util.go
|
@ -2,12 +2,53 @@ package util
|
|||
|
||||
import (
|
||||
"context"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"log"
|
||||
"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{}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue