Skip to content

Commit

Permalink
Merge pull request #14 from HealthMer/feat/#13-CategoryAPI
Browse files Browse the repository at this point in the history
feat: CategoryAPI 로직 구현
  • Loading branch information
Dylan-yoon authored Nov 26, 2024
2 parents 514f978 + b0f2aee commit 27c3d32
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<Timer> 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<Timer> 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.");
}
}
}
Original file line number Diff line number Diff line change
@@ -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<Timer> selectTimersByUserIdAndCategory(Map<String, Object> params);
}
Original file line number Diff line number Diff line change
@@ -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<Timer> getTimersByCategory(long userId, int categoryId) {
List<Timer> tm = categorySelectingDao.selectTimersByUserIdAndCategory(Map.of("userId", userId, "categoryId", categoryId));
return tm;
}
}
14 changes: 14 additions & 0 deletions HealthMer/src/main/resources/Mappers/CategorySelectingMapper.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.minijean.healthmer.model.dao.CategorySelectingDao">
<select id="selectTimersByUserIdAndCategory" parameterType="map" resultType="Timer">
SELECT ti.*
FROM timer_info ti
JOIN timer_category tc ON ti.id = tc.timer_info_id
JOIN health_category hc ON tc.health_category_id = hc.id
WHERE hc.id = #{categoryId} AND ti.user_id = #{userId}
</select>
</mapper>

0 comments on commit 27c3d32

Please sign in to comment.