Skip to content

Commit

Permalink
Merge pull request #57 from MOONSHOT-Team/feature/#48
Browse files Browse the repository at this point in the history
[Feat] #48 - OKR 전체 보기(대시보드) API
  • Loading branch information
its-sky authored Jan 10, 2024
2 parents df4dbae + a2d8842 commit bc9ae06
Show file tree
Hide file tree
Showing 16 changed files with 153 additions and 14 deletions.
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ dependencies {

// Redis
implementation 'org.springframework.boot:spring-boot-starter-data-redis'

// Mybatis
implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:3.0.0'
}

dependencyManagement {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.moonshot.server.domain.keyresult.dto.response;

import java.util.List;
import org.moonshot.server.domain.keyresult.model.KeyResult;
import org.moonshot.server.domain.task.dto.response.TaskResponseDto;

public record KeyResultResponseDto(
Long keyResultId,
String keyResultTitle,
List<TaskResponseDto> taskList
) {
public static List<KeyResultResponseDto> of(List<KeyResult> krList) {
return krList.stream().map(kr -> new KeyResultResponseDto(
kr.getId(),
kr.getTitle(),
TaskResponseDto.of(kr.getTaskList())
)).toList();
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package org.moonshot.server.domain.keyresult.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import java.util.ArrayList;
import java.util.List;
import lombok.*;
import org.hibernate.annotations.BatchSize;
import org.hibernate.annotations.DynamicInsert;
import org.moonshot.server.domain.objective.model.Objective;
import org.moonshot.server.domain.task.model.Task;
import org.moonshot.server.global.common.model.Period;

@Entity
Expand Down Expand Up @@ -34,12 +39,13 @@ public class KeyResult {
@Column(nullable = false)
private String metric;

@Column(nullable = false)
private String descriptionBefore;

@Column(nullable = false)
private String descriptionAfter;

@Builder.Default
private short progress = 0;

@Enumerated(EnumType.STRING)
@Column(nullable = false, columnDefinition = "enum('DONE','HOLD','PROGRESS','WAITING') default 'PROGRESS'")
private KRState state;
Expand All @@ -48,6 +54,11 @@ public class KeyResult {
@JoinColumn(name = "objective_id")
private Objective objective;

@JsonIgnore
@BatchSize(size = 10)
@OneToMany(mappedBy = "keyResult", fetch = FetchType.LAZY)
List<Task> taskList = new ArrayList<>();

public void incrementIdx() {
++this.idx;
}
Expand Down Expand Up @@ -76,4 +87,8 @@ public void modifyState(KRState state) {
this.state = state;
}

public void modifyProgress(short progress) {
this.progress = progress;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.moonshot.server.domain.objective.model.Objective;
import org.moonshot.server.domain.objective.repository.ObjectiveRepository;
import org.moonshot.server.domain.task.dto.request.TaskCreateRequestDto;
import org.moonshot.server.domain.task.model.Task;
import org.moonshot.server.domain.task.repository.TaskRepository;
import org.moonshot.server.domain.task.service.TaskService;
import org.moonshot.server.domain.user.service.UserService;
Expand Down Expand Up @@ -65,11 +64,6 @@ public void createInitKRWithObjective(Objective objective, List<KeyResultCreateR
.build());
logService.createKRLog(dto, keyResult.getId());
if (dto.taskList() != null) {
taskRepository.saveAll(dto.taskList().stream().map((task) -> Task.builder()
.title(task.title())
.idx(task.idx())
.keyResult(keyResult)
.build()).toList());
for (TaskCreateRequestDto taskDto : dto.taskList()) {
taskService.saveTask(keyResult, taskDto);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.moonshot.server.domain.log.dto.response;

import org.moonshot.server.domain.log.model.LogState;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@
import org.moonshot.server.domain.log.model.Log;
import org.moonshot.server.domain.log.model.LogState;
import org.moonshot.server.domain.log.repository.LogRepository;
import org.moonshot.server.domain.user.exception.UserNotFoundException;
import org.moonshot.server.domain.user.model.User;
import org.moonshot.server.domain.user.repository.UserRepository;
import org.moonshot.server.global.auth.exception.AccessDeniedException;
import org.moonshot.server.global.auth.exception.InvalidAuthException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,18 @@
import lombok.extern.slf4j.Slf4j;
import org.moonshot.server.domain.objective.dto.request.ModifyObjectiveRequestDto;
import org.moonshot.server.domain.objective.dto.request.OKRCreateRequestDto;
import org.moonshot.server.domain.objective.dto.response.DashboardResponseDto;
import org.moonshot.server.domain.objective.service.ObjectiveService;
import org.moonshot.server.global.auth.jwt.JwtTokenProvider;
import org.moonshot.server.global.common.response.ApiResponse;
import org.moonshot.server.global.common.response.SuccessType;
import org.springframework.web.bind.annotation.DeleteMapping;
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;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
Expand Down Expand Up @@ -39,4 +47,10 @@ public ApiResponse<?> modifyObjective(Principal principal, @RequestBody ModifyOb
return ApiResponse.success(SuccessType.PATCH_OBJECTIVE_SUCCESS);
}

@GetMapping
public ApiResponse<DashboardResponseDto> getObjectiveInDashboard(Principal principal) {
DashboardResponseDto response = objectiveService.getObjectiveInDashboard(JwtTokenProvider.getUserIdFromPrincipal(principal));
return ApiResponse.success(SuccessType.GET_OKR_LIST_SUCCESS, response);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.moonshot.server.domain.objective.dto.response;

import java.util.List;
import org.moonshot.server.domain.keyresult.dto.response.KeyResultResponseDto;
import org.moonshot.server.domain.objective.model.Objective;

public record DashboardResponseDto(
Long objId,
String objTitle,
int objListSize,
List<KeyResultResponseDto> krList,
List<ObjectiveResponseDto> objList
) {
public static DashboardResponseDto of(Objective objective, List<Objective> objList) {
return new DashboardResponseDto(
objective.getId(),
objective.getTitle(),
objList.size(),
KeyResultResponseDto.of(objective.getKeyResultList()),
objList.stream().map(ObjectiveResponseDto::of).toList()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.moonshot.server.domain.objective.dto.response;

import org.moonshot.server.domain.objective.model.Category;
import org.moonshot.server.domain.objective.model.Objective;

public record ObjectiveResponseDto(
Long id,
String title,
String content,
Category category,
String date
) {
public static ObjectiveResponseDto of(Objective objective) {
return new ObjectiveResponseDto(
objective.getId(),
objective.getTitle(),
objective.getContent(),
objective.getCategory(),
objective.getDateString()
);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package org.moonshot.server.domain.objective.model;

import jakarta.persistence.*;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import lombok.*;
import org.hibernate.annotations.BatchSize;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;
import org.moonshot.server.domain.keyresult.model.KeyResult;
import org.moonshot.server.domain.user.model.User;
import org.moonshot.server.global.common.model.Period;

Expand Down Expand Up @@ -45,13 +50,22 @@ public class Objective {

@Builder.Default
@Column(columnDefinition = "smallint default -1")
private short order = -1;
private short idx = -1;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
@OnDelete(action = OnDeleteAction.CASCADE)
private User user;

@BatchSize(size = 10)
@OneToMany(mappedBy = "objective", fetch = FetchType.LAZY)
List<KeyResult> keyResultList = new ArrayList<>();

public String getDateString() {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yy-MM-dd");
return this.period.getStartAt().format(formatter) + " - " + this.period.getExpireAt().format(formatter);
}

public void modifyClosed(boolean isClosed) { this.isClosed = isClosed; }
public void modifyPeriod(Period period) { this.period = period; }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.moonshot.server.domain.objective.repository;

import java.util.List;
import java.util.Optional;
import org.moonshot.server.domain.objective.model.Objective;
import org.moonshot.server.domain.user.model.User;
Expand All @@ -12,5 +13,9 @@ public interface ObjectiveRepository extends JpaRepository<Objective, Long> {
Long countAllByUserAndIsClosed(User user, boolean isClosed);
@Query("select o from Objective o join fetch o.user where o.id = :objective_id")
Optional<Objective> findObjectiveAndUserById(@Param("objective_id") Long objectiveId);
@Query("select distinct o from Objective o left join fetch o.keyResultList kr left join kr.taskList t where o.id = :objectiveId and o.isClosed = false and kr.id = t.keyResult.id")
Optional<Objective> findByIdWithKeyResultsAndTasks(@Param("objectiveId") Long objectiveId);
@Query("select distinct o from Objective o where o.user.id = :userId and o.isClosed = false order by o.id desc")
List<Objective> findAllByUserId(@Param("userId") Long userId);

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package org.moonshot.server.domain.objective.service;

import java.util.List;
import lombok.RequiredArgsConstructor;
import org.moonshot.server.domain.keyresult.service.KeyResultService;
import org.moonshot.server.domain.objective.dto.request.ModifyObjectiveRequestDto;
import org.moonshot.server.domain.objective.dto.request.OKRCreateRequestDto;
import org.moonshot.server.domain.objective.dto.response.DashboardResponseDto;
import org.moonshot.server.domain.objective.exception.InvalidExpiredAtException;
import org.moonshot.server.domain.objective.exception.ObjectiveNotFoundException;
import org.moonshot.server.domain.objective.exception.ObjectiveNumberExceededException;
Expand Down Expand Up @@ -76,4 +78,14 @@ public void modifyObjective(Long userId, ModifyObjectiveRequestDto request) {
}
}

public DashboardResponseDto getObjectiveInDashboard(Long userId) {
List<Objective> objList = objectiveRepository.findAllByUserId(userId);
Objective objective = objectiveRepository.findByIdWithKeyResultsAndTasks(objList.get(0).getId())
.orElseThrow(ObjectiveNotFoundException::new);
if (!objective.getUser().getId().equals(userId)) {
throw new AccessDeniedException();
}
return DashboardResponseDto.of(objective, objList);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.moonshot.server.domain.task.dto.response;

import java.util.List;
import org.moonshot.server.domain.task.model.Task;

public record TaskResponseDto(
Long id,
String title
) {
public static List<TaskResponseDto> of(List<Task> taskList) {
return taskList.stream().map(task -> new TaskResponseDto(
task.getId(),
task.getTitle()
)).toList();
}
}
2 changes: 2 additions & 0 deletions src/main/java/org/moonshot/server/domain/task/model/Task.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.moonshot.server.domain.task.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import lombok.*;
import org.moonshot.server.domain.keyresult.model.KeyResult;
Expand All @@ -22,6 +23,7 @@ public class Task {
@Column(nullable = false)
private Integer idx;

@JsonIgnore
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "key_result_id")
private KeyResult keyResult;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public enum SuccessType {
*/
OK(HttpStatus.OK, "성공"),
GET_PRESIGNED_URL_SUCCESS(HttpStatus.OK, "Presigned Url 조회에 성공하였습니다."),
GET_OKR_LIST_SUCCESS(HttpStatus.OK, "O-KR 목록 조회에 성공하였습니다."),
POST_LOGIN_SUCCESS(HttpStatus.OK, "로그인에 성공하였습니다."),
POST_REISSUE_SUCCESS(HttpStatus.OK, "엑세스 토큰 재발급에 성공하였습니다."),
POST_LOGOUT_SUCCESS(HttpStatus.OK, "로그아웃에 성공하였습니다."),
Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ spring:
format_sql: true
show_sql: true
auto_quote_keyword: true
default_batch_fetch_size: 10

config:
import: optional:application-secret.properties
Expand Down Expand Up @@ -66,3 +67,6 @@ springdoc:
cache:
disabled: true
show-login-endpoint: true

mybatis:
mapper-locations: mappers/*.xml

0 comments on commit bc9ae06

Please sign in to comment.