154 lines
4.8 KiB
Go
154 lines
4.8 KiB
Go
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)
|
|
}
|