first commit
This commit is contained in:
commit
740323333f
7 changed files with 302 additions and 0 deletions
189
main.go
Normal file
189
main.go
Normal file
|
@ -0,0 +1,189 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"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
|
||||
|
||||
type Person struct {
|
||||
ID primitive.ObjectID `bson:"_id,omitempty"`
|
||||
Name string `bson:"name"`
|
||||
}
|
||||
|
||||
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"`
|
||||
}
|
||||
|
||||
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 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:example@localhost:27017"))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// 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("/persons/{id}", updatePerson).Methods("PUT")
|
||||
r.HandleFunc("/persons/{id}", deletePerson).Methods("DELETE")
|
||||
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 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 Person
|
||||
json.NewDecoder(r.Body).Decode(&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,
|
||||
},
|
||||
}
|
||||
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")
|
||||
var person Person
|
||||
json.NewDecoder(r.Body).Decode(&person)
|
||||
collection := client.Database("debtlist").Collection("persons")
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
filter := bson.M{"_id": person.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 getPerson(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
personID := vars["id"]
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
var person 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 []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 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)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue