Trying to refactor my code for the controller below:
@PutMapping("products/{productId}")
public ProductResponse updateProduct(@PathVariable("productId") Long productId, @RequestBody ProductForm productForm) {
Optional<Product> foundProductOpt = productRepository.findById(productId);
Product foundProduct = foundProductOpt.orElseThrow(() -> new EntityNotFoundException("productId" + productId + "not found."));
//would like to refactor code below!!
foundProduct.setProductTitle(productForm.getProductTitle());
foundProduct.setProductPrice(productForm.getProductPrice());
foundProduct.setProductDescription(productForm.getProductDescription());
foundProduct.setProductImage(productForm.getProductImage());
productRepository.save(foundProduct);
return new ProductResponse(null, "product updated");
}
Where I am simply transferring values from a form object into an entity object. Thought of creating a method but did not want to write a method in an entity so not sure what other solutions are out there.
As per request, below is my Product and form object. I would like to use a form object for validation and then have data transferred to the Product object.
Product.java
package com.assignment.restapi.domain;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.validation.constraints.Min;
import java.util.Date;
@Entity
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long productId;
private String productImage;
private String productTitle;
private String productDescription;
private Integer productPrice;
//https://stackoverflow.com/questions/49954812/how-can-you-make-a-created-at-column-generate-the-creation-date-time-automatical/49954965#49954965
@CreationTimestamp
private Date createdAt;
@UpdateTimestamp
private Date updatedAt;
// default constructor
public Product() {
}
// parameterized constructor-User enetered value goes here to set the fields of the instantiated object.
public Product(String productImage, String productTitle, String productDescription, Integer productPrice, Date createdAt) {
this.productImage = productImage;
this.productTitle = productTitle;
this.productDescription = productDescription;
this.productPrice = productPrice;
this.createdAt = createdAt;
}
// getter methods are used to retrieve a value from an object.
// setter methods are used to set a new value to an object.
public Long getProductId() {
return productId;
}
public void setProductId(Long productId) {
this.productId = productId;
}
public String getProductImage() {
return productImage;
}
public void setProductImage(String productImage) {
this.productImage = productImage;
}
public String getProductTitle() {
return productTitle;
}
public void setProductTitle(String productTitle) {
this.productTitle = productTitle;
}
public String getProductDescription() {
return productDescription;
}
public void setProductDescription(String productDescription) {
this.productDescription = productDescription;
}
public Integer getProductPrice() {
return productPrice;
}
public void setProductPrice(@Min(value = 1, message = "値段は0以上の値を設定してください。") Integer productPrice) {
this.productPrice = productPrice;
}
public Date getCreatedAt() {
return createdAt;
}
public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
}
public Date getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(Date updatedAt) {
this.updatedAt = updatedAt;
}
}
ProductForm.java
package com.assignment.restapi.web.view;
import com.assignment.restapi.domain.Product;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
public class ProductForm {
String productImage;
@NotBlank(message = "Product title is necessary")
@Size(max = 100, message = "Product title has to be less than 100 letters")
String productTitle;
@Size(max = 500, message = "Product title has to be less than 500 letters")
String productDescription;
@Min(value = 1, message = "price has to have a value larger than 1")
Integer productPrice;
public ProductForm() {
}
public ProductForm(String productImage, String productTitle, String productDescription, Integer productPrice) {
this.productImage = productImage;
this.productTitle = productTitle;
this.productDescription = productDescription;
this.productPrice = productPrice;
}
public String getProductImage() {
return productImage;
}
public void setProductImage(String productImage) {
this.productImage = productImage;
}
public String getProductTitle() {
return productTitle;
}
public void setProductTitle(String productTitle) {
this.productTitle = productTitle;
}
public String getProductDescription() {
return productDescription;
}
public void setProductDescription(String productDescription) {
this.productDescription = productDescription;
}
public Integer getProductPrice() {
return productPrice;
}
public void setProductPrice(Integer productPrice) {
this.productPrice = productPrice;
}
//turns productForm into Product object.
public Product convertToProduct() {
//step by step debug mode, new object constructor function in Product.java gets called.
//setter methods get called and values of the ProductForm object gets passed and becomes the new value of the Product object.
Product product = new Product();
product.setProductTitle(this.productTitle);
product.setProductImage(this.productImage);
product.setProductDescription(this.productDescription);
product.setProductPrice(this.productPrice);
return product;
}
}