-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
163 additions
and
0 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
ShoppingCart/Backend/src/main/java/com/yen/ShoppingCart/common/ApiResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
ShoppingCart/Backend/src/main/java/com/yen/ShoppingCart/model/Category.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
ShoppingCart/Backend/src/main/java/com/yen/ShoppingCart/repository/Categoryrepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
ShoppingCart/Backend/src/main/java/com/yen/ShoppingCart/service/CategoryService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |