Skip to content
Snippets Groups Projects
Commit 7460987d authored by Yege1893's avatar Yege1893
Browse files

cancel running

parent a24105fb
No related branches found
No related tags found
1 merge request!4Master
...@@ -69,7 +69,9 @@ func CancelOrder(w http.ResponseWriter, r *http.Request) { ...@@ -69,7 +69,9 @@ func CancelOrder(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusBadRequest) http.Error(w, err.Error(), http.StatusBadRequest)
return return
} }
order, err := getOrder(r) fmt.Println(orderId, "orderid")
order, err := service.GetOrderById(orderId)
fmt.Println("order ", order)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest) http.Error(w, err.Error(), http.StatusBadRequest)
return return
...@@ -89,9 +91,9 @@ func CancelOrder(w http.ResponseWriter, r *http.Request) { ...@@ -89,9 +91,9 @@ func CancelOrder(w http.ResponseWriter, r *http.Request) {
log.Errorf("Failure loading internal user Info %v", err) log.Errorf("Failure loading internal user Info %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
} }
order.User = *internalUser if order.User != *internalUser {
order.ID = orderId sendJson(w, "user is not allowed to cancel this order")
fmt.Println(orderId, "orderId") }
err = service.CancelOrder(id, order) err = service.CancelOrder(id, order)
if err != nil { if err != nil {
......
...@@ -60,7 +60,7 @@ func (s NatsServer) ConfirmCancel(e *model.EmialContent) error { ...@@ -60,7 +60,7 @@ func (s NatsServer) ConfirmCancel(e *model.EmialContent) error {
fmt.Println(errMarshal) fmt.Println(errMarshal)
return fmt.Errorf(errMarshal.Error()) return fmt.Errorf(errMarshal.Error())
} }
response, err := s.Nc.Request("confirmOrder."+string(e.OrderID), []byte(emailContenct), 2*time.Second) response, err := s.Nc.Request("confirmCancel."+string(e.OrderID), []byte(emailContenct), 2*time.Second)
if err != nil { if err != nil {
log.Println("Error making NATS request:", err) log.Println("Error making NATS request:", err)
return fmt.Errorf(err.Error()) return fmt.Errorf(err.Error())
...@@ -70,6 +70,6 @@ func (s NatsServer) ConfirmCancel(e *model.EmialContent) error { ...@@ -70,6 +70,6 @@ func (s NatsServer) ConfirmCancel(e *model.EmialContent) error {
return fmt.Errorf(err.Error()) return fmt.Errorf(err.Error())
} }
fmt.Println("hier die nats response", &res) fmt.Println("hier die nats response", *res)
return nil return nil
} }
...@@ -2,6 +2,7 @@ package service ...@@ -2,6 +2,7 @@ package service
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"gitlab.reutlingen-university.de/ege/highlander-ticketing-go-ss2023/src/highlanderticketing/db" "gitlab.reutlingen-university.de/ege/highlander-ticketing-go-ss2023/src/highlanderticketing/db"
...@@ -179,6 +180,31 @@ func CancelOrder(matchID primitive.ObjectID, order *model.Order) error { ...@@ -179,6 +180,31 @@ func CancelOrder(matchID primitive.ObjectID, order *model.Order) error {
} }
func GetOrderById(orderID primitive.ObjectID) (*model.Order, error) {
client, err := db.GetMongoClient()
if err != nil {
return nil, err
}
collection := client.Database(db.DB).Collection(db.MATCHES)
filter := bson.M{"orders._id": orderID}
var result model.Match
err = collection.FindOne(context.TODO(), filter).Decode(&result)
if err != nil {
return nil, err
}
for _, order := range result.Orders {
if order.ID == orderID {
return &order, nil
}
}
return nil, errors.New("Order not found")
}
func deleteOrder(matchID primitive.ObjectID, orderID primitive.ObjectID) error { func deleteOrder(matchID primitive.ObjectID, orderID primitive.ObjectID) error {
filter := bson.D{primitive.E{Key: "_id", Value: matchID}} filter := bson.D{primitive.E{Key: "_id", Value: matchID}}
updater := bson.D{primitive.E{Key: "$pull", Value: bson.D{ updater := bson.D{primitive.E{Key: "$pull", Value: bson.D{
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment