Skip to content

Commit

Permalink
feat: User id 에 따른 Timer List Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Dylan-yoon committed Nov 25, 2024
1 parent 4ae500e commit 69312e1
Show file tree
Hide file tree
Showing 7 changed files with 214 additions and 183 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@

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.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

Expand All @@ -21,108 +22,111 @@
import com.minijean.healthmer.model.dto.TimerCategory;
import com.minijean.healthmer.model.dto.TimerRequest;
import com.minijean.healthmer.model.service.TimerService;
import com.minijean.healthmer.util.JwtUtil;
import com.minijean.healthmer.model.dto.SearchCondition;

@RestController
@RequestMapping("/api/v1/timer")
public class TimerController {

private final TimerService timerService;

public TimerController(TimerService timerService) {
private final JwtUtil jtwUtil;

public TimerController(TimerService timerService, JwtUtil jtwUtil) {
this.timerService = timerService;
this.jtwUtil = jtwUtil;

}

@GetMapping("")
public ResponseEntity<?> listAll(){
List<Timer> list = timerService.getTimerList();

if(list == null || list.size() == 0) {
public ResponseEntity<?> listAll(@RequestHeader(HttpHeaders.AUTHORIZATION) String authorizationHeader) {
long userId = jtwUtil.extractUserId(authorizationHeader);
List<Timer> list = timerService.getTimerList(userId);
if (list == null || list.size() == 0) {
return new ResponseEntity<Void>(HttpStatus.NO_CONTENT);
}
return new ResponseEntity<>(list, HttpStatus.OK);
}

@GetMapping("/search")
public ResponseEntity<?> search(@ModelAttribute SearchCondition condition){
public ResponseEntity<?> search(@ModelAttribute SearchCondition condition) {
System.out.println("안 된다");
System.out.println(condition.toString());
List<Timer> list = timerService.searchTimer(condition);
if(list == null || list.size() == 0) {

if (list == null || list.size() == 0) {
return new ResponseEntity<Void>(HttpStatus.NO_CONTENT);
}
return new ResponseEntity<>(list, HttpStatus.OK);
}

@GetMapping("/{id}")
public ResponseEntity<?> oneTimer(@PathVariable("id") long id){
public ResponseEntity<?> oneTimer(@PathVariable("id") long id) {
TimerRequest timerReq = timerService.getOneTimer(id);
if(timerReq == null) {
if (timerReq == null) {
return new ResponseEntity<Void>(HttpStatus.NO_CONTENT);
}
return new ResponseEntity<>(timerReq, HttpStatus.OK);
}

@PutMapping("/{id}")
public ResponseEntity<?> updateOneTimer(@PathVariable("id") long id, @RequestBody TimerRequest timerRequest){
public ResponseEntity<?> updateOneTimer(@PathVariable("id") long id, @RequestBody TimerRequest timerRequest) {

TimerRequest updatedTimer = timerService.modifyOneTimer(id, timerRequest);
if(updatedTimer == null) {

if (updatedTimer == null) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("잘못된 입력입니다.");
}

return ResponseEntity.ok(updatedTimer);
}

@DeleteMapping("/{id}")
public ResponseEntity<?> deleteTimer(@PathVariable("id") long id){
public ResponseEntity<?> deleteTimer(@PathVariable("id") long id) {
boolean isDeleted = timerService.removeTimer(id);
if(isDeleted) {

if (isDeleted) {
return ResponseEntity.status(HttpStatus.OK).body("Timer deleted");
}
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Fail Delete");
}

@GetMapping("/{id}/routine")
public ResponseEntity<?> listRoutine(@PathVariable("id") long id){
public ResponseEntity<?> listRoutine(@PathVariable("id") long id) {
List<Routine> list = timerService.getRoutineList(id);
if(list == null || list.size() == 0) {

if (list == null || list.size() == 0) {
return new ResponseEntity<Void>(HttpStatus.NO_CONTENT);
}
return new ResponseEntity<>(list, HttpStatus.OK);
}

@GetMapping("/category")
public ResponseEntity<?> listCategory(){
public ResponseEntity<?> listCategory() {
List<HealthCategory> list = timerService.getHealthCategoryList();
if(list == null || list.size() == 0) {

if (list == null || list.size() == 0) {
return new ResponseEntity<Void>(HttpStatus.NO_CONTENT);
}
return new ResponseEntity<>(list, HttpStatus.OK);
}

@GetMapping("/{id}/category")
public ResponseEntity<?> listCategory(@PathVariable("id") long id){
public ResponseEntity<?> listCategory(@PathVariable("id") long id) {
List<TimerCategory> list = timerService.getCategoryList(id);
if(list == null || list.size() == 0) {

if (list == null || list.size() == 0) {
return new ResponseEntity<Void>(HttpStatus.NO_CONTENT);
}
return new ResponseEntity<>(list, HttpStatus.OK);
}

@PostMapping("/create")
public ResponseEntity<Timer> create(@RequestBody TimerRequest timerRequest){
public ResponseEntity<Timer> create(@RequestBody TimerRequest timerRequest) {

Timer createdTimer = timerService.createTimer(timerRequest);

return new ResponseEntity<>(createdTimer, HttpStatus.CREATED);
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
public interface TimerDao {

/** 전체 타이머 조회 */
public List<Timer> selectAll();
public List<Timer> selectAll(long userId);

/** 타이머 한 개 조회 */
public Timer selectOne(long id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
public interface TimerService {

/** 전체 타이머 조회 */
public List<Timer> getTimerList();
public List<Timer> getTimerList(long userId);

/** 타이머 한 개 조회 (카테고리와 하위 루틴도 함께 조회함) */
public TimerRequest getOneTimer(long id);
Expand Down
Loading

0 comments on commit 69312e1

Please sign in to comment.