Skip to content

Commit

Permalink
refactor: 가능시간 갱신 (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
oxdjww authored Sep 24, 2024
1 parent 469709a commit 0382666
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
Expand Down Expand Up @@ -40,20 +39,16 @@ private String getUserNameByAuthentication(Authentication authentication) throws
return principal.getUsername();
}

@PutMapping()
@Operation(summary = "멘토가 직접 커피챗 가능시간 추가하기")
@PostMapping()
@Operation(summary = "멘토가 직접 커피챗 가능시간 갱신하기")
@ApiResponse(responseCode = "201", description = "DTO형식으로 정보 반환")
public ResponseEntity<ApiResponseGenerator<List<PossibleDateCreateGetResponseDto>>> addPossibleDate(
Authentication authentication,
@RequestBody List<PossibleDateCreateRequestDto> dtos) throws Exception {
String username = getUserNameByAuthentication(authentication);

List<PossibleDateCreateGetResponseDto> responseDtos = dtos.stream()
.map(dto -> possibleDateService.createPossibleDate(dto, username))
.collect(Collectors.toList());

return ResponseEntity.created(URI.create(POSSIBLEDATE_URI)).body(
ApiResponseGenerator.onSuccessCREATED(responseDtos)
ApiResponseGenerator.onSuccessCREATED(
possibleDateService.updatePossibleDate(dtos, getUserNameByAuthentication(authentication))
)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,17 @@
import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import com.soongsil.CoffeeChat.entity.Mentor;
import com.soongsil.CoffeeChat.entity.PossibleDate;

public interface PossibleDateRepository extends JpaRepository<PossibleDate, Long>, PossibleDateRepositoryCustom {
List<PossibleDate> getPossibleDatesByMentorId(Long mentorId);

@Modifying
@Query("DELETE FROM PossibleDate pd WHERE pd.mentor = :mentor")
void deleteAllByMentor(@Param("mentor") Mentor mentor);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,54 @@
import com.soongsil.CoffeeChat.repository.User.UserRepository;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import static com.soongsil.CoffeeChat.controller.exception.enums.UserErrorCode.USER_NOT_FOUND;

@Service
@RequiredArgsConstructor
@Slf4j
@Transactional(readOnly = true)
public class PossibleDateService {
private final PossibleDateRepository possibleDateRepository;
private final UserRepository userRepository;

private User findUserByUsername(String username){
return userRepository.findByUsername(username)
.orElseThrow(() -> new CustomException(
USER_NOT_FOUND.getHttpStatusCode(),
USER_NOT_FOUND.getErrorMessage())
);
.orElseThrow(() -> new CustomException(
USER_NOT_FOUND.getHttpStatusCode(),
USER_NOT_FOUND.getErrorMessage())
);
}

@Transactional
public PossibleDateCreateGetResponseDto createPossibleDate(PossibleDateCreateRequestDto dto,
public List<PossibleDateCreateGetResponseDto> updatePossibleDate(List<PossibleDateCreateRequestDto> dtos,
String username) {

User user = findUserByUsername(username);
Mentor mentor = user.getMentor();
PossibleDate possibleDate = PossibleDate.from(dto);
possibleDate.setMentor(mentor);
mentor.addPossibleDate(possibleDate);
possibleDateRepository.save(possibleDate);
return PossibleDateCreateGetResponseDto.from(possibleDate);

// 가능 시간을 갱신하기 위해 모든 가능 시간을 삭제 후 새로운 값 삽입
possibleDateRepository.deleteAllByMentor(mentor);
log.info("[*] 멘토(" + username + ")의 가능시간 모두 삭제(가능시간 갱신 API 일부)");

List<PossibleDate> possibleDates = dtos.stream()
.map(dto -> {
PossibleDate possibleDate = PossibleDate.from(dto);
possibleDate.setMentor(mentor);
mentor.addPossibleDate(possibleDate);
return possibleDate;
})
.collect(Collectors.toList());

possibleDateRepository.saveAll(possibleDates);

return possibleDates.stream()
.map(PossibleDateCreateGetResponseDto::from)
.collect(Collectors.toList());
}


public List<PossibleDateCreateGetResponseDto> findPossibleDateListByMentor(String username) {
User user = findUserByUsername(username);
Long mentorId = user.getMentor().getId();
Expand Down

0 comments on commit 0382666

Please sign in to comment.