From 6948d29ad70e4b4fc8b20c9bd453e1fff4dbfd35 Mon Sep 17 00:00:00 2001 From: Maria Flores Garcia Date: Mon, 24 Feb 2020 23:29:45 +0100 Subject: [PATCH] Edit advertisement Implemented edit advertisement --- .../controller/AdvertisementController.java | 29 +++- .../java/com/daw/webapp12/entity/Users.java | 1 + .../main/java/com/daw/webapp12/iniBBDD.java | 19 ++- .../security/SpringSecurityConfig.java | 1 + .../src/main/resources/templates/index.html | 2 +- .../resources/templates/properties-edit.html | 28 ++-- .../templates/properties-single.html | 3 +- .../main/resources/templates/properties.html | 2 +- .../resources/templates/property-upload.html | 124 ++---------------- 9 files changed, 73 insertions(+), 136 deletions(-) diff --git a/daw.webapp12/src/main/java/com/daw/webapp12/controller/AdvertisementController.java b/daw.webapp12/src/main/java/com/daw/webapp12/controller/AdvertisementController.java index 02df248..f82a8fb 100644 --- a/daw.webapp12/src/main/java/com/daw/webapp12/controller/AdvertisementController.java +++ b/daw.webapp12/src/main/java/com/daw/webapp12/controller/AdvertisementController.java @@ -1,5 +1,9 @@ package com.daw.webapp12.controller; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; @@ -25,6 +29,7 @@ import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.multipart.MultipartFile; @Controller public class AdvertisementController { @@ -251,13 +256,29 @@ public String searchAdvertisement(Model model , @RequestParam String location , public String editProperties(Model model, @PathVariable long id) { model.addAttribute("advertisement", advertisementService.findById(id)); //advertisementRepository.save(advertisement); - return "redirect:/property-edit"; + return "properties-edit"; } @PostMapping("/editProperties/{id}") - public String editProperties(Model model, Advertisement advertisement,@PathVariable long id) { - //model.addAttribute("advertisement", advertisementService.findById(id)); + public String editProperties(Model model, Advertisement advertisement,@PathVariable long id,@RequestParam("file") MultipartFile[] multipartFile, @RequestParam String location) { + List files = new ArrayList<>(5); + for (int i = 0; i < multipartFile.length; i++) { + if (!multipartFile[i].isEmpty()) { + Path directorioRecursos = Paths.get("daw.webapp12//src//main/resources//static//images"); + String rootPath = directorioRecursos.toFile().getAbsolutePath(); + try { + byte[] bytes = multipartFile[i].getBytes(); + Path rupacompleta = Paths.get(rootPath + "//" + multipartFile[i].getOriginalFilename()); + Files.write(rupacompleta, bytes); + files.add(multipartFile[i].getOriginalFilename()); + + } catch (IOException e) { + e.printStackTrace(); + } + } + } + advertisement.setImages(files); advertisementRepository.save(advertisement); - return "properties-modificar"; + return "redirect:/properties-modificar"; } @RequestMapping("/deleteAdvertisement/{id}") diff --git a/daw.webapp12/src/main/java/com/daw/webapp12/entity/Users.java b/daw.webapp12/src/main/java/com/daw/webapp12/entity/Users.java index 2d5dd53..d4962a6 100644 --- a/daw.webapp12/src/main/java/com/daw/webapp12/entity/Users.java +++ b/daw.webapp12/src/main/java/com/daw/webapp12/entity/Users.java @@ -44,6 +44,7 @@ public Users(String name, String email, String password,String... roles) { this.password = new BCryptPasswordEncoder().encode(password); this.roles = new ArrayList<>(Arrays.asList(roles)); this.myFavourites = new ArrayList(); + this.myAdvertisements = new ArrayList(); this.mySearches = new ArrayList(); } diff --git a/daw.webapp12/src/main/java/com/daw/webapp12/iniBBDD.java b/daw.webapp12/src/main/java/com/daw/webapp12/iniBBDD.java index 47109f3..24f2ab6 100644 --- a/daw.webapp12/src/main/java/com/daw/webapp12/iniBBDD.java +++ b/daw.webapp12/src/main/java/com/daw/webapp12/iniBBDD.java @@ -9,6 +9,9 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; +import java.util.ArrayList; +import java.util.List; + import javax.annotation.PostConstruct; @Component @@ -35,7 +38,7 @@ public void init(){ userRepository.save(users4); //Users users5 = new Users("Maria","maria@gmail.com","12345678"); //userRepository.save(users5); - + //Advertisement(String tipo, String vivienda, Integer habitaciones, Integer baños, String metros2, String localizacion, String direccion, double precio) Advertisement anun1 = new Advertisement("Venta","Casa",(Integer)4,(Integer)2,120,"Madrid","calle azul,2",(double)200000); Comment coment1= new Comment("Hola, me ha encantado"); @@ -43,24 +46,32 @@ public void init(){ userRepository.save(users5); coment1.setAuthor(users5); commentRepository.save(coment1); - + anun1.getImages().add("work-1.jpg"); anun1.getComments().add(coment1); anuncioRepository.save(anun1); Advertisement anun2 = new Advertisement("Venta","Local",(Integer)2,(Integer)1,50,"Madrid","calle verde,3",(double)120000); + anun2.getImages().add("work-7.jpg"); anuncioRepository.save(anun2); Advertisement anun3 = new Advertisement("Alquiler","Piso",(Integer)3,(Integer)1,90,"Pontevedra","calle carlos v,4,2 C",(double)1600); + anun3.getImages().add("work-3.jpg"); anuncioRepository.save(anun3); Advertisement anun4 = new Advertisement("Venta","Casa",(Integer)2,(Integer)1,56,"Madrid","calle verde,3",(double)78990); + anun4.getImages().add("work-5.jpg"); anuncioRepository.save(anun4); Advertisement anun5 = new Advertisement("Alquiler","Local",(Integer)1,(Integer)1,78,"Madrid","calle verde,3",(double)1200); + anun5.getImages().add("work-4.jpg"); anuncioRepository.save(anun5); Advertisement anun6 = new Advertisement("Venta","Local",(Integer)4,(Integer)2,85,"Mostoles","calle verde,3",(double)140000); + anun6.getImages().add("work-7.jpg"); anuncioRepository.save(anun6); Advertisement anun7 = new Advertisement("Alquiler","Local",(Integer)3,(Integer)1,78,"Mostoles","calle verde,3",(double)1200); + anun7.getImages().add("work-6.jpg"); anuncioRepository.save(anun7); Advertisement anun8 = new Advertisement("Alquiler","Local",(Integer)1,(Integer)1,78,"Valencia","calle verde,3",(double)1000); + anun8.getImages().add("work-7.jpg"); anuncioRepository.save(anun8); Advertisement anun9 = new Advertisement("Alquiler","Local",(Integer)3,(Integer)1,78,"Ciudad Real","calle verde,3",(double)650); + anun9.getImages().add("work-2.jpg"); anuncioRepository.save(anun9); Search search1 = new Search("Alquiler",(Integer)2,(Integer)1,60,"Madrid",(double)800); @@ -74,6 +85,10 @@ public void init(){ users1.getMyFavourites().add(anun2); users1.getMyFavourites().add(anun3); + /*users1.getMyAdvertisements().add(anun4); + users1.getMyAdvertisements().add(anun5); + users1.getMyAdvertisements().add(anun6);*/ + users1.getMySearches().add(search1); users1.getMySearches().add(search2); users1.getMySearches().add(search4); diff --git a/daw.webapp12/src/main/java/com/daw/webapp12/security/SpringSecurityConfig.java b/daw.webapp12/src/main/java/com/daw/webapp12/security/SpringSecurityConfig.java index 7ca7d46..5883f5c 100644 --- a/daw.webapp12/src/main/java/com/daw/webapp12/security/SpringSecurityConfig.java +++ b/daw.webapp12/src/main/java/com/daw/webapp12/security/SpringSecurityConfig.java @@ -50,6 +50,7 @@ protected void configure(HttpSecurity http) throws Exception { //only admin & register users http.authorizeRequests().antMatchers("/property-upload").hasAnyRole("USER", "ADMIN"); + http.authorizeRequests().antMatchers("/properties-edit").hasAnyRole("USER"); http.authorizeRequests().antMatchers("/properties").hasAnyRole("USER", "ADMIN"); http.authorizeRequests().antMatchers("/property-modificar").hasAnyRole("USER", "ADMIN"); diff --git a/daw.webapp12/src/main/resources/templates/index.html b/daw.webapp12/src/main/resources/templates/index.html index e845353..370b6b0 100644 --- a/daw.webapp12/src/main/resources/templates/index.html +++ b/daw.webapp12/src/main/resources/templates/index.html @@ -81,7 +81,7 @@

Viviendas seleccionadas para ti

{{#recommendedAds}}
- +

{{price}}{{price}}/mo

    diff --git a/daw.webapp12/src/main/resources/templates/properties-edit.html b/daw.webapp12/src/main/resources/templates/properties-edit.html index e20e1de..63e7282 100644 --- a/daw.webapp12/src/main/resources/templates/properties-edit.html +++ b/daw.webapp12/src/main/resources/templates/properties-edit.html @@ -1,7 +1,7 @@ - property upload page + property edit page @@ -20,23 +20,23 @@

    PUBLICAR ANUNCIO


    {{#advertisement}} -
    +

    Tipo de anuncio:

    -

    Tipo de vivienda:

    -

    Nº de habitaciones:

    -

    Nº de baños:

    - @@ -59,27 +59,27 @@

    Nº de baños:

    Metros cuadrados:

    -

    Introduzca la Localidad:




    - +

    Introduzca el precio del anuncio:

    + value="{{price}}">

    Introduzca la dirección:




    -
    - + - - + {{/advertisement}} +
diff --git a/daw.webapp12/src/main/resources/templates/properties-single.html b/daw.webapp12/src/main/resources/templates/properties-single.html index 1f04ec2..f227215 100644 --- a/daw.webapp12/src/main/resources/templates/properties-single.html +++ b/daw.webapp12/src/main/resources/templates/properties-single.html @@ -102,7 +102,6 @@

{{property}} en {{location}}

- - + {{>footer}} \ No newline at end of file diff --git a/daw.webapp12/src/main/resources/templates/properties.html b/daw.webapp12/src/main/resources/templates/properties.html index c377298..ec6c703 100644 --- a/daw.webapp12/src/main/resources/templates/properties.html +++ b/daw.webapp12/src/main/resources/templates/properties.html @@ -21,7 +21,7 @@

Tus
anuncios favoritos

{{#Favourites}}
- +

{{price}}$

    diff --git a/daw.webapp12/src/main/resources/templates/property-upload.html b/daw.webapp12/src/main/resources/templates/property-upload.html index 6b5e713..027a3a4 100644 --- a/daw.webapp12/src/main/resources/templates/property-upload.html +++ b/daw.webapp12/src/main/resources/templates/property-upload.html @@ -22,20 +22,20 @@

    PUBLICAR ANUNCIO

    Tipo de anuncio:

    -

    Tipo de vivienda:

    -

    Nº de habitaciones:

    -

    Nº de baños:

    -

    Metros cuadrados:

    -

    Introduzca la Localidad:




    -
    - - - -

    Introduzca el precio del anuncio:

    -

    Introduzca la dirección:





    - + - + - -
-
- +
+ + \ No newline at end of file