Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] 카테고리 추가 API #705

Merged
merged 11 commits into from
Mar 8, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
import gg.data.party.Category;

public interface CategoryAdminRepository extends JpaRepository<Category, Long> {

Boolean existsByName(String categoryName);
}
3 changes: 3 additions & 0 deletions gg-data/src/main/java/gg/data/party/Category.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,7 @@ public class Category extends BaseTimeEntity {
@Column(name = "name", length = 10)
private String name;

public Category(String name) {
this.name = name;
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package gg.party.api.admin.category.controller;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import gg.party.api.admin.category.controller.request.CategoryAddAdminReqDto;
import gg.party.api.admin.category.service.CategoryAdminService;
import lombok.RequiredArgsConstructor;

Expand All @@ -22,6 +26,17 @@ public class CategoryAdminController {
@DeleteMapping("{category_id}")
public ResponseEntity<Void> categoryRemove(@PathVariable("category_id") Long categoryId) {
categoryAdminService.removeCategory(categoryId);
return ResponseEntity.noContent().build();
return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
}

/**
* 카테고리 추가
* @param reqDto 추가할 카테고리 이름
* @return 추가 성공 여부
*/
@PostMapping
public ResponseEntity<Void> categoryAdd(@RequestBody CategoryAddAdminReqDto reqDto) {
categoryAdminService.addCategory(reqDto);
return ResponseEntity.status(HttpStatus.CREATED).build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package gg.party.api.admin.category.controller.request;

import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
public class CategoryAddAdminReqDto {
private String categoryName;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import gg.admin.repo.category.CategoryAdminRepository;
import gg.admin.repo.room.RoomAdminRepository;
import gg.data.party.Category;
import gg.party.api.admin.category.controller.request.CategoryAddAdminReqDto;
import gg.utils.exception.party.CategoryDuplicateException;
import gg.utils.exception.party.CategoryNotFoundException;
import gg.utils.exception.party.DefaultCategoryNeedException;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -40,4 +42,19 @@ public void removeCategory(Long categoryId) {

categoryAdminRepository.deleteById(categoryId);
}

/**
* 카테고리 추가
* @param reqDto 추가할 카테고리 이름
* @exception CategoryDuplicateException 중복된 카테고리
*/
@Transactional
public void addCategory(CategoryAddAdminReqDto reqDto) {
String categoryName = reqDto.getCategoryName();

if (categoryAdminRepository.existsByName(categoryName)) {
throw new CategoryDuplicateException();
}
categoryAdminRepository.save(new Category(categoryName));
}
}
1 change: 1 addition & 0 deletions gg-utils/src/main/java/gg/utils/exception/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ public enum ErrorCode {
ROOM_SAME_STATUS(400, "PT204", "이미 처리된 방 입니다."),
ROOM_NOT_OPEN(400, "PT205", "모집중인 방이 아닙니다."),
ROOM_NOT_PARTICIPANT(400, "PT206", "참여하지 않은 방 입니다."),
CATEGORY_DUPLICATE(400, "PT207", "중복된 카테고리 입니다."),
USER_ALREADY_IN_ROOM(409, "PT301", "이미 참여한 방 입니다."),
ALREADY_REPORTED_ROOM(409, "PT302", "이미 신고한 방 입니다.");

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package gg.utils.exception.party;

import gg.utils.exception.ErrorCode;
import gg.utils.exception.custom.DuplicationException;

public class CategoryDuplicateException extends DuplicationException {
public CategoryDuplicateException() {
super(ErrorCode.CATEGORY_DUPLICATE.getMessage(), ErrorCode.CATEGORY_DUPLICATE);
}
}
Loading