package com.trifulcas.SpringBootBiblioteca.model; import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; import jakarta.persistence.Table; @Entity @Table(name = "genero") public class Genero { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int idgenero; private String nombre; // Constructor vacío public Genero() { } // Constructor con parámetros public Genero(String nombre) { this.nombre = nombre; } // Getters y Setters public int getIdgenero() { return idgenero; } public void setIdgenero(int idgenero) { this.idgenero = idgenero; } public String getNombre() { return nombre; } public void setNombre(String nombre) { this.nombre = nombre; } // Método toString (opcional) @Override public String toString() { return "Genero{" + "idgenero=" + idgenero + ", nombre='" + nombre + '\'' + '}'; } }
package com.trifulcas.SpringBootBiblioteca.repository; import org.springframework.data.jpa.repository.JpaRepository; import com.trifulcas.SpringBootBiblioteca.model.Genero; public interface GeneroRepository extends JpaRepository<Genero, Integer> { }
package com.trifulcas.SpringBootBiblioteca.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.DeleteMapping; 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.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.trifulcas.SpringBootBiblioteca.model.Genero; import com.trifulcas.SpringBootBiblioteca.repository.GeneroRepository; @RestController @RequestMapping("/genero") public class GeneroController { @Autowired GeneroRepository generoRepository; @GetMapping("") public List<Genero> getAll() { try { return generoRepository.findAll(); } catch (Exception ex) { System.out.println(ex.getMessage()); return null; } } // Poner los valores en la URL, no parámetros nombrados @GetMapping("/{id}") public ResponseEntity<Genero> getById(@PathVariable int id) { System.out.println(id); try { Genero cat= generoRepository.findById(id).orElse(null); if (cat!=null) { return new ResponseEntity<>(cat, HttpStatus.OK); } else { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } } catch (Exception ex) { System.out.println(ex.getMessage()); return null; } } @PostMapping("") public Genero add(@RequestBody Genero cat) { System.out.println(cat); try { if (cat.getIdgenero() == 0 && cat.getNombre() != null) { return generoRepository.save(cat); } else { return null; } } catch (Exception ex) { System.out.println(ex.getMessage()); return null; } } @PutMapping("/{id}") public Genero put(@RequestBody Genero cat, @PathVariable int id) { System.out.println(cat); System.out.println(id); try { if (cat.getIdgenero() == id) { return generoRepository.save(cat); } else { return null; } } catch (Exception ex) { System.out.println(ex.getMessage()); return null; } } @DeleteMapping("/{id}") public int delete(@PathVariable int id) { try { System.out.println(id); generoRepository.deleteById(id); return id; } catch (Exception ex) { System.out.println(ex.getMessage()); return 0; } } }