-
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.
Merge pull request #45 from 9oormDari/develop
feat: ๋ง์ดํ์ด์ง ๋ชฉํ ์๋ฃ ๊ธฐ๋ก
- Loading branch information
Showing
17 changed files
with
502 additions
and
16 deletions.
There are no files selected for viewing
148 changes: 148 additions & 0 deletions
148
src/main/java/com/goormdari/domain/history/application/HistoryService.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,148 @@ | ||
package com.goormdari.domain.history.application; | ||
|
||
import com.goormdari.domain.history.domain.History; | ||
import com.goormdari.domain.history.domain.dto.response.HistoryResponse; | ||
import com.goormdari.domain.history.domain.repository.HistoryRepository; | ||
import com.goormdari.domain.history.exception.ResourceNotFoundException; | ||
import com.goormdari.domain.routine.domain.Routine; | ||
import com.goormdari.domain.routine.domain.repository.RoutineRepository; | ||
import com.goormdari.domain.team.domain.Team; | ||
import com.goormdari.domain.team.domain.repository.TeamRepository; | ||
import com.goormdari.domain.user.domain.User; | ||
import com.goormdari.domain.user.domain.repository.UserRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.time.LocalDate; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional | ||
public class HistoryService { | ||
|
||
private final HistoryRepository historyRepository; | ||
private final TeamRepository teamRepository; | ||
private final RoutineRepository routineRepository; | ||
private final UserRepository userRepository; | ||
|
||
// ์ต์ด ๊ธฐ๋ก ์์ฑ | ||
public void createHistory(Long userId, Long teamId, boolean isSuccess) { | ||
// ํ ์ ๋ณด ๊ฐ์ ธ์ค๊ธฐ | ||
Team team = teamRepository.findById(teamId) | ||
.orElseThrow(() -> new ResourceNotFoundException("Team " + teamId + " not found")); | ||
|
||
// ํ์ ์์์ผ๊ณผ ๋ฐ๋๋ผ์ธ | ||
LocalDate startDate = team.getCreatedAt().toLocalDate(); | ||
LocalDate endDate = team.getDeadLine(); | ||
|
||
// ์ค๋ณต ํ์ธ: ํด๋น ์ ์ ์ ๊ธฐ๊ฐ(์์์ผ ~ ๋ฐ๋๋ผ์ธ)์ ๋ํ ๊ธฐ๋ก์ด ์ด๋ฏธ ์กด์ฌํ๋์ง ํ์ธ | ||
boolean historyExists = historyRepository.existsByUserIdAndDateRange(userId, startDate, endDate); | ||
|
||
if (historyExists) { | ||
// ์ด๋ฏธ ๊ธฐ๋ก์ด ์กด์ฌํ๋ฉด ๋ ์ด์ ์์ฑํ์ง ์์ | ||
return; | ||
} | ||
|
||
// ์ ์ ์ ๋ณด ๊ฐ์ ธ์ค๊ธฐ | ||
User user = userRepository.findById(userId) | ||
.orElseThrow(() -> new ResourceNotFoundException("User " + userId + " not found")); | ||
|
||
// ํ์คํ ๋ฆฌ ์์ฑ ๋ฐ ์ ์ฅ | ||
History history = History.builder() | ||
.goal(team.getGoal()) | ||
.routine1(team.getRoutine1()) | ||
.routine2(team.getRoutine2()) | ||
.routine3(team.getRoutine3()) | ||
.routine4(team.getRoutine4()) | ||
.user(user) // ์ ์ ์ ๋ณด ํ ๋น | ||
.isSuccess(isSuccess) // 70% ์ด์ ์ฑ๊ณต ์ฌ๋ถ ๋ฐ์ | ||
.build(); | ||
historyRepository.save(history); | ||
} | ||
|
||
|
||
@Scheduled(cron = "0 0 0 * * *") // ๋งค์ผ ์์ ์คํ | ||
public void checkTeamGoalsAndCreateHistory() { | ||
LocalDate today = LocalDate.now(); | ||
List<Team> expiredTeams = teamRepository.findAllByDeadLineBefore(today); | ||
|
||
for (Team team : expiredTeams) { | ||
List<User> teamMembers = userRepository.findAllByTeamId(team.getId()); | ||
|
||
for (User user : teamMembers) { | ||
// ๋ชฉํ ๋ฌ์ฑ ์ฌ๋ถ ๊ณ์ฐ | ||
boolean isGoalAchieved = calculateGoalAchievement(user.getId(), team.getId()); | ||
|
||
// ์ค๋ณต ๊ธฐ๋ก ์์ฑ ๋ฐฉ์ง ์ฒ๋ฆฌ๋ ํ์คํ ๋ฆฌ ์์ฑ | ||
createHistory(user.getId(), team.getId(), isGoalAchieved); | ||
} | ||
} | ||
} | ||
|
||
// D-Day ์์ ๋ง๋ค ์ด๊ธฐํ | ||
// ๋งค์ผ ์์ ์ ์คํ (cron ํํ์: "0 0 0 * * *") | ||
@Scheduled(cron = "0 0 0 * * *") | ||
public void updateDDayPlus() { | ||
// ๋ชจ๋ ํ์คํ ๋ฆฌ๋ฅผ ์กฐํ | ||
List<History> histories = historyRepository.findAll(); | ||
|
||
for (History history : histories) { | ||
// dDayPlus ๊ฐ์ 1์ฉ ์ฆ๊ฐ | ||
history.incrementDDayPlus(); | ||
} | ||
// ์ ๋ฐ์ดํธ๋ ํ์คํ ๋ฆฌ ์ ์ฅ | ||
historyRepository.saveAll(histories); | ||
} | ||
|
||
// 70% ์ด์ ๋ฃจํด ์์ ์์ ์ฑ๊ณต์ผ๋ก ๊ณ์ฐ | ||
public boolean calculateGoalAchievement(Long userId, Long teamId) { | ||
// ์ ์ ์ ํ ์ ๋ณด ๊ฐ์ ธ์ค๊ธฐ | ||
Team team = teamRepository.findById(teamId) | ||
.orElseThrow(() -> new ResourceNotFoundException("Team " + teamId + " not found")); | ||
|
||
// ํ์ ์์์ผ๊ณผ ๋ฐ๋๋ผ์ธ ๊ฐ์ ธ์ค๊ธฐ | ||
LocalDate startDate = team.getCreatedAt().toLocalDate(); | ||
LocalDate endDate = team.getDeadLine(); | ||
|
||
// ํด๋น ๊ธฐ๊ฐ ๋ด์ ์ ์ ๊ฐ ์ํํ ๋ฃจํด ๋ชฉ๋ก ๊ฐ์ ธ์ค๊ธฐ | ||
List<Routine> routines = routineRepository.findRoutinesByUserAndDateRange(userId, startDate, endDate); | ||
|
||
// ์ฑ๊ณตํ ๋ฃจํด ๊ฐ์ ๊ณ์ฐ (์ด๋ฏธ์ง๊ฐ ์๋ ๊ฒฝ์ฐ ์ฑ๊ณตํ ๊ฒ์ผ๋ก ๊ฐ์ฃผ) | ||
long successfulRoutines = routines.stream() | ||
.filter(routine -> routine.getRoutineImg() != null && !routine.getRoutineImg().isEmpty()) | ||
.count(); | ||
|
||
// ์ ์ฒด ๋ฃจํด ๊ฐ์ ๊ณ์ฐ | ||
long totalRoutines = routines.size(); | ||
|
||
// ์ฑ๊ณต๋ฅ ๊ณ์ฐ (70% ์ด์ ์ฑ๊ณต ์ ๋ชฉํ ๋ฌ์ฑ) | ||
if (totalRoutines > 0) { | ||
double successRate = (double) successfulRoutines / totalRoutines; | ||
return successRate >= 0.7; | ||
} | ||
return false; // ๋ฃจํด์ด ์๋ ๊ฒฝ์ฐ ์คํจ๋ก ๊ฐ์ฃผ | ||
} | ||
|
||
// ํน์ ์ ์ ์ ํ์คํ ๋ฆฌ ํ์ด์ง ์ฒ๋ฆฌ | ||
public List<HistoryResponse> getAllHistoriesByUser(Long userId) { | ||
List<History> histories = historyRepository.findAllByUserId(userId); | ||
|
||
return histories.stream().map(history -> { | ||
String[] routines = { | ||
history.getRoutine1(), | ||
history.getRoutine2(), | ||
history.getRoutine3(), | ||
history.getRoutine4() | ||
}; | ||
|
||
int dDay = history.getDDayPlus(); | ||
String result = history.getIsSuccess() ? "์ฑ๊ณต" : "์คํจ"; | ||
|
||
return new HistoryResponse(dDay, history.getGoal(), routines, result); | ||
}).collect(Collectors.toList()); | ||
} | ||
} |
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
17 changes: 17 additions & 0 deletions
17
src/main/java/com/goormdari/domain/history/domain/dto/request/CreateHistoryRequest.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,17 @@ | ||
package com.goormdari.domain.history.domain.dto.request; | ||
|
||
import lombok.Builder; | ||
|
||
import java.util.List; | ||
|
||
@Builder | ||
public record CreateHistoryRequest( | ||
String goal, | ||
String routine1, | ||
String routine2, | ||
String routine3, | ||
String routine4, | ||
List<Long> routineIds | ||
// Long teamId | ||
) { | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/goormdari/domain/history/domain/dto/response/HistoryResponse.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,15 @@ | ||
package com.goormdari.domain.history.domain.dto.response; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class HistoryResponse { | ||
private int dDay; | ||
private String goal; | ||
private String[] routineList; | ||
private String result; | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/com/goormdari/domain/history/domain/repository/HistoryRepository.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,20 @@ | ||
package com.goormdari.domain.history.domain.repository; | ||
|
||
import com.goormdari.domain.history.domain.History; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.data.repository.query.Param; | ||
|
||
import java.time.LocalDate; | ||
import java.util.List; | ||
|
||
public interface HistoryRepository extends JpaRepository<History, Long> { | ||
|
||
// ํน์ ์ ์ ์ ๊ธฐ๊ฐ(์์์ผ~๋ฐ๋๋ผ์ธ)์ ๋ํ ๊ธฐ๋ก์ด ์๋์ง ํ์ธ | ||
@Query("SELECT COUNT(h) > 0 FROM History h WHERE h.user.id = :userId AND DATE(h.createdAt) BETWEEN :startDate AND :endDate") | ||
boolean existsByUserIdAndDateRange(@Param("userId") Long userId, | ||
@Param("startDate") LocalDate startDate, | ||
@Param("endDate") LocalDate endDate); | ||
|
||
List<History> findAllByUserId(Long userId); | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/goormdari/domain/history/exception/ResourceNotFoundException.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,8 @@ | ||
package com.goormdari.domain.history.exception; | ||
|
||
public class ResourceNotFoundException extends RuntimeException { | ||
|
||
public ResourceNotFoundException(String message) { | ||
super(message); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/main/java/com/goormdari/domain/history/presentation/HistoryController.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,40 @@ | ||
package com.goormdari.domain.history.presentation; | ||
|
||
import com.goormdari.domain.calendar.exception.InvalidTokenException; | ||
import com.goormdari.domain.history.application.HistoryService; | ||
import com.goormdari.domain.history.domain.dto.response.HistoryResponse; | ||
import com.goormdari.global.config.security.jwt.JWTUtil; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.Parameter; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.List; | ||
|
||
@RestController | ||
@RequestMapping("/histories") | ||
@RequiredArgsConstructor | ||
public class HistoryController { | ||
|
||
private final HistoryService historyService; | ||
private final JWTUtil jwtUtil; | ||
|
||
@Operation(summary = "๋ฃจํด ์์ ๊ธฐ๋ก ํ๋ฉด ์กฐํ", description = "๋ฃจํด ์์ ๊ธฐ๋ก์ list ํํ๋ก ๋ฐํฅํฉ๋๋ค.") | ||
@GetMapping | ||
public ResponseEntity<List<HistoryResponse>> getHistoriesByUser(@Parameter(description = "Accesstoken์ ์ ๋ ฅํด์ฃผ์ธ์.", required = true) @RequestHeader("Authorization") String token) { | ||
if (token == null) { | ||
throw new InvalidTokenException(); | ||
} | ||
|
||
String jwt = token.startsWith("Bearer ") ? token.substring(7) : token; | ||
if (!jwtUtil.validateToken(jwt)) { | ||
throw new IllegalArgumentException("Invalid token"); | ||
} | ||
Long userId = jwtUtil.extractId(jwt); | ||
|
||
List<HistoryResponse> histories = historyService.getAllHistoriesByUser(userId); | ||
return ResponseEntity.ok(histories); | ||
} | ||
|
||
} |
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
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
2 changes: 1 addition & 1 deletion
2
...omain/user/domain/dto/AddUserRequest.java โ ...er/domain/dto/request/AddUserRequest.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
2 changes: 1 addition & 1 deletion
2
.../domain/user/domain/dto/LoginRequest.java โ ...user/domain/dto/request/LoginRequest.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
2 changes: 1 addition & 1 deletion
2
...i/domain/user/domain/dto/JwtResponse.java โ ...user/domain/dto/response/JwtResponse.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
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
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
6 changes: 3 additions & 3 deletions
6
src/main/java/com/goormdari/domain/user/presentation/AuthController.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
10 changes: 10 additions & 0 deletions
10
src/main/java/com/goormdari/global/config/SchedulingConfig.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,10 @@ | ||
package com.goormdari.global.config; | ||
|
||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.scheduling.annotation.EnableScheduling; | ||
|
||
@Configuration | ||
@EnableScheduling | ||
public class SchedulingConfig { | ||
// ์ค์ผ์ค๋ง ํ์ฑํ | ||
} |
Oops, something went wrong.