Country

Entidad

package com.trifulcas.SpringBootData;

import java.sql.Timestamp;
import java.util.Date;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;

@Entity
@Table(name = "country")
public class Country {
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "country_id")
	private int countryId;

	@Column(name = "country", nullable = false)
	private String country;

	@Column(name = "last_update", nullable = false)
	private Timestamp lastUpdate;

	
	public Country() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Country(String country) {
		super();
		this.country = country;
		Date now = new Date();
		this.lastUpdate = new Timestamp(now.getTime());
	}

	public int getCountryId() {
		return countryId;
	}

	public void setCountryId(int countryId) {
		this.countryId = countryId;
	}

	

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}

	public Timestamp getLastUpdate() {
		return lastUpdate;
	}

	public void setLastUpdate(Timestamp lastUpdate) {
		this.lastUpdate = lastUpdate;
	}

	@Override
	public String toString() {
		return "Country [countryId=" + countryId + ", name=" + country + ", lastUpdate=" + lastUpdate + "]";
	}

}

Repositorio

package com.trifulcas.SpringBootData;

import org.springframework.data.repository.CrudRepository;

public interface CountryRepository extends CrudRepository<Country, Integer> {

}

Controlador

package com.trifulcas.SpringBootData;

import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

// Esta anotación serializa por defecto todo lo que las funciones retornen
@RestController
@RequestMapping("/country") // Esto permite poner un prefijo a todas las peticiones
public class CountryController {

	// Lo que hace es crear e inyectar un objeto de tipo
	// CRUD con la entidad 'Country'
	// Este objeto nos servirá como un DAO, nos permite crear, modificar
	// obtener y borrar cualquier elemento de la web
	// el objeto repository nos permite usar findall, findbyid, deletebyid, save...
	@Autowired
	private CountryRepository countryRepository;

	@GetMapping("/add")
	public Country addNewCountry(@RequestParam(value = "name", defaultValue = "Nueva categoría") String name) {
		Country cat = new Country(name);
		countryRepository.save(cat);
		return cat;
	}

	@GetMapping("/delete")
	public String deleteCountry(@RequestParam(value = "id", defaultValue = "0") String id) {
		try {
			countryRepository.deleteById(Integer.parseInt(id));
			return "Borrado " + id;
		} catch (Exception ex) {
			return ex.getMessage();
		}
	}

	@GetMapping("/get")
	public Optional<Country> getCountry(@RequestParam(value = "id", defaultValue = "0") String id) {
		try {
			Optional<Country> cat = countryRepository.findById(Integer.parseInt(id));
			return cat;
		} catch (Exception ex) {
			return Optional.empty();
		}
	}

	@GetMapping("/all")
	public Iterable<Country> viewAll() {
		return countryRepository.findAll();
	}
}

Publicado por

Avatar del usuario

Juan Pablo Fuentes

Formador de programación y bases de datos