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) }