Skip to content

Commit

Permalink
feat: category table 생성
Browse files Browse the repository at this point in the history
  • Loading branch information
CYJhub committed Feb 24, 2024
1 parent 1aea7f5 commit 2c9d328
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 14 deletions.
25 changes: 21 additions & 4 deletions src/main/java/shop/hooking/hooking/controller/DraftController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import shop.hooking.hooking.dto.request.CategoryReqDto;
import shop.hooking.hooking.dto.request.DraftReqDto;
import shop.hooking.hooking.dto.response.CategoryResDto;
import shop.hooking.hooking.service.DraftService;
Expand All @@ -24,15 +25,31 @@ public ResponseEntity<?> createDraft(HttpServletRequest httpRequest, @RequestBod

}

// (카테고리 -> 시안)이 리스트가 보여지는 화면
@Operation(summary = "시안 리스트 보여주기")
@GetMapping
@GetMapping("/{index}")
public ResponseEntity<CategoryResDto> getDraftList(HttpServletRequest httpRequest,@PathVariable int index) {
return ResponseEntity.ok(draftService.getDraftList(httpRequest,index)); //
return ResponseEntity.ok(draftService.getDraftList(httpRequest,index)); // 카테고리 3개씩 인덱싱
}

@Operation(summary = "시안 검색하기")
@GetMapping("/search")
@GetMapping("/search/{index}")
public ResponseEntity<CategoryResDto> searchDraftList(HttpServletRequest httpRequest, @RequestParam(name = "keyword") String q, @PathVariable int index) {
return ResponseEntity.ok(draftService.searchDraftList(httpRequest,q, index)); //
return ResponseEntity.ok(draftService.searchDraftList(httpRequest,q, index)); // 카테고리 3개씩 인덱싱
}

@Operation(summary = "시안 삭제하기")
@PostMapping("/{draftId}")
public ResponseEntity<?> deleteDraft(@PathVariable Long draftId) {
draftService.deleteDraft(draftId);
return ResponseEntity.ok().build();

}

@Operation(summary = "카테고리 생성하기")
@PostMapping("/create")
public ResponseEntity<?> createCategory(HttpServletRequest httpRequest, @RequestBody CategoryReqDto categoryReqDto){
draftService.createCategory(httpRequest, categoryReqDto);
return ResponseEntity.ok().build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package shop.hooking.hooking.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import shop.hooking.hooking.entity.Category;
import shop.hooking.hooking.entity.Contain;

import java.util.Optional;

@Repository
public interface CategoryRepository extends JpaRepository<Category, Long> {

Optional<Category> findByCategoryName(String categoryName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ public interface DraftRepository extends JpaRepository<Draft, Long>{
List<Draft> findByUser(User user);


List<Draft> findBydraftId(Long id);
Draft findBydraftId(Long id);


}
75 changes: 66 additions & 9 deletions src/main/java/shop/hooking/hooking/service/DraftService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import shop.hooking.hooking.dto.request.CategoryReqDto;
import shop.hooking.hooking.dto.request.DraftReqDto;
import shop.hooking.hooking.dto.response.*;
import shop.hooking.hooking.entity.Card;
import shop.hooking.hooking.entity.Draft;
import shop.hooking.hooking.entity.Scrap;
import shop.hooking.hooking.entity.User;
import shop.hooking.hooking.entity.*;
import shop.hooking.hooking.exception.CardNotFoundException;
import shop.hooking.hooking.exception.OutOfIndexException;
import shop.hooking.hooking.global.jwt.JwtTokenProvider;
import shop.hooking.hooking.repository.CategoryRepository;
import shop.hooking.hooking.repository.DraftJpaRepository;
import shop.hooking.hooking.repository.DraftRepository;

Expand All @@ -26,12 +26,47 @@ public class DraftService {
private final DraftRepository draftRepository;
private final DraftJpaRepository draftJpaRepository;

private final CategoryRepository categoryRepository;

// 카테고리 없을 때
@Transactional
public void createCategory(HttpServletRequest httpRequest, CategoryReqDto categoryReqDto) {
User user = jwtTokenProvider.getUserInfoByToken(httpRequest);

// 이미 같은 이름의 카테고리가 있는지 확인
Optional<Category> existingCategory = categoryRepository.findByCategoryName(categoryReqDto.getCategoryName());

// 이미 존재하는 경우, 예외처리 또는 다른 로직 수행
if (existingCategory.isPresent()) {
throw new RuntimeException("Category with the same name already exists");
}

// 존재하지 않는 경우 새로운 카테고리 생성
Category newCategory = Category.builder()
.user(user)
.categoryName(categoryReqDto.getCategoryName())
.createdAt(categoryReqDto.getCreatedAt())
.build();


// 저장
categoryRepository.save(newCategory);
}


// 카테고리 있을 때 Draft 생성
@Transactional
public void createDraft(HttpServletRequest httpRequest, DraftReqDto draftReqDto){
User user = jwtTokenProvider.getUserInfoByToken(httpRequest);

Category category = categoryRepository.findByCategoryName(draftReqDto.getCategoryName())
.orElseGet(() -> Category.builder()
.categoryName(draftReqDto.getCategoryName())
.build());

Draft draft = Draft.builder()
.user(user)
.categoryName(draftReqDto.getCategoryName())
.category(category)
.createdAt(draftReqDto.getCreatedAt())
.text(draftReqDto.getText())
.build();
Expand All @@ -45,10 +80,11 @@ public DraftResDto createDraftRes(Draft draft) {
return DraftResDto.builder()
.text(draft.getText())
.createdAt(draft.getCreatedAt())
.categoryName(draft.getCategoryName())
.categoryName(draft.getCategory().getCategoryName())
.build();
}
public CategoryResDto getDraftList(HttpServletRequest httpRequest,int index) {

public CategoryResDto getDraftList(HttpServletRequest httpRequest, int index) {
User user = jwtTokenProvider.getUserInfoByToken(httpRequest);

// 현재 사용자의 모든 Draft을 데이터베이스에서 조회
Expand All @@ -68,9 +104,12 @@ public CategoryResDto getDraftList(HttpServletRequest httpRequest,int index) {
.build())
.collect(Collectors.toList());

// 3개씩 묶어 반환
List<CategoryDraftResDto> result = paginate(categoryDraftResList, index);

// 최종적으로 CategoryResDto를 생성하고 반환
return CategoryResDto.builder()
.data(categoryDraftResList)
.data(result)
.build();
}

Expand Down Expand Up @@ -99,12 +138,30 @@ public CategoryResDto searchDraftList(HttpServletRequest httpRequest, String q,
.build())
.collect(Collectors.toList());

// 3개씩 묶어 반환
List<CategoryDraftResDto> result = paginate(categoryDraftResList, index);

// 결과를 CategoryResDto로 묶어서 반환
return CategoryResDto.builder()
.data(categoryDraftResList)
.data(result)
.build();
}


// 인덱싱을 수행하는 함수
private List<CategoryDraftResDto> paginate(List<CategoryDraftResDto> list, int index) {
int startIndex = index * 3;
int endIndex = Math.min(startIndex + 3, list.size());
if (startIndex >= endIndex) {
throw new OutOfIndexException();
}
return list.subList(startIndex, endIndex);
}

public void deleteDraft(Long draftId){
Draft draft = draftRepository.findBydraftId(draftId);
draft.setDeleteFlag(true);
draftRepository.save(draft);
}

}

0 comments on commit 2c9d328

Please sign in to comment.