Skip to content

Commit 5387e08

Browse files
committed
Final configuration
1 parent aac3517 commit 5387e08

File tree

3 files changed

+132
-124
lines changed

3 files changed

+132
-124
lines changed

backend/src/main/java/es/codeurjc/wallypop/controller/DataBaseUsage.java

Lines changed: 0 additions & 123 deletions
This file was deleted.
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package es.codeurjc.wallypop.service;
2+
3+
import es.codeurjc.wallypop.model.Article;
4+
import es.codeurjc.wallypop.model.Category;
5+
import es.codeurjc.wallypop.model.Report;
6+
import es.codeurjc.wallypop.model.User;
7+
import es.codeurjc.wallypop.repository.UserRepository;
8+
import org.hibernate.engine.jdbc.BlobProxy;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.core.io.ClassPathResource;
11+
import org.springframework.core.io.Resource;
12+
import org.springframework.security.crypto.password.PasswordEncoder;
13+
import org.springframework.stereotype.Service;
14+
15+
import javax.annotation.PostConstruct;
16+
import java.io.IOException;
17+
import java.net.URISyntaxException;
18+
import java.sql.Blob;
19+
import java.util.LinkedList;
20+
import java.util.List;
21+
22+
@Service
23+
public class DatabaseInitializer {
24+
25+
@Autowired
26+
private UserRepository userRepository;
27+
28+
@Autowired
29+
private PasswordEncoder passwordEncoder;
30+
31+
@Autowired
32+
private CategoryService categoryService;
33+
34+
@Autowired
35+
private UserService userService;
36+
37+
@Autowired
38+
private ArticleService articleService;
39+
40+
@Autowired
41+
private ReportService reportService;
42+
43+
public Blob convertToBLOB(String ruta) throws IOException {
44+
Resource image = new ClassPathResource(ruta);
45+
return BlobProxy.generateProxy(image.getInputStream(), image.contentLength());
46+
}
47+
48+
@PostConstruct
49+
public void init() throws IOException, URISyntaxException {
50+
51+
if(userRepository.count() > 0) {
52+
return;
53+
}
54+
55+
// User
56+
userService.save(new User("Jesús", passwordEncoder.encode("1234"), "[email protected]", "666666666", true));
57+
userService.save(new User("David", passwordEncoder.encode("1234"), "[email protected]", "666666666"));
58+
59+
// Category
60+
categoryService.save(new Category("Videojuegos", "Compra y vende videojuegos de todo tipo",
61+
convertToBLOB("/sample_image/videojuego.jpg"), "gamepad"));
62+
categoryService.save(new Category("Ropa", "Dale una segunda vida a las prendas que ya no usas",
63+
convertToBLOB("/sample_image/ropa.png"), "shirt"));
64+
categoryService.save(new Category("Artesanía", "Lo mejor de la tierra cerca de tí",
65+
convertToBLOB("/sample_image/figurasArtesaniaCanaria.jpg"), "artstation"));
66+
categoryService.save(new Category("Herramientas", "Hazlo tu mismo, lo bueno sale barato",
67+
convertToBLOB("/sample_image/herramientas.jpg"), "screwdriver-wrench"));
68+
categoryService.save(new Category("Informática", "Viste a tu ordenador con los mejores complementos",
69+
convertToBLOB("/sample_image/informatica.jpg"), "desktop"));
70+
categoryService.save(new Category("Naturaleza", "Salir al campo nunca fue tan fácil",
71+
convertToBLOB("/sample_image/naturaleza.jpg"), "person-hiking"));
72+
categoryService.save(new Category("Perfumes", "Los perfumes más conocidos a precios desconocidos",
73+
convertToBLOB("/sample_image/perfumes.jpg"), "gift"));
74+
75+
List<Category> lCat = new LinkedList<>();
76+
for (int i = 1; i < categoryService.findAll().size(); i++) {
77+
lCat.add(categoryService.findById(i).get());
78+
}
79+
80+
// Article
81+
articleService.save(new Article(userService.findById(1).get(), "Zapatillas deportivas", "Descripción", "Madrid",
82+
"28001", (float) 99.99, convertToBLOB("/sample_image/ropa.png"), lCat.subList(0, 2)));
83+
articleService.save(new Article(userService.findById(2).get(), "Zapatillas correr", "Descripción", "Madrid",
84+
"28001", (float) 50.99, convertToBLOB("/sample_image/ropa.png"), lCat.subList(1, 3)));
85+
articleService.save(new Article(userService.findById(1).get(), "Botas montaña",
86+
"Deja que tus pies descanses mientras caminas", "Extremadura", "23001", (float) 59.99,
87+
convertToBLOB("/sample_image/botasMontana.jpg"), lCat.subList(0, 4)));
88+
articleService.save(new Article(userService.findById(1).get(), "Cestas de mimbre",
89+
"Cestas de mimbre hechas a mano, con asa muy resistente", "Canarias", "28001", (float) 8.99,
90+
convertToBLOB("/sample_image/cestaMimbre.jpg"), lCat.subList(3, 5)));
91+
articleService.save(new Article(userService.findById(1).get(), "Jarrones",
92+
"Jarrones de cerámica hechos a mano, con colores vivos", "Teruel", "28001", (float) 8.99,
93+
convertToBLOB("/sample_image/jarron.jpg"), lCat.subList(0, 1)));
94+
articleService.save(
95+
new Article(userService.findById(1).get(), "Llaves inglesas", "Se venden llaves inglesas con mango",
96+
"Barcelona", "28001", (float) 18.99, convertToBLOB("/sample_image/llaveInglesa.jpg"), null));
97+
articleService.save(new Article(userService.findById(1).get(), "Kit electrónica",
98+
"Se vende kit de electrónica para que puedas arreglar tus dispositivos electrónicos como móviles, ordenadores y todo tipo de dispositivos",
99+
"A Coruña", "28001", (float) 14.99, convertToBLOB("/sample_image/kitElectronica.jpg"),
100+
lCat.subList(0, 0)));
101+
articleService.save(new Article(userService.findById(1).get(), "Teclados",
102+
"Teclado castellano. Tu imaginación no tiene límites", "Madrid", "28001", (float) 9.99,
103+
convertToBLOB("/sample_image/teclado.jpg"), lCat.subList(4, 5)));
104+
articleService.save(new Article(userService.findById(1).get(), "Ratones",
105+
"Ratones inalámbricos muy veloces. Deja que tu puntero apunte por ti", "Cuenca", "28001", (float) 4.99,
106+
convertToBLOB("/sample_image/raton.jpg"), lCat.subList(2, 3)));
107+
articleService.save(new Article(userService.findById(1).get(), "Saco de dormir",
108+
"Se venden sacos de dormir muy confortables, para que no pases frio durante la noche", "Madrid",
109+
"28001", (float) 29.99, convertToBLOB("/sample_image/sacoDormir.jpg"), lCat));
110+
articleService.save(new Article(userService.findById(1).get(), "Linterna",
111+
"Se venden linternas ligeras a buen precio, viene sin dos pilas AAA", "Aragón", "28001", (float) 4.99,
112+
convertToBLOB("/sample_image/linterna.jpg"), lCat.subList(1, 4)));
113+
articleService.save(new Article(userService.findById(1).get(), "Colonia Aquarel",
114+
"Se venden perfume Aquarel para mujer a buen precio, el olor dura mucho tiempo", "Asturias", "28001",
115+
(float) 11.99, convertToBLOB("/sample_image/perfume.jpg"), lCat.subList(3, 5)));
116+
articleService.save(new Article(userService.findById(1).get(), "Colonia para niños",
117+
"Se venden colonias para niños a buen precio", "Granada", "28001", (float) 4.99,
118+
convertToBLOB("/sample_image/perfumeNinno.jpg"), lCat.subList(3, 4)));
119+
120+
// Report
121+
reportService.save(new Report(articleService.findAll().get(0), "[email protected]", "Descripción"));
122+
123+
}
124+
125+
public void setCategoryImage(Category category, String ruta) throws IOException {
126+
Resource image = new ClassPathResource(ruta);
127+
category.setPHOTO(BlobProxy.generateProxy(image.getInputStream(), image.contentLength()));
128+
}
129+
130+
}
131+

backend/src/main/resources/application.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ spring.mustache.suffix=.html
1010
spring.datasource.url=jdbc:postgresql://localhost:5433/postgres
1111
spring.datasource.username=postgres
1212
spring.datasource.password=password
13-
spring.jpa.hibernate.ddl-auto=create-drop
13+
spring.jpa.hibernate.ddl-auto=update
1414
spring.jpa.show-sql=true
1515
spring.jpa.properties.hibernate.format_sql=true
1616
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQL81Dialect

0 commit comments

Comments
 (0)