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
@@ -0,0 +1,10 @@
package gg.admin.repo.category;

import org.springframework.data.jpa.repository.JpaRepository;

import gg.data.party.Category;

public interface CategoryAdminRepository extends JpaRepository<Category, Long> {

Boolean existsByName(String categoryName);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package gg.admin.repo.room;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;

import gg.data.party.Category;
import gg.data.party.Room;

public interface RoomAdminRepository extends JpaRepository<Room, Long> {
List<Room> findByCategory(Category category);

}
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;
}
}
15 changes: 11 additions & 4 deletions gg-data/src/main/java/gg/data/party/Room.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,24 @@ public class Room extends BaseTimeEntity {
@Column(name = "due_date")
private LocalDateTime dueDate;

@Column(name = "start_date")
private LocalDateTime startDate;

@OneToMany(mappedBy = "room", cascade = CascadeType.ALL)
private List<Comment> comments = new ArrayList<>();

@Enumerated(EnumType.STRING)
@Column(name = "status")
private RoomType status;

public void updateCurrentPeople(int currentPeople) {
this.currentPeople = currentPeople;
}

public void updateCategory(Category category) {
this.category = category;
}

@Builder
public Room(User host, User creator, Category category, String title, String content, Integer currentPeople,
Integer maxPeople, Integer minPeople, LocalDateTime dueDate, RoomType status) {
Expand All @@ -85,10 +96,6 @@ public Room(User host, User creator, Category category, String title, String con
this.status = status;
}

public void updateCurrentPeople(int currentPeople) {
this.currentPeople = currentPeople;
}

public void updateStatus(RoomType status) {
this.status = status;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,42 @@
package gg.pingpong.api.party.admin.category.controller;
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;

@RestController
@RequiredArgsConstructor
@RequestMapping("/party/admin/categories")
public class CategoryAdminController {
private final CategoryAdminService categoryAdminService;

/**
* 카테고리 삭제
* @return 삭제 성공 여부
*/
@DeleteMapping("{category_id}")
public ResponseEntity<Void> categoryRemove(@PathVariable("category_id") Long categoryId) {
categoryAdminService.removeCategory(categoryId);
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
@@ -0,0 +1,60 @@
package gg.party.api.admin.category.service;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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;

@Service
@RequiredArgsConstructor
public class CategoryAdminService {
private final CategoryAdminRepository categoryAdminRepository;
private final RoomAdminRepository roomAdminRepository;

/**
* 카테고리 삭제
* 삭제 시 기존에 room에 연결되어 있던 카테고리는 default(1) 로 변경
* @param categoryId 삭제할 카테고리 id
* @exception CategoryNotFoundException 유효하지 않은 카테고리
* @exception DefaultCategoryNeedException default 카테고리 존재 x 또는 default 카테고리 삭제 요청
*/
@Transactional
public void removeCategory(Long categoryId) {
Category category = categoryAdminRepository.findById(categoryId)
.orElseThrow(CategoryNotFoundException::new);

Category defaultCategory = categoryAdminRepository.findById(DefaultCategoryNeedException.DEFAULT_CATEGORY_ID)
.orElseThrow(DefaultCategoryNeedException::new);

if (category.equals(defaultCategory)) {
throw new DefaultCategoryNeedException();
}

roomAdminRepository.findByCategory(category)
.forEach(room -> room.updateCategory(defaultCategory));

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));
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
package gg.pingpong.api.party.user.category.controller;
package gg.party.api.user.category.controller;

import java.util.List;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import gg.party.api.user.category.controller.response.CategoryResDto;
import gg.party.api.user.category.service.CategoryService;
import lombok.RequiredArgsConstructor;

@RestController
@RequiredArgsConstructor
@RequestMapping("/party/categories")
public class CategoryController {
private final CategoryService categoryService;

/**
* 카테고리 조회
* @return 카테고리 전체 리스트
*/
@GetMapping
public ResponseEntity<List<CategoryResDto>> categoryList() {
return ResponseEntity.status(HttpStatus.OK).body(categoryService.findCategoryList());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package gg.party.api.user.category.controller.response;

import gg.data.party.Category;
import lombok.Getter;

@Getter
public class CategoryResDto {
private Long categoryId;
private String categoryName;

public CategoryResDto(Category category) {
this.categoryId = category.getId();
this.categoryName = category.getName();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package gg.party.api.user.category.service;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import gg.party.api.user.category.controller.response.CategoryResDto;
import gg.repo.party.CategoryRepository;
import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
public class CategoryService {
private final CategoryRepository categoryRepository;

/**
* 카테고리 전체 조회
* @return 카테고리 전체 리스트 (id 순으로 오름차순 정렬)
*/
@Transactional(readOnly = true)
public List<CategoryResDto> findCategoryList() {
return categoryRepository.findAll(Sort.by(Sort.Direction.ASC, "id")).stream()
.map(CategoryResDto::new)
.collect(Collectors.toList());
}

}
Loading
Loading