Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] isDeleted 분기처리 추가 #110

Merged
merged 11 commits into from
Jun 12, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@ public ResponseEntity<Void> enterRoom(
}

@DeleteMapping("/rooms/{roomId}")
public ResponseEntity<Room> deleteRoom(
public ResponseEntity<Void> deleteRoom(
@UserId final Long userId,
@PathVariable final Long roomId
) {
return ResponseEntity.ok(roomService.deleteRoom(roomId, userId));
roomService.deleteRoom(roomId, userId);
return ResponseEntity.ok().build();
}
}
8 changes: 8 additions & 0 deletions src/main/java/com/telepigeon/server/domain/Profile.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public class Profile {
@Enumerated(EnumType.STRING)
private Relation relation;

private boolean isDeleted;

private LocalDateTime createdAt;

private LocalDateTime updatedAt;
Expand All @@ -54,6 +56,7 @@ private Profile(User user, Room room, Relation relation) {
this.createdAt = LocalDateTime.now();
this.updatedAt = LocalDateTime.now();
this.emotion = 0.0;
this.isDeleted = false;
}

private Profile(User user, Room room, Relation relation, String keywords) {
Expand Down Expand Up @@ -111,4 +114,9 @@ public void updateProfileInfo(
this.relation = relation;
this.updatedAt = LocalDateTime.now();
}

public void updateIsDeleted(){
this.isDeleted = true;
this.updatedAt = LocalDateTime.now();
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/telepigeon/server/domain/Room.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class Room {

private LocalDateTime createdAt;

@OneToMany(mappedBy="room", cascade=CascadeType.ALL, fetch=FetchType.LAZY)
@OneToMany(mappedBy="room", fetch=FetchType.LAZY)
private List<Profile> profiles;

@Builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import jakarta.validation.constraints.Size;

public record RoomCreateDto(
@NotNull @Size(max=8, min=2)
@NotNull @Size(max=8, min=1)
String name
) {
public static RoomCreateDto of(Room room) {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/telepigeon/server/dto/type/FcmContent.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public enum FcmContent {
QUESTION("질문이 도착했습니다.", "오늘의 질문을 확인하세요!", "ROOM_CLICK", "question"),
ANSWER("답변이 도착했습니다.", "상대방의 답변을 확인하세요!", "ROOM_CLICK", "answer"),
HURRY("재촉하기가 도착했습니다.", "상대방에게 답변을 보내보세요!", "ROOM_CLICK", "hurry"),
ROOM_ENTER("상대방이 입장했습니다", "상대방과 대화를 시작하세요!", "ROOM_CLICK", "room"),
ROOM_LEAVE("상대방이 퇴장했습니다", "상대방이 퇴장했습니다", "ROOM_CLICK", "room"),
;

private final String title;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public enum BusinessErrorCode implements DefaultErrorCode{
BUSINESS_TEST(HttpStatus.OK,"conflict","선착순 마감됐어요"),
HURRY_ALREADY_EXISTS(HttpStatus.OK, "conflict", "재촉하기를 이미 보냈습니다."),
QUESTION_ALREADY_EXISTS(HttpStatus.OK, "conflict", "처리되지 않은 질문하기가 이미 있습니다."),
PROFILE_DELETED_ERROR(HttpStatus.OK, "conflict", "프로필이 삭제되었습니다."),
KAKAO_SERVER_ERROR(HttpStatus.SERVICE_UNAVAILABLE, "external", "카카오 서버 오류"),
INVALID_KAKAO_ADMIN_KEY(HttpStatus.INTERNAL_SERVER_ERROR, "internal", "카카오 어드민 키가 유효하지 않습니다."),
NAVER_CLOUD_SERVER_ERROR(HttpStatus.SERVICE_UNAVAILABLE, "external", "네이버 클라우드 서버 오류"),
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import com.telepigeon.server.dto.naverCloud.ConfidenceDto;
import com.telepigeon.server.dto.room.response.RoomStateDto;
import com.telepigeon.server.dto.type.FcmContent;
import com.telepigeon.server.exception.BusinessException;
import com.telepigeon.server.exception.code.BusinessErrorCode;
import com.telepigeon.server.service.external.S3Service;
import com.telepigeon.server.service.external.FcmService;
import com.telepigeon.server.service.external.NaverCloudService;
Expand Down Expand Up @@ -60,12 +62,14 @@ public Answer create(
User user = userRetriever.findById(userId);
Room room = roomRetriever.findById(roomId);
Profile profile = profileRetriever.findByUserAndRoom(user, room);
Profile receiver = profileRetriever.findByUserNotAndRoom(user, room);
if (receiver.isDeleted())
throw new BusinessException(BusinessErrorCode.PROFILE_DELETED_ERROR);
Question question = questionRetriever.findById(questionId);
ConfidenceDto confidence = naverCloudService.getConfidence(
ConfidenceCreateDto.of(answerCreateDto.content())
);
Double emotion = (confidence.positive() - confidence.negative()) * 0.01;

Answer answer = answerSaver.create(
Answer.create(
answerCreateDto.content(),
Expand All @@ -81,7 +85,6 @@ public Answer create(
emotion
)
);
Profile receiver = profileRetriever.findByUserNotAndRoom(user, room);
fcmService.send(
receiver.getUser().getFcmToken(),
FcmMessageDto.of(
Expand Down Expand Up @@ -217,7 +220,7 @@ private double CalculateEmotion(
) {
if (totEmotion == 0.0)
return emotion;
return totEmotion * 0.9 + emotion * 0.1;
return totEmotion * 0.5 + emotion * 0.5;
}

private String uploadImage(MultipartFile image) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ private Message createMessage(
.setTitle(fcmMessageDto.title())
.setBody(fcmMessageDto.body())
.setClickAction(fcmMessageDto.clickAction())
.setSound("default")
.build()
)
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public void create(
if (hurryRetriever.existsByRoomIdAndSenderId(roomId, user.getId()))
throw new BusinessException(BusinessErrorCode.HURRY_ALREADY_EXISTS);
Profile receiver = profileRetriever.findByUserNotAndRoom(user, room);
if (receiver.isDeleted())
throw new BusinessException(BusinessErrorCode.PROFILE_DELETED_ERROR);
hurrySaver.save(Hurry.create(roomId, userId));
fcmService.send(
receiver.getUser().getFcmToken(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class OpenAiService {
private String apiKey;
@Value("${openai.api.url}")
private String url;
@Value("${openai.role-content")
@Value("${openai.system-content}")
private String roleContent;
@Value("${openai.assistant-content}")
private String assistantContent;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,4 @@ public List<Profile> findByUserId(final long userId) {
public List<Profile> findAll() {
return profileRepository.findAll();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public class ProfileService {
private final UserRetriever userRetriever;
private final ProfileRetriever profileRetriever;
private final ProfileUpdater profileUpdater;
private final ProfileSaver profileSaver;

@Transactional(readOnly = true)
public ProfileKeywordsDto getProfileKeywords(final Long roomId, final Long userId) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public Question create(final Profile profile){
Profile receiver = profileRetriever.findByUserNotAndRoom(
profile.getUser(), profile.getRoom()
);
if (receiver.isDeleted())
throw new BusinessException(BusinessErrorCode.PROFILE_DELETED_ERROR);
Question prevQuestion = questionRetriever.findFirstByProfile(profile); //최근 질문 가져오기
if (
prevQuestion != null &&
Expand Down
59 changes: 35 additions & 24 deletions src/main/java/com/telepigeon/server/service/room/RoomService.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,10 @@
import com.telepigeon.server.exception.BusinessException;
import com.telepigeon.server.exception.code.BusinessErrorCode;
import com.telepigeon.server.repository.RoomRepository;
import com.telepigeon.server.service.answer.AnswerRemover;
import com.telepigeon.server.service.answer.AnswerRetriever;
import com.telepigeon.server.service.external.FcmService;
import com.telepigeon.server.service.profile.ProfileRemover;
import com.telepigeon.server.service.profile.ProfileRetriever;
import com.telepigeon.server.service.profile.ProfileSaver;
import com.telepigeon.server.service.question.QuestionRemover;
import com.telepigeon.server.service.question.QuestionRetriever;
import com.telepigeon.server.service.question.QuestionSaver;
import com.telepigeon.server.service.user.UserRetriever;
import com.telepigeon.server.service.worry.WorryRemover;
Expand All @@ -40,13 +36,10 @@ public class RoomService {
private final ProfileRetriever profileRetriever;
private final AnswerRetriever answerRetriever;
private final RoomRetriever roomRetriever;
private final QuestionRetriever questionRetriever;
private final WorryRetriever worryRetriever;
private final ProfileRemover profileRemover;
private final QuestionRemover questionRemover;
private final WorryRemover worryRemover;
private final AnswerRemover answerRemover;
private final QuestionSaver questionSaver;
private final RoomRemover roomRemover;
private final FcmService fcmService;

@Transactional
Expand Down Expand Up @@ -81,7 +74,7 @@ public RoomListDto.RoomDto createRoomDto(final User user, final Room room) {
int sentence = 3, emotion = 0;
Profile myProfile = profileRetriever.findByUserAndRoom(user, room);
String myRelation = myProfile.getRelation() != null ? myProfile.getRelation().getContent() : "-";
if (!profileRetriever.existsByUserNotAndRoom(user, room)){
if (!profileRetriever.existsByUserNotAndRoom(user, room)) {
return RoomListDto.RoomDto.of(
room.getId(),
room.getName(),
Expand All @@ -97,10 +90,11 @@ public RoomListDto.RoomDto createRoomDto(final User user, final Room room) {
boolean myState = answerRetriever.existsByProfile(myProfile);
boolean opponentState = answerRetriever.existsByProfile(opponentProfile);

// 감정 측정 시 업데이트
// 감정 측정 시 업데이트
emotion = getEmotion(opponentProfile.getEmotion());

if (myState && opponentState) {
if (opponentProfile.isDeleted()){
sentence = 4;
} else if (myState && opponentState) {
sentence = 0;
} else if (myState) {
sentence = 1;
Expand Down Expand Up @@ -141,27 +135,44 @@ public Profile enterRoom(final RoomEnterDto roomEnterDto, final Long userId) {

Profile profile = profileSaver.save(Profile.create(user, room));
Profile receiver = profileRetriever.findByUserNotAndRoom(user, room);
fcmService.send(
receiver.getUser().getFcmToken(),
FcmMessageDto.of(
FcmContent.ROOM_ENTER,
room.getId()
)
);
sendQuestionFirst(receiver, profile);
sendQuestionFirst(profile, receiver);
return profile;
}

@Transactional
public Room deleteRoom(final Long roomId, final Long userId) {
public void deleteRoom(final Long roomId, final Long userId) {
Room room = roomRetriever.findById(roomId);
User user = userRetriever.findById(userId);

Profile profile = profileRetriever.findByUserAndRoom(user, room);
List<Answer> answerList = answerRetriever.findAllByProfile(profile);
List<Question> questionList = questionRetriever.findAllByProfile(profile);
List<Worry> worryList = worryRetriever.findAllByProfile(profile);

profileRemover.remove(profile);
answerList.forEach(answerRemover::remove);
questionList.forEach(questionRemover::remove);
worryList.forEach(worryRemover::remove);

return room;
if (profileRetriever.existsByUserNotAndRoom(user, room)){
Profile opponentProfile = profileRetriever.findByUserNotAndRoom(user, room);
if (!opponentProfile.isDeleted()){
Profile profile = profileRetriever.findByUserAndRoom(user, room);
List<Worry> worryList = worryRetriever.findAllByProfile(profile);

profile.updateIsDeleted();
worryList.forEach(worryRemover::remove);
fcmService.send(
opponentProfile.getUser().getFcmToken(),
FcmMessageDto.of(
FcmContent.ROOM_LEAVE,
room.getId()
)
);
} else {
roomRemover.remove(room);
}
} else {
roomRemover.remove(room);
}
}

private String createCode() {
Expand Down
Loading