Skip to content

Commit

Permalink
♻️ Refactor: 모임 버그 해결 및 리팩토링
Browse files Browse the repository at this point in the history
<body>
- application-aws.yml
  - base-url-images 환경 변수로 빼기
- BaseResponseStatus.java
  - 오류명 적절하게 변경
- Moim.java
  - 모임 생성시 memberCount 는 0이 되어야함
- MoimFacade.java
  - BASSE_URL 환경 변수로 빼기
  - color 를 찾는 로직 코드 리팩토링

<footer>
- 관련: #48
  • Loading branch information
김현재 committed Mar 29, 2024
1 parent 7c09698 commit 4d984b2
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.example.namo2.domain.moim.application;

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.springframework.orm.ObjectOptimisticLockingFailureException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
Expand Down Expand Up @@ -39,17 +40,19 @@ public class MoimFacade {
* BaseURL을 직접 넣어주세요.
*/
private static final int[] MOIM_USERS_COLOR = new int[] {5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
private static final String BASE_URL = "BASE_URL을 넣어주세요";
private final MoimService moimService;
private final UserService userService;
private final MoimAndUserService moimAndUserService;
private final FileUtils fileUtils;

@Value("${moim.base-url-image}")
private String BASE_URL;

@Transactional(readOnly = false)
public MoimResponse.MoimIdDto createMoim(Long userId, String groupName, MultipartFile img) {
User user = userService.getUser(userId);
String url = BASE_URL;
if (!img.isEmpty()) {
if (img != null && !img.isEmpty()) {
url = fileUtils.uploadImage(img);
}
Moim moim = MoimConverter.toMoim(groupName, url);
Expand Down Expand Up @@ -84,30 +87,24 @@ public Long modifyMoimName(MoimRequest.PatchMoimNameDto patchMoimNameDto, Long u

@Transactional(readOnly = false)
public MoimResponse.MoimParticipantDto createMoimAndUser(Long userId, String code) {
try {
User user = userService.getUser(userId);
Moim moim = moimService.getMoimWithMoimAndUsersByCode(code);

MoimAndUser moimAndUser = MoimAndUserConverter
.toMoimAndUser(moim.getName(), selectColor(moim), user, moim);
moimAndUserService.create(moimAndUser, moim);
return MoimResponseConverter.toMoimParticipantDto(moim);
} catch (ObjectOptimisticLockingFailureException e) {
throw new BaseException(BaseResponseStatus.MOIM_IS_FULL_ERROR);
}
User user = userService.getUser(userId);
Moim moim = moimService.getMoimWithMoimAndUsersByCode(code);

MoimAndUser moimAndUser = MoimAndUserConverter
.toMoimAndUser(moim.getName(), selectColor(moim), user, moim);
moimAndUserService.create(moimAndUser, moim);
return MoimResponseConverter.toMoimParticipantDto(moim);
}

private int selectColor(Moim moim) {
Set<Integer> colors = moim.getMoimAndUsers()
.stream()
.map(MoimAndUser::getColor)
.collect(Collectors.toSet());
for (int color : MOIM_USERS_COLOR) {
if (!colors.contains(color)) {
return color;
}
}
throw new BaseException(BaseResponseStatus.NOT_FOUND_COLOR);
return Arrays.stream(MOIM_USERS_COLOR)
.filter((color) -> !colors.contains(color))
.findFirst()
.orElseThrow(() -> new BaseException(BaseResponseStatus.NOT_FOUND_COLOR));
}

@Transactional(readOnly = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public Moim(Long id, String name, String imgUrl) {
this.name = name;
this.imgUrl = imgUrl;
this.code = createCode();
this.memberCount = 1;
this.memberCount = 0;
this.status = MoimStatus.ACTIVE;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public enum BaseResponseStatus {
NOT_DELETE_BASE_CATEGORY_FAILURE(404, "일정 및 모임 카테고리는 삭제 될 수 없습니다."),
NOT_CHANGE_SPECIFIED_NAME_FAILURE(404, "일정 및 모임은 기본 카테고리로 지정된 이름입니다."),
NOT_CHECK_TERM_ERROR(404, "약관에 무조건 동의 해야합니다."),
MOIM_IS_FULL_ERROR(404, "일정 및 모임 카테고리는 삭제 될 수 없습니다."),
MOIM_IS_FULL_ERROR(404, "모임이 가득 차 있습니다."),
NOT_INCLUDE_MOIM_USER(404, "모임 안에 포함되어 있지 않은 유저입니다."),

/**
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/application-aws.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ cloud:
redis:
host: ${REDIS_HOST:localhost}
port: ${REDIS_PORT:6379}

moim:
base-url-image: ${BASE_MOIM_IMAGE_URL}

0 comments on commit 4d984b2

Please sign in to comment.