I'm trying to make simple REST Web Service by Spring Boot in IntelliJ. I also have to make the CRUD client for that but that later.
I have to make a Products table with fields: id, name, price and status. And I have a problem with making a unique "name" field. If I send a data by postman with the same name it shouldn't add it to the database and should give me an server error.
Also I have to make that "status" field can only have 3 unique values: "available", "out of stock" and "withdrawn from sale". You can not add a new product with the status "withdrawn from sale". So you can use only those three values to post "status" data.
Could anyone help me with this?
My Products class:
@Entity
public class Products {
private Long id;
private String nazwa;
private Float cena;
private String status;
@Id
@GeneratedValue
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getNazwa() {
return nazwa;
}
public void setNazwa(String nazwa) {
this.nazwa = nazwa;
}
public Float getCena() {
return cena;
}
public void setCena(Float cena) {
this.cena = cena;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
My ProductsController
@RestController
@RequestMapping("/products")
public class ProductsController {
@Autowired
private ProductJpaRepository productJpaRepository;
@GetMapping(value = "/all")
public List<Products> findAll() {
return productJpaRepository.findAll();
}
@GetMapping(value = "/{nazwa}")
public Products findByName(@PathVariable final String nazwa) {
return productJpaRepository.findByNazwa(nazwa);
}
@PostMapping(value = "/load")
public Products load(@RequestBody final Products products) {
return productJpaRepository.save(products);
}
}