From f7b0104d0c3a532c120817924c45b812e96ccd5c Mon Sep 17 00:00:00 2001 From: Cristobal Bernal Mayordomo <90463533+Racriberny@users.noreply.github.com> Date: Tue, 21 Feb 2023 15:40:41 +0100 Subject: [PATCH] =?UTF-8?q?Intento=20de=20a=C3=B1adir=20foros=20y=20poder?= =?UTF-8?q?=20responder!!!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cristobalbernal/foro/Entidades/Foro.java | 38 ++++--------------- .../foro/Entidades/RespuestasEntity.java | 31 +++++---------- .../foro/Servicios/ServicioCategoria.java | 18 +++++++++ .../foro/Servicios/ServicioRespuestas.java | 3 ++ .../foro/controlador/Controlador.java | 23 ++++++++--- .../templates/Ask_Question/ask_question.html | 25 ++++++------ .../templates/Respuestas/post-detall.html | 20 ++++++++-- 7 files changed, 85 insertions(+), 73 deletions(-) create mode 100644 src/main/java/com/cristobalbernal/foro/Servicios/ServicioCategoria.java diff --git a/src/main/java/com/cristobalbernal/foro/Entidades/Foro.java b/src/main/java/com/cristobalbernal/foro/Entidades/Foro.java index be05ef0..3099b44 100644 --- a/src/main/java/com/cristobalbernal/foro/Entidades/Foro.java +++ b/src/main/java/com/cristobalbernal/foro/Entidades/Foro.java @@ -3,6 +3,7 @@ package com.cristobalbernal.foro.Entidades; import jakarta.persistence.*; import java.sql.Timestamp; +import java.util.Objects; @Entity public class Foro { @@ -22,9 +23,9 @@ public class Foro { @Basic @Column(name = "imagen", nullable = false) private int imagen; - @Basic - @Column(name = "categoria_foro_id", nullable = false) - private int categoriaForoId; + @ManyToOne + @JoinColumn(name = "categoria_foro_id", nullable = false) + private CategoriaForoEntity categoriaForoId; public Foro() { } @@ -69,11 +70,11 @@ public class Foro { this.imagen = imagen; } - public int getCategoriaForoId() { + public CategoriaForoEntity getCategoriaForoId() { return categoriaForoId; } - public void setCategoriaForoId(int categoriaForoId) { + public void setCategoriaForoId(CategoriaForoEntity categoriaForoId) { this.categoriaForoId = categoriaForoId; } @@ -81,35 +82,12 @@ public class Foro { public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; - Foro foro = (Foro) o; - - if (id != foro.id) return false; - if (imagen != foro.imagen) return false; - if (categoriaForoId != foro.categoriaForoId) return false; - if (titulo != null ? !titulo.equals(foro.titulo) : foro.titulo != null) return false; - if (descripcion != null ? !descripcion.equals(foro.descripcion) : foro.descripcion != null) return false; - if (fechaYHora != null ? !fechaYHora.equals(foro.fechaYHora) : foro.fechaYHora != null) return false; - - return true; + return id == foro.id && imagen == foro.imagen && Objects.equals(titulo, foro.titulo) && Objects.equals(descripcion, foro.descripcion) && Objects.equals(fechaYHora, foro.fechaYHora) && Objects.equals(categoriaForoId, foro.categoriaForoId); } @Override public int hashCode() { - int result = id; - result = 31 * result + (titulo != null ? titulo.hashCode() : 0); - result = 31 * result + (descripcion != null ? descripcion.hashCode() : 0); - result = 31 * result + (fechaYHora != null ? fechaYHora.hashCode() : 0); - result = 31 * result + imagen; - result = 31 * result + categoriaForoId; - return result; - } - - public Foro(int id, String titulo, String descripcion, int imagen, int categoriaForoId) { - this.id = id; - this.titulo = titulo; - this.descripcion = descripcion; - this.imagen = imagen; - this.categoriaForoId = categoriaForoId; + return Objects.hash(id, titulo, descripcion, fechaYHora, imagen, categoriaForoId); } } diff --git a/src/main/java/com/cristobalbernal/foro/Entidades/RespuestasEntity.java b/src/main/java/com/cristobalbernal/foro/Entidades/RespuestasEntity.java index 323cfe8..faefba9 100644 --- a/src/main/java/com/cristobalbernal/foro/Entidades/RespuestasEntity.java +++ b/src/main/java/com/cristobalbernal/foro/Entidades/RespuestasEntity.java @@ -3,6 +3,7 @@ package com.cristobalbernal.foro.Entidades; import jakarta.persistence.*; import java.sql.Timestamp; +import java.util.Objects; @Entity @Table(name = "respuestas", schema = "forogruas", catalog = "") @@ -14,12 +15,13 @@ public class RespuestasEntity { @Basic @Column(name = "Post", nullable = true, length = 45) private String post; + @Version @Basic @Column(name = "fechaYHora", nullable = true) private Timestamp fechaYHora; - @Basic - @Column(name = "Respuestas_id", nullable = false) - private int respuestasId; + @ManyToOne + @JoinColumn(name = "Respuestas_id", nullable = false) + private RespuestasEntity respuestasId; @Basic @Column(name = "users_id", nullable = false) private int usersId; @@ -51,11 +53,11 @@ public class RespuestasEntity { this.fechaYHora = fechaYHora; } - public int getRespuestasId() { + public RespuestasEntity getRespuestasId() { return respuestasId; } - public void setRespuestasId(int respuestasId) { + public void setRespuestasId(RespuestasEntity respuestasId) { this.respuestasId = respuestasId; } @@ -79,27 +81,12 @@ public class RespuestasEntity { public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; - RespuestasEntity that = (RespuestasEntity) o; - - if (id != that.id) return false; - if (respuestasId != that.respuestasId) return false; - if (usersId != that.usersId) return false; - if (foroId != that.foroId) return false; - if (post != null ? !post.equals(that.post) : that.post != null) return false; - if (fechaYHora != null ? !fechaYHora.equals(that.fechaYHora) : that.fechaYHora != null) return false; - - return true; + return id == that.id && usersId == that.usersId && foroId == that.foroId && Objects.equals(post, that.post) && Objects.equals(fechaYHora, that.fechaYHora) && Objects.equals(respuestasId, that.respuestasId); } @Override public int hashCode() { - int result = id; - result = 31 * result + (post != null ? post.hashCode() : 0); - result = 31 * result + (fechaYHora != null ? fechaYHora.hashCode() : 0); - result = 31 * result + respuestasId; - result = 31 * result + usersId; - result = 31 * result + foroId; - return result; + return Objects.hash(id, post, fechaYHora, respuestasId, usersId, foroId); } } diff --git a/src/main/java/com/cristobalbernal/foro/Servicios/ServicioCategoria.java b/src/main/java/com/cristobalbernal/foro/Servicios/ServicioCategoria.java new file mode 100644 index 0000000..744b829 --- /dev/null +++ b/src/main/java/com/cristobalbernal/foro/Servicios/ServicioCategoria.java @@ -0,0 +1,18 @@ +package com.cristobalbernal.foro.Servicios; + +import com.cristobalbernal.foro.Entidades.CategoriaForoEntity; +import com.cristobalbernal.foro.Entidades.Foro; +import com.cristobalbernal.foro.Respositorios.ICategoriaForo; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; +@Service +public class ServicioCategoria { + @Autowired + private ICategoriaForo iCategoriaForo; + + public List findAll(){ + return iCategoriaForo.findAll(); + } +} diff --git a/src/main/java/com/cristobalbernal/foro/Servicios/ServicioRespuestas.java b/src/main/java/com/cristobalbernal/foro/Servicios/ServicioRespuestas.java index da8bd9c..668f121 100644 --- a/src/main/java/com/cristobalbernal/foro/Servicios/ServicioRespuestas.java +++ b/src/main/java/com/cristobalbernal/foro/Servicios/ServicioRespuestas.java @@ -17,4 +17,7 @@ public class ServicioRespuestas { public List respuestasEntityList(int id){ return iRespuestas.findAllByForoId(id); } + public void save(RespuestasEntity respuestas){ + iRespuestas.save(respuestas); + } } diff --git a/src/main/java/com/cristobalbernal/foro/controlador/Controlador.java b/src/main/java/com/cristobalbernal/foro/controlador/Controlador.java index 57d7cd1..2231671 100644 --- a/src/main/java/com/cristobalbernal/foro/controlador/Controlador.java +++ b/src/main/java/com/cristobalbernal/foro/controlador/Controlador.java @@ -1,22 +1,22 @@ package com.cristobalbernal.foro.controlador; +import com.cristobalbernal.foro.Entidades.CategoriaForoEntity; import com.cristobalbernal.foro.Entidades.Foro; import com.cristobalbernal.foro.Entidades.RespuestasEntity; import com.cristobalbernal.foro.Entidades.UsersEntity; import com.cristobalbernal.foro.Respositorios.IUsers; +import com.cristobalbernal.foro.Servicios.ServicioCategoria; import com.cristobalbernal.foro.Servicios.ServicioForo; import com.cristobalbernal.foro.Servicios.ServicioRespuestas; import com.cristobalbernal.foro.seguridad.models.UserDetailsImpl; +import org.apache.commons.logging.Log; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.core.context.SecurityContext; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.*; import java.util.List; @@ -27,6 +27,8 @@ public class Controlador { @Autowired private ServicioRespuestas servicioRespuestas; @Autowired + private ServicioCategoria servicioCategoria; + @Autowired private ServicioForo servicioForo; @GetMapping("/miperfil") @@ -50,15 +52,26 @@ public class Controlador { return "Registrar/Registrar"; } @GetMapping("/crearPregunta") - public String pregunta(){ + public String pregunta(Model model){ + model.addAttribute("listaCategoria",servicioCategoria.findAll()); return "Ask_Question/ask_question"; } + @PostMapping("/postdetall/add/submit/{id}") + public String addRespuesta(@ModelAttribute("respuesta") RespuestasEntity respuestas, @PathVariable int id){ + respuestas.setForoId(id); + respuestas.setRespuestasId(respuestas); + respuestas.setUsersId(1); + servicioRespuestas.save(respuestas); + return "redirect:/postdetall/" + id; + } @GetMapping("/postdetall/{id}") public String postDetall(@PathVariable int id,Model model){ Foro foro = servicioForo.findById(id); List respuestas = servicioRespuestas.respuestasEntityList(id); + model.addAttribute("listaRespuestas",respuestas); model.addAttribute("Foro",foro); + model.addAttribute("respuesta",new RespuestasEntity()); return "Respuestas/post-detall"; } @GetMapping("/all") diff --git a/src/main/resources/templates/Ask_Question/ask_question.html b/src/main/resources/templates/Ask_Question/ask_question.html index 5d242cc..e49d6bd 100644 --- a/src/main/resources/templates/Ask_Question/ask_question.html +++ b/src/main/resources/templates/Ask_Question/ask_question.html @@ -25,24 +25,25 @@

Ask Question

-
-
- User name* -
- +
- Question-Title* + Question-Title*
-
- +
+ Respuesta *
- - +
+ Seleciona Categoria: +
+ + +
+
+ +
diff --git a/src/main/resources/templates/Respuestas/post-detall.html b/src/main/resources/templates/Respuestas/post-detall.html index 4580e0c..0d534dd 100644 --- a/src/main/resources/templates/Respuestas/post-detall.html +++ b/src/main/resources/templates/Respuestas/post-detall.html @@ -1,5 +1,5 @@ - + @@ -39,13 +39,22 @@
+
-
    Hola
+
    +
  • +
    +
    +
    Lorem ipsum dolor sit amet, consectetur adipisicin iure laudantium vitae, praesentium optio, sapiente distinctio illo?
    +
    +
  • +
+

Responder


@@ -53,8 +62,11 @@
-
- +
+ + +
+