77import com .csaba79coder .littersnap .util .Mapper ;
88import org .springframework .stereotype .Controller ;
99import org .springframework .ui .Model ;
10- import org .springframework .web .bind .annotation .GetMapping ;
11- import org .springframework .web .bind .annotation .ModelAttribute ;
12- import org .springframework .web .bind .annotation .PathVariable ;
13- import org .springframework .web .bind .annotation .PostMapping ;
14- import org .springframework .web .bind .annotation .RequestMapping ;
15- import org .springframework .web .bind .annotation .RequestParam ;
10+ import org .springframework .web .bind .annotation .*;
1611import org .springframework .web .multipart .MultipartFile ;
1712
1813import java .util .Base64 ;
@@ -32,9 +27,9 @@ public LitterViewController(LitterService litterService) {
3227
3328
3429 @ GetMapping
35- public String getAllLitters (Model model ) {
30+ public String renderAllLitters (Model model ) {
3631 try {
37- List <LitterModel > litters = litterService .getAllLitters ();
32+ List <LitterModel > litters = litterService .findAllLitters ();
3833 model .addAttribute ("litters" , litters );
3934 model .addAttribute ("view" , "litter_list" );
4035 return "welcome" ; // Replace with the actual view name for displaying the list of litters
@@ -45,9 +40,9 @@ public String getAllLitters(Model model) {
4540 }
4641
4742 @ GetMapping ("/{id}" )
48- public String getLitterById (@ PathVariable ("id" ) UUID id , Model model ) {
43+ public String renderLitterById (@ PathVariable ("id" ) UUID id , Model model ) {
4944 try {
50- LitterModel litter = litterService .getLitterById (id );
45+ LitterModel litter = litterService .findLitterById (id );
5146 model .addAttribute ("id" , litter .getId ());
5247 model .addAttribute ("createdAt" , litter .getCreatedAt ());
5348 model .addAttribute ("updatedAt" , litter .getUpdatedAt ());
@@ -71,25 +66,23 @@ public String getLitterById(@PathVariable("id") UUID id, Model model) {
7166 }
7267
7368 @ GetMapping ("/create" )
74- public String showAddLitterForm (Model model ) {
69+ public String showAddLitterForm (Model model , @ RequestParam ( value = "city" , required = false ) String capturedCity ) {
7570 try {
7671 LitterCreateOrModifyModel litterModel = new LitterCreateOrModifyModel ();
77-
7872 // Set any other necessary properties in the litterModel object
7973
74+ model .addAttribute ("city" , capturedCity );
8075 model .addAttribute ("litter" , litterModel );
81- return "litter_add_form" ;
76+ model .addAttribute ("view" ,"litter_add_form" );
77+ return "welcome" ;
8278 } catch (NoSuchElementException e ) {
8379 model .addAttribute ("errorMessage" , e .getMessage ());
8480 return "error_page" ; // Redirect to the error page to display the error message
8581 }
8682 }
8783
8884 @ PostMapping ("/create" )
89- public String addNewLitter (@ ModelAttribute ("litter" ) LitterCreateOrModifyModel litterModel ,
90- @ ModelAttribute ("address" ) Address address ,
91- @ RequestParam ("file" ) MultipartFile file ,
92- Model model ) {
85+ public String addNewLitter (@ ModelAttribute ("litter" ) LitterCreateOrModifyModel litterModel , @ ModelAttribute ("address" ) Address address , @ RequestParam ("file" ) MultipartFile file , Model model ) {
9386 try {
9487 litterService .addNewLitter (litterModel , address , file );
9588 return "redirect:/thy/litter" ;
@@ -102,15 +95,15 @@ public String addNewLitter(@ModelAttribute("litter") LitterCreateOrModifyModel l
10295 @ GetMapping ("/edit/{id}" )
10396 public String showEditForm (@ PathVariable UUID id , Model model ) {
10497 try {
105- LitterCreateOrModifyModel litter = Mapper .mapModelToLitterCreateOrModifyModel (litterService .getLitterById (id ));
98+ LitterCreateOrModifyModel litter = Mapper .mapModelToLitterCreateOrModifyModel (litterService .findLitterById (id ));
10699 model .addAttribute ("id" , litter .getId ());
107100 model .addAttribute ("firstline" , litter .getAddress ().getFirstLine ());
108101 model .addAttribute ("city" , litter .getAddress ().getCity ());
109102 model .addAttribute ("country" , litter .getAddress ().getCountry ());
110103 model .addAttribute ("postcode" , litter .getAddress ().getPostCode ());
111104 model .addAttribute ("description" , litter .getDescription ());
112105 model .addAttribute ("image" , litter .getImage ());
113- model .addAttribute ("view" ,"litter_edit_form" );
106+ model .addAttribute ("view" , "litter_edit_form" );
114107 return "welcome" ;
115108 } catch (NoSuchElementException e ) {
116109 model .addAttribute ("errorMessage" , e .getMessage ());
@@ -119,9 +112,9 @@ public String showEditForm(@PathVariable UUID id, Model model) {
119112 }
120113
121114 @ PostMapping ("/edit/{id}" )
122- public String updateLitter (@ PathVariable UUID id , @ ModelAttribute ("report" ) LitterCreateOrModifyModel litterModel , Model model ) {
115+ public String modifyExistingLitter (@ PathVariable UUID id , @ ModelAttribute ("report" ) LitterCreateOrModifyModel litterModel , Model model ) {
123116 try {
124- litterService .updateExistingLitter (id , litterModel );
117+ litterService .modifyAnExistingLitter (id , litterModel );
125118 return "redirect:/reports" ; // Redirect to the URL for displaying all reports after successful update
126119 } catch (NoSuchElementException e ) {
127120 model .addAttribute ("errorMessage" , e .getMessage ());
@@ -131,18 +124,13 @@ public String updateLitter(@PathVariable UUID id, @ModelAttribute("report") Litt
131124
132125
133126 @ GetMapping ("/delete/{id}" )
134- public String deleteLitter (@ PathVariable UUID id ,Model model ) {
135-
127+ public String deleteAnExistingLitter (@ PathVariable UUID id , Model model ) {
136128 try {
137- litterService .deleteLitter (id );
129+ litterService .deleteAnExistingLitter (id );
138130 return "redirect:/thy/litter" ; // Redirect to the URL for displaying all reports after successful deletion
139131 } catch (NoSuchElementException e ) {
140132 model .addAttribute ("errorMessage" , e .getMessage ());
141133 return "error_page" ; // Redirect to the error page to display the error message
142134 }
143-
144135 }
145-
146-
147-
148136}
0 commit comments