-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReservaHabitacionController.java
130 lines (105 loc) · 4.22 KB
/
ReservaHabitacionController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package pe.edu.upc.spring.controller;
import java.util.Map;
import javax.validation.Valid;
import java.text.ParseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import pe.edu.upc.spring.service.IHabitacionService;
import pe.edu.upc.spring.service.IReservaHabitacionService;
import pe.edu.upc.spring.service.IReservaService;
import pe.edu.upc.spring.model.ReservaHabitacion;
@Controller
@RequestMapping("/reservahabitacion")
public class ReservaHabitacionController {
@Autowired
private IReservaHabitacionService rhService;
@Autowired
private IHabitacionService hService;
@Autowired
private IReservaService rService;
@RequestMapping("/listar")
public String listar(Map<String, Object> model) {
model.put("listaReservaHabitaciones", rhService.listar());
model.put("listaHabitacion", hService.listar());
model.put("listaReserva", rService.listar());
return "listReservaHabitaciones";
}
@RequestMapping("/irRegistrar")
public String irRegistrar(Model model) {
model.addAttribute("reservahabitacion", new ReservaHabitacion());
model.addAttribute("listaHabitacion",hService.listar());
model.addAttribute("listaReserva",rService.listar());
return "reservahabitacion";
}
@RequestMapping("/registrar")
public String registrar(@ModelAttribute @Valid ReservaHabitacion objReservaHabitacion, BindingResult binRes, Model model) throws ParseException{
if(binRes.hasErrors()) {
model.addAttribute("reservahabitacion",new ReservaHabitacion());
model.addAttribute("listaHabitacion",hService.listar());
model.addAttribute("listaReserva",rService.listar());
return "reservahabitacion";
}
else {
boolean flag=rhService.insertar(objReservaHabitacion);
if(flag) {
return "redirect:/reservahabitacion/listar";
}
else {
model.addAttribute("mensaje","Ocurrió un error");
return "redirect:/reservahabitacion/irRegistrar";
}
}
}
@RequestMapping("/actualizar")
public String actualizar(@ModelAttribute @Valid ReservaHabitacion objReservaHabitacion, BindingResult binRes, Model model, RedirectAttributes objRedir)
throws ParseException{
if(binRes.hasErrors()) {
return "redirect:/reservahabitacion/listar";
}
else {
boolean flag=rhService.modificar(objReservaHabitacion);
if(flag) {
objRedir.addFlashAttribute("mensaje","Se actualizó correctamente");
return "redirect:/reservahabitacion/listar";
}
else {
objRedir.addFlashAttribute("mensaje", "ocurrió un error");
return "redirect:/reservahabitacion/listar";
}
}
}
@RequestMapping("/modificar/{id}")
public String modificar(@PathVariable int id, Model model, RedirectAttributes objRedir) {
ReservaHabitacion objReservaHabitacion=rhService.listarId(id);
if(objReservaHabitacion==null) {
objRedir.addFlashAttribute("mensaje", "ocurrió un error");
return "redirect:/reservahabitacion/listar";
}
else {
model.addAttribute("listaHabitacion", hService.listar());
model.addAttribute("listaReserva",rService.listar());
model.addAttribute("reservahabitacion", objReservaHabitacion);
return "reservahabitacion";
}
}
@RequestMapping("/eliminar")
public String eliminar(Map<String, Object> model, @RequestParam(value="id")Integer id) {
if(id !=null && id>0) {
rhService.eliminar(id);
model.put("listaReservaHabitaciones", rhService.listar());
}
return "listReservaHabitaciones";
}
@RequestMapping("/listarId")
public String listarId(Map<String, Object> model, @ModelAttribute ReservaHabitacion reservahabitacion) {
rhService.listarId(reservahabitacion.getIdReservaHabitacion());
return "listReservaHabitaciones";
}
}