Skip to content

Commit

Permalink
feat: 초대코드로 미션 조회 API 구현 (#37)
Browse files Browse the repository at this point in the history
* feat: 초대코드로 미션 조회 API 구현

* 값객체 equals, hashcode 정의
  • Loading branch information
songyi00 authored Aug 9, 2024
1 parent dcb86dd commit ae5d3a9
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.nexters.goalpanzi.domain.mission.repository.MissionRepository;
import com.nexters.goalpanzi.exception.ErrorCode;
import com.nexters.goalpanzi.exception.ForbiddenException;
import com.nexters.goalpanzi.exception.NotFoundException;
import lombok.RequiredArgsConstructor;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -63,6 +64,13 @@ public MissionDetailResponse getMission(final Long missionId) {
return MissionDetailResponse.from(mission);
}

public MissionDetailResponse getMissionByInvitationCode(final InvitationCode invitationCode) {
Mission mission = missionRepository.findByInvitationCode(invitationCode)
.orElseThrow(() -> new NotFoundException(ErrorCode.NOT_FOUND_MISSION, invitationCode));

return MissionDetailResponse.from(mission);
}

@Transactional
public void deleteMission(final Long memberId, final Long missionId) {
Mission mission = missionRepository.getMission(missionId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import lombok.NoArgsConstructor;
import org.springframework.util.StringUtils;

import java.util.Objects;
import java.util.Random;

import static com.nexters.goalpanzi.exception.ErrorCode.INVALID_INVITATION_CODE;
Expand Down Expand Up @@ -46,4 +47,24 @@ private void validate() {
throw new IllegalArgumentException(INVALID_INVITATION_CODE.getMessage());
}
}

@Override
public boolean equals(final Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
InvitationCode code1 = (InvitationCode) o;
return Objects.equals(code, code1.code);
}

@Override
public int hashCode() {
return Objects.hashCode(code);
}

@Override
public String toString() {
return "InvitationCode{" +
"code='" + code + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.nexters.goalpanzi.application.mission.MissionService;
import com.nexters.goalpanzi.application.mission.dto.response.MissionDetailResponse;
import com.nexters.goalpanzi.common.argumentresolver.LoginMemberId;
import com.nexters.goalpanzi.domain.mission.InvitationCode;
import com.nexters.goalpanzi.presentation.mission.dto.CreateMissionRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
Expand All @@ -13,6 +14,7 @@
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RequiredArgsConstructor
Expand Down Expand Up @@ -51,4 +53,13 @@ public ResponseEntity<MissionDetailResponse> deleteMission(

return ResponseEntity.noContent().build();
}

@Override
@GetMapping
public ResponseEntity<MissionDetailResponse> getMissionByInvitationCode(
@RequestParam final String invitationCode) {
MissionDetailResponse response = missionService.getMissionByInvitationCode(new InvitationCode(invitationCode));

return ResponseEntity.ok(response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

@Tag(name = "미션")
public interface MissionControllerDocs {
Expand All @@ -31,4 +32,10 @@ ResponseEntity<MissionDetailResponse> deleteMission(
@PathVariable("/{missionId}") Long missionId,
@Parameter(in = ParameterIn.HEADER, hidden = true) @LoginMemberId final Long memberId
);

@Operation(summary = "초대코드로 미션 조회", description = "초대코드로 미션정보를 조회합니다.")
ResponseEntity<MissionDetailResponse> getMissionByInvitationCode(
@RequestParam String invitationCode
);

}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,31 @@ public class MissionAcceptanceTest extends AcceptanceTest {
() -> assertThat(actual.hostMemberId()).isEqualTo(1L));
}

@Test
void 초대코드로_미션을_조회한다() {
LoginResponse login = 구글_로그인(new GoogleLoginCommand(EMAIL_HOST)).as(LoginResponse.class);

CreateMissionRequest request = new CreateMissionRequest(DESCRIPTION, LocalDateTime.now(),
LocalDateTime.now().plusDays(5), TimeOfDay.EVERYDAY,
List.of(DayOfWeek.MONDAY, DayOfWeek.FRIDAY), 20);

MissionDetailResponse mission = 미션_생성(request, login.accessToken()).as(MissionDetailResponse.class);

MissionDetailResponse actual = RestAssured.given().log().all()
.contentType(MediaType.APPLICATION_JSON_VALUE)
.header(HttpHeaders.AUTHORIZATION, BEARER + login.accessToken())
.when().get("/api/missions?invitationCode=" + mission.invitationCode())
.then().log().all()
.statusCode(HttpStatus.OK.value())
.extract().as(MissionDetailResponse.class);

assertAll(
() -> assertThat(actual.description()).isEqualTo(DESCRIPTION),
() -> assertThat(actual.boardCount()).isEqualTo(20),
() -> assertThat(actual.missionDays()).containsExactly(DayOfWeek.MONDAY, DayOfWeek.FRIDAY),
() -> assertThat(actual.hostMemberId()).isEqualTo(1L));
}

@Test
void 미션을_생성한_사용자는_자동으로_경쟁에_참가된다() {
LoginResponse login = 구글_로그인(new GoogleLoginCommand(EMAIL_HOST)).as(LoginResponse.class);
Expand Down

0 comments on commit ae5d3a9

Please sign in to comment.