Skip to content

Commit

Permalink
Edit advertisement
Browse files Browse the repository at this point in the history
Implemented edit advertisement
  • Loading branch information
mariafg11 committed Feb 24, 2020
1 parent e0f24dd commit 6948d29
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 136 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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<String> 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}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Advertisement>();
this.myAdvertisements = new ArrayList<Advertisement>();
this.mySearches = new ArrayList<Search>();
}

Expand Down
19 changes: 17 additions & 2 deletions daw.webapp12/src/main/java/com/daw/webapp12/iniBBDD.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -35,32 +38,40 @@ 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");
Users users5 = new Users("Maria","maria@gmail.com","12345678","ROLE_ADMIN");
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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down
2 changes: 1 addition & 1 deletion daw.webapp12/src/main/resources/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ <h2 class="mb-2">Viviendas seleccionadas para ti</h2>
{{#recommendedAds}}
<div class="col-md-4">
<div class="property-wrap ftco-animate">
<a href="#" class="img" style="background-image: url(images/work-1.jpg);"></a>
<a href= "/properties/{{id}}" class="img" style="background-image: url(images/{{images.0}});"></a>
<div class="text">
<p class="price"><span class="old-price">{{price}}</span><span class="orig-price">{{price}}<small>/mo</small></span></p>
<ul class="property_list">
Expand Down
28 changes: 14 additions & 14 deletions daw.webapp12/src/main/resources/templates/properties-edit.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<html>
<head>
<title>property upload page</title>
<title>property edit page</title>
<link rel="stylesheet" href="css/login.css">
<link rel="stylesheet" href="css/propertyUpload.css">
<meta charset="utf-8">
Expand All @@ -20,23 +20,23 @@
<h2 class="text-center witt" id="submitAdvertisementButton">PUBLICAR ANUNCIO <br></h2>
<br>
{{#advertisement}}
<form action="/editProperties" method="post" enctype="multipart/form-data">
<form action="/editProperties/{{id}}" method="post" enctype="multipart/form-data">

<h3 class="botonDesp1">Tipo de anuncio:</h3>
<select class="botonDesp" name="type" id="type"
<select required class="botonDesp" name="type" id="type"
value="{{type}}">
<option value="venta">Venta</option>
<option value="alquiler">Alquiler</option>
</select>
<h3 class="botonDesp1">Tipo de vivienda:</h3>
<select class="botonDesp" name="property" id="property"
<select required class="botonDesp" name="property" id="property"
value="{{property}}">
<option value="Piso">Piso</option>
<option value="Casa">Casa</option>
<option value="Local">Local</option>
</select>
<h3 class="botonDesp1">Nº de habitaciones:</h3>
<select class="botonDesp" name="rooms" id="rooms"
<select required class="botonDesp" name="rooms" id="rooms"
value="{{rooms}}">
<option value="1">1</option>
<option value="2">2</option>
Expand All @@ -49,7 +49,7 @@ <h3 class="botonDesp1">Nº de habitaciones:</h3>
<option value="9">9</option>
</select>
<h3 class="botonDesp1">Nº de baños:</h3>
<select class="botonDesp" name="bathrooms" id="bathrooms" value="{{bathrooms}}">
<select required class="botonDesp" name="bathrooms" id="bathrooms" value="{{bathrooms}}">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
Expand All @@ -59,27 +59,27 @@ <h3 class="botonDesp1">Nº de baños:</h3>
</select>
<h3 class="botonDesp1">Metros cuadrados:</h3>

<input type="number" min="15" max="800" id="squareMeters" name="squareMeters"
<input required type="number" min="15" max="800" id="squareMeters" name="squareMeters"
value="{{squareMeters}}"/>

<h3 class="botonDesp2">Introduzca la Localidad:</h3><br><br><br>
<input name="location" type="text" id="location"
value="{{#location}}">
<input required name="location" type="text" id="location"
value="{{location}}">
<br>

<h3 class="botonDesp1">Introduzca el precio del anuncio:</h3>
<input class="numberInput" type="number" min="100" max="2000000" name="price" id="price"
value="{{advertisement.price}}">
value="{{price}}">
<h3 class="botonDesp2">Introduzca la dirección:</h3><br><br><br>
<input name="address" type="text" id="address"
<input required name="address" type="text" id="address"
value="{{address}}">
<br>
<label for="file">Select image:</label>
<input type="file" id="file" name="file" multiple>
<input required value="{{images}}" type="file" id="file" name="file" multiple>
<input type="submit">


<input type="submit" value="subir anuncio" class="btn btn-primary py-3 px-5">
{{/advertisement}}
<!-- <input type="submit" value="subir anuncio" class="btn btn-primary py-3 px-5"> -->
<input type="hidden" name="_csrf" value="{{token}}"/>
</form>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ <h2>{{property}} en {{location}}</h2>
</div>
</div>
</div>
</div>
</section>
</section>
{{>footer}}
</html>
2 changes: 1 addition & 1 deletion daw.webapp12/src/main/resources/templates/properties.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ <h1 class="mb-3 bread">Tus <br>anuncios favoritos</h1>
{{#Favourites}}
<div class="col-md-4">
<div class="property-wrap ftco-animate">
<a href="properties/{{id}}" class="img" style="background-image: url(images/work-1.jpg);"></a>
<a href="properties/{{id}}" class="img" style="background-image: url(images/{{images.0}});"></a>
<div class="text">
<p class="price"><span class="orig-price">{{price}}<small>$</small></span></p>
<ul class="property_list">
Expand Down
Loading

0 comments on commit 6948d29

Please sign in to comment.