-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dev-be' into feat/47-write-checklist-api
- Loading branch information
Showing
18 changed files
with
407 additions
and
14 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
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
32 changes: 32 additions & 0 deletions
32
backend/bang-ggood/src/main/java/com/bang_ggood/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,32 @@ | ||
package com.bang_ggood.category.controller; | ||
|
||
import com.bang_ggood.category.dto.CategoriesReadResponse; | ||
import com.bang_ggood.category.dto.CategoryPriorityCreateRequest; | ||
import com.bang_ggood.category.service.CategoryService; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
public class CategoryController { | ||
|
||
private final CategoryService categoryService; | ||
|
||
public CategoryController(CategoryService categoryService) { | ||
this.categoryService = categoryService; | ||
} | ||
|
||
@PostMapping("/categories/priority") | ||
public ResponseEntity<Void> createCategoriesPriority(@RequestBody CategoryPriorityCreateRequest request) { | ||
// TODO: List 요소 null check 필요 | ||
categoryService.createCategoriesPriority(request); | ||
return ResponseEntity.noContent().build(); | ||
} | ||
|
||
@GetMapping("/categories") | ||
public ResponseEntity<CategoriesReadResponse> readCategories() { | ||
return ResponseEntity.ok(categoryService.readCategories()); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
backend/bang-ggood/src/main/java/com/bang_ggood/category/domain/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,35 @@ | ||
package com.bang_ggood.category.domain; | ||
|
||
import java.util.Arrays; | ||
|
||
public enum Category { | ||
|
||
CLEAN(1, "청결"), | ||
ROOM_CONDITION(2, "방 컨디션"), | ||
AMENITY(3, "편의시설"), | ||
OPTION(4, "옵션"), | ||
ENVIRONMENT(5, "주거환경"), | ||
SECURITY(6, "보안"), | ||
ECONOMIC(7, "경제적"); | ||
|
||
private final int id; | ||
private final String description; | ||
|
||
Category(int id, String description) { | ||
this.id = id; | ||
this.description = description; | ||
} | ||
|
||
public static boolean contains(int id) { | ||
return Arrays.stream(values()) | ||
.anyMatch(category -> category.id == id); | ||
} | ||
|
||
public int getId() { | ||
return id; | ||
} | ||
|
||
public String getDescription() { | ||
return description; | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
backend/bang-ggood/src/main/java/com/bang_ggood/category/domain/CategoryPriority.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,60 @@ | ||
package com.bang_ggood.category.domain; | ||
|
||
import com.bang_ggood.BaseEntity; | ||
import com.bang_ggood.user.domain.User; | ||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.FetchType; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.ManyToOne; | ||
import java.util.Objects; | ||
|
||
@Entity | ||
public class CategoryPriority extends BaseEntity { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
@Column(nullable = false) | ||
private Integer categoryId; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
private User user; | ||
|
||
protected CategoryPriority() { | ||
} | ||
|
||
public CategoryPriority(Integer categoryId, User user) { | ||
this.categoryId = categoryId; | ||
this.user = user; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
CategoryPriority that = (CategoryPriority) o; | ||
return Objects.equals(id, that.id); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(id); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "CategoryPriority{" + | ||
"id=" + id + | ||
", categoryId=" + categoryId + | ||
", user=" + user + | ||
'}'; | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
backend/bang-ggood/src/main/java/com/bang_ggood/category/dto/CategoriesReadResponse.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,6 @@ | ||
package com.bang_ggood.category.dto; | ||
|
||
import java.util.List; | ||
|
||
public record CategoriesReadResponse(List<CategoryReadResponse> categories) { | ||
} |
6 changes: 6 additions & 0 deletions
6
...d/bang-ggood/src/main/java/com/bang_ggood/category/dto/CategoryPriorityCreateRequest.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,6 @@ | ||
package com.bang_ggood.category.dto; | ||
|
||
import java.util.List; | ||
|
||
public record CategoryPriorityCreateRequest(List<Integer> categoryIds) { | ||
} |
10 changes: 10 additions & 0 deletions
10
backend/bang-ggood/src/main/java/com/bang_ggood/category/dto/CategoryReadResponse.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,10 @@ | ||
package com.bang_ggood.category.dto; | ||
|
||
import com.bang_ggood.category.domain.Category; | ||
|
||
public record CategoryReadResponse(Integer categoryId, String categoryName) { | ||
|
||
public static CategoryReadResponse from(Category category) { | ||
return new CategoryReadResponse(category.getId(), category.getDescription()); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...ng-ggood/src/main/java/com/bang_ggood/category/repository/CategoryPriorityRepository.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,7 @@ | ||
package com.bang_ggood.category.repository; | ||
|
||
import com.bang_ggood.category.domain.CategoryPriority; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface CategoryPriorityRepository extends JpaRepository<CategoryPriority, Long> { | ||
} |
75 changes: 75 additions & 0 deletions
75
backend/bang-ggood/src/main/java/com/bang_ggood/category/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,75 @@ | ||
package com.bang_ggood.category.service; | ||
|
||
import com.bang_ggood.category.domain.Category; | ||
import com.bang_ggood.category.domain.CategoryPriority; | ||
import com.bang_ggood.category.dto.CategoriesReadResponse; | ||
import com.bang_ggood.category.dto.CategoryPriorityCreateRequest; | ||
import com.bang_ggood.category.dto.CategoryReadResponse; | ||
import com.bang_ggood.category.repository.CategoryPriorityRepository; | ||
import com.bang_ggood.exception.BangggoodException; | ||
import com.bang_ggood.user.domain.User; | ||
import com.bang_ggood.user.repository.UserRepository; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import static com.bang_ggood.exception.ExceptionCode.CATEGORY_DUPLICATED; | ||
import static com.bang_ggood.exception.ExceptionCode.CATEGORY_NOT_FOUND; | ||
import static com.bang_ggood.exception.ExceptionCode.CATEGORY_PRIORITY_INVALID_COUNT; | ||
|
||
@Service | ||
public class CategoryService { | ||
|
||
private static final int MAX_CATEGORY_PRIORITY_COUNT = 3; | ||
|
||
private final CategoryPriorityRepository categoryPriorityRepository; | ||
private final UserRepository userRepository; | ||
|
||
public CategoryService(CategoryPriorityRepository categoryPriorityRepository, UserRepository userRepository) { | ||
this.categoryPriorityRepository = categoryPriorityRepository; | ||
this.userRepository = userRepository; | ||
} | ||
|
||
@Transactional | ||
public void createCategoriesPriority(CategoryPriorityCreateRequest request) { | ||
validateDuplication(request); | ||
validateCategoryCount(request); | ||
validateCategoryId(request); | ||
|
||
User user = userRepository.getUserById(1L); | ||
List<CategoryPriority> categoryPriorities = request.categoryIds().stream() | ||
.map(id -> new CategoryPriority(id, user)) | ||
.toList(); | ||
|
||
categoryPriorityRepository.saveAll(categoryPriorities); | ||
} | ||
|
||
private void validateDuplication(CategoryPriorityCreateRequest request) { | ||
if (request.categoryIds().size() != Set.copyOf(request.categoryIds()).size()) { | ||
throw new BangggoodException(CATEGORY_DUPLICATED); | ||
} | ||
} | ||
|
||
private void validateCategoryCount(CategoryPriorityCreateRequest request) { | ||
if (request.categoryIds().size() > MAX_CATEGORY_PRIORITY_COUNT) { | ||
throw new BangggoodException(CATEGORY_PRIORITY_INVALID_COUNT); | ||
} | ||
} | ||
|
||
private void validateCategoryId(CategoryPriorityCreateRequest request) { | ||
for (Integer id : request.categoryIds()) { | ||
if (!Category.contains(id)) { | ||
throw new BangggoodException(CATEGORY_NOT_FOUND); | ||
} | ||
} | ||
} | ||
|
||
public CategoriesReadResponse readCategories() { | ||
List<CategoryReadResponse> categoryReadResponses = Arrays.stream(Category.values()) | ||
.map(CategoryReadResponse::from) | ||
.toList(); | ||
return new CategoriesReadResponse(categoryReadResponses); | ||
} | ||
} |
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
13 changes: 13 additions & 0 deletions
13
backend/bang-ggood/src/main/java/com/bang_ggood/user/repository/UserRepository.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,13 @@ | ||
package com.bang_ggood.user.repository; | ||
|
||
import com.bang_ggood.exception.BangggoodException; | ||
import com.bang_ggood.exception.ExceptionCode; | ||
import com.bang_ggood.user.domain.User; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface UserRepository extends JpaRepository<User, Long> { | ||
|
||
default User getUserById(Long id) { | ||
return findById(id).orElseThrow(() -> new BangggoodException(ExceptionCode.USER_NOT_FOUND)); | ||
} | ||
} |
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
Oops, something went wrong.