diff --git a/menu/manageReservation/createReservation.go b/menu/manageReservation/createReservationMenu.go similarity index 97% rename from menu/manageReservation/createReservation.go rename to menu/manageReservation/createReservationMenu.go index 26d337c75716df452150991fce24214b461a0652..20406ca489d7e0516b04664e8a8983a1426b3705 100644 --- a/menu/manageReservation/createReservation.go +++ b/menu/manageReservation/createReservationMenu.go @@ -14,7 +14,7 @@ import ( "strings" ) -func createReservation(fechaIda string, fechaVuelta string, origen string, destino string, cantidadPasajeros int) { +func createReservationMenu(fechaIda string, fechaVuelta string, origen string, destino string, cantidadPasajeros int) { // Seleccionar vuelos fmt.Println("Vuelos disponibles:") vuelos, precioPasajeIda, precioPasajeVuelta := chooseVuelos(fechaIda, fechaVuelta, origen, destino) @@ -65,7 +65,7 @@ func createReservation(fechaIda string, fechaVuelta string, origen string, desti log.Fatal("Error del servidor al crear la reserva") } - if resp.StatusCode != http.StatusOK { + if resp.StatusCode != http.StatusCreated { log.Fatal("Error del servidor al crear la reserva") return } diff --git a/menu/manageReservation/getReservation.go b/menu/manageReservation/getReservationMenu.go similarity index 85% rename from menu/manageReservation/getReservation.go rename to menu/manageReservation/getReservationMenu.go index a701a3617627d96175294c1be5cda17428f2970c..99f4537d866255cd59afb75923c1bb58151bd858 100644 --- a/menu/manageReservation/getReservation.go +++ b/menu/manageReservation/getReservationMenu.go @@ -11,7 +11,7 @@ import ( ) // Obtener reserva mostrando los datos necesarios para el menú -func getReservation(pnr string, apellido string) { +func getReservationMenu(pnr string, apellido string) { queries := map[string]string{ "pnr": pnr, "apellido": apellido, @@ -86,7 +86,7 @@ func getReservation(pnr string, apellido string) { } // Obtener reserva, pero sin prints -func discreteGetReservation(pnr string, apellido string) *models.Reserva { +func discreteGetReservation(pnr string, apellido string) models.Reserva { queries := map[string]string{ "pnr": pnr, "apellido": apellido, @@ -96,18 +96,21 @@ func discreteGetReservation(pnr string, apellido string) *models.Reserva { resp, err := http.Get(url) if err != nil { - return nil + log.Fatal("Reserva no encontrada") + return models.Reserva{} } body, err := ioutil.ReadAll(resp.Body) if err != nil { - return nil + log.Fatal("Reserva no encontrada") + return models.Reserva{} } var reserva models.Reserva if err := json.Unmarshal(body, &reserva); err != nil { - return nil + log.Fatal("Reserva no encontrada") + return models.Reserva{} } - return &reserva + return reserva } diff --git a/menu/manageReservation/manageReservation.go b/menu/manageReservation/manageReservationMenu.go similarity index 86% rename from menu/manageReservation/manageReservation.go rename to menu/manageReservation/manageReservationMenu.go index 2914043204c7f2784dec1dfbf7a480abd184d4f4..46865d02707a9f96cf1c7e8d5988c2cc71be9fa8 100644 --- a/menu/manageReservation/manageReservation.go +++ b/menu/manageReservation/manageReservationMenu.go @@ -4,7 +4,7 @@ import ( "fmt" ) -func ManageReservation() { +func ManageReservationMenu() { var keepRunning = true for keepRunning { @@ -37,7 +37,7 @@ func ManageReservation() { fmt.Print("Ingrese la cantidad de pasajeros: ") fmt.Scanln(&cantidadPasajeros) - createReservation(fechaIda, fechaVuelta, origen, destino, cantidadPasajeros) + createReservationMenu(fechaIda, fechaVuelta, origen, destino, cantidadPasajeros) } if option == 2 { @@ -50,7 +50,7 @@ func ManageReservation() { fmt.Print("Ingrese el apellido: ") fmt.Scanln(&apellido) - getReservation(pnr, apellido) + getReservationMenu(pnr, apellido) } if option == 3 { @@ -63,7 +63,7 @@ func ManageReservation() { fmt.Print("Ingrese el apellido: ") fmt.Scanln(&apellido) - updateReservation(pnr, apellido) + updateReservationMenu(pnr, apellido) } if option == 4 { diff --git a/menu/manageReservation/updateReservation.go b/menu/manageReservation/updateReservation.go deleted file mode 100644 index 90222cc408dcb34f6412c807610dab99af5a90b1..0000000000000000000000000000000000000000 --- a/menu/manageReservation/updateReservation.go +++ /dev/null @@ -1,143 +0,0 @@ -package menu - -import ( - "bytes" - "distribuidos/tarea-1/models" - "distribuidos/tarea-1/utilities" - "encoding/json" - "fmt" - "io/ioutil" - "log" - "net/http" -) - -func updateReservation(pnr string, apellido string) { - keepRunning := true - - for keepRunning { - fmt.Println( - "Opciones:\n", - "1. Cambiar fecha de vuelo\n", - "2. Adicionar ancillaries\n", - "3. Salir", - ) - - var option int - fmt.Print("Ingrese una opción: ") - fmt.Scanln(&option) - - if option == 3 { - keepRunning = false - break - } - - var updatedReservation *models.Reserva - - if option == 1 { - var err error - updatedReservation, err = changeFlightDate(pnr, apellido) - - if err != nil { - log.Fatal("No existen vuelos para la fecha ingresada") - continue - } - } - - performUpdate(pnr, apellido, updatedReservation) - } -} - -func changeFlightDate(pnr string, apellido string) (*models.Reserva, error) { - reserva := discreteGetReservation(pnr, apellido) - - if reserva == nil { - return nil, fmt.Errorf("No existe una reserva con los datos ingresados") - } - - fmt.Println("Vuelos:") - fmt.Printf("1. ida: %s %s - %s\n", reserva.Vuelos[0].NumeroVuelo, reserva.Vuelos[0].HoraSalida, reserva.Vuelos[0].HoraLlegada) - fmt.Printf("2. vuelta: %s %s - %s\n", reserva.Vuelos[1].NumeroVuelo, reserva.Vuelos[1].HoraSalida, reserva.Vuelos[1].HoraLlegada) - - var vueloToReplace int - fmt.Print("Ingrese una opción: ") - fmt.Scanln(&vueloToReplace) - vueloToReplace -= 1 - - vuelo := reserva.Vuelos[vueloToReplace] - - var newDate string - fmt.Print("Ingrese nueva fecha: ") - fmt.Scanln(&newDate) - - vuelos := getVuelos(vuelo, newDate) - - fmt.Println("Vuelos disponibles:") - for i, vuelo := range vuelos { - fmt.Printf("%d. %s %s - %s\n", i+1, vuelo.NumeroVuelo, vuelo.HoraSalida, vuelo.HoraLlegada) - } - var selectedNewVuelo string - fmt.Print("Ingrese una opción: ") - fmt.Scanln(&selectedNewVuelo) - - // Actualizar reserva - reserva.Vuelos[vueloToReplace] = vuelos[vueloToReplace] - - return reserva, nil -} - -func getVuelos(vuelo models.Vuelo, newDate string) []models.Vuelo { - queries := map[string]string{ - "origen": vuelo.Origen, - "destino": vuelo.Destino, - "fecha": newDate, - } - url := utilities.CreateUrl("vuelo", queries) - - resp, err := http.Get(url) - if err != nil { - return nil - } - - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - return nil - } - - var vuelos []models.Vuelo - if err := json.Unmarshal(body, &vuelos); err != nil { - return nil - } - - return vuelos -} - -func performUpdate(pnr string, apellido string, updatedReservation *models.Reserva) { - // Actualizar la reserva en la base de datos - queries := map[string]string{ - "pnr": pnr, - "apellido": apellido, - } - url := utilities.CreateUrl("reserva", queries) - - updatedReservationJson, err := json.Marshal(&updatedReservation) - if err != nil { - log.Fatal("Error al actualizar la reserva") - } - - req, err := http.NewRequest(http.MethodPut, url, bytes.NewBuffer(updatedReservationJson)) - if err != nil { - log.Fatal("Error al actualizar la reserva") - } - - req.Header.Set("Content-Type", "application/json") - client := http.Client{} - resp, err := client.Do(req) - - defer req.Body.Close() - - if resp.StatusCode == http.StatusOK { - fmt.Println("La reserva fué modificada exitosamente!") - } else { - log.Fatal("Error del servidor al actualizar la reserva") - } -} diff --git a/menu/manageReservation/updateReservationMenu.go b/menu/manageReservation/updateReservationMenu.go new file mode 100644 index 0000000000000000000000000000000000000000..8a2a419d93eb26f1db9119df605ced6c837a7a5a --- /dev/null +++ b/menu/manageReservation/updateReservationMenu.go @@ -0,0 +1,200 @@ +package menu + +import ( + "bytes" + "distribuidos/tarea-1/models" + "distribuidos/tarea-1/utilities" + "encoding/json" + "fmt" + "io/ioutil" + "log" + "net/http" +) + +func updateReservationMenu(pnr string, apellido string) { + keepRunning := true + + for keepRunning { + fmt.Println( + "Opciones:\n", + "1. Cambiar fecha de vuelo\n", + "2. Adicionar ancillaries\n", + "3. Salir", + ) + + var option int + fmt.Print("Ingrese una opción: ") + fmt.Scanln(&option) + + if option == 3 { + keepRunning = false + break + } + + // Obtener reserva con los datos ingresados + originalReservation := discreteGetReservation(pnr, apellido) + + if len(originalReservation.Pasajeros) == 0 && len(originalReservation.Vuelos) == 0 { + log.Fatal("Reserva no encontrada") + break + } + + if option == 1 { + // Mostrar vuelos que se pueden cambiar + fmt.Println("Vuelos:") + fmt.Printf("1. ida: %s %s - %s\n", originalReservation.Vuelos[0].NumeroVuelo, originalReservation.Vuelos[0].HoraSalida, originalReservation.Vuelos[0].HoraLlegada) + fmt.Printf("2. vuelta: %s %s - %s\n", originalReservation.Vuelos[1].NumeroVuelo, originalReservation.Vuelos[1].HoraSalida, originalReservation.Vuelos[1].HoraLlegada) + + // Elegir vuelo a cambiar + var vueloToReplace int + fmt.Print("Ingrese una opción: ") + fmt.Scanln(&vueloToReplace) + vueloToReplace -= 1 + + updatedReservation, err := changeFlightDate(originalReservation, vueloToReplace, pnr, apellido) + if err != nil { + log.Fatal(err) + continue + } + + updateReservation(pnr, apellido, updatedReservation) + updateStocks(updatedReservation, updatedReservation.Vuelos[vueloToReplace], originalReservation.Vuelos[vueloToReplace]) + } + } +} + +func changeFlightDate(reserva models.Reserva, vueloToReplace int, pnr string, apellido string) (models.Reserva, error) { + vuelo := reserva.Vuelos[vueloToReplace] + + // Elegir nueva fecha + var newDate string + fmt.Print("Ingrese nueva fecha: ") + fmt.Scanln(&newDate) + + vuelos := getVuelos(vuelo, newDate) + if len(vuelos) == 0 { + return models.Reserva{}, fmt.Errorf("No hay vuelos disponibles para la nueva fecha") + } + + // Mostrar vuelos disponibles para la nueva fecha + fmt.Println("Vuelos disponibles:") + for i, v := range vuelos { + fmt.Printf("%d. %s %s - %s\n", i+1, v.NumeroVuelo, v.HoraSalida, v.HoraLlegada) + } + var selectedNewVuelo string + fmt.Print("Ingrese una opción: ") + fmt.Scanln(&selectedNewVuelo) + + // Actualizar objeto con reserva + var newReserva models.Reserva + newReserva.PNR = reserva.PNR + newReserva.Pasajeros = append(newReserva.Pasajeros, reserva.Pasajeros...) + newReserva.Vuelos = append(newReserva.Vuelos, reserva.Vuelos...) + + newReserva.Vuelos[vueloToReplace] = vuelos[vueloToReplace] + + return newReserva, nil +} + +func getVuelos(vuelo models.Vuelo, newDate string) []models.Vuelo { + queries := map[string]string{ + "origen": vuelo.Origen, + "destino": vuelo.Destino, + "fecha": newDate, + } + url := utilities.CreateUrl("vuelo", queries) + + resp, err := http.Get(url) + if err != nil { + return nil + } + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil + } + + var vuelos []models.Vuelo + if err := json.Unmarshal(body, &vuelos); err != nil { + return nil + } + + return vuelos +} + +func updateReservation(pnr string, apellido string, updatedReservation models.Reserva) { + queries := map[string]string{ + "pnr": pnr, + "apellido": apellido, + } + url := utilities.CreateUrl("reserva", queries) + + updatedReservationJson, err := json.Marshal(&updatedReservation) + if err != nil { + log.Fatal("Error al actualizar la reserva") + } + + req, err := http.NewRequest(http.MethodPut, url, bytes.NewBuffer(updatedReservationJson)) + if err != nil { + log.Fatal("Error al actualizar la reserva") + } + + req.Header.Set("Content-Type", "application/json") + client := http.Client{} + resp, err := client.Do(req) + + defer req.Body.Close() + + if resp.StatusCode == http.StatusOK { + fmt.Println("La reserva fué modificada exitosamente!") + } else { + log.Fatal("Error del servidor al actualizar la reserva") + } +} + +func updateStocks(reserva models.Reserva, newVuelo models.Vuelo, oldVuelo models.Vuelo) { + updateStock(reserva, newVuelo, "decrease") + updateStock(reserva, oldVuelo, "add") +} + +func updateStock(reserva models.Reserva, vuelo models.Vuelo, operator string) { + queries := map[string]string{ + "numero_vuelo": vuelo.NumeroVuelo, + "origen": vuelo.Origen, + "destino": vuelo.Destino, + "fecha": vuelo.Fecha, + } + url := utilities.CreateUrl("vuelo", queries) + + var stock models.Stock + + if operator == "add" { + stock = models.Stock{ + Stock: vuelo.Avion.StockDePasajeros + len(reserva.Pasajeros), + } + } else { + stock = models.Stock{ + Stock: vuelo.Avion.StockDePasajeros - len(reserva.Pasajeros), + } + } + + stockJson, err := json.Marshal(&stock) + if err != nil { + log.Fatal("Error al actualizar el stock") + } + + req, err := http.NewRequest(http.MethodPut, url, bytes.NewBuffer(stockJson)) + if err != nil { + log.Fatal("Error al actualizar el stock") + } + + req.Header.Set("Content-Type", "application/json") + client := http.Client{} + resp, err := client.Do(req) + + defer req.Body.Close() + + if resp.StatusCode != http.StatusOK { + log.Fatal("Error del servidor al actualizar el stock") + } +} diff --git a/menu/menu.go b/menu/menu.go index 1d85095401d19e900d011b64134c712346dbba00..9b28e50566e41b7ca0e433ca4bf33de2f0e5fa07 100644 --- a/menu/menu.go +++ b/menu/menu.go @@ -20,7 +20,7 @@ func main() { fmt.Scanln(&option) if option == 1 { - menu.ManageReservation() + menu.ManageReservationMenu() } if option == 3 { diff --git a/models/models.go b/models/models.go index 0cef92da2b8f62cad8be7b0ade33c074cf9008cb..afcf17d6de0b90ac6b6643963fe8581e39a80078 100644 --- a/models/models.go +++ b/models/models.go @@ -63,3 +63,7 @@ type AncillarieData struct { Precio int `json:"precio,omitempty" bson:"precio,omitempty"` Ssr string `json:"ssr,omitempty" bson:"ssr,omitempty"` } + +type Stock struct { + Stock int `json:"stock_de_pasajeros" bson:"stock_de_pasajeros"` +} diff --git a/server/routes/vuelos.go b/server/routes/vuelos.go index 9e747f1299f50d72595f9fab5589578f826fa95d..53be13373f9accb1e9ff4da333ed3d04bfc2fcc8 100644 --- a/server/routes/vuelos.go +++ b/server/routes/vuelos.go @@ -75,9 +75,7 @@ func UpdateStock(c *gin.Context) { } // Bindear JSON a estructura newStock - newStock := new(struct { - Stock int `json:"stock_de_pasajeros" bson:"stock_de_pasajeros"` - }) + newStock := new(models.Stock) if err := c.BindJSON(&newStock); err != nil { c.JSON(http.StatusBadRequest, gin.H{"message": "JSON incorrecto"}) @@ -85,7 +83,8 @@ func UpdateStock(c *gin.Context) { return } - fmt.Println("newStock: ", newStock) + fmt.Printf("---%+v---\n", newStock) + // Actualizar stock en la base de datos filter := bson.M{"numero_vuelo": numero_vuelo, "origen": origen, "destino": destino, "fecha": fecha} update := bson.M{"$set": bson.M{"avion.stock_de_pasajeros": newStock.Stock}} @@ -112,8 +111,6 @@ func UpdateStock(c *gin.Context) { return } - fmt.Println("result: ", result) - c.JSON(http.StatusOK, gin.H{ "numero_vuelo": result.NumeroVuelo, "origen": result.Origen,