-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #69 from Donut-DONationUTile/feature/message
feat: 수혜자의 메세지 전송 기능 구현
- Loading branch information
Showing
6 changed files
with
103 additions
and
1 deletion.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
src/main/java/zero/eight/donut/controller/MessageController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package zero.eight.donut.controller; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import zero.eight.donut.common.response.ApiResponse; | ||
import zero.eight.donut.dto.SendMessageRequestDto; | ||
import zero.eight.donut.service.MessageService; | ||
|
||
@RequiredArgsConstructor | ||
@RequestMapping("/api/message") | ||
@RestController | ||
public class MessageController { | ||
|
||
private final MessageService messageService; | ||
|
||
@PostMapping("/receiver") | ||
public ApiResponse<?> sendMessage(@RequestBody SendMessageRequestDto requestDto) { | ||
return messageService.sendMessage(requestDto); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/main/java/zero/eight/donut/dto/SendMessageRequestDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package zero.eight.donut.dto; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public class SendMessageRequestDto { | ||
private Long boxId; // 꾸러미 고유 ID | ||
private String content; // 메세지 본문 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
src/main/java/zero/eight/donut/service/MessageService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package zero.eight.donut.service; | ||
|
||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import zero.eight.donut.common.response.ApiResponse; | ||
import zero.eight.donut.config.jwt.AuthUtils; | ||
import zero.eight.donut.domain.Gift; | ||
import zero.eight.donut.domain.Giftbox; | ||
import zero.eight.donut.domain.Message; | ||
import zero.eight.donut.domain.Receiver; | ||
import zero.eight.donut.dto.SendMessageRequestDto; | ||
import zero.eight.donut.exception.Success; | ||
import zero.eight.donut.repository.GiftboxRepository; | ||
import zero.eight.donut.repository.MessageRepository; | ||
|
||
import java.util.List; | ||
|
||
@Service | ||
public class MessageService { | ||
|
||
private final GiftboxRepository giftboxRepository; | ||
private final AuthUtils authUtils; | ||
private final MessageRepository messageRepository; | ||
|
||
public MessageService(GiftboxRepository giftboxRepository, AuthUtils authUtils, MessageRepository messageRepository) { | ||
this.giftboxRepository = giftboxRepository; | ||
this.authUtils = authUtils; | ||
this.messageRepository = messageRepository; | ||
} | ||
|
||
@Transactional | ||
public ApiResponse<?> sendMessage(SendMessageRequestDto requestDto) { | ||
// 수혜자 | ||
Receiver receiver = authUtils.getReceiver(); | ||
|
||
// 꾸러미 ID로 꾸러미에 속한 기프티콘 리스트 찾기 | ||
Giftbox box = giftboxRepository.findById(requestDto.getBoxId()).orElseThrow(); | ||
List<Gift> giftList = box.getGiftList(); | ||
|
||
// 기프티콘마다 신규 메세지 등록 여부(idMsgReceived)를 true로 변경하기 | ||
// 메세지 객체 생성 및 저장하기(기프티콘 고유 ID, 기부자 고유 ID, 수혜자 고유 ID, requestDto.content) | ||
for (Gift g : giftList) { | ||
g.updateIsMsgReceived(); | ||
Message message = Message.builder() | ||
.content(requestDto.getContent()) | ||
.receiver(receiver) | ||
.gift(g) | ||
.giver(g.getGiver()) | ||
.build(); | ||
messageRepository.save(message); | ||
} | ||
|
||
///////////////////////////////////// | ||
///////////////////////////////////// | ||
///////////////////////////////////// | ||
// 기부자에게 알림 보내기 | ||
///////////////////////////////////// | ||
///////////////////////////////////// | ||
///////////////////////////////////// | ||
|
||
|
||
// 반환하기 | ||
return ApiResponse.success(Success.SEND_MESSAGE_SUCCESS); | ||
} | ||
} |