-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor: status 제거 * refactor: Mafia 결과 조회 제거 및 v2 명시 제거 * refactor: Http 통신 제거 및 버전 표식 제거 * refactor: 테스트 수정
- Loading branch information
Showing
11 changed files
with
81 additions
and
461 deletions.
There are no files selected for viewing
50 changes: 30 additions & 20 deletions
50
src/main/java/mafia/mafiatogether/chat/application/ChatService.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 |
---|---|---|
@@ -1,51 +1,61 @@ | ||
package mafia.mafiatogether.chat.application; | ||
|
||
import java.time.Clock; | ||
import java.util.List; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import mafia.mafiatogether.chat.application.dto.request.ChatRequest; | ||
import mafia.mafiatogether.chat.application.dto.response.ChatResponse; | ||
import mafia.mafiatogether.chat.domain.Chat; | ||
import mafia.mafiatogether.chat.domain.ChatRepository; | ||
import mafia.mafiatogether.chat.domain.Message; | ||
import mafia.mafiatogether.common.exception.ExceptionCode; | ||
import mafia.mafiatogether.common.exception.GameException; | ||
import mafia.mafiatogether.job.domain.PlayerJob; | ||
import mafia.mafiatogether.job.domain.PlayerJobRepository; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.List; | ||
import java.util.NoSuchElementException; | ||
import java.util.function.Supplier; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class ChatService { | ||
|
||
private final PlayerJobRepository playerJobRepository; | ||
private final ChatRepository chatRepository; | ||
|
||
@Transactional(readOnly = true) | ||
public List<ChatResponse> findAllChat(final String code, final String name) { | ||
final PlayerJob playerJobs = playerJobRepository.findById(code) | ||
.orElseThrow(() -> new GameException(ExceptionCode.INVALID_NOT_FOUND_ROOM_CODE)); | ||
final Chat chat = chatRepository.findById(code) | ||
.orElseThrow(() -> new GameException(ExceptionCode.INVALID_NOT_FOUND_ROOM_CODE)); | ||
final boolean isMafia = playerJobs.isMafia(name); | ||
return chat.getMessages().stream() | ||
|
||
return chat.getMessages() | ||
.stream() | ||
.map(message -> ChatResponse.of( | ||
message, | ||
message.getName().equals(name), | ||
isMafia, | ||
playerJobs.findJobByName(message.getName()).getJobType() | ||
)) | ||
.toList(); | ||
message.getName().equals(name) | ||
)).toList(); | ||
} | ||
|
||
@Transactional | ||
public void saveChat(final String code, final String name, final ChatRequest chatRequest) { | ||
final Chat chat = chatRepository.findById(code) | ||
.orElseThrow(() -> new GameException(ExceptionCode.INVALID_NOT_FOUND_ROOM_CODE)); | ||
final Message message = Message.ofChat(name, chatRequest.content()); | ||
public Message enter(final String name, final String code) { | ||
return saveChat(code, () -> Message.fromEnter(name)); | ||
} | ||
|
||
@Transactional | ||
public Message leave(final String name, final String code) { | ||
return saveChat(code, () -> Message.fromLeave(name)); | ||
} | ||
|
||
@Transactional | ||
public Message chat(final String name, final String code, final String content) { | ||
return saveChat(code, () -> Message.ofChat(name, content)); | ||
} | ||
|
||
private Message saveChat(final String code, Supplier<Message> messageFunction) { | ||
Chat chat = chatRepository.findById(code) | ||
.orElseThrow(NoSuchElementException::new); | ||
Message message = messageFunction.get(); | ||
chat.saveMessage(message); | ||
chatRepository.save(chat); | ||
return message; | ||
} | ||
|
||
|
||
} |
61 changes: 0 additions & 61 deletions
61
src/main/java/mafia/mafiatogether/chat/application/ChatV2Service.java
This file was deleted.
Oops, something went wrong.
29 changes: 0 additions & 29 deletions
29
src/main/java/mafia/mafiatogether/chat/application/dto/ChatV2Response.java
This file was deleted.
Oops, something went wrong.
32 changes: 8 additions & 24 deletions
32
src/main/java/mafia/mafiatogether/chat/application/dto/response/ChatResponse.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 |
---|---|---|
@@ -1,45 +1,29 @@ | ||
package mafia.mafiatogether.chat.application.dto.response; | ||
|
||
import mafia.mafiatogether.chat.domain.Message; | ||
import mafia.mafiatogether.chat.domain.vo.MessageType; | ||
|
||
import java.sql.Timestamp; | ||
|
||
import mafia.mafiatogether.chat.domain.Message; | ||
import mafia.mafiatogether.job.domain.jobtype.JobType; | ||
|
||
public record ChatResponse( | ||
String name, | ||
String content, | ||
MessageType messageType, | ||
Timestamp timestamp, | ||
Boolean isOwner, | ||
JobType job | ||
Boolean isOwner | ||
) { | ||
|
||
|
||
public static ChatResponse of( | ||
Message message, | ||
boolean isOwner, | ||
boolean isMafia, | ||
JobType jobType | ||
boolean isOwner | ||
) { | ||
return new ChatResponse( | ||
message.getName(), | ||
message.getContent(), | ||
message.getMessageType(), | ||
new Timestamp(message.getTimestamp()), | ||
isOwner, | ||
filteringMafia(isOwner, isMafia, jobType) | ||
isOwner | ||
); | ||
} | ||
|
||
private static JobType filteringMafia( | ||
final boolean isOwner, | ||
final boolean isMafia, | ||
final JobType jobType | ||
) { | ||
if (isMafia && jobType.equals(JobType.MAFIA)) { | ||
return jobType; | ||
} | ||
if (isOwner) { | ||
return jobType; | ||
} | ||
return null; | ||
} | ||
} |
57 changes: 38 additions & 19 deletions
57
src/main/java/mafia/mafiatogether/chat/ui/ChatController.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 |
---|---|---|
@@ -1,41 +1,60 @@ | ||
package mafia.mafiatogether.chat.ui; | ||
|
||
import jakarta.validation.Valid; | ||
|
||
import java.util.List; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import mafia.mafiatogether.common.annotation.PlayerInfo; | ||
import mafia.mafiatogether.chat.annotation.SendToChatWithRedis; | ||
import mafia.mafiatogether.chat.application.ChatService; | ||
import mafia.mafiatogether.chat.application.dto.request.ChatRequest; | ||
import mafia.mafiatogether.chat.application.dto.response.ChatResponse; | ||
import mafia.mafiatogether.chat.domain.Message; | ||
import mafia.mafiatogether.common.annotation.PlayerInfo; | ||
import mafia.mafiatogether.common.resolver.PlayerInfoDto; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.messaging.handler.annotation.DestinationVariable; | ||
import org.springframework.messaging.handler.annotation.MessageMapping; | ||
import org.springframework.messaging.handler.annotation.Payload; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
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; | ||
|
||
@RestController | ||
import java.util.List; | ||
|
||
@Controller | ||
@RequiredArgsConstructor | ||
@RequestMapping("/chat") | ||
public class ChatController { | ||
|
||
private final ChatService chatService; | ||
|
||
@GetMapping | ||
@GetMapping("/chat") | ||
public ResponseEntity<List<ChatResponse>> findAllChat(@PlayerInfo PlayerInfoDto playerInfoDto) { | ||
return ResponseEntity.ok(chatService.findAllChat(playerInfoDto.code(), playerInfoDto.name())); | ||
} | ||
|
||
@PostMapping | ||
public ResponseEntity<Void> saveChat( | ||
@PlayerInfo PlayerInfoDto playerInfoDto, | ||
@Valid @RequestBody ChatRequest request | ||
@MessageMapping("/chat/enter/{code}/{name}") | ||
@SendToChatWithRedis("/sub/chat/{code}") | ||
public Message enterChat( | ||
@DestinationVariable("code") String code, | ||
@DestinationVariable("name") String name | ||
) { | ||
chatService.saveChat(playerInfoDto.code(), playerInfoDto.name(), request); | ||
return ResponseEntity.status(HttpStatus.CREATED).build(); | ||
return chatService.enter(name, code); | ||
} | ||
|
||
@MessageMapping("/chat/leave/{code}/{name}") | ||
@SendToChatWithRedis("/sub/chat/{code}") | ||
public Message leaveChat( | ||
@DestinationVariable("code") String code, | ||
@DestinationVariable("name") String name | ||
) { | ||
return chatService.leave(name, code); | ||
} | ||
|
||
@MessageMapping("/chat/{code}/{name}") | ||
@SendToChatWithRedis("/sub/chat/{code}") | ||
public Message createChat( | ||
@DestinationVariable("code") String code, | ||
@DestinationVariable("name") String name, | ||
@Payload ChatRequest request | ||
) { | ||
return chatService.chat(name, code, request.content()); | ||
} | ||
|
||
|
||
} |
60 changes: 0 additions & 60 deletions
60
src/main/java/mafia/mafiatogether/chat/ui/ChatV2Controller.java
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.