diff --git a/HealthMer/src/main/java/com/minijean/healthmer/controller/CategorySelectingController.java b/HealthMer/src/main/java/com/minijean/healthmer/controller/CategorySelectingController.java new file mode 100644 index 0000000..6cdf876 --- /dev/null +++ b/HealthMer/src/main/java/com/minijean/healthmer/controller/CategorySelectingController.java @@ -0,0 +1,66 @@ +package com.minijean.healthmer.controller; + +import java.util.List; + +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestHeader; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.minijean.healthmer.model.dto.Timer; +import com.minijean.healthmer.model.service.CategorySelectingService; +import com.minijean.healthmer.util.JwtUtil; + +@RestController +@RequestMapping("/api/v1/category") +public class CategorySelectingController { + + private CategorySelectingService categorySelectingService; + private JwtUtil jwtUtil; + + public CategorySelectingController(CategorySelectingService categorySelectingService, JwtUtil jwtUtil) { + this.categorySelectingService = categorySelectingService; + this.jwtUtil = jwtUtil; + } + +// @GetMapping("/{categoryId}/timers") +// @GetMapping("/{categoryId}") +// public ResponseEntity getTimersByCategory(@PathVariable("categoryId") int categoryId) { +// if (categoryId < 1 || categoryId > 6) { +// return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Invalid categoryId. Please provide a value between 1 and 6."); +// } +// System.out.println(categoryId); +// try { +// List timers = categorySelectingService.getTimersByCategory(categoryId); +// System.out.println(timers); +// if (timers.isEmpty()) { +// return ResponseEntity.status(HttpStatus.NO_CONTENT).build(); +// } +// return ResponseEntity.ok(timers); +// } catch (Exception e) { +// return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("An error occurred while fetching timers."); +// } +// } + + @GetMapping("/{categoryId}") + public ResponseEntity getTimersByCategory(@RequestHeader(HttpHeaders.AUTHORIZATION) String authorizationHeader, @PathVariable("categoryId") int categoryId) { + if (categoryId < 1 || categoryId > 6) { + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Invalid categoryId. Please provide a value between 1 and 6."); + } + try { + long userId = jwtUtil.extractUserId(authorizationHeader); + System.out.println(userId); + List timers = categorySelectingService.getTimersByCategory(userId, categoryId); + if (timers.isEmpty()) { + return ResponseEntity.status(HttpStatus.NO_CONTENT).build(); + } + return ResponseEntity.ok(timers); + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("An error occurred while fetching timers."); + } + } +} \ No newline at end of file diff --git a/HealthMer/src/main/java/com/minijean/healthmer/model/dao/CategorySelectingDao.java b/HealthMer/src/main/java/com/minijean/healthmer/model/dao/CategorySelectingDao.java new file mode 100644 index 0000000..cb0874d --- /dev/null +++ b/HealthMer/src/main/java/com/minijean/healthmer/model/dao/CategorySelectingDao.java @@ -0,0 +1,13 @@ +package com.minijean.healthmer.model.dao; + +import org.springframework.stereotype.Repository; + +import com.minijean.healthmer.model.dto.Timer; + +import java.util.List; +import java.util.Map; + +@Repository +public interface CategorySelectingDao { + List selectTimersByUserIdAndCategory(Map params); +} \ No newline at end of file diff --git a/HealthMer/src/main/java/com/minijean/healthmer/model/service/CategorySelectingService.java b/HealthMer/src/main/java/com/minijean/healthmer/model/service/CategorySelectingService.java new file mode 100644 index 0000000..8e5c02b --- /dev/null +++ b/HealthMer/src/main/java/com/minijean/healthmer/model/service/CategorySelectingService.java @@ -0,0 +1,22 @@ +package com.minijean.healthmer.model.service; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import com.minijean.healthmer.model.dao.CategorySelectingDao; +import com.minijean.healthmer.model.dto.Timer; + +import java.util.List; +import java.util.Map; + +@Service +public class CategorySelectingService { + + @Autowired + private CategorySelectingDao categorySelectingDao; + + public List getTimersByCategory(long userId, int categoryId) { + List tm = categorySelectingDao.selectTimersByUserIdAndCategory(Map.of("userId", userId, "categoryId", categoryId)); + return tm; + } +} \ No newline at end of file diff --git a/HealthMer/src/main/resources/Mappers/CategorySelectingMapper.xml b/HealthMer/src/main/resources/Mappers/CategorySelectingMapper.xml new file mode 100644 index 0000000..500e658 --- /dev/null +++ b/HealthMer/src/main/resources/Mappers/CategorySelectingMapper.xml @@ -0,0 +1,14 @@ + + + + + +