Skip to content

Commit

Permalink
습관 그만두기API
Browse files Browse the repository at this point in the history
  • Loading branch information
jiixon committed Jul 6, 2023
1 parent 3744119 commit 3a3e91a
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 60 deletions.
48 changes: 44 additions & 4 deletions src/main/java/com/itsu/threedays/controller/HabitController.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.itsu.threedays.controller;

import com.itsu.threedays.dto.HabitDto;
import com.itsu.threedays.dto.HabitEditResponseDto;
import com.itsu.threedays.dto.HabitResponseDto;
import com.itsu.threedays.dto.HabitUpdateRequestDto;
import com.itsu.threedays.entity.HabitEntity;
Expand All @@ -18,6 +19,7 @@
import java.math.BigInteger;
import java.time.LocalDateTime;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.stream.Collectors;

@Slf4j
Expand Down Expand Up @@ -77,6 +79,29 @@ ResponseEntity<List<HabitResponseDto>> getHabitList(@RequestParam("email") Strin
return ResponseEntity.ok(habitResponseDtos);
}

@GetMapping("habits/edit-list") //편집시 습관목록조회(중지일 포함)
ResponseEntity<List<HabitEditResponseDto>> getHabitEditList(@RequestParam("email") String email) throws Exception{
List<HabitEntity> habits = habitService.findUndeletedAndAllHabits(email);
log.info("undeletedAndAllHabits: {}",habits);
List<HabitEditResponseDto> habitEditListDto = habits.stream()
.map(habitEntity -> {
HabitEditResponseDto editResponseDto = new HabitEditResponseDto();
editResponseDto.setId(habitEntity.getId());
editResponseDto.setTitle(habitEntity.getTitle());
editResponseDto.setDuration(habitEntity.getDuration());
editResponseDto.setVisible(habitEntity.isVisible());
editResponseDto.setComboCount(habitEntity.getComboCount());
editResponseDto.setAchievementRate(habitEntity.getAchievementRate());
editResponseDto.setAchievementCount(habitEntity.getAchievementCount());
editResponseDto.setStopDate(habitEntity.getStopDate()); //중지일
return editResponseDto;
})
.collect(Collectors.toList());
return ResponseEntity.ok(habitEditListDto)
}



@PutMapping("habits/{habitId}/edit") //습관수정(이름, 기간, 공개여부)
ResponseEntity<HabitResponseDto> updateHabit(@PathVariable("habitId") Long habitId,
@RequestBody HabitUpdateRequestDto habitUpdateDto) throws Exception {
Expand All @@ -90,16 +115,31 @@ ResponseEntity<HabitResponseDto> updateHabit(@PathVariable("habitId") Long habit
ResponseEntity<?> deleteHabit(@PathVariable("habitId") Long habitId){
habitService.deleteHabit(habitId);

return ResponseEntity.ok("습관이 성공적으로 삭제되었습니다.");
return ResponseEntity.ok("Habit deleted successfully.");

}

@PutMapping("habits/{habitId}/stop") //습관 중지
ResponseEntity<String> stopHabit(@PathVariable("habitId") Long habitId){
try {
habitService.stopHabit(habitId);
return ResponseEntity.ok("Habit stopped successfully.");
} catch (NoSuchElementException e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Habit not found.");
} catch (IllegalStateException e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("An error occurred.");
}

}

//습관 그만두기
//습관 편집시 목록


@GetMapping("habits/{habitId}/reset") //매주마다 달성횟수 리셋
ResponseEntity<?> resetAcievement(@PathVariable("habitId") Long habitId){
ResponseEntity<String> resetAchievement(@PathVariable("habitId") Long habitId){
habitService.resetAchievement(habitId);
return ResponseEntity.ok("달성횟수가 성공적으로 리셋되었습니다.");
return ResponseEntity.ok("Resetting the achievement count was successful.");
}
}
23 changes: 23 additions & 0 deletions src/main/java/com/itsu/threedays/dto/HabitEditResponseDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.itsu.threedays.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class HabitEditResponseDto {
Long id;
String title;
int duration;
boolean visible;
int comboCount;
int achievementRate;
int achievementCount;
LocalDateTime stopDate;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public interface HabitRepository extends JpaRepository<HabitEntity,Long> {
List<HabitEntity> findAllByUserId(UserEntity userId);
List<HabitEntity> findAllByUserIdAndDeleteYnAndStopDateIsNull(UserEntity userId, boolean deleteYn);

List<HabitEntity> findAllByUserIdAndDeleteYn(boolean deleteYn);

Optional<HabitEntity> findById(Long habitId);

}
45 changes: 41 additions & 4 deletions src/main/java/com/itsu/threedays/service/HabitService.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,26 @@ public List<HabitEntity> findUndeletedAndActiveHabits(String email) throws Excep
Optional<UserEntity> byEmail = userRepository.findByEmail(email);
if(byEmail.isPresent()){
UserEntity user = byEmail.get();
log.info("email로 user 찾기:{}",user);
log.info("email로 user 찾기:{}",user.getEmail());
return habitRepository.findAllByUserIdAndDeleteYnAndStopDateIsNull(user,false);
}
else {
throw new Exception("User NOT FOUND!");
}
}

public List<HabitEntity> findUndeletedAndAllHabits(String email) throws Exception{ //삭제여부 false 전부(중지일 상관없이 = 그만두기 포함)
Optional<UserEntity> byEmail = userRepository.findByEmail(email);
if (byEmail.isPresent()){
UserEntity user = byEmail.get();
log.info("email로 user 찾기:{}",user.getEmail());
return habitRepository.findAllByUserIdAndDeleteYn(false);
}
else {
throw new Exception("User NOT FOUND!");
}
}

public HabitResponseDto updateHabit(Long habitId, HabitUpdateRequestDto habitUpdateDto) throws Exception{

//습관 ID로 습관 찾기
Expand Down Expand Up @@ -82,8 +94,10 @@ public void deleteHabit(Long habitId){
Optional<HabitEntity> byUserId = habitRepository.findById(habitId);
if(byUserId.isPresent()){
HabitEntity habit = byUserId.get();
habit.setDeleteYn(true);
habit.setLastModifiedDate(LocalDateTime.now());
log.info("습관ID로 습관찾기: 습관명 - {}",habit.getTitle());

habit.setDeleteYn(true); //삭제여부 true로 변경
habit.setLastModifiedDate(LocalDateTime.now()); //수정일 현재날짜로 변경
habitRepository.save(habit);
} else {
throw new NotFoundException("Habit not found with id: " + habitId);
Expand Down Expand Up @@ -123,7 +137,29 @@ public void updateAchievementAndCombo(Long habitId){
habit.setComboCount(newComboCount); //콤보횟수 +1
}

habitRepository.save(habit);

}

public void stopHabit(Long habitId){
Optional<HabitEntity> optionalHabit = habitRepository.findById(habitId);
if(optionalHabit.isPresent()){
HabitEntity habit = optionalHabit.get();

if(habit.getStopDate() == null){ //중지일이 null 이면
habit.setStopDate(LocalDateTime.now()); //중지일을 현재 날짜로 설정
habitRepository.save(habit);
}
else {
throw new IllegalStateException("Habit is already stopped.");
}

} else {
throw new NotFoundException("Habit not found with id: " + habitId);
}
}


public void resetAchievement(Long habitId) {
//습관ID로 해당습관 찾기
HabitEntity habit = habitRepository.findById(habitId)
Expand All @@ -145,7 +181,8 @@ public void resetAchievement(Long habitId) {
habit.setAchievementCount(0); //달성횟수 초기화
}
}

}



}
52 changes: 0 additions & 52 deletions src/main/java/com/itsu/threedays/service/KakaoUserService.java

This file was deleted.

0 comments on commit 3a3e91a

Please sign in to comment.