Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/team-moabam/moabam-BE into
Browse files Browse the repository at this point in the history
#38-add-member
  • Loading branch information
parksey committed Nov 7, 2023
2 parents 00b1fe0 + 3305dff commit 42ddbd6
Show file tree
Hide file tree
Showing 35 changed files with 431 additions and 185 deletions.
18 changes: 18 additions & 0 deletions src/main/java/com/moabam/api/application/ItemService.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package com.moabam.api.application;

import static com.moabam.global.error.model.ErrorMessage.*;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.moabam.api.domain.entity.Inventory;
import com.moabam.api.domain.entity.Item;
import com.moabam.api.domain.entity.enums.ItemType;
import com.moabam.api.domain.repository.InventorySearchRepository;
import com.moabam.api.domain.repository.ItemSearchRepository;
import com.moabam.api.dto.ItemMapper;
import com.moabam.api.dto.ItemsResponse;
import com.moabam.global.error.exception.NotFoundException;

import lombok.RequiredArgsConstructor;

Expand All @@ -28,4 +32,18 @@ public ItemsResponse getItems(Long memberId, ItemType type) {

return ItemMapper.toItemsResponse(purchasedItems, notPurchasedItems);
}

@Transactional
public void selectItem(Long memberId, Long itemId) {
Inventory inventory = getInventory(memberId, itemId);

inventorySearchRepository.findDefault(memberId, inventory.getItemType())
.ifPresent(Inventory::deselect);
inventory.select();
}

private Inventory getInventory(Long memberId, Long itemId) {
return inventorySearchRepository.findOne(memberId, itemId)
.orElseThrow(() -> new NotFoundException(INVENTORY_NOT_FOUND));
}
}
25 changes: 14 additions & 11 deletions src/main/java/com/moabam/api/application/RoomService.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@
import com.moabam.api.domain.entity.Room;
import com.moabam.api.domain.entity.Routine;
import com.moabam.api.domain.entity.enums.RoomType;
import com.moabam.api.domain.repository.CertificationsMapper;
import com.moabam.api.domain.repository.CertificationsSearchRepository;
import com.moabam.api.domain.repository.ParticipantRepository;
import com.moabam.api.domain.repository.ParticipantSearchRepository;
import com.moabam.api.domain.repository.RoomRepository;
import com.moabam.api.domain.repository.RoutineRepository;
import com.moabam.api.domain.repository.RoutineSearchRepository;
import com.moabam.api.dto.CertificationImageResponse;
import com.moabam.api.dto.CertificationsMapper;
import com.moabam.api.dto.CreateRoomRequest;
import com.moabam.api.dto.EnterRoomRequest;
import com.moabam.api.dto.ModifyRoomRequest;
Expand Down Expand Up @@ -57,7 +57,7 @@ public class RoomService {
private final MemberService memberService;

@Transactional
public void createRoom(Long memberId, CreateRoomRequest createRoomRequest) {
public Long createRoom(Long memberId, CreateRoomRequest createRoomRequest) {
Room room = RoomMapper.toRoomEntity(createRoomRequest);
List<Routine> routines = RoutineMapper.toRoutineEntities(room, createRoomRequest.routines());
Participant participant = Participant.builder()
Expand All @@ -66,9 +66,11 @@ public void createRoom(Long memberId, CreateRoomRequest createRoomRequest) {
.build();

participant.enableManager();
roomRepository.save(room);
Room savedRoom = roomRepository.save(room);
routineRepository.saveAll(routines);
participantRepository.save(participant);

return savedRoom.getId();
}

@Transactional
Expand All @@ -85,6 +87,12 @@ public void modifyRoom(Long memberId, Long roomId, ModifyRoomRequest modifyRoomR
room.changePassword(modifyRoomRequest.password());
room.changeCertifyTime(modifyRoomRequest.certifyTime());
room.changeMaxCount(modifyRoomRequest.maxUserCount());

List<Routine> routines = routineSearchRepository.findByRoomId(roomId);
routineRepository.deleteAll(routines);

List<Routine> newRoutines = RoutineMapper.toRoutineEntities(room, modifyRoomRequest.routines());
routineRepository.saveAll(newRoutines);
}

@Transactional
Expand Down Expand Up @@ -134,7 +142,7 @@ public RoomDetailsResponse getRoomDetails(Long memberId, Long roomId) {
certificationsSearchRepository.findSortedDailyMemberCertifications(roomId, today);
List<RoutineResponse> routineResponses = getRoutineResponses(roomId);
List<TodayCertificateRankResponse> todayCertificateRankResponses = getTodayCertificateRankResponses(roomId,
routineResponses, dailyMemberCertifications, today);
dailyMemberCertifications, today);
List<LocalDate> certifiedDates = getCertifiedDates(roomId, today);
double completePercentage = calculateCompletePercentage(dailyMemberCertifications.size(),
room.getCurrentUserCount());
Expand Down Expand Up @@ -208,15 +216,10 @@ private List<RoutineResponse> getRoutineResponses(Long roomId) {
}

private List<TodayCertificateRankResponse> getTodayCertificateRankResponses(Long roomId,
List<RoutineResponse> routines, List<DailyMemberCertification> dailyMemberCertifications, LocalDate today) {
List<DailyMemberCertification> dailyMemberCertifications, LocalDate today) {

List<TodayCertificateRankResponse> responses = new ArrayList<>();
List<Long> routineIds = routines.stream()
.map(RoutineResponse::routineId)
.toList();
List<Certification> certifications = certificationsSearchRepository.findCertifications(
routineIds,
today);
List<Certification> certifications = certificationsSearchRepository.findCertifications(roomId, today);
List<Participant> participants = participantSearchRepository.findParticipants(roomId);
List<Member> members = memberService.getRoomMembers(participants.stream()
.map(Participant::getMemberId)
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/moabam/api/domain/entity/Inventory.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import org.hibernate.annotations.ColumnDefault;

import com.moabam.api.domain.entity.enums.ItemType;
import com.moabam.global.common.entity.BaseTimeEntity;

import jakarta.persistence.Column;
Expand Down Expand Up @@ -49,4 +50,16 @@ private Inventory(Long memberId, Item item, boolean isDefault) {
this.item = requireNonNull(item);
this.isDefault = isDefault;
}

public ItemType getItemType() {
return this.item.getType();
}

public void select() {
this.isDefault = true;
}

public void deselect() {
this.isDefault = false;
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/moabam/api/domain/entity/Room.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class Room extends BaseTimeEntity {
@Column(name = "id")
private Long id;

@Column(name = "title", nullable = false, length = 30)
@Column(name = "title", nullable = false, length = 20)
private String title;

@Column(name = "password", length = 8)
Expand Down
16 changes: 14 additions & 2 deletions src/main/java/com/moabam/api/domain/entity/Routine.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.moabam.api.domain.entity;

import static com.moabam.global.error.model.ErrorMessage.*;
import static java.util.Objects.*;

import org.apache.commons.lang3.StringUtils;

import com.moabam.global.common.entity.BaseTimeEntity;
import com.moabam.global.error.exception.BadRequestException;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
Expand Down Expand Up @@ -33,17 +37,25 @@ public class Routine extends BaseTimeEntity {
@JoinColumn(name = "room_id", updatable = false)
private Room room;

@Column(name = "content", nullable = false, length = 60)
@Column(name = "content", nullable = false, length = 20)
private String content;

@Builder
private Routine(Long id, Room room, String content) {
this.id = id;
this.room = requireNonNull(room);
this.content = requireNonNull(content);
this.content = validateContent(content);
}

public void changeContent(String content) {
this.content = content;
}

private String validateContent(String content) {
if (StringUtils.isBlank(content) || content.length() > 20) {
throw new BadRequestException(ROUTINE_LENGTH_ERROR);
}

return content;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import com.moabam.api.domain.entity.Certification;
import com.moabam.api.domain.entity.DailyMemberCertification;
import com.moabam.api.domain.entity.DailyRoomCertification;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.impl.JPAQueryFactory;

import lombok.RequiredArgsConstructor;
Expand All @@ -25,18 +24,10 @@ public class CertificationsSearchRepository {

private final JPAQueryFactory jpaQueryFactory;

public List<Certification> findCertifications(List<Long> routineIds, LocalDate date) {
BooleanExpression expression = null;

for (Long routineId : routineIds) {
BooleanExpression routineExpression = certification.routine.id.eq(routineId);
expression = expression == null ? routineExpression : expression.or(routineExpression);
}

return jpaQueryFactory
.selectFrom(certification)
public List<Certification> findCertifications(Long roomId, LocalDate date) {
return jpaQueryFactory.selectFrom(certification)
.where(
expression,
certification.routine.room.id.eq(roomId),
certification.createdAt.between(date.atStartOfDay(), date.atTime(LocalTime.MAX))
)
.fetch();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
import static com.moabam.api.domain.entity.QItem.*;

import java.util.List;
import java.util.Optional;

import org.springframework.stereotype.Repository;

import com.moabam.api.domain.entity.Inventory;
import com.moabam.api.domain.entity.Item;
import com.moabam.api.domain.entity.enums.ItemType;
import com.moabam.global.common.util.DynamicQuery;
Expand All @@ -20,6 +22,27 @@ public class InventorySearchRepository {

private final JPAQueryFactory jpaQueryFactory;

public Optional<Inventory> findOne(Long memberId, Long itemId) {
return Optional.ofNullable(
jpaQueryFactory.selectFrom(inventory)
.where(
DynamicQuery.generateEq(memberId, inventory.memberId::eq),
DynamicQuery.generateEq(itemId, inventory.item.id::eq))
.fetchOne()
);
}

public Optional<Inventory> findDefault(Long memberId, ItemType type) {
return Optional.ofNullable(
jpaQueryFactory.selectFrom(inventory)
.where(
DynamicQuery.generateEq(memberId, inventory.memberId::eq),
DynamicQuery.generateEq(type, inventory.item.type::eq),
inventory.isDefault.isTrue())
.fetchOne()
);
}

public List<Item> findItems(Long memberId, ItemType type) {
return jpaQueryFactory.selectFrom(inventory)
.join(inventory.item, item)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.moabam.api.domain.repository;
package com.moabam.api.dto;

import java.util.ArrayList;
import java.util.List;
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/moabam/api/dto/CreateRoomRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;

import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.Range;

import com.moabam.api.domain.entity.enums.RoomType;
Expand All @@ -12,7 +13,7 @@
import jakarta.validation.constraints.Size;

public record CreateRoomRequest(
@NotBlank String title,
@NotBlank @Length(max = 20) String title,
@Pattern(regexp = "^(|[0-9]{4,8})$") String password,
@NotNull @Size(min = 1, max = 4) List<String> routines,
@NotNull RoomType roomType,
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/moabam/api/dto/ModifyRoomRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import jakarta.validation.constraints.Size;

public record ModifyRoomRequest(
@NotBlank String title,
@NotBlank @Length(max = 20) String title,
@Length(max = 255, message = "방 공지의 길이가 너무 깁니다.") String announcement,
@NotNull @Size(min = 1, max = 4) List<String> routines,
@Pattern(regexp = "^(|\\d{4,8})$") String password,
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/moabam/api/presentation/ItemController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
Expand All @@ -25,4 +27,10 @@ public class ItemController {
public ItemsResponse getItems(@RequestParam ItemType type) {
return itemService.getItems(1L, type);
}

@PostMapping("/{itemId}/select")
@ResponseStatus(HttpStatus.OK)
public void selectItem(@PathVariable Long itemId) {
itemService.selectItem(1L, itemId);
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/moabam/api/presentation/RoomController.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public class RoomController {

@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public void createRoom(@Valid @RequestBody CreateRoomRequest createRoomRequest) {
roomService.createRoom(1L, createRoomRequest);
public Long createRoom(@Valid @RequestBody CreateRoomRequest createRoomRequest) {
return roomService.createRoom(1L, createRoomRequest);
}

@PutMapping("/{roomId}")
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/moabam/global/error/model/ErrorMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public enum ErrorMessage {
WRONG_ROOM_PASSWORD("방의 비밀번호가 일치하지 않습니다."),
ROOM_MAX_USER_REACHED("방의 인원수가 찼습니다."),
ROOM_DETAILS_ERROR("방 정보를 불러오는데 실패했습니다."),
ROUTINE_LENGTH_ERROR("루틴의 길이가 잘못 되었습니다."),

LOGIN_FAILED("로그인에 실패했습니다."),
REQUEST_FAILED("네트워크 접근 실패입니다."),
Expand All @@ -29,6 +30,8 @@ public enum ErrorMessage {
MEMBER_NOT_FOUND("존재하지 않는 회원입니다."),
MEMBER_ROOM_EXCEED("참여할 수 있는 방의 개수가 모두 찼습니다."),

INVENTORY_NOT_FOUND("구매하지 않은 아이템은 적용할 수 없습니다."),

INVALID_BUG_COUNT("벌레 개수는 0 이상이어야 합니다."),
INVALID_PRICE("가격은 0 이상이어야 합니다."),
INVALID_QUANTITY("수량은 1 이상이어야 합니다."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
import com.moabam.api.dto.AuthorizationTokenResponse;
import com.moabam.api.dto.LoginResponse;
import com.moabam.api.dto.OAuthMapper;
import com.moabam.fixture.AuthorizationResponseFixture;
import com.moabam.global.config.OAuthConfig;
import com.moabam.global.error.exception.BadRequestException;
import com.moabam.global.error.model.ErrorMessage;
import com.moabam.support.fixture.AuthorizationResponseFixture;

import jakarta.servlet.http.Cookie;

Expand Down
4 changes: 2 additions & 2 deletions src/test/java/com/moabam/api/application/BugServiceTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.moabam.api.application;

import static com.moabam.support.fixture.MemberFixture.*;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.BDDMockito.*;

Expand All @@ -13,7 +14,6 @@
import com.moabam.api.domain.entity.Bug;
import com.moabam.api.domain.entity.Member;
import com.moabam.api.dto.BugResponse;
import com.moabam.fixture.MemberFixture;

@ExtendWith(MockitoExtension.class)
class BugServiceTest {
Expand All @@ -29,7 +29,7 @@ class BugServiceTest {
void get_bug_success() {
// given
Long memberId = 1L;
Member member = MemberFixture.member();
Member member = member();
given(memberService.getById(memberId)).willReturn(member);

// when
Expand Down
Loading

0 comments on commit 42ddbd6

Please sign in to comment.