-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
feat: CategoryAPI 로직 구현
- Loading branch information
Showing
4 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
HealthMer/src/main/java/com/minijean/healthmer/controller/CategorySelectingController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
HealthMer/src/main/java/com/minijean/healthmer/model/dao/CategorySelectingDao.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
22 changes: 22 additions & 0 deletions
22
HealthMer/src/main/java/com/minijean/healthmer/model/service/CategorySelectingService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
14
HealthMer/src/main/resources/Mappers/CategorySelectingMapper.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |