diff --git a/ShoppingCart/Backend/src/main/java/com/yen/ShoppingCart/common/ApiResponse.java b/ShoppingCart/Backend/src/main/java/com/yen/ShoppingCart/common/ApiResponse.java new file mode 100644 index 000000000..a71d4feac --- /dev/null +++ b/ShoppingCart/Backend/src/main/java/com/yen/ShoppingCart/common/ApiResponse.java @@ -0,0 +1,27 @@ +package com.yen.ShoppingCart.common; + +import java.time.LocalDateTime; + +// TODO : modify to enums + +public class ApiResponse { + private final boolean success; + private final String message; + + public ApiResponse(boolean success, String message) { + this.success = success; + this.message = message; + } + + public boolean isSuccess() { + return success; + } + + public String getMessage() { + return message; + } + + public String getTimestamp() { + return LocalDateTime.now().toString(); + } +} \ No newline at end of file diff --git a/ShoppingCart/Backend/src/main/java/com/yen/ShoppingCart/model/Category.java b/ShoppingCart/Backend/src/main/java/com/yen/ShoppingCart/model/Category.java new file mode 100644 index 000000000..fcb79b411 --- /dev/null +++ b/ShoppingCart/Backend/src/main/java/com/yen/ShoppingCart/model/Category.java @@ -0,0 +1,81 @@ +package com.yen.ShoppingCart.model; + +import javax.persistence.CascadeType; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.OneToMany; +import javax.persistence.Table; + + +@Entity +@Table(name = "categories") +public class Category { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Integer id; + + @Column(name = "category_name") + private String categoryName; + + private String description; + + private String imageUrl; + + + public Category() { + } + + public Category(String categoryName, String description) { + this.categoryName = categoryName; + this.description = description; + } + + public Category(String categoryName, String description,String imageUrl) { + this.categoryName = categoryName; + this.description = description; + this.imageUrl = imageUrl; + } + + public String getCategoryName() { + return this.categoryName; + } + + public void setCategoryName(String categoryName) { + this.categoryName = categoryName; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + @Override + public String toString() { + return "User {category id=" + id + ", category name='" + categoryName + "', description='" + description + "'}"; + } + + public String getImageUrl() { + return imageUrl; + } + + public void setImageUrl(String imageUrl) { + this.imageUrl = imageUrl; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + +} \ No newline at end of file diff --git a/ShoppingCart/Backend/src/main/java/com/yen/ShoppingCart/repository/Categoryrepository.java b/ShoppingCart/Backend/src/main/java/com/yen/ShoppingCart/repository/Categoryrepository.java new file mode 100644 index 000000000..4ed95607e --- /dev/null +++ b/ShoppingCart/Backend/src/main/java/com/yen/ShoppingCart/repository/Categoryrepository.java @@ -0,0 +1,12 @@ +package com.yen.ShoppingCart.repository; + +import com.yen.ShoppingCart.model.Category; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface Categoryrepository extends JpaRepository { + + public Category findByCategoryName(String categoryName); + +} \ No newline at end of file diff --git a/ShoppingCart/Backend/src/main/java/com/yen/ShoppingCart/service/CategoryService.java b/ShoppingCart/Backend/src/main/java/com/yen/ShoppingCart/service/CategoryService.java new file mode 100644 index 000000000..3590a1dc5 --- /dev/null +++ b/ShoppingCart/Backend/src/main/java/com/yen/ShoppingCart/service/CategoryService.java @@ -0,0 +1,43 @@ +package com.yen.ShoppingCart.service; + +import com.yen.ShoppingCart.model.Category; +import com.yen.ShoppingCart.repository.Categoryrepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import javax.transaction.Transactional; +import java.util.List; +import java.util.Optional; + +@Service +public class CategoryService { + + @Autowired + private Categoryrepository categoryrepository; + + public List listCategories() { + + return categoryrepository.findAll(); + } + + public void createCategory(Category category) { + categoryrepository.save(category); + } + + public Category readCategory(String categoryName) { + return categoryrepository.findByCategoryName(categoryName); + } + + public Optional readCategory(Integer categoryId) { + return categoryrepository.findById(categoryId); + } + + public void updateCategory(Integer categoryID, Category newCategory) { + Category category = categoryrepository.findById(categoryID).get(); + category.setCategoryName(newCategory.getCategoryName()); + category.setDescription(newCategory.getDescription()); + category.setImageUrl(newCategory.getImageUrl()); + categoryrepository.save(category); + } + +} \ No newline at end of file