Skip to content

Commit 3a3e91a

Browse files
committed
습관 그만두기API
1 parent 3744119 commit 3a3e91a

5 files changed

Lines changed: 110 additions & 60 deletions

File tree

src/main/java/com/itsu/threedays/controller/HabitController.java

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.itsu.threedays.controller;
22

33
import com.itsu.threedays.dto.HabitDto;
4+
import com.itsu.threedays.dto.HabitEditResponseDto;
45
import com.itsu.threedays.dto.HabitResponseDto;
56
import com.itsu.threedays.dto.HabitUpdateRequestDto;
67
import com.itsu.threedays.entity.HabitEntity;
@@ -18,6 +19,7 @@
1819
import java.math.BigInteger;
1920
import java.time.LocalDateTime;
2021
import java.util.List;
22+
import java.util.NoSuchElementException;
2123
import java.util.stream.Collectors;
2224

2325
@Slf4j
@@ -77,6 +79,29 @@ ResponseEntity<List<HabitResponseDto>> getHabitList(@RequestParam("email") Strin
7779
return ResponseEntity.ok(habitResponseDtos);
7880
}
7981

82+
@GetMapping("habits/edit-list") //편집시 습관목록조회(중지일 포함)
83+
ResponseEntity<List<HabitEditResponseDto>> getHabitEditList(@RequestParam("email") String email) throws Exception{
84+
List<HabitEntity> habits = habitService.findUndeletedAndAllHabits(email);
85+
log.info("undeletedAndAllHabits: {}",habits);
86+
List<HabitEditResponseDto> habitEditListDto = habits.stream()
87+
.map(habitEntity -> {
88+
HabitEditResponseDto editResponseDto = new HabitEditResponseDto();
89+
editResponseDto.setId(habitEntity.getId());
90+
editResponseDto.setTitle(habitEntity.getTitle());
91+
editResponseDto.setDuration(habitEntity.getDuration());
92+
editResponseDto.setVisible(habitEntity.isVisible());
93+
editResponseDto.setComboCount(habitEntity.getComboCount());
94+
editResponseDto.setAchievementRate(habitEntity.getAchievementRate());
95+
editResponseDto.setAchievementCount(habitEntity.getAchievementCount());
96+
editResponseDto.setStopDate(habitEntity.getStopDate()); //중지일
97+
return editResponseDto;
98+
})
99+
.collect(Collectors.toList());
100+
return ResponseEntity.ok(habitEditListDto)
101+
}
102+
103+
104+
80105
@PutMapping("habits/{habitId}/edit") //습관수정(이름, 기간, 공개여부)
81106
ResponseEntity<HabitResponseDto> updateHabit(@PathVariable("habitId") Long habitId,
82107
@RequestBody HabitUpdateRequestDto habitUpdateDto) throws Exception {
@@ -90,16 +115,31 @@ ResponseEntity<HabitResponseDto> updateHabit(@PathVariable("habitId") Long habit
90115
ResponseEntity<?> deleteHabit(@PathVariable("habitId") Long habitId){
91116
habitService.deleteHabit(habitId);
92117

93-
return ResponseEntity.ok("습관이 성공적으로 삭제되었습니다.");
118+
return ResponseEntity.ok("Habit deleted successfully.");
119+
120+
}
121+
122+
@PutMapping("habits/{habitId}/stop") //습관 중지
123+
ResponseEntity<String> stopHabit(@PathVariable("habitId") Long habitId){
124+
try {
125+
habitService.stopHabit(habitId);
126+
return ResponseEntity.ok("Habit stopped successfully.");
127+
} catch (NoSuchElementException e) {
128+
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Habit not found.");
129+
} catch (IllegalStateException e) {
130+
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
131+
} catch (Exception e) {
132+
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("An error occurred.");
133+
}
94134

95135
}
96136

97-
//습관 그만두기
98137
//습관 편집시 목록
99138

139+
100140
@GetMapping("habits/{habitId}/reset") //매주마다 달성횟수 리셋
101-
ResponseEntity<?> resetAcievement(@PathVariable("habitId") Long habitId){
141+
ResponseEntity<String> resetAchievement(@PathVariable("habitId") Long habitId){
102142
habitService.resetAchievement(habitId);
103-
return ResponseEntity.ok("달성횟수가 성공적으로 리셋되었습니다.");
143+
return ResponseEntity.ok("Resetting the achievement count was successful.");
104144
}
105145
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.itsu.threedays.dto;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Builder;
5+
import lombok.Data;
6+
import lombok.NoArgsConstructor;
7+
8+
import java.time.LocalDateTime;
9+
10+
@Data
11+
@Builder
12+
@AllArgsConstructor
13+
@NoArgsConstructor
14+
public class HabitEditResponseDto {
15+
Long id;
16+
String title;
17+
int duration;
18+
boolean visible;
19+
int comboCount;
20+
int achievementRate;
21+
int achievementCount;
22+
LocalDateTime stopDate;
23+
}

src/main/java/com/itsu/threedays/repository/HabitRepository.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ public interface HabitRepository extends JpaRepository<HabitEntity,Long> {
1313
List<HabitEntity> findAllByUserId(UserEntity userId);
1414
List<HabitEntity> findAllByUserIdAndDeleteYnAndStopDateIsNull(UserEntity userId, boolean deleteYn);
1515

16+
List<HabitEntity> findAllByUserIdAndDeleteYn(boolean deleteYn);
17+
1618
Optional<HabitEntity> findById(Long habitId);
1719

1820
}

src/main/java/com/itsu/threedays/service/HabitService.java

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,26 @@ public List<HabitEntity> findUndeletedAndActiveHabits(String email) throws Excep
3636
Optional<UserEntity> byEmail = userRepository.findByEmail(email);
3737
if(byEmail.isPresent()){
3838
UserEntity user = byEmail.get();
39-
log.info("email로 user 찾기:{}",user);
39+
log.info("email로 user 찾기:{}",user.getEmail());
4040
return habitRepository.findAllByUserIdAndDeleteYnAndStopDateIsNull(user,false);
4141
}
4242
else {
4343
throw new Exception("User NOT FOUND!");
4444
}
4545
}
4646

47+
public List<HabitEntity> findUndeletedAndAllHabits(String email) throws Exception{ //삭제여부 false 전부(중지일 상관없이 = 그만두기 포함)
48+
Optional<UserEntity> byEmail = userRepository.findByEmail(email);
49+
if (byEmail.isPresent()){
50+
UserEntity user = byEmail.get();
51+
log.info("email로 user 찾기:{}",user.getEmail());
52+
return habitRepository.findAllByUserIdAndDeleteYn(false);
53+
}
54+
else {
55+
throw new Exception("User NOT FOUND!");
56+
}
57+
}
58+
4759
public HabitResponseDto updateHabit(Long habitId, HabitUpdateRequestDto habitUpdateDto) throws Exception{
4860

4961
//습관 ID로 습관 찾기
@@ -82,8 +94,10 @@ public void deleteHabit(Long habitId){
8294
Optional<HabitEntity> byUserId = habitRepository.findById(habitId);
8395
if(byUserId.isPresent()){
8496
HabitEntity habit = byUserId.get();
85-
habit.setDeleteYn(true);
86-
habit.setLastModifiedDate(LocalDateTime.now());
97+
log.info("습관ID로 습관찾기: 습관명 - {}",habit.getTitle());
98+
99+
habit.setDeleteYn(true); //삭제여부 true로 변경
100+
habit.setLastModifiedDate(LocalDateTime.now()); //수정일 현재날짜로 변경
87101
habitRepository.save(habit);
88102
} else {
89103
throw new NotFoundException("Habit not found with id: " + habitId);
@@ -123,7 +137,29 @@ public void updateAchievementAndCombo(Long habitId){
123137
habit.setComboCount(newComboCount); //콤보횟수 +1
124138
}
125139

140+
habitRepository.save(habit);
141+
126142
}
143+
144+
public void stopHabit(Long habitId){
145+
Optional<HabitEntity> optionalHabit = habitRepository.findById(habitId);
146+
if(optionalHabit.isPresent()){
147+
HabitEntity habit = optionalHabit.get();
148+
149+
if(habit.getStopDate() == null){ //중지일이 null 이면
150+
habit.setStopDate(LocalDateTime.now()); //중지일을 현재 날짜로 설정
151+
habitRepository.save(habit);
152+
}
153+
else {
154+
throw new IllegalStateException("Habit is already stopped.");
155+
}
156+
157+
} else {
158+
throw new NotFoundException("Habit not found with id: " + habitId);
159+
}
160+
}
161+
162+
127163
public void resetAchievement(Long habitId) {
128164
//습관ID로 해당습관 찾기
129165
HabitEntity habit = habitRepository.findById(habitId)
@@ -145,7 +181,8 @@ public void resetAchievement(Long habitId) {
145181
habit.setAchievementCount(0); //달성횟수 초기화
146182
}
147183
}
148-
149184
}
150185

186+
187+
151188
}

src/main/java/com/itsu/threedays/service/KakaoUserService.java

Lines changed: 0 additions & 52 deletions
This file was deleted.

0 commit comments

Comments
 (0)