Skip to content

Commit

Permalink
♻️ Feat: 카테고리 조회 api 리팩토링
Browse files Browse the repository at this point in the history
♻️ Feat: 카테고리 조회 api 리팩토링
  • Loading branch information
chaerinryu authored Aug 22, 2024
2 parents c0445dc + 28f7d15 commit b4364c4
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

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

@RestController
Expand Down Expand Up @@ -281,19 +282,14 @@ public LectureResponseDTO.LecturePreViewList getLecturesByCategory(@RequestParam
.build();
}

// 메인 카테고리 조회 api
@GetMapping("/categories/main")
// 카테고리 조회 api
@GetMapping("/categories")
@ResponseBody
public LectureResponseDTO.MainCategory getMainCategory() {
return LectureResponseDTO.MainCategory.builder()
.mainCategoryList(lectureService.getMainCategory())
.build();
}
public LectureResponseDTO.CategoryList getCategoryList() {
List<LectureResponseDTO.CategoryResponseDTO> categories = lectureService.getAllCategories();

// 카테고리 id 조회 api
@GetMapping("/categories/id")
@ResponseBody
public Long getCategoryId(@RequestParam(name = "mainCategory") String mainCategory) {
return lectureService.getCategoryId(mainCategory);
return LectureResponseDTO.CategoryList.builder()
.categoryList(categories)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import com.example.majorLink.dto.response.PaymentResponseDTO;
import com.example.majorLink.dto.response.ProductOrderResponseDTO;
import com.example.majorLink.global.auth.AuthUser;
import com.example.majorLink.service.PaymentService.PaymentServiceImpl;
import com.example.majorLink.service.PaymentService.PaymentService;
import com.siot.IamportRestClient.exception.IamportResponseException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -23,7 +23,7 @@
@RequestMapping("/payment")
public class PaymentController {

private final PaymentServiceImpl paymentService;
private final PaymentService paymentService;

// 결제 요청
@PostMapping("/order")
Expand Down Expand Up @@ -57,4 +57,4 @@ public PaymentResponseDTO validatePayment(@AuthenticationPrincipal AuthUser auth
.build();
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,16 @@ public static class CreateTuteeLecture {
@Getter
@AllArgsConstructor
@NoArgsConstructor
public static class MainCategory {
List<String> mainCategoryList;
public static class CategoryResponseDTO {
Long categoryId;
String mainCategory;
}

@Builder
@Getter
@AllArgsConstructor
@NoArgsConstructor
public static class SubCategory {
List<String> subCategoryList;
public static class CategoryList {
List<CategoryResponseDTO> categoryList;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,4 @@

@Repository
public interface CategoryRepository extends JpaRepository<Category, Long> {
@Query("SELECT DISTINCT c.mainCategory FROM Category c")
List<String> findMainCategory();

Optional<Category> findByMainCategory(String mainCategory);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.example.majorLink.domain.Lecture;
import com.example.majorLink.domain.mapping.TuteeLecture;
import com.example.majorLink.dto.request.LectureRequestDTO;
import com.example.majorLink.dto.response.LectureResponseDTO;
import org.springframework.data.domain.Page;

import java.util.List;
Expand All @@ -25,7 +26,5 @@ public interface LectureService {
Page<Lecture> getMostRecruitedLecture(Integer page);
Page<Lecture> getLectureByCategory(Integer page, Long categoryId);

List<String> getMainCategory();

Long getCategoryId(String mainCategory);
List<LectureResponseDTO.CategoryResponseDTO> getAllCategories();
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.example.majorLink.domain.mapping.TuteeLecture;
import com.example.majorLink.domain.mapping.TutorLecture;
import com.example.majorLink.dto.request.LectureRequestDTO;
import com.example.majorLink.dto.response.LectureResponseDTO;
import com.example.majorLink.repository.*;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
Expand All @@ -16,6 +17,7 @@

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

@Service
@Transactional
Expand Down Expand Up @@ -242,15 +244,12 @@ public Page<Lecture> getLectureByCategory(Integer page, Long categoryId) {
return lectureRepository.orderByCategoryId(categoryId, PageRequest.of(page, 10));
}

// 메인 카테고리 조회
public List<String> getMainCategory() {
return categoryRepository.findMainCategory();
}
// 카테고리 조회
public List<LectureResponseDTO.CategoryResponseDTO> getAllCategories() {
List<Category> categories = categoryRepository.findAll();

// 카테고리 ID 조회
public Long getCategoryId(String mainCategory) {
Category category = categoryRepository.findByMainCategory(mainCategory)
.orElseThrow(() -> new IllegalArgumentException("Category not found"));
return category.getId();
return categories.stream()
.map(category -> new LectureResponseDTO.CategoryResponseDTO(category.getId(), category.getMainCategory()))
.collect(Collectors.toList());
}
}

0 comments on commit b4364c4

Please sign in to comment.