Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task/fix todo #105

Merged
merged 3 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ dependencies {
implementation(libs.jjwt.api)
runtimeOnly(libs.jjwt.impl)
runtimeOnly(libs.jjwt.jackson)

// lombok
annotationProcessor(libs.lombok)
compileOnly(libs.lombok)
}

kotlin {
Expand Down
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ ktlint = "12.1.0"
coroutines = "1.7.3"
springdoc = "2.7.0"
jjwt = "0.11.5"
lombok = "1.18.22"

[libraries]
spring-boot-starter-web = { group = "org.springframework.boot", name = "spring-boot-starter-web" }
Expand All @@ -18,6 +19,7 @@ spring-boot-starter-data-jpa = { group = "org.springframework.boot", name = "spr
springdoc-openapi-starter-webmvc-ui = { group = "org.springdoc", name = "springdoc-openapi-starter-webmvc-ui", version.ref = "springdoc" }
springdoc-openapi-starter-webmvc-api = { group = "org.springdoc", name = "springdoc-openapi-starter-webmvc-api", version.ref = "springdoc" }
postgresql = { group = "org.postgresql", name = "postgresql" }
lombok = { group = "org.projectlombok", name = "lombok" , version.ref = "lombok" }

# JWT
jjwt-api = { module = "io.jsonwebtoken:jjwt-api", version.ref = "jjwt" }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.official.memento.global.handler;

import com.official.memento.alarm.domain.port.AlarmOutputPort;
import com.official.memento.alarm.service.command.AlarmExceptionCommand;
import com.official.memento.alarm.service.command.AlarmSendUseCase;
import com.official.memento.global.dto.ErrorResponse;
import com.official.memento.global.exception.*;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
Expand All @@ -10,43 +14,52 @@
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@RequiredArgsConstructor
public class GlobalExceptionHandler {

private final AlarmSendUseCase alarmSendUseCase;

private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

@ExceptionHandler(Exception.class)
public ResponseEntity<ErrorResponse> exception(Exception exception) {
logger.error("Unhandled exception occurred ", exception);
alarmSendUseCase.sendException(new AlarmExceptionCommand(exception));
return ErrorResponse.of(HttpStatus.INTERNAL_SERVER_ERROR, ErrorCode.INTERNAL_SERVER_ERROR);
}

@ExceptionHandler(MementoException.class)
public ResponseEntity<ErrorResponse> mementoException(MementoException exception) {
logger.error("MementoException", exception);
alarmSendUseCase.sendException(new AlarmExceptionCommand(exception));
return ErrorResponse.of(HttpStatus.INTERNAL_SERVER_ERROR, ErrorCode.INTERNAL_SERVER_ERROR);
}

@ExceptionHandler(InvalidRequestBodyException.class)
public ResponseEntity<ErrorResponse> invalidRequestBodyException(InvalidRequestBodyException exception) {
logger.warn("Invalid request body: {}", exception.getMessage());
alarmSendUseCase.sendException(new AlarmExceptionCommand(exception));
return ErrorResponse.of(HttpStatus.BAD_REQUEST, ErrorCode.INVALID_REQUEST_BODY);
}

@ExceptionHandler(UnauthorizedException.class)
public ResponseEntity<ErrorResponse> unauthorizedException(UnauthorizedException exception) {
logger.error("Unauthorized access attempt", exception);
alarmSendUseCase.sendException(new AlarmExceptionCommand(exception));
return ErrorResponse.of(HttpStatus.UNAUTHORIZED, ErrorCode.UNAUTHORIZED_USER);
}

@ExceptionHandler(EntityNotFoundException.class)
public ResponseEntity<ErrorResponse> entityNotFoundException(EntityNotFoundException exception) {
logger.error("Entity not found:", exception);
alarmSendUseCase.sendException(new AlarmExceptionCommand(exception));
return ErrorResponse.of(HttpStatus.NOT_FOUND, ErrorCode.NOT_FOUND_ENTITY);
}

@ExceptionHandler(NullPointException.class)
public ResponseEntity<ErrorResponse> nullPointException(NullPointException exception) {
logger.error("Invalid request:", exception);
alarmSendUseCase.sendException(new AlarmExceptionCommand(exception));
return ErrorResponse.of(HttpStatus.NOT_FOUND, ErrorCode.NOT_FOUND_ENTITY);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
import com.official.memento.orderinfo.domain.PlanType;
import com.official.memento.orderinfo.domain.OrderInfo;
import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.time.LocalDate;
import java.time.LocalDateTime;

@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Table(name = "order_info")
public class OrderInfoEntity {

Expand Down Expand Up @@ -57,10 +60,6 @@ private OrderInfoEntity(
this.createdAt = createdAt;
}

protected OrderInfoEntity() {

}

public static OrderInfoEntity of(final OrderInfo orderInfo) {
return new OrderInfoEntity(
orderInfo.getScheduleId(),
Expand Down
93 changes: 13 additions & 80 deletions src/main/java/com/official/memento/todo/domain/ToDo.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@
import com.official.memento.tag.domain.enums.TagColor;
import com.official.memento.todo.domain.enums.PriorityType;
import com.official.memento.todo.domain.enums.ToDoType;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDate;
import java.time.LocalDateTime;

@NoArgsConstructor
@Getter
public class ToDo extends BaseTimeEntity {
private Long id;
private long memberId;
Expand Down Expand Up @@ -364,95 +368,23 @@ public void updateCompletion(
this.isCompleted = isCompleted;
}

public Long getId() {
return id;
public void updateTagId(Long tagId) {
this.tagId = tagId;
}

public long getMemberId() {
return memberId;
}

public String getGroupId() {
return groupId;
}

public LocalDate getStartDate() {
return startDate;
}

public String getDescription() {
return description;
}

public LocalDate getEndDate() {
return endDate;
}

public boolean getIsCompleted() {
return isCompleted;
}

public RepeatOption getRepeatOption() {
return repeatOption;
}

public LocalDate getRepeatExpiredDate() {
return repeatExpiredDate;
}

public Double getPriorityUrgency() {
return priorityUrgency;
}

public Double getPriorityImportance() {
return priorityImportance;
}

public Double getPriorityValue() {
return priorityValue;
}

public PriorityType getPriorityType() {
return priorityType;
}

public ToDoType getType() {
return type;
}

public Integer getOrderNum() {
return orderNum;
}

public void setOrderNum(Integer orderNum) {
this.orderNum = orderNum;
}

public Long getTagId(){return tagId;}

public void setTagId(Long tagId){this.tagId=tagId;}

public String getTagName() {
return tagName;
}

public void setTagName(String tagName) {
this.tagName = tagName;
}

public TagColor getTagColor() {
return tagColor;
}

public void setTagColor(TagColor tagColor) {
public void updateTagColor(TagColor tagColor) {
this.tagColor = tagColor;
}

public void setTag(Tag tag) {
public void updateTag(Tag tag) {
this.tagName = tag.getName();
this.tagColor = tag.getColor();
}


public void updateOrderNum(Integer orderNum) {
this.orderNum = orderNum;

public void setId(Long id) {
this.id = id;
}
Expand Down Expand Up @@ -507,6 +439,7 @@ public void setPriorityType(PriorityType priorityType) {

public void setType(ToDoType type) {
this.type = type;

}

public String toTaskDescription() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.official.memento.todo.service;

public class ToDoQueryService {
}
71 changes: 56 additions & 15 deletions src/main/java/com/official/memento/todo/service/ToDoService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.official.memento.todo.service;

import com.official.memento.global.entity.enums.RepeatOption;
import com.official.memento.global.exception.ErrorCode;
import com.official.memento.global.exception.UnauthorizedException;
import com.official.memento.orderinfo.domain.OrderInfo;
import com.official.memento.orderinfo.domain.OrderInfoRepository;
import com.official.memento.orderinfo.domain.OrderWithScheduleOrToDo;
Expand All @@ -13,6 +15,7 @@
import com.official.memento.todo.domain.ToDoTagRepository;
import com.official.memento.todo.domain.enums.PriorityType;
import com.official.memento.todo.service.command.*;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -27,25 +30,14 @@
import static com.official.memento.todo.domain.enums.ToDoType.NORMAL;

@Service
@RequiredArgsConstructor
public class ToDoService implements ToDoCreateUseCase, ToDoDeleteUseCase, ToDoUpdateUseCase, ToDoGetUseCase {

private final ToDoRepository toDoRepository;
private final ToDoTagRepository toDoTagRepository;
private final TagRepository tagRepository;
private final OrderInfoRepository orderInfoRepository;

public ToDoService(
final ToDoRepository toDoRepository,
final ToDoTagRepository toDoTagRepository,
final TagRepository tagRepository,
final OrderInfoRepository orderInfoRepository
) {
this.toDoRepository = toDoRepository;
this.toDoTagRepository = toDoTagRepository;
this.tagRepository = tagRepository;
this.orderInfoRepository = orderInfoRepository;
}

@Override
@Transactional
public void create(final ToDoCreateCommand command) {
Expand Down Expand Up @@ -101,12 +93,12 @@ public void update(final ToDoUpdateCommand command) {
public boolean updateCompletion(final ToDoCompletionUpdateCommand command) {
ToDo toDo = toDoRepository.findById(command.toDoId());
checkOwn(command.memberId(), toDo);
if (toDo.getIsCompleted()) {
if (toDo.isCompleted()) {
toDo.updateCompletion(false);
} else {
toDo.updateCompletion(true);
}
return toDoRepository.update(toDo).getIsCompleted();
return toDoRepository.update(toDo).isCompleted();
}

@Override
Expand Down Expand Up @@ -138,6 +130,55 @@ public void updatePosition(final ToDoPositionUpdateCommand command) {
orderInfoRepository.update(orderInfo);
}


@Transactional(readOnly = true)
public List<ToDo> getToDos(final long memberId) {
List<ToDo> todos = toDoRepository.findAllByMemberId(memberId);
return todos.stream()
.peek(todo -> {
Integer order = orderInfoRepository.findOrderByToDoId(todo.getId());
ToDoTag toDoTag = toDoTagRepository.findByToDoId(todo.getId());

if (toDoTag != null) {
Tag tag = tagRepository.findById(toDoTag.getTagId());
todo.updateTag(tag);
}

todo.updateOrderNum(order);
})
.toList();
}

@Override
public List<ToDo> getTodosByDate(long memberId, LocalDate date) {
List<ToDo> toDos = toDoRepository.findAllByMemberIdAndStartDate(memberId, date);
toDos.forEach(todo -> {
int orderNum = orderInfoRepository.findByToDoId(todo.getId()).getOrderNum();
todo.update(
todo.getStartDate(),
todo.getDescription(),
todo.getEndDate(),
todo.getPriorityUrgency(),
todo.getPriorityImportance(),
orderNum
);
});
return toDos;
}

@Override
public ToDo getDetail(long memberId, long toDoId){
ToDo toDo = toDoRepository.findById(toDoId);
checkOwn(memberId, toDo);
ToDoTag toDoTag = toDoTagRepository.findByToDoId(toDoId);
Tag tag = tagRepository.findById(toDoTag.getTagId());
if (toDoTag!=null) {
toDo.updateTagId(toDoTag.getTagId());
toDo.updateTag(tag);
}
return toDo;
}

private void createSingleToDo(final ToDoCreateCommand command, final String toDoGroupId) {
Double priorityValue = calculatePriorityValue(command.priorityUrgency(), command.priorityImportance());
PriorityType priorityType = determinePriorityType(command.priorityUrgency(), command.priorityImportance());
Expand Down Expand Up @@ -224,7 +265,7 @@ private void connectTag(final Long tagId, final ToDo toDo) {

private static void checkOwn(final long memberId, final ToDo toDo) {
if (toDo.getMemberId() != memberId) {
throw new IllegalArgumentException("해당 투두를 소유하지 않음");//커스텀으로 바꿔야함
throw new UnauthorizedException(ErrorCode.UNAUTHORIZED_USER);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@ interface AlarmOutputPort {
uri: String,
content: String,
)

fun sendExceptionAlarm(
e: Exception
)
}
Loading
Loading