Skip to content

Commit

Permalink
#150 - 레거시 코드 제거 (#151)
Browse files Browse the repository at this point in the history
* refactor: status 제거

* refactor: Mafia 결과 조회 제거 및 v2 명시 제거

* refactor: Http 통신 제거 및 버전 표식 제거

* refactor: 테스트 수정
  • Loading branch information
kpeel5839 authored Nov 15, 2024
1 parent a1b3e8b commit f470eae
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 461 deletions.
50 changes: 30 additions & 20 deletions src/main/java/mafia/mafiatogether/chat/application/ChatService.java
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;
}


}

This file was deleted.

This file was deleted.

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 src/main/java/mafia/mafiatogether/chat/ui/ChatController.java
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 src/main/java/mafia/mafiatogether/chat/ui/ChatV2Controller.java

This file was deleted.

8 changes: 0 additions & 8 deletions src/main/java/mafia/mafiatogether/game/ui/GameController.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import mafia.mafiatogether.game.application.dto.response.GameExistResponse;
import mafia.mafiatogether.game.application.dto.response.GameInfoResponse;
import mafia.mafiatogether.game.application.dto.response.GameResultResponse;
import mafia.mafiatogether.game.application.dto.response.GameStatusResponse;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -28,13 +27,6 @@ public class GameController {
private final GameService gameService;
private static final String SSE_STATUS = "gameStatus";

@GetMapping("/status")
public ResponseEntity<GameStatusResponse> findStatus(
@PlayerInfo final PlayerInfoDto playerInfoDto
) {
return ResponseEntity.ok(gameService.findStatus(playerInfoDto.code()));
}

@PostMapping("/start")
public ResponseEntity<Void> startGame(
@PlayerInfo final PlayerInfoDto playerInfoDto
Expand Down
Loading

0 comments on commit f470eae

Please sign in to comment.