Skip to content

Commit 5ee22d2

Browse files
authored
Feature/155 기관 to 봉사자 쪽지 송신 기능 구현 (#167)
* feat: 봉사자 Id를 통한 존재여부 검증 메서드 구현 - 영속성 레이어에 exists 메서드 구현 - 서비스 레이어에 exists 메서드 구현 - 테스트 및 검증 완 * feat: 기관 to 봉사자 쪽지 송신 기능 구현 - 요청 객체 클랙스 생성 - 유스케이스 구현 - 송신된 쪽지 생성 메서드 구현 - 테스트 코드 작성및 검증 완료 * feat: 기관 to 봉사자 쪽지 송신 기능 엔드 포인트 구현 - 컨트롤러에 메서드 구현 - 테스트 코드 작성및 검증 완료 * style(SendNoteToCenterService): 의존성 주입 객체 라인 수정 - 유스케이스 의존성이 가장 위에 위치하도록 수정
1 parent 3ec15d6 commit 5ee22d2

14 files changed

+252
-5
lines changed

src/main/java/com/somemore/note/controller/NoteCommandApiController.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import com.somemore.auth.annotation.CurrentUser;
44
import com.somemore.global.common.response.ApiResponse;
55
import com.somemore.note.dto.SendNoteToCenterRequestDto;
6+
import com.somemore.note.dto.SendNoteToVolunteerRequestDto;
67
import com.somemore.note.usecase.SendNoteToCenterUseCase;
8+
import com.somemore.note.usecase.SendNoteToVolunteerUseCase;
79
import io.swagger.v3.oas.annotations.Operation;
810
import io.swagger.v3.oas.annotations.tags.Tag;
911
import jakarta.validation.Valid;
@@ -23,13 +25,24 @@
2325
public class NoteCommandApiController {
2426

2527
private final SendNoteToCenterUseCase sendNoteToCenterUseCase;
28+
private final SendNoteToVolunteerUseCase sendNoteToVolunteerUseCase;
2629

2730
@Secured("ROLE_VOLUNTEER")
2831
@Operation(summary = "봉사자 to 기관 쪽지 송신")
2932
@PostMapping("/volunteer-to-center")
30-
public ApiResponse<Long> sendNoteToCenter(@CurrentUser UUID userId, @Valid @RequestBody SendNoteToCenterRequestDto requestDto) {
33+
public ApiResponse<Long> sendNoteToCenter(@CurrentUser UUID volunteerId, @Valid @RequestBody SendNoteToCenterRequestDto requestDto) {
3134

32-
Long noteId = sendNoteToCenterUseCase.sendNoteToCenter(userId, requestDto);
35+
Long noteId = sendNoteToCenterUseCase.sendNoteToCenter(volunteerId, requestDto);
36+
37+
return ApiResponse.ok(201, noteId, "쪽지 송신 성공");
38+
}
39+
40+
@Secured("ROLE_CENTER")
41+
@Operation(summary = "기관 to 봉사자 쪽지 송신")
42+
@PostMapping("/center-to-volunteer")
43+
public ApiResponse<Long> sendNoteToCenter(@CurrentUser UUID centerId, @Valid @RequestBody SendNoteToVolunteerRequestDto requestDto) {
44+
45+
Long noteId = sendNoteToVolunteerUseCase.sendNoteToVolunteer(centerId, requestDto);
3346

3447
return ApiResponse.ok(201, noteId, "쪽지 송신 성공");
3548
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.somemore.note.dto;
2+
3+
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
4+
import com.fasterxml.jackson.databind.annotation.JsonNaming;
5+
import com.somemore.note.domain.Note;
6+
import io.swagger.v3.oas.annotations.media.Schema;
7+
import jakarta.validation.constraints.NotNull;
8+
9+
import java.util.UUID;
10+
11+
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
12+
public record SendNoteToVolunteerRequestDto(
13+
14+
@Schema(description = "쪽지 수신 봉사자 아이디", example = "123e4567-e89b-12d3-a456-426614174000")
15+
@NotNull(message = "수신 봉사자 아이디는 필수 값입니다.")
16+
UUID receiverId,
17+
18+
@Schema(description = "쪽지 제목", example = "서울 도서관입니다. 답변 드립니다")
19+
@NotNull(message = "쪽지 제목은 필수 값입니다.")
20+
String title,
21+
22+
@Schema(description = "쪽지 내용", example = "~~~~한 일을 할 것 같습니다.")
23+
@NotNull(message = "쪽지 내용은 필수 값입니다.")
24+
String content
25+
) {
26+
public Note toEntity(UUID senderId){
27+
return Note.create(senderId, receiverId, title, content);
28+
}
29+
}

src/main/java/com/somemore/note/service/SendNoteToCenterService.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,11 @@
1616
@Transactional
1717
public class SendNoteToCenterService implements SendNoteToCenterUseCase {
1818

19-
private final NoteRepository noteRepository;
2019
private final CenterQueryUseCase centerQueryUseCase;
20+
private final NoteRepository noteRepository;
2121

2222
@Override
2323
public Long sendNoteToCenter(UUID senderId, SendNoteToCenterRequestDto requestDto) {
24-
2524
centerQueryUseCase.validateCenterExists(requestDto.receiverId());
2625

2726
Note note = requestDto.toEntity(senderId);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.somemore.note.service;
2+
3+
import com.somemore.note.domain.Note;
4+
import com.somemore.note.dto.SendNoteToVolunteerRequestDto;
5+
import com.somemore.note.repository.NoteRepository;
6+
import com.somemore.note.usecase.SendNoteToVolunteerUseCase;
7+
import com.somemore.volunteer.usecase.VolunteerQueryUseCase;
8+
import lombok.RequiredArgsConstructor;
9+
import org.springframework.stereotype.Service;
10+
import org.springframework.transaction.annotation.Transactional;
11+
12+
import java.util.UUID;
13+
14+
15+
@RequiredArgsConstructor
16+
@Service
17+
@Transactional
18+
public class SendNoteToVolunteerService implements SendNoteToVolunteerUseCase {
19+
20+
private final VolunteerQueryUseCase volunteerQueryUseCase;
21+
private final NoteRepository noteRepository;
22+
23+
@Override
24+
public Long sendNoteToVolunteer(UUID senderId, SendNoteToVolunteerRequestDto requestDto) {
25+
volunteerQueryUseCase.validateVolunteerExists(senderId);
26+
27+
Note note = requestDto.toEntity(senderId);
28+
noteRepository.save(note);
29+
return note.getId();
30+
}
31+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.somemore.note.usecase;
2+
3+
import com.somemore.note.dto.SendNoteToVolunteerRequestDto;
4+
5+
import java.util.UUID;
6+
7+
public interface SendNoteToVolunteerUseCase {
8+
Long sendNoteToVolunteer(UUID senderId, SendNoteToVolunteerRequestDto requestDto);
9+
}

src/main/java/com/somemore/volunteer/repository/VolunteerJpaRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
public interface VolunteerJpaRepository extends JpaRepository<Volunteer, Long> {
99

1010
List<Volunteer> findAllByIdInAndDeletedFalse(List<UUID> ids);
11-
11+
boolean existsByIdAndDeletedIsFalse(UUID id);
1212
}

src/main/java/com/somemore/volunteer/repository/VolunteerRepository.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,8 @@ public interface VolunteerRepository {
2626
List<Volunteer> findAllByIds(List<UUID> volunteerIds);
2727

2828
List<VolunteerSimpleInfo> findSimpleInfoByIds(List<UUID> ids);
29+
30+
boolean existsByVolunteerId(UUID volunteerId);
31+
32+
default boolean doesNotExistsByVolunteerId(UUID volunteerId) { return !existsByVolunteerId(volunteerId); }
2933
}

src/main/java/com/somemore/volunteer/repository/VolunteerRepositoryImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ private <T> Optional<T> findDynamicField(UUID id, Path<T> field) {
120120
);
121121
}
122122

123+
@Override
124+
public boolean existsByVolunteerId(UUID volunteerId) {
125+
return volunteerJpaRepository.existsByIdAndDeletedIsFalse(volunteerId);
126+
}
127+
123128
private BooleanExpression isNotDeleted() {
124129
return volunteer.deleted.isFalse();
125130
}

src/main/java/com/somemore/volunteer/service/VolunteerQueryService.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,13 @@ public List<VolunteerSimpleInfo> getVolunteerSimpleInfosByIds(List<UUID> ids) {
9292
return volunteerRepository.findSimpleInfoByIds(ids);
9393
}
9494

95+
@Override
96+
public void validateVolunteerExists(UUID volunteerId) {
97+
if (volunteerRepository.doesNotExistsByVolunteerId(volunteerId)) {
98+
throw new BadRequestException(NOT_EXISTS_VOLUNTEER.getMessage());
99+
}
100+
}
101+
95102
private Volunteer findVolunteer(UUID volunteerId) {
96103
return volunteerRepository.findById(volunteerId)
97104
.orElseThrow(() -> new BadRequestException(NOT_EXISTS_VOLUNTEER));
@@ -101,4 +108,5 @@ private VolunteerDetail findVolunteerDetail(UUID volunteerId) {
101108
return volunteerDetailRepository.findByVolunteerId(volunteerId)
102109
.orElseThrow(() -> new BadRequestException(NOT_EXISTS_VOLUNTEER));
103110
}
111+
104112
}

src/main/java/com/somemore/volunteer/usecase/VolunteerQueryUseCase.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,6 @@ public interface VolunteerQueryUseCase {
2424
List<Volunteer> getAllByIds(List<UUID> volunteerIds);
2525

2626
List<VolunteerSimpleInfo> getVolunteerSimpleInfosByIds(List<UUID> ids);
27+
28+
void validateVolunteerExists(UUID volunteerId);
2729
}

0 commit comments

Comments
 (0)