Skip to content

Commit

Permalink
add model, repo, service, common
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Nov 26, 2023
1 parent 490618c commit 63e97da
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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<Category, Integer> {

public Category findByCategoryName(String categoryName);

}
Original file line number Diff line number Diff line change
@@ -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<Category> listCategories() {

return categoryrepository.findAll();
}

public void createCategory(Category category) {
categoryrepository.save(category);
}

public Category readCategory(String categoryName) {
return categoryrepository.findByCategoryName(categoryName);
}

public Optional<Category> 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);
}

}

0 comments on commit 63e97da

Please sign in to comment.