-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from noisrucer/feat/category-domain
Feat/category domain
- Loading branch information
Showing
23 changed files
with
472 additions
and
15 deletions.
There are no files selected for viewing
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
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
12 changes: 12 additions & 0 deletions
12
src/main/java/com/girok/girokserver/core/exception/CustomInternalException.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.girok.girokserver.core.exception; | ||
|
||
public class CustomInternalException extends RuntimeException { | ||
|
||
public CustomInternalException(String message) { | ||
super(message); | ||
} | ||
|
||
public CustomInternalException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
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
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
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
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
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
44 changes: 44 additions & 0 deletions
44
src/main/java/com/girok/girokserver/domain/category/controller/CategoryController.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,44 @@ | ||
package com.girok.girokserver.domain.category.controller; | ||
|
||
import com.girok.girokserver.core.security.jwt.JwtTokenProvider; | ||
import com.girok.girokserver.core.security.jwt.dto.JwtUserInfo; | ||
import com.girok.girokserver.domain.category.controller.request.CreateCategoryRequest; | ||
import com.girok.girokserver.domain.category.controller.response.CreateCategoryResponse; | ||
import com.girok.girokserver.domain.category.facade.CategoryFacade; | ||
import com.girok.girokserver.domain.category.vo.CategoryPath; | ||
import com.girok.girokserver.global.enums.CategoryColor; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import jakarta.validation.Valid; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.List; | ||
|
||
@Tag(name = "Category") | ||
@CrossOrigin(origins = "*", allowedHeaders = "*") | ||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/api/v1") | ||
public class CategoryController { | ||
|
||
private final CategoryFacade categoryFacade; | ||
private final JwtTokenProvider jwtTokenProvider; | ||
|
||
@PostMapping("/categories") | ||
@ResponseStatus(HttpStatus.OK) | ||
@Operation(summary = "Create a new category") | ||
public ResponseEntity<CreateCategoryResponse> createCategory(@Valid @RequestBody CreateCategoryRequest request) { | ||
JwtUserInfo jwtUserInfo = jwtTokenProvider.getCurrentUserInfo(); | ||
Long userId = jwtUserInfo.getUserId(); | ||
|
||
CategoryColor color = request.getColor(); | ||
CategoryPath path = new CategoryPath(request.getPath()); | ||
|
||
Long categoryId = categoryFacade.createCategory(userId, color, path); | ||
return ResponseEntity.ok().body(new CreateCategoryResponse(categoryId)); | ||
} | ||
} | ||
|
25 changes: 25 additions & 0 deletions
25
.../java/com/girok/girokserver/domain/category/controller/request/CreateCategoryRequest.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,25 @@ | ||
package com.girok.girokserver.domain.category.controller.request; | ||
|
||
import com.girok.girokserver.domain.category.vo.CategoryPath; | ||
import com.girok.girokserver.global.enums.CategoryColor; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import jakarta.validation.Valid; | ||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.NotEmpty; | ||
import jakarta.validation.constraints.NotNull; | ||
import lombok.Getter; | ||
|
||
import java.util.List; | ||
|
||
@Getter | ||
public class CreateCategoryRequest { | ||
|
||
@NotNull | ||
@Schema(example = "YELLOW") | ||
private CategoryColor color; | ||
|
||
@NotEmpty | ||
@Schema(example = "[\"Career\", \"Work\", \"Interview\"]") | ||
private List<String> path; | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
...ava/com/girok/girokserver/domain/category/controller/response/CreateCategoryResponse.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,11 @@ | ||
package com.girok.girokserver.domain.category.controller.response; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@AllArgsConstructor | ||
@Getter | ||
public class CreateCategoryResponse { | ||
|
||
private Long categoryId; | ||
} |
77 changes: 77 additions & 0 deletions
77
src/main/java/com/girok/girokserver/domain/category/entity/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,77 @@ | ||
package com.girok.girokserver.domain.category.entity; | ||
|
||
import com.girok.girokserver.domain.member.entity.Member; | ||
import com.girok.girokserver.global.baseentity.AuditBase; | ||
import com.girok.girokserver.global.enums.CategoryColor; | ||
import jakarta.persistence.*; | ||
import lombok.*; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
@Entity | ||
@Table(name = "category", uniqueConstraints = { | ||
@UniqueConstraint(columnNames = {"parent_id", "name"}) | ||
}) | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@AllArgsConstructor | ||
@Getter | ||
@Builder | ||
public class Category extends AuditBase { | ||
|
||
@Id @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
@Column(name = "id") | ||
private Long id; | ||
|
||
@Column(name = "name", nullable = false) | ||
private String name; | ||
|
||
@Setter | ||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "member_id", nullable = false) | ||
private Member member; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "parent_id") | ||
private Category parent; | ||
|
||
@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL) | ||
private List<Category> children = new ArrayList<>(); | ||
|
||
@Enumerated(EnumType.STRING) | ||
@Column(name = "color", nullable = false) | ||
private CategoryColor color; | ||
|
||
// Constructor methods | ||
public static Category createCategory(Member member, Category parentCategory, String name, CategoryColor color) { | ||
Category category = Category.builder() | ||
.member(member) | ||
.parent(parentCategory) // parent can be null | ||
.name(name) | ||
.color(color) | ||
.build(); | ||
|
||
member.addCategory(category); // TODO: member.addCategory(category)하면 category.assignMember() 중복 | ||
if (parentCategory != null) { | ||
parentCategory.addChildCategory(category); | ||
} | ||
return category; | ||
} | ||
|
||
|
||
// Relation methods | ||
/** | ||
* Add a child category (<->) | ||
*/ | ||
public void addChildCategory(Category childCategory) { | ||
this.children.add(childCategory); | ||
childCategory.parent = this; | ||
} | ||
|
||
public void assignParent(Category parentCategory) { | ||
this.parent = parentCategory; | ||
parentCategory.getChildren().add(this); | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
...java/com/girok/girokserver/domain/category/exception/RootCategoryNameAccessException.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.girok.girokserver.domain.category.exception; | ||
|
||
public class RootCategoryNameAccessException extends RuntimeException { | ||
|
||
public RootCategoryNameAccessException() { | ||
super("Root category does not have a parent category."); | ||
} | ||
|
||
public RootCategoryNameAccessException(String message) { | ||
super(message); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...va/com/girok/girokserver/domain/category/exception/RootCategoryParentAccessException.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,14 @@ | ||
package com.girok.girokserver.domain.category.exception; | ||
|
||
import com.girok.girokserver.core.exception.CustomInternalException; | ||
|
||
public class RootCategoryParentAccessException extends CustomInternalException { | ||
|
||
public RootCategoryParentAccessException() { | ||
super("Root category does not have a parent category."); | ||
} | ||
|
||
public RootCategoryParentAccessException(String message) { | ||
super(message); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/com/girok/girokserver/domain/category/facade/CategoryFacade.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,30 @@ | ||
package com.girok.girokserver.domain.category.facade; | ||
|
||
import com.girok.girokserver.domain.category.entity.Category; | ||
import com.girok.girokserver.domain.category.service.CategoryService; | ||
import com.girok.girokserver.domain.category.vo.CategoryPath; | ||
import com.girok.girokserver.domain.member.entity.Member; | ||
import com.girok.girokserver.domain.member.service.MemberService; | ||
import com.girok.girokserver.global.enums.CategoryColor; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.Optional; | ||
|
||
@Service | ||
@Transactional(readOnly = true) | ||
@RequiredArgsConstructor | ||
public class CategoryFacade { | ||
|
||
private final CategoryService categoryService; | ||
private final MemberService memberService; | ||
|
||
@Transactional | ||
public Long createCategory(Long userId, CategoryColor color, CategoryPath path) { | ||
Member member = memberService.findMemberById(userId); | ||
System.out.println("A"); | ||
Category category = categoryService.createCategory(member, color, path); | ||
return category.getId(); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/girok/girokserver/domain/category/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.girok.girokserver.domain.category.repository; | ||
|
||
import com.girok.girokserver.domain.category.entity.Category; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.Optional; | ||
|
||
public interface CategoryRepository extends JpaRepository<Category, Long> { | ||
|
||
Optional<Category> findByParentAndName(Category parentCategory, String name); | ||
|
||
} |
Oops, something went wrong.