diff --git a/src/main/java/com/mju/management/domain/schedule/controller/ScheduleController.java b/src/main/java/com/mju/management/domain/schedule/controller/ScheduleController.java index 2348965..4ddc346 100644 --- a/src/main/java/com/mju/management/domain/schedule/controller/ScheduleController.java +++ b/src/main/java/com/mju/management/domain/schedule/controller/ScheduleController.java @@ -67,4 +67,13 @@ public CommonResult deleteSchedule(@PathVariable Long scheduleId) { scheduleService.deleteSchedule(scheduleId); return responseService.getSuccessfulResult(); } + + //일정 완료 +// @Operation(summary = "일정 완료하기", description = "일정을 완료하는 API") +// @ResponseStatus(OK) +// @DeleteMapping("/schedules/finish/{scheduleId}") +// public CommonResult finishSchedule(@PathVariable Long scheduleId) { +// scheduleService.finishSchedule(scheduleId); +// return responseService.getSuccessfulResult(); +// } } diff --git a/src/main/java/com/mju/management/domain/schedule/controller/UserScheduleController.java b/src/main/java/com/mju/management/domain/schedule/controller/UserScheduleController.java new file mode 100644 index 0000000..3ed87c3 --- /dev/null +++ b/src/main/java/com/mju/management/domain/schedule/controller/UserScheduleController.java @@ -0,0 +1,78 @@ +package com.mju.management.domain.schedule.controller; + +import com.mju.management.domain.schedule.dto.reqeust.CreateScheduleRequestDto; +import com.mju.management.domain.schedule.dto.response.GetScheduleResponseDto; +import com.mju.management.domain.schedule.dto.response.GetUserScheduleRes; +import com.mju.management.domain.schedule.infrastructure.UserSchedule; +import com.mju.management.domain.schedule.service.UserScheduleService; +import com.mju.management.global.config.jwtInterceptor.JwtContextHolder; +import com.mju.management.global.model.Result.CommonResult; +import com.mju.management.global.service.ResponseService; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.validation.Valid; +import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +import static org.springframework.http.HttpStatus.OK; + +@Tag(name = "내 일정 CRUD API", description = "내 일정 CRUD API") +@RestController +@RequiredArgsConstructor +@RequestMapping("/api/userSchedules") +@CrossOrigin("*") +public class UserScheduleController { + + private final UserScheduleService userScheduleService; + private final ResponseService responseService; + + //일정 등록 + @Operation(summary = "내 일정 등록", description = "내 일정 등록 API") + @ResponseStatus(OK) + @PostMapping + public CommonResult createMySchedule(@Valid @RequestBody CreateScheduleRequestDto createScheduleRequestDto){ + userScheduleService.createMySchedule(JwtContextHolder.getUserId(), createScheduleRequestDto); + return responseService.getSuccessfulResult(); + } + + // 내 일정 하나 조회 + @Operation(summary = "내 일정 하나 조회", description = "내 일정 하나 조회 API") + @ResponseStatus(OK) + @GetMapping("/{userScheduleId}") + public CommonResult getMyScheduleOne(@PathVariable Long userScheduleId) { + GetUserScheduleRes schedule = userScheduleService.getMyScheduleOne(userScheduleId); + return responseService.getSingleResult(schedule); + } + + // 내 일정 목록 조회 + @Operation(summary = "내 일정 목록 조회", description = "내 일정 목록 조회 API") + @ResponseStatus(OK) + @GetMapping + public CommonResult getMySchedule() { + List userScheduleList = userScheduleService.getMySchedule(JwtContextHolder.getUserId()); + return responseService.getListResult(userScheduleList); + } + + // 내 일정 수정 + @Operation(summary = "내 일정 수정", description = "내 일정 수정 API") + @ResponseStatus(OK) + @PutMapping("/{userScheduleId}") + public CommonResult updateMySchedule( + @PathVariable Long userScheduleId, + @Valid @RequestBody CreateScheduleRequestDto updateScheduleRequestDto + ) { + userScheduleService.updateMySchedule(JwtContextHolder.getUserId(), userScheduleId, updateScheduleRequestDto); + return responseService.getSuccessfulResult(); + } + + // 내 일정 삭제 + @DeleteMapping("/{userScheduleId}") + @Operation(summary = "내 일정 삭제", description = "내 일정 삭제 api") + public CommonResult deleteMyToDo(@PathVariable Long userScheduleId) { + userScheduleService.deleteMyToDo(JwtContextHolder.getUserId(), userScheduleId); + return responseService.getSuccessfulResult(); + } + +} diff --git a/src/main/java/com/mju/management/domain/schedule/dto/response/GetUserScheduleRes.java b/src/main/java/com/mju/management/domain/schedule/dto/response/GetUserScheduleRes.java new file mode 100644 index 0000000..4898e99 --- /dev/null +++ b/src/main/java/com/mju/management/domain/schedule/dto/response/GetUserScheduleRes.java @@ -0,0 +1,33 @@ +package com.mju.management.domain.schedule.dto.response; + +import com.mju.management.domain.schedule.infrastructure.Schedule; +import com.mju.management.domain.schedule.infrastructure.UserSchedule; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.time.LocalDate; + +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class GetUserScheduleRes { + + private Long scheduleId; + private Long userId; + private String content; + private LocalDate startDate; + private LocalDate endDate; + + public static GetUserScheduleRes from(UserSchedule userSchedule){ + return GetUserScheduleRes.builder() + .scheduleId(userSchedule.getUserScheduleId()) + .userId(userSchedule.getUserId()) + .content(userSchedule.getContent()) + .startDate(userSchedule.getStartDate()) + .endDate(userSchedule.getEndDate()) + .build(); + } +} diff --git a/src/main/java/com/mju/management/domain/schedule/infrastructure/Schedule.java b/src/main/java/com/mju/management/domain/schedule/infrastructure/Schedule.java index 4c541e6..e061260 100644 --- a/src/main/java/com/mju/management/domain/schedule/infrastructure/Schedule.java +++ b/src/main/java/com/mju/management/domain/schedule/infrastructure/Schedule.java @@ -36,6 +36,9 @@ public class Schedule { @Column(name = "end_date", nullable = false) private LocalDate endDate; + @Column(name = "is_checked") + private boolean isChecked; + public void update(CreateScheduleRequestDto updateScheduleRequestDto) { this.content = updateScheduleRequestDto.getContent(); this.startDate = updateScheduleRequestDto.readStartDateAsLocalDateType(); diff --git a/src/main/java/com/mju/management/domain/schedule/infrastructure/UserSchedule.java b/src/main/java/com/mju/management/domain/schedule/infrastructure/UserSchedule.java new file mode 100644 index 0000000..700534c --- /dev/null +++ b/src/main/java/com/mju/management/domain/schedule/infrastructure/UserSchedule.java @@ -0,0 +1,52 @@ +package com.mju.management.domain.schedule.infrastructure; + +import com.mju.management.domain.project.infrastructure.Project; +import com.mju.management.domain.schedule.dto.reqeust.CreateScheduleRequestDto; +import jakarta.persistence.*; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import java.time.LocalDate; + +@Getter +@NoArgsConstructor +@Entity +public class UserSchedule { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "user_schedule_id") + private Long userScheduleId; + + @Column(name = "user_id") + private Long userId; + + @Column(name = "content", nullable = false) + private String content; + + @Column(name = "start_date", nullable = false) + private LocalDate startDate; + + @Column(name = "end_date", nullable = false) + private LocalDate endDate; + + @Column(name = "is_checked") + private boolean isChecked; + + public void update(CreateScheduleRequestDto updateScheduleRequestDto) { + this.content = updateScheduleRequestDto.getContent(); + this.startDate = updateScheduleRequestDto.readStartDateAsLocalDateType(); + this.endDate = updateScheduleRequestDto.readEndDateAsLocalDateType(); + } + + @Builder + public UserSchedule(Long userScheduleId, Long userId, String content, LocalDate startDate, LocalDate endDate, boolean isChecked) { + this.userScheduleId = userScheduleId; + this.userId = userId; + this.content = content; + this.startDate = startDate; + this.endDate = endDate; + this.isChecked = isChecked; + } +} diff --git a/src/main/java/com/mju/management/domain/schedule/infrastructure/UserScheduleRepository.java b/src/main/java/com/mju/management/domain/schedule/infrastructure/UserScheduleRepository.java new file mode 100644 index 0000000..d569edf --- /dev/null +++ b/src/main/java/com/mju/management/domain/schedule/infrastructure/UserScheduleRepository.java @@ -0,0 +1,15 @@ +package com.mju.management.domain.schedule.infrastructure; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import java.util.List; +import java.util.Optional; + +@Repository +public interface UserScheduleRepository extends JpaRepository { + + List findAllByUserId(Long userId); + + Optional findByUserScheduleIdAndUserId(Long userScheduleId, Long userId); +} diff --git a/src/main/java/com/mju/management/domain/schedule/service/UserScheduleService.java b/src/main/java/com/mju/management/domain/schedule/service/UserScheduleService.java new file mode 100644 index 0000000..01406eb --- /dev/null +++ b/src/main/java/com/mju/management/domain/schedule/service/UserScheduleService.java @@ -0,0 +1,19 @@ +package com.mju.management.domain.schedule.service; + +import com.mju.management.domain.schedule.dto.reqeust.CreateScheduleRequestDto; +import com.mju.management.domain.schedule.dto.response.GetUserScheduleRes; +import com.mju.management.domain.schedule.infrastructure.UserSchedule; + +import java.util.List; + +public interface UserScheduleService { + void createMySchedule(Long userId, CreateScheduleRequestDto createScheduleRequestDto); + + List getMySchedule(Long userId); + + void updateMySchedule(Long userId, Long userScheduleId, CreateScheduleRequestDto updateScheduleRequestDto); + + void deleteMyToDo(Long userId, Long userScheduleId); + + GetUserScheduleRes getMyScheduleOne(Long userScheduleId); +} diff --git a/src/main/java/com/mju/management/domain/schedule/service/UserScheduleServiceImpl.java b/src/main/java/com/mju/management/domain/schedule/service/UserScheduleServiceImpl.java new file mode 100644 index 0000000..7d0ff54 --- /dev/null +++ b/src/main/java/com/mju/management/domain/schedule/service/UserScheduleServiceImpl.java @@ -0,0 +1,80 @@ +package com.mju.management.domain.schedule.service; + +import com.mju.management.domain.schedule.dto.reqeust.CreateScheduleRequestDto; +import com.mju.management.domain.schedule.dto.response.GetUserScheduleRes; +import com.mju.management.domain.schedule.infrastructure.UserSchedule; +import com.mju.management.domain.schedule.infrastructure.UserScheduleRepository; +import com.mju.management.global.model.Exception.ExceptionList; +import com.mju.management.global.model.Exception.NonExistentException; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; +import java.util.Optional; + +@Service +@RequiredArgsConstructor +@Transactional(readOnly = true) +public class UserScheduleServiceImpl implements UserScheduleService{ + + private final UserScheduleRepository userScheduleRepository; + @Override + @Transactional + public void createMySchedule(Long userId, CreateScheduleRequestDto createScheduleRequestDto) { + + UserSchedule userSchedule = UserSchedule.builder() + .userId(userId) + .content(createScheduleRequestDto.getContent()) + .startDate(createScheduleRequestDto.readStartDateAsLocalDateType()) + .endDate(createScheduleRequestDto.readEndDateAsLocalDateType()) + .isChecked(false) + .build(); + + userScheduleRepository.save(userSchedule); + } + + @Override + public List getMySchedule(Long userId) { + + List allByUserId = userScheduleRepository.findAllByUserId(userId); + if (!allByUserId.isEmpty()) + return allByUserId; + else + throw new NonExistentException(ExceptionList.NON_EXISTENT_CHECKLIST); + } + + @Override + @Transactional + public void updateMySchedule(Long userId, Long userScheduleId, CreateScheduleRequestDto updateScheduleRequestDto) { + Optional findUserSchedule = userScheduleRepository.findByUserScheduleIdAndUserId(userScheduleId, userId); + if (findUserSchedule.isPresent()) { + UserSchedule userSchedule = findUserSchedule.get(); + userSchedule.update(updateScheduleRequestDto); + } else + throw new NonExistentException(ExceptionList.NON_EXISTENT_PROJECT); + + } + + @Override + @Transactional + public void deleteMyToDo(Long userId, Long userScheduleId) { + + Optional findUserSchedule = userScheduleRepository.findByUserScheduleIdAndUserId(userScheduleId, userId); + if (findUserSchedule.isPresent()) { + UserSchedule userSchedule = findUserSchedule.get(); + userScheduleRepository.delete(userSchedule); + } else + throw new NonExistentException(ExceptionList.NON_EXISTENT_PROJECT); + + } + + @Override + public GetUserScheduleRes getMyScheduleOne(Long userScheduleId) { + + UserSchedule userSchedule = userScheduleRepository.findById(userScheduleId) + .orElseThrow(()-> new NonExistentException(ExceptionList.NON_EXISTENT_SCHEDULE)); + return GetUserScheduleRes.from(userSchedule); + } + +} diff --git a/src/main/java/com/mju/management/domain/todo/controller/ToDoController.java b/src/main/java/com/mju/management/domain/todo/controller/ToDoController.java index 1c98565..08ce1f1 100644 --- a/src/main/java/com/mju/management/domain/todo/controller/ToDoController.java +++ b/src/main/java/com/mju/management/domain/todo/controller/ToDoController.java @@ -3,6 +3,7 @@ import com.mju.management.domain.todo.dto.ToDoRequestDto; import com.mju.management.domain.todo.infrastructure.ToDoEntity; import com.mju.management.domain.todo.service.ToDoService; +import com.mju.management.global.config.jwtInterceptor.JwtContextHolder; import com.mju.management.global.model.Result.CommonResult; import com.mju.management.global.service.ResponseService; import io.swagger.v3.oas.annotations.Operation; @@ -43,6 +44,16 @@ public CommonResult showToDo(@PathVariable Long projectId) { CommonResult commonResult = responseService.getListResult(toDoEntity); return commonResult; } + +// // 내 할 일 조회 +// @GetMapping("/my/todo") +// @Operation(summary = "내 할 일 조회", description = "내 할 일 조회 api") +// public CommonResult showMyToDo() { +// List toDoEntity = toDoService.getMyToDo(JwtContextHolder.getUserId()); +// CommonResult commonResult = responseService.getListResult(toDoEntity); +// return commonResult; +// } + //체크박스 하나만 선택 @GetMapping("/todo/{todoIndex}") @Operation(summary = "할일 선택 조회", description = "할일 선택 조회 api") diff --git a/src/main/java/com/mju/management/domain/todo/controller/UserTodoController.java b/src/main/java/com/mju/management/domain/todo/controller/UserTodoController.java new file mode 100644 index 0000000..05b0f38 --- /dev/null +++ b/src/main/java/com/mju/management/domain/todo/controller/UserTodoController.java @@ -0,0 +1,73 @@ +package com.mju.management.domain.todo.controller; + +import com.mju.management.domain.todo.dto.ToDoRequestDto; +import com.mju.management.domain.todo.infrastructure.ToDoEntity; +import com.mju.management.domain.todo.infrastructure.UserTodo; +import com.mju.management.domain.todo.service.ToDoService; +import com.mju.management.domain.todo.service.UserTodoService; +import com.mju.management.global.config.jwtInterceptor.JwtContextHolder; +import com.mju.management.global.model.Result.CommonResult; +import com.mju.management.global.service.ResponseService; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.tags.Tag; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@Tag(name = "유저의 체크리스트 CRUD API", description = "유저의 체크리스트(할일) 관련 API") +@RestController +@RequestMapping("/api/userTodo") +@CrossOrigin("*") +public class UserTodoController { + + @Autowired + UserTodoService userTodoService; + @Autowired + ResponseService responseService; + + // 내 할 일 조회 + @GetMapping + @Operation(summary = "내 할 일 목록 조회", description = "내 할 일 목록 조회 api") + public CommonResult showMyToDo() { + List userTodoList = userTodoService.getMyTodo(JwtContextHolder.getUserId()); + CommonResult commonResult = responseService.getListResult(userTodoList); + return commonResult; + } + + @GetMapping("/{userTodoId}") + @Operation(summary = "내 할 일 하나 조회", description = "내 할 일 하나 조회 api") + public CommonResult showMyToDoOne(@PathVariable Long userTodoId) { + UserTodo userTodo = userTodoService.showMyToDoOne(userTodoId); + CommonResult commonResult = responseService.getSingleResult(userTodo); + return commonResult; + } + + // 내 할 일 생성 + @PostMapping + @Operation(summary = "할일 등록", description = "할일 등록 api") + public CommonResult registerMyToDo(@RequestBody ToDoRequestDto toDoRequestDto){ + userTodoService.registerMyToDo(JwtContextHolder.getUserId(), toDoRequestDto); + return responseService.getSuccessfulResult(); + } + + // 내 할 일 수정 + @PutMapping("/{userTodoId}") + @Operation(summary = "내 할 일 수정", description = "내 할 일 수정 api") + public CommonResult updateMyToDo( + @PathVariable Long userTodoId, + @RequestBody ToDoRequestDto toDoRequestDto + ) { + userTodoService.updateMyToDo(JwtContextHolder.getUserId(), userTodoId, toDoRequestDto); + return responseService.getSuccessfulResult(); + } + + // 내 할 일 삭제 + @DeleteMapping("/{userTodoId}") + @Operation(summary = "내 할 일 삭제", description = "내 할 일 삭제 api") + public CommonResult deleteMyToDo(@PathVariable Long userTodoId) { + userTodoService.deleteMyToDo(JwtContextHolder.getUserId(), userTodoId); + return responseService.getSuccessfulResult(); + } + +} diff --git a/src/main/java/com/mju/management/domain/todo/infrastructure/ToDoEntity.java b/src/main/java/com/mju/management/domain/todo/infrastructure/ToDoEntity.java index 7fc26be..04e183f 100644 --- a/src/main/java/com/mju/management/domain/todo/infrastructure/ToDoEntity.java +++ b/src/main/java/com/mju/management/domain/todo/infrastructure/ToDoEntity.java @@ -2,6 +2,7 @@ import com.fasterxml.jackson.annotation.JsonIgnore; import com.mju.management.domain.project.infrastructure.Project; +import com.mju.management.domain.project.infrastructure.ProjectUser; import jakarta.persistence.*; import lombok.Builder; import lombok.Getter; @@ -36,6 +37,7 @@ public ToDoEntity(/*String userId, */String todoContent, boolean todoEmergency,P @Column(name = "is_checked") private boolean isChecked; + public void setProject(Project project){ this.project = project; } diff --git a/src/main/java/com/mju/management/domain/todo/infrastructure/ToDoJpaRepository.java b/src/main/java/com/mju/management/domain/todo/infrastructure/ToDoJpaRepository.java index 4d5bfae..67062c0 100644 --- a/src/main/java/com/mju/management/domain/todo/infrastructure/ToDoJpaRepository.java +++ b/src/main/java/com/mju/management/domain/todo/infrastructure/ToDoJpaRepository.java @@ -3,6 +3,7 @@ import com.mju.management.domain.project.infrastructure.Project; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; import java.util.List; @@ -11,4 +12,5 @@ public interface ToDoJpaRepository extends JpaRepository { @Query("SELECT t FROM ToDoEntity t WHERE t.project = :project ORDER BY CASE WHEN t.todoEmergency = true AND t.isChecked = false THEN 1 WHEN t.todoEmergency = false AND t.isChecked = false THEN 2 WHEN t.todoEmergency = true AND t.isChecked = true THEN 3 ELSE 4 END") List findByProjectAndOrderByConditions(Project project); + } diff --git a/src/main/java/com/mju/management/domain/todo/infrastructure/UserTodo.java b/src/main/java/com/mju/management/domain/todo/infrastructure/UserTodo.java new file mode 100644 index 0000000..08d2590 --- /dev/null +++ b/src/main/java/com/mju/management/domain/todo/infrastructure/UserTodo.java @@ -0,0 +1,54 @@ +package com.mju.management.domain.todo.infrastructure; + +import com.mju.management.domain.todo.dto.ToDoRequestDto; +import jakarta.persistence.*; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@NoArgsConstructor +@Entity +public class UserTodo { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "user_todo_id") + private Long userTodoId; + + @Column(name = "todo_content") + private String todoContent; + + @Column(name = "is_checked") + private boolean isChecked; + + @Column(name = "user_id") + private Long userId; + + + @Column(name = "todo_emergency") + private boolean todoEmergency; + + + public void update(ToDoRequestDto toDoRequestDto) { + this.todoContent = toDoRequestDto.getTodoContent(); + this.isChecked = false; + this.todoEmergency = toDoRequestDto.isTodoEmergency(); + } + + public void finish(boolean isChecked) { + if(!isChecked) + this.isChecked = true; + else + this.isChecked = false; + } + + @Builder + public UserTodo(Long userTodoId, String todoContent, boolean isChecked, Long userId, boolean todoEmergency) { + this.userTodoId = userTodoId; + this.todoContent = todoContent; + this.isChecked = isChecked; + this.userId = userId; + this.todoEmergency = todoEmergency; + } +} diff --git a/src/main/java/com/mju/management/domain/todo/infrastructure/UserTodoRepository.java b/src/main/java/com/mju/management/domain/todo/infrastructure/UserTodoRepository.java new file mode 100644 index 0000000..7f004e1 --- /dev/null +++ b/src/main/java/com/mju/management/domain/todo/infrastructure/UserTodoRepository.java @@ -0,0 +1,15 @@ +package com.mju.management.domain.todo.infrastructure; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import java.util.List; +import java.util.Optional; + +@Repository +public interface UserTodoRepository extends JpaRepository { + + List findAllByUserId(Long userId); + + Optional findByUserTodoIdAndUserId(Long userTodoId, Long userId); +} diff --git a/src/main/java/com/mju/management/domain/todo/service/ToDoService.java b/src/main/java/com/mju/management/domain/todo/service/ToDoService.java index 5505bd8..29ce093 100644 --- a/src/main/java/com/mju/management/domain/todo/service/ToDoService.java +++ b/src/main/java/com/mju/management/domain/todo/service/ToDoService.java @@ -18,4 +18,6 @@ public interface ToDoService { public void updateToDo(Long todoIndex, ToDoRequestDto toDoRequestDto); public void finishToDo(Long todoIndex); + +// List getMyToDo(Long userId); } diff --git a/src/main/java/com/mju/management/domain/todo/service/ToDoServiceServiceImpl.java b/src/main/java/com/mju/management/domain/todo/service/ToDoServiceServiceImpl.java index b1798f1..253e2df 100644 --- a/src/main/java/com/mju/management/domain/todo/service/ToDoServiceServiceImpl.java +++ b/src/main/java/com/mju/management/domain/todo/service/ToDoServiceServiceImpl.java @@ -62,6 +62,18 @@ public List getToDo(Long projectId) { } } + // 내 할 일 조회 +// @Override +// @Transactional +// public List getMyToDo(Long userId) { +// +// List allByUserId = toDoJpaRepository.findAllByUserId(userId); +// if (!allByUserId.isEmpty()) +// return allByUserId; +// else +// throw new NonExistentException(ExceptionList.NON_EXISTENT_CHECKLIST); +// } + @Override @Transactional public void deleteToDo(Long todoIndex) { @@ -119,4 +131,6 @@ public void finishToDo(Long todoIndex) { throw new NonExistentException(ExceptionList.NON_EXISTENT_CHECKLIST); } } + + } diff --git a/src/main/java/com/mju/management/domain/todo/service/UserTodoService.java b/src/main/java/com/mju/management/domain/todo/service/UserTodoService.java new file mode 100644 index 0000000..3b8cc8e --- /dev/null +++ b/src/main/java/com/mju/management/domain/todo/service/UserTodoService.java @@ -0,0 +1,19 @@ +package com.mju.management.domain.todo.service; + +import com.mju.management.domain.todo.dto.ToDoRequestDto; +import com.mju.management.domain.todo.infrastructure.UserTodo; + +import java.util.List; + +public interface UserTodoService { + + List getMyTodo(Long userId); + + void registerMyToDo(Long userId, ToDoRequestDto toDoRequestDto); + + void updateMyToDo(Long userId, Long userTodoId, ToDoRequestDto toDoRequestDto); + + void deleteMyToDo(Long userId, Long userTodoId); + + UserTodo showMyToDoOne(Long userTodoId); +} diff --git a/src/main/java/com/mju/management/domain/todo/service/UserTodoServiceImpl.java b/src/main/java/com/mju/management/domain/todo/service/UserTodoServiceImpl.java new file mode 100644 index 0000000..56fac87 --- /dev/null +++ b/src/main/java/com/mju/management/domain/todo/service/UserTodoServiceImpl.java @@ -0,0 +1,87 @@ +package com.mju.management.domain.todo.service; + +import com.mju.management.domain.todo.dto.ToDoRequestDto; +import com.mju.management.domain.todo.infrastructure.UserTodo; +import com.mju.management.domain.todo.infrastructure.UserTodoRepository; +import com.mju.management.global.model.Exception.ExceptionList; +import com.mju.management.global.model.Exception.NonExistentException; +import com.mju.management.global.model.Exception.UnauthorizedAccessException; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; +import java.util.Optional; + +@Service +@RequiredArgsConstructor +@Transactional(readOnly = true) +public class UserTodoServiceImpl implements UserTodoService{ + + private final UserTodoRepository userTodoRepository; + + // Description : 내 할 일 조회 + @Override + public List getMyTodo(Long userId) { + List allByUserId = userTodoRepository.findAllByUserId(userId); + if (!allByUserId.isEmpty()) + return allByUserId; + else + throw new NonExistentException(ExceptionList.NON_EXISTENT_CHECKLIST); + } + + // Description : 내 할 일 하나 조회 + @Override + public UserTodo showMyToDoOne(Long userTodoId) { + + UserTodo userTodo = userTodoRepository.findById(userTodoId) + .orElseThrow(() -> new UnauthorizedAccessException(ExceptionList.UNAUTHORIZED_ACCESS)); + return userTodo; + + } + + // Description : 내 할 일 생성 + @Override + @Transactional + public void registerMyToDo(Long userId, ToDoRequestDto toDoRequestDto) { + + UserTodo userTodo = UserTodo.builder() + .todoContent(toDoRequestDto.getTodoContent()) + .isChecked(false) + .userId(userId) + .todoEmergency(toDoRequestDto.isTodoEmergency()) + .build(); + + userTodoRepository.save(userTodo); + } + + // Description : 내 할 일 수정 + @Override + @Transactional + public void updateMyToDo(Long userId, Long userTodoId, ToDoRequestDto toDoRequestDto) { + + Optional findUserTodo = userTodoRepository.findByUserTodoIdAndUserId(userTodoId, userId); + if (findUserTodo.isPresent()) { + UserTodo userTodo = findUserTodo.get(); + userTodo.update(toDoRequestDto); + + } else + throw new NonExistentException(ExceptionList.NON_EXISTENT_PROJECT); + + } + + // Description : 내 할 일 삭제 + @Override + @Transactional + public void deleteMyToDo(Long userId, Long userTodoId) { + + Optional findUserTodo = userTodoRepository.findByUserTodoIdAndUserId(userTodoId, userId); + if (findUserTodo.isPresent()) { + UserTodo userTodo = findUserTodo.get(); + userTodoRepository.delete(userTodo); + + } else + throw new NonExistentException(ExceptionList.NON_EXISTENT_PROJECT); + } + +}