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

단일 ToDo 수정 API 구현 #59

Merged
merged 2 commits into from
Jan 20, 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
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
import com.official.memento.global.annotation.Authorization;
import com.official.memento.global.annotation.AuthorizationUser;
import com.official.memento.global.dto.SuccessResponse;
import com.official.memento.global.entity.enums.RepeatOption;
import com.official.memento.schedule.service.command.ScheduleDeleteCommand;
import com.official.memento.todo.controller.dto.ToDoCreateRequest;
import com.official.memento.todo.controller.dto.ToDoUpdateRequest;
import com.official.memento.todo.domain.ToDo;
import com.official.memento.todo.service.ToDoCreateUseCase;
import com.official.memento.todo.service.ToDoDeleteUseCase;
import com.official.memento.todo.service.ToDoUpdateUseCase;
import com.official.memento.todo.service.command.ToDoCreateCommand;
import com.official.memento.todo.service.command.ToDoDeleteCommand;
import com.official.memento.todo.service.command.ToDoUpdateCommand;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
Expand All @@ -20,13 +24,16 @@ public class ToDoApiController {

private final ToDoCreateUseCase toDoCreateUseCase;
private final ToDoDeleteUseCase toDoDeleteUseCase;
private final ToDoUpdateUseCase toDoUpdateUseCase;

public ToDoApiController(
final ToDoCreateUseCase toDoCreateUseCase,
final ToDoDeleteUseCase toDoDeleteUseCase
final ToDoDeleteUseCase toDoDeleteUseCase,
final ToDoUpdateUseCase toDoUpdateUseCase
) {
this.toDoCreateUseCase = toDoCreateUseCase;
this.toDoDeleteUseCase = toDoDeleteUseCase;
this.toDoUpdateUseCase = toDoUpdateUseCase;
}

@PostMapping
Expand All @@ -42,8 +49,8 @@ public ResponseEntity<SuccessResponse<?>> createToDo(
request.date(),
request.description(),
request.deadline(),
request.repeatOption(),
request.repeatExpiredDate(),
RepeatOption.NONE,
null,
request.tagId(),
request.priorityUrgency(),
request.priorityImportance()
Expand All @@ -67,4 +74,27 @@ ResponseEntity<SuccessResponse<?>> deleteToDo(
"단일 스케줄 삭제 성공"
);
}

@PatchMapping("/{toDoId}")
ResponseEntity<SuccessResponse<?>> updateToDo(
//@Authorization final AuthorizationUser authorizationUser,
@PathVariable final long toDoId,
@RequestBody final ToDoUpdateRequest request
) {
toDoUpdateUseCase.update(ToDoUpdateCommand.of(
2,
toDoId,
request.date(),
request.description(),
request.deadline(),
request.tagId(),
request.priorityUrgency(),
request.priorityImportance()
));
return SuccessResponse.of(
HttpStatus.OK,
"단일 스케줄 업데이트 성공"
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ public record ToDoCreateRequest(
String description,
@Schema(description = "마감 기한", example = "2025-01-25")
LocalDate deadline,
@Schema(description = "반복 옵션")
RepeatOption repeatOption,
@Schema(description = "반복 종료 날짜")
LocalDate repeatExpiredDate,
@Schema(description = "태그 ID", example = "12345")
Long tagId,
@Schema(description = "긴급도 우선순위 (0~1)", example = "0.5")
Expand All @@ -31,13 +27,11 @@ public ToDoCreateRequest(
final LocalDate date,
final String description,
final LocalDate deadline,
final RepeatOption repeatOption,
final LocalDate repeatExpiredDate,
final Long tagId,
final Double priorityUrgency,
final Double priorityImportance
) {
checkNullData(date, description, repeatOption);
checkNullData(date, description);

if (description.length() > 30) {
throw new IllegalArgumentException("30자 이하로만 작성이 가능합니다.");
Expand All @@ -46,19 +40,16 @@ public ToDoCreateRequest(
this.date = date;
this.description = description;
this.deadline = deadline;
this.repeatOption = repeatOption;
this.repeatExpiredDate = repeatExpiredDate;
this.tagId = tagId;
this.priorityUrgency = priorityUrgency;
this.priorityImportance = priorityImportance;
}

private static void checkNullData(
final LocalDate date,
final String description,
final RepeatOption repeatOption
final String description
) {
if (date == null || description == null || repeatOption == null) {
if (date == null || description == null) {
throw new NullPointException(NULL_DATA_ERROR);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.official.memento.todo.controller.dto;

import com.official.memento.global.entity.enums.RepeatOption;
import com.official.memento.global.exception.NullPointException;
import io.swagger.v3.oas.annotations.media.Schema;

import java.time.LocalDate;

import static com.official.memento.global.exception.ErrorCode.NULL_DATA_ERROR;

@Schema(name = " ToDo 수정 요청")
public record ToDoUpdateRequest(
@Schema(description = "수정 ToDo 날짜 (Today or 사용자 지정)", example = "2025-01-20")
LocalDate date,
@Schema(description = "수정 ToDo 내용", maxLength = 30, example = "팀 프로젝트 준비")
String description,
@Schema(description = "수정 마감 기한", example = "2025-01-25")
LocalDate deadline,
@Schema(description = "수정 태그 ID", example = "12345")
Long tagId,
@Schema(description = "수정 긴급도 우선순위 (0~1)", example = "0.5")
Double priorityUrgency,
@Schema(description = "수정 중요도 우선순위 (0~1)", example = "0.5")
Double priorityImportance
) {
public ToDoUpdateRequest of(
final LocalDate date,
final String description,
final LocalDate deadline,
final Long tagId,
final Double priorityUrgency,
final Double priorityImportance
) {
checkNullData(date, description);

if (description.length() > 30) {
throw new IllegalArgumentException("30자 이하로만 작성이 가능합니다.");
}

return new ToDoUpdateRequest(date, description, deadline, tagId, priorityUrgency, priorityImportance);
}

private static void checkNullData(
final LocalDate date,
final String description
) {
if (date == null || description == null) {
throw new NullPointException(NULL_DATA_ERROR);
}
}
}
35 changes: 30 additions & 5 deletions src/main/java/com/official/memento/todo/domain/ToDo.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.official.memento.todo.domain;

import com.official.memento.global.entity.BaseTimeEntity;
import com.official.memento.global.entity.enums.RepeatOption;
import com.official.memento.todo.domain.enums.PriorityType;
import com.official.memento.todo.domain.enums.ToDoType;

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

public class ToDo {
public class ToDo extends BaseTimeEntity {
private Long id;
private long memberId;
private String groupId;
Expand Down Expand Up @@ -36,8 +38,10 @@ private ToDo(
final Double priorityImportance,
final Double priorityValue,
final String priorityType,
final ToDoType type
) {
final ToDoType type,
final LocalDateTime createdAt,
final LocalDateTime updatedAt
) {
this.id = id;
this.memberId = memberId;
this.groupId = groupId;
Expand All @@ -52,6 +56,8 @@ private ToDo(
this.priorityValue = priorityValue;
this.priorityType = priorityType;
this.type = type;
this.createdAt = createdAt;
this.updatedAt = updatedAt;
}

private ToDo(
Expand Down Expand Up @@ -98,7 +104,9 @@ public static ToDo withId(
final Double priorityImportance,
final Double priorityValue,
final String priorityType,
final ToDoType type
final ToDoType type,
final LocalDateTime createdAt,
final LocalDateTime updatedAt
) {
return new ToDo(
id,
Expand All @@ -114,7 +122,9 @@ public static ToDo withId(
priorityImportance,
priorityValue,
priorityType,
type
type,
createdAt,
updatedAt
);
}

Expand Down Expand Up @@ -150,6 +160,21 @@ public static ToDo of(
);
}

public void update(
final LocalDate date,
final String description,
final LocalDate deadline,
final Double priorityUrgency,
final Double priorityImportance
){
this.date=date;
this.description=description;
this.deadline=deadline;
this.priorityUrgency = priorityUrgency;
this.priorityImportance=priorityImportance;

}

public Long getId() {
return id;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
public interface ToDoRepository {
ToDo save(final ToDo toDo);

ToDo update(final ToDo toDo);

ToDo findById(final long toDoId);

void deleteById(final long toDoId);
Expand Down
30 changes: 26 additions & 4 deletions src/main/java/com/official/memento/todo/domain/ToDoTag.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
package com.official.memento.todo.domain;

public class ToDoTag {
import com.official.memento.global.entity.BaseTimeEntity;

import java.time.LocalDateTime;

public class ToDoTag extends BaseTimeEntity {
private Long id;
private long tagId;
private long toDoId;

public ToDoTag(final Long id, final long tagId, final long toDoId) {
public ToDoTag(
final Long id,
final long tagId,
final long toDoId,
final LocalDateTime createdAt,
final LocalDateTime updatedAt
) {
this.id = id;
this.tagId = tagId;
this.toDoId = toDoId;
this.createdAt = createdAt;
this.updatedAt = updatedAt;
}

public ToDoTag(final long tagId, final long toDoId) {
Expand All @@ -19,9 +31,11 @@ public ToDoTag(final long tagId, final long toDoId) {
public static ToDoTag withId(
final Long id,
final long tagId,
final long toDoId
final long toDoId,
final LocalDateTime createdAt,
final LocalDateTime updatedAt
) {
return new ToDoTag(id, tagId, toDoId);
return new ToDoTag(id, tagId, toDoId, createdAt, updatedAt);
}

public static ToDoTag of(
Expand All @@ -31,6 +45,14 @@ public static ToDoTag of(
return new ToDoTag(tagId, toDoId);
}

public void updateTag(
final long tagId,
final LocalDateTime updatedAt
) {
this.tagId = tagId;
this.updatedAt = updatedAt;
}

public Long getId() {
return id;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@

public interface ToDoTagRepository {
ToDoTag save(final ToDoTag toDoTag);

ToDoTag update(final ToDoTag toDoTag);

ToDoTag findByToDoId(final long toDoId);

void deleteByToDoId(final long toDoId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,40 @@ public ToDo save(final ToDo toDo) {
toDoEntity.getPriorityImportance(),
toDoEntity.getPriorityValue(),
toDoEntity.getPriorityType(),
toDoEntity.getType()
toDoEntity.getType(),
toDoEntity.getCreatedAt(),
toDoEntity.getUpdatedAt()
);
}

@Override
public ToDo update(final ToDo toDo){
ToDoEntity toDoEntity = toDoJpaRepository.save(ToDoEntity.withId(toDo));
toDoEntity.update(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

서비스에서 업데이트 되어 어댑터로 전달되었으니 추가적인 업데이트는 없어도 될 듯 합니다

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

최종적으로 엔티티도 업데이트 된 값들이 저장 되어야 된다고 생각해서 엔티티 수정하는 로직을 추가했습니다!

toDo.getDate(),
toDo.getDescription(),
toDo.getDeadline(),
toDo.getPriorityUrgency(),
toDo.getPriorityImportance()
);

return ToDo.withId(
toDoEntity.getId(),
toDoEntity.getMemberId(),
toDoEntity.getGroupId(),
toDoEntity.getDate(),
toDoEntity.getDescription(),
toDoEntity.getDeadline(),
toDoEntity.isCompleted(),
toDoEntity.getRepeatOption(),
toDoEntity.getRepeatExpiredDate(),
toDoEntity.getPriorityUrgency(),
toDoEntity.getPriorityImportance(),
toDoEntity.getPriorityValue(),
toDoEntity.getPriorityType(),
toDoEntity.getType(),
toDoEntity.getCreatedAt(),
toDoEntity.getUpdatedAt()
);
}

Expand All @@ -58,7 +91,9 @@ public ToDo findById(long toDoId) {
toDoEntity.getPriorityImportance(),
toDoEntity.getPriorityValue(),
toDoEntity.getPriorityType(),
toDoEntity.getType()
toDoEntity.getType(),
toDoEntity.getCreatedAt(),
toDoEntity.getUpdatedAt()
);
}

Expand Down
Loading