Skip to content

Commit

Permalink
Merge pull request #19 from Domitory-CheckMate/feature/12-checkList
Browse files Browse the repository at this point in the history
[feat] 체크리스트 관련 기능
  • Loading branch information
RyuKwanKon authored Jan 4, 2024
2 parents a037274 + f109470 commit 0aedb8f
Show file tree
Hide file tree
Showing 9 changed files with 204 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.gachon.checkmate.domain.checkList.controller;

import lombok.RequiredArgsConstructor;
import org.gachon.checkmate.domain.checkList.dto.request.CheckListRequestDto;
import org.gachon.checkmate.domain.checkList.dto.response.CheckListResponseDto;
import org.gachon.checkmate.domain.checkList.service.CheckListService;
import org.gachon.checkmate.global.common.SuccessResponse;
import org.gachon.checkmate.global.config.auth.UserId;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RequiredArgsConstructor
@RequestMapping("/api/checkList")
@RestController
public class CheckListController {
private final CheckListService checkListService;

@PostMapping("/new")
public ResponseEntity<SuccessResponse<?>> createCheckList(@UserId final Long userId,
@RequestBody final CheckListRequestDto checkListRequestDto) {
checkListService.createCheckList(userId, checkListRequestDto);
return SuccessResponse.created(null);
}

@PatchMapping("/my")
public ResponseEntity<SuccessResponse<?>> updateCheckList(@UserId final Long userId,
@RequestBody final CheckListRequestDto checkListRequestDto) {
checkListService.updateCheckList(userId, checkListRequestDto);
return SuccessResponse.ok(null);
}

@GetMapping("/my")
public ResponseEntity<SuccessResponse<?>> getCheckList(@UserId final Long userId) {
final CheckListResponseDto checkListResponseDto = checkListService.getCheckList(userId);
return SuccessResponse.ok(checkListResponseDto);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.gachon.checkmate.domain.checkList.dto.request;

import org.gachon.checkmate.domain.checkList.entity.*;

public record CheckListRequestDto(
CleanType cleanType,
DrinkType drinkType,
HomeType homeType,
LifePatterType lifePatterType,
NoiseType noiseType,
SleepType sleepType

) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.gachon.checkmate.domain.checkList.dto.response;

import lombok.Builder;

@Builder
public record CheckListResponseDto(
String cleanType,
String drinkType,
String homeType,
String lifePatterType,
String noiseType,
String sleepType
) {
public static CheckListResponseDto of(String cleanType,
String drinkType,
String homeType,
String lifePatterType,
String noiseType,
String sleepType) {
return CheckListResponseDto.builder()
.cleanType(cleanType)
.drinkType(drinkType)
.homeType(homeType)
.lifePatterType(lifePatterType)
.noiseType(noiseType)
.sleepType(sleepType)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import jakarta.persistence.*;
import lombok.*;
import org.gachon.checkmate.domain.checkList.converter.*;
import org.gachon.checkmate.domain.checkList.dto.request.CheckListRequestDto;
import org.gachon.checkmate.domain.member.entity.User;
import org.gachon.checkmate.global.common.BaseTimeEntity;

Expand Down Expand Up @@ -31,4 +32,27 @@ public class CheckList extends BaseTimeEntity {
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private User user;

public static CheckList createCheckList(User user, CheckListRequestDto checkListRequestDto){
CheckList checkList = CheckList.builder()
.cleanType(checkListRequestDto.cleanType())
.drinkType(checkListRequestDto.drinkType())
.homeType(checkListRequestDto.homeType())
.lifePatterType(checkListRequestDto.lifePatterType())
.noiseType(checkListRequestDto.noiseType())
.sleepType(checkListRequestDto.sleepType())
.user(user)
.build();
user.setCheckList(checkList);
return checkList;
}

public void updateCheckList(CheckListRequestDto checkListRequestDto) {
this.cleanType = checkListRequestDto.cleanType();
this.drinkType = checkListRequestDto.drinkType();
this.homeType = checkListRequestDto.homeType();
this.lifePatterType = checkListRequestDto.lifePatterType();
this.noiseType = checkListRequestDto.noiseType();
this.sleepType = checkListRequestDto.sleepType();
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package org.gachon.checkmate.domain.checkList.repository;

import org.gachon.checkmate.domain.checkList.entity.CheckList;
import org.gachon.checkmate.domain.member.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface CheckListRepository extends JpaRepository<CheckList, Long> {
Optional<CheckList> findByUserId(Long userId);

boolean existsByUser(User user);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package org.gachon.checkmate.domain.checkList.service;

import lombok.RequiredArgsConstructor;
import org.gachon.checkmate.domain.checkList.dto.request.CheckListRequestDto;
import org.gachon.checkmate.domain.checkList.dto.response.CheckListResponseDto;
import org.gachon.checkmate.domain.checkList.entity.CheckList;
import org.gachon.checkmate.domain.checkList.repository.CheckListRepository;
import org.gachon.checkmate.domain.member.entity.User;
import org.gachon.checkmate.domain.member.repository.UserRepository;
import org.gachon.checkmate.global.error.exception.ConflictException;
import org.gachon.checkmate.global.error.exception.EntityNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static org.gachon.checkmate.global.error.ErrorCode.*;

@RequiredArgsConstructor
@Transactional
@Service
public class CheckListService {

private final CheckListRepository checkListRepository;
private final UserRepository userRepository;

public void createCheckList(Long userId, CheckListRequestDto checkListRequestDto) {
User user = findByIdOrThrow(userId);
checkIfCheckListExists(user);
CheckList checkList = CheckList.createCheckList(user, checkListRequestDto);
checkListRepository.save(checkList);
}

public void updateCheckList(Long userId, CheckListRequestDto checkListRequestDto) {
User user = findByIdOrThrow(userId);
checkIfCheckListNotExists(user);
CheckList checkList = user.getCheckList();
checkList.updateCheckList(checkListRequestDto);
checkListRepository.save(checkList);
}

@Transactional(readOnly = true)
public CheckListResponseDto getCheckList(Long userId) {
User user = findByIdOrThrow(userId);
CheckList checkList = user.getCheckList();
return CheckListResponseDto.of(checkList.getCleanType().getDesc(),
checkList.getDrinkType().getDesc(),
checkList.getHomeType().getDesc(),
checkList.getLifePatterType().getDesc(),
checkList.getNoiseType().getDesc(),
checkList.getSleepType().getDesc());
}

private User findByIdOrThrow(Long userId) {
return userRepository.findById(userId)
.orElseThrow(() -> new EntityNotFoundException(USER_NOT_FOUND));
}

public void checkIfCheckListExists(User user) {
if (checkListRepository.existsByUser(user)) {
throw new ConflictException(DUPLICATE_CHECK_LIST);
}
}

public void checkIfCheckListNotExists(User user) {
if (!checkListRepository.existsByUser(user)) {
throw new EntityNotFoundException(CHECK_LIST_NOT_FOUND);
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,8 @@ public static User createUser(String email, String storedPassword, String name,
public void setPassword(String newPassword) {
this.password = newPassword;
}

public void setCheckList(CheckList checkList) {
this.checkList = checkList;
}
}
21 changes: 21 additions & 0 deletions src/main/java/org/gachon/checkmate/global/config/WebConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.gachon.checkmate.global.config;

import lombok.RequiredArgsConstructor;
import org.gachon.checkmate.global.config.auth.UserIdArgumentResolver;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@RequiredArgsConstructor
@Configuration
public class WebConfig implements WebMvcConfigurer {

private final UserIdArgumentResolver userIdArgumentResolver;

@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers){
resolvers.add(userIdArgumentResolver);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public enum ErrorCode {
*/
CONFLICT(HttpStatus.CONFLICT, "이미 존재하는 리소스입니다."),
DUPLICATE_EMAIL(HttpStatus.CONFLICT, "이미 가입된 이메일입니다."),
DUPLICATE_CHECK_LIST(HttpStatus.CONFLICT, "체크리스트가 이미 존재합니다."),

/**
* 500 Internal Server Error
Expand Down

0 comments on commit 0aedb8f

Please sign in to comment.