Skip to content

Commit

Permalink
feat: 월 별 통계 기능 추가 #79
Browse files Browse the repository at this point in the history
  • Loading branch information
wlsh44 committed Dec 15, 2022
1 parent 2585379 commit afa5c10
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/main/java/mustodo/backend/todo/domain/TodoRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import mustodo.backend.sns.application.dto.FeedTodoQueryDto;
import mustodo.backend.user.domain.User;
import mustodo.backend.user.ui.dto.StatsMonthDto;
import mustodo.backend.user.ui.dto.StatsResponse;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
Expand All @@ -27,4 +29,8 @@ public interface TodoRepository extends JpaRepository<Todo, Long> {

@Query("select todo from Todo todo where todo.user=:user and :monthStart <= todo.date and todo.date <= :monthEnd")
List<Todo> findTodoByMonth(User user, LocalDate monthStart, LocalDate monthEnd);

@Query("select todo.date as date, sum(case when todo.achieve = true then 1 else 0 end) as achieveCount, count(todo) as totalTodo from Todo todo " +
"where todo.user=:user and :monthStart <= todo.date and todo.date <= :monthEnd group by todo.date")
List<StatsMonthDto> findTodoStatsByMonth(User user, LocalDate monthStart, LocalDate monthEnd);
}
18 changes: 18 additions & 0 deletions src/main/java/mustodo/backend/user/application/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,24 @@
import lombok.RequiredArgsConstructor;
import mustodo.backend.config.ImageConfig;
import mustodo.backend.exception.user.UserNotFoundException;
import mustodo.backend.todo.domain.Todo;
import mustodo.backend.todo.domain.TodoRepository;
import mustodo.backend.todo.ui.dto.TodoMonthResponse;
import mustodo.backend.user.domain.User;
import mustodo.backend.user.domain.UserRepository;
import mustodo.backend.user.ui.dto.MeResponse;
import mustodo.backend.user.ui.dto.StatsMonthDto;
import mustodo.backend.user.ui.dto.StatsResponse;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.persistence.EntityManager;
import java.time.LocalDate;
import java.time.YearMonth;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
@RequiredArgsConstructor
Expand All @@ -19,6 +29,7 @@ public class UserService {
private final ImageConfig imageConfig;
private final EntityManager entityManager;
private final UserRepository userRepository;
private final TodoRepository todoRepository;

@Transactional
public void updateProfileImage(User user, MultipartFile multipartFile) {
Expand All @@ -31,4 +42,11 @@ public MeResponse getMe(User user) {
return userRepository.findFollowCountByUser(user)
.orElseThrow(() -> new UserNotFoundException(user.getEmail()));
}

@Transactional(readOnly = true)
public StatsResponse findTodoStats(User user, String date) {
YearMonth month = YearMonth.parse(date);
List<StatsMonthDto> todoByMonth = todoRepository.findTodoStatsByMonth(user, month.atDay(1), month.atEndOfMonth());
return new StatsResponse(todoByMonth);
}
}
7 changes: 7 additions & 0 deletions src/main/java/mustodo/backend/user/ui/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import mustodo.backend.user.application.UserService;
import mustodo.backend.user.domain.User;
import mustodo.backend.user.ui.dto.MeResponse;
import mustodo.backend.user.ui.dto.StatsResponse;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -29,4 +31,9 @@ public void updateProfileImage(@Login User user, @RequestBody MultipartFile imag
public ResponseEntity<MeResponse> getMe(@Login User user) {
return ResponseEntity.ok(userService.getMe(user));
}

@GetMapping("/stats/{date}")
public ResponseEntity<StatsResponse> findStats(@Login User user, @PathVariable String date) {
return ResponseEntity.ok(userService.findTodoStats(user, date));
}
}
9 changes: 9 additions & 0 deletions src/main/java/mustodo/backend/user/ui/dto/StatsMonthDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package mustodo.backend.user.ui.dto;

import java.time.LocalDate;

public interface StatsMonthDto {
LocalDate getDate();
int getAchieveCount();
int getTotalTodo();
}
12 changes: 12 additions & 0 deletions src/main/java/mustodo/backend/user/ui/dto/StatsResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package mustodo.backend.user.ui.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.List;

@Getter
@AllArgsConstructor
public class StatsResponse {
private List<StatsMonthDto> month;
}

0 comments on commit afa5c10

Please sign in to comment.