Browse Source

Update - controlardor Actualiza

master
Juanjo 1 year ago
parent
commit
70384caa6d
4 changed files with 77 additions and 1 deletions
  1. +16
    -0
      src/main/java/es/fp/edu/conecta2/controladores/WebControlador.java
  2. +7
    -0
      src/main/java/es/fp/edu/conecta2/repo/user/IUserRepo.java
  3. +12
    -1
      src/main/java/es/fp/edu/conecta2/servicios/UserService.java
  4. +42
    -0
      src/main/resources/templates/editar_usuario.html

+ 16
- 0
src/main/java/es/fp/edu/conecta2/controladores/WebControlador.java View File

@ -34,6 +34,22 @@ public class WebControlador {
return "redirect:/";
}
@GetMapping ("/editar/{1}")
public String editarUser(@PathVariable Integer id, Model modelo){
modelo.addAttribute("user",
servicioUser.buscarId(id).get());
return "editar_usuario";
}
@PostMapping ("/actualizar")
public String actualizaUser(@ModelAttribute("user") User usuario){
servicioUser.guardarId(usuario);
return "redirect:/";
}
@GetMapping ("/borrar")
public String borrarUser(Model modelo){
modelo.addAttribute("user",servicioUser.listarUser());


+ 7
- 0
src/main/java/es/fp/edu/conecta2/repo/user/IUserRepo.java View File

@ -2,8 +2,15 @@ package es.fp.edu.conecta2.repo.user;
import es.fp.edu.conecta2.modelo.user.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
@Repository
public interface IUserRepo extends JpaRepository<User,Integer> {
@Transactional
@Modifying
@Query("update User u set u.idUsuario = ?1, u.nombres = ?2")
int actualizaId(Integer idUsuario, String nombres);
}

+ 12
- 1
src/main/java/es/fp/edu/conecta2/servicios/UserService.java View File

@ -6,6 +6,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
public class UserService {
@ -18,8 +19,18 @@ public class UserService {
}
public List<User> listarUser(){
return repo.findAll();
return repo.findAll();
}
public Optional<User> buscarId(Integer id){
return Optional.of(repo.findById(id).get());
}
public void guardarId(User t) {
repo.actualizaId(t.getIdUsuario(),t.getNombres());
}
}

+ 42
- 0
src/main/resources/templates/editar_usuario.html View File

@ -0,0 +1,42 @@
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-4.dtd">
<html lang="es"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head th:replace="~{head/head :: mihead}"></head>
<body class="d-flex flex-column h-100">
<header th:replace="~{menu/m1 :: usuarios}"></header>
<div class="p-5 mb-4 bg-light rounded-3">
<div class="container-fluid py-5">
<h1 class="display-5 fw-bold">Editaro usuario</h1>
<p class="col-md-8 fs-4">Ejemplo de CRUD</p>
<a class="btn btn-lg btn-primary" href="https://2dam.fp.edu.es/gitea/conecta2">Código fuente</a>
</div>
</div>
<main>
<div class="container">
<form th:action="@{/actualizar}" th:object="${user}" method="post">
<table class="table table-sm">
<thead>
<tr>
<th scope="col">#idUsuario</th>
<th scope="col">Nombres</th>
<th scope="col"></th>
</thead>
<tbody>
<th><input type="text" name="idUsuario" th:field="*{idUsuario}" class="form-control" placeholder="Nº de usuario" required></th>
<td><input type="text" name="nombres" th:field="*{nombres}" class="form-control" placeholder="Nombre y apellidos" required></td>
<td><button class="btn btn-success" >Actualizar</button></td>
</tbody>
</table>
</form>
</div>
</main>
<footer th:replace="~{foot/javascript :: javascript}"></footer>
</body>
</html>

Loading…
Cancel
Save