diff --git a/src/docs/asciidoc/Fire-API.adoc b/src/docs/asciidoc/Fire-API.adoc new file mode 100644 index 00000000..3da56ea5 --- /dev/null +++ b/src/docs/asciidoc/Fire-API.adoc @@ -0,0 +1,16 @@ + + +[[Fire-API]] += Fire API + +[[Fire-불던지기]] +=== 불 던지기 +operation::fire-controller-test/불_던지기[snippets='http-request,path-parameters,request-fields,http-response,response-fields'] + +--- +[[Fire-불던질사람조회]] +=== 불 던질 사람 조회 +operation::fire-controller-test/불_던질_사람_조회[snippets='http-request,path-parameters,request-fields,http-response,response-fields'] + +--- + diff --git a/src/docs/asciidoc/api.adoc b/src/docs/asciidoc/api.adoc index d103ef14..12774927 100644 --- a/src/docs/asciidoc/api.adoc +++ b/src/docs/asciidoc/api.adoc @@ -22,6 +22,8 @@ include::MissionArchive-API.adoc[] include::MissionBoard-API.adoc[] +include::Fire-API.adoc[] + include::Board-API.adoc[] include::BoardComment_API.adoc[] diff --git a/src/main/java/com/moing/backend/domain/fire/application/dto/res/FireReceiveRes.java b/src/main/java/com/moing/backend/domain/fire/application/dto/res/FireReceiveRes.java new file mode 100644 index 00000000..7b59d0a9 --- /dev/null +++ b/src/main/java/com/moing/backend/domain/fire/application/dto/res/FireReceiveRes.java @@ -0,0 +1,19 @@ +package com.moing.backend.domain.fire.application.dto.res; + +import lombok.Builder; +import lombok.Getter; + +@Builder +@Getter +public class FireReceiveRes { + + private Long receiveMemberId; + private String nickname; + private String fireStatus; + + public void updateFireStatus(boolean status) { + if (status) + this.fireStatus = "True"; + else this.fireStatus = "False"; + } +} diff --git a/src/main/java/com/moing/backend/domain/fire/application/dto/res/FireThrowRes.java b/src/main/java/com/moing/backend/domain/fire/application/dto/res/FireThrowRes.java new file mode 100644 index 00000000..12d19db3 --- /dev/null +++ b/src/main/java/com/moing/backend/domain/fire/application/dto/res/FireThrowRes.java @@ -0,0 +1,11 @@ +package com.moing.backend.domain.fire.application.dto.res; + +import lombok.Builder; +import lombok.Getter; + +@Getter +@Builder +public class FireThrowRes { + + private Long receiveMemberId; +} diff --git a/src/main/java/com/moing/backend/domain/fire/application/mapper/FireMapper.java b/src/main/java/com/moing/backend/domain/fire/application/mapper/FireMapper.java new file mode 100644 index 00000000..c8a803b7 --- /dev/null +++ b/src/main/java/com/moing/backend/domain/fire/application/mapper/FireMapper.java @@ -0,0 +1,34 @@ +package com.moing.backend.domain.fire.application.mapper; + +import com.moing.backend.domain.fire.application.dto.res.FireReceiveRes; +import com.moing.backend.domain.fire.application.dto.res.FireThrowRes; +import com.moing.backend.domain.fire.domain.entity.Fire; +import com.moing.backend.domain.member.domain.entity.Member; + +import java.util.ArrayList; +import java.util.List; + +public class FireMapper { + + public static FireThrowRes mapToFireThrowRes(Fire fire) { + return FireThrowRes.builder() + .receiveMemberId(fire.getReceiveMemberId()) + .build(); + } + + public static List mapToFireReceiversList(List members) { + List fireReceiveResList = new ArrayList<>(); + members.forEach( + member -> fireReceiveResList.add(FireMapper.mapToFireReceiveRes(member)) + ); + return fireReceiveResList; + } + + public static FireReceiveRes mapToFireReceiveRes(Member member) { + return FireReceiveRes.builder() + .receiveMemberId(member.getMemberId()) + .nickname(member.getNickName()) + .fireStatus("TRUE") + .build(); + } +} diff --git a/src/main/java/com/moing/backend/domain/fire/application/service/FireThrowUseCase.java b/src/main/java/com/moing/backend/domain/fire/application/service/FireThrowUseCase.java new file mode 100644 index 00000000..36d5175f --- /dev/null +++ b/src/main/java/com/moing/backend/domain/fire/application/service/FireThrowUseCase.java @@ -0,0 +1,52 @@ +package com.moing.backend.domain.fire.application.service; + +import com.moing.backend.domain.fire.application.dto.res.FireReceiveRes; +import com.moing.backend.domain.fire.application.dto.res.FireThrowRes; +import com.moing.backend.domain.fire.application.mapper.FireMapper; +import com.moing.backend.domain.fire.domain.entity.Fire; +import com.moing.backend.domain.fire.domain.repository.FireCustomRepository; +import com.moing.backend.domain.fire.domain.service.FireQueryService; +import com.moing.backend.domain.fire.domain.service.FireSaveService; +import com.moing.backend.domain.fire.exception.NoAuthThrowFireException; +import com.moing.backend.domain.member.domain.service.MemberGetService; +import com.moing.backend.domain.mission.application.service.MissionCreateUseCase; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; + +@Service +@Transactional +@RequiredArgsConstructor +public class FireThrowUseCase { + + public final FireSaveService fireSaveService; + public final FireQueryService fireQueryService; + private final MemberGetService memberGetService; + + public FireThrowRes createFireThrow(String userId, Long receiveMemberId) { + + Long throwMemberId = memberGetService.getMemberBySocialId(userId).getMemberId(); + + if (!fireQueryService.hasFireCreatedWithinOneHour(throwMemberId, receiveMemberId)) { + throw new NoAuthThrowFireException(); + } + + return FireMapper.mapToFireThrowRes(fireSaveService.save(Fire.builder() + .throwMemberId(throwMemberId) + .receiveMemberId(receiveMemberId) + .build())); + } + + public List getFireReceiveList(String userId,Long teamId, Long missionId) { + Long memberId = memberGetService.getMemberBySocialId(userId).getMemberId(); + + List fireReceiveRes = FireMapper.mapToFireReceiversList(fireQueryService.getNotYetMissionMember(teamId, missionId)); + fireReceiveRes.forEach( + res -> res.updateFireStatus(fireQueryService.hasFireCreatedWithinOneHour(memberId,res.getReceiveMemberId()) + )); + + return fireReceiveRes; + } +} diff --git a/src/main/java/com/moing/backend/domain/fire/domain/entity/Fire.java b/src/main/java/com/moing/backend/domain/fire/domain/entity/Fire.java new file mode 100644 index 00000000..9b32a630 --- /dev/null +++ b/src/main/java/com/moing/backend/domain/fire/domain/entity/Fire.java @@ -0,0 +1,28 @@ +package com.moing.backend.domain.fire.domain.entity; + +import com.moing.backend.global.entity.BaseTimeEntity; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import javax.persistence.*; + +@Entity +@Builder +@Getter +@NoArgsConstructor +@AllArgsConstructor +public class Fire extends BaseTimeEntity { + + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "fire_id") + private Long id; + + private Long throwMemberId; + + private Long receiveMemberId; + +} diff --git a/src/main/java/com/moing/backend/domain/fire/domain/repository/FireCustomRepository.java b/src/main/java/com/moing/backend/domain/fire/domain/repository/FireCustomRepository.java new file mode 100644 index 00000000..4e2d2b10 --- /dev/null +++ b/src/main/java/com/moing/backend/domain/fire/domain/repository/FireCustomRepository.java @@ -0,0 +1,16 @@ +package com.moing.backend.domain.fire.domain.repository; + + +import com.moing.backend.domain.fire.domain.entity.Fire; +import com.moing.backend.domain.member.domain.entity.Member; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.List; +import java.util.Optional; + +public interface FireCustomRepository { + + boolean hasFireCreatedWithinOneHour(Long throwMemberId, Long receiveMemberId); + Optional> getFireReceivers(Long teamId, Long missionId); + + } diff --git a/src/main/java/com/moing/backend/domain/fire/domain/repository/FireCustomRepositoryImpl.java b/src/main/java/com/moing/backend/domain/fire/domain/repository/FireCustomRepositoryImpl.java new file mode 100644 index 00000000..da51ece9 --- /dev/null +++ b/src/main/java/com/moing/backend/domain/fire/domain/repository/FireCustomRepositoryImpl.java @@ -0,0 +1,74 @@ +package com.moing.backend.domain.fire.domain.repository; + +import com.moing.backend.domain.fire.domain.entity.Fire; +import com.moing.backend.domain.fire.domain.entity.QFire; +import com.moing.backend.domain.member.domain.entity.Member; +import com.moing.backend.domain.missionArchive.domain.entity.MissionArchive; +import com.querydsl.jpa.JPAExpressions; +import com.querydsl.jpa.impl.JPAQueryFactory; + +import javax.persistence.EntityManager; +import java.time.LocalDateTime; +import java.util.List; +import java.util.Optional; + +import static com.moing.backend.domain.fire.domain.entity.QFire.fire; +import static com.moing.backend.domain.member.domain.entity.QMember.member; +import static com.moing.backend.domain.mission.domain.entity.QMission.mission; +import static com.moing.backend.domain.missionArchive.domain.entity.QMissionArchive.missionArchive; +import static com.moing.backend.domain.team.domain.entity.QTeam.team; +import static com.moing.backend.domain.teamMember.domain.entity.QTeamMember.teamMember; +import static com.querydsl.jpa.JPAExpressions.max; +import static com.querydsl.jpa.JPAExpressions.select; + +public class FireCustomRepositoryImpl implements FireCustomRepository { + private final JPAQueryFactory queryFactory; + + public FireCustomRepositoryImpl(EntityManager em) { + this.queryFactory = new JPAQueryFactory(em); + } + + + public boolean hasFireCreatedWithinOneHour(Long throwMemberId, Long receiveMemberId) { + + + LocalDateTime oneHourAgo = LocalDateTime.now().minusHours(1); // 현재 시간에서 1시간을 뺀 시간 + long count = queryFactory + .select() + .from(fire) + .where( + fire.throwMemberId.eq(throwMemberId), + fire.receiveMemberId.eq(receiveMemberId), + fire.createdDate.after(oneHourAgo) // createdDate가 oneHourAgo 이후인 데이터 + ) + .fetchCount(); + + return count <= 0; // 1시간 이내에 생성된 데이터가 존재하면 true를 반환, 그렇지 않으면 false 반환 + } + + + public Optional> getFireReceivers(Long teamId, Long missionId) { + return Optional.ofNullable(queryFactory + .select(teamMember.member) + .from(teamMember) + .where( + teamMember.team.teamId.eq(teamId), + teamMember.member.memberId.notIn( + JPAExpressions + .select(missionArchive.member.memberId) + .from(missionArchive,mission) + .where(missionArchive.mission.id.eq(missionId), + mission.id.eq(missionId)) + .groupBy(missionArchive.member.memberId, missionArchive.mission.id, + missionArchive.count, mission.number) + .having( + missionArchive.mission.id.eq(missionId), + missionArchive.count.max().goe(mission.number) + ) + ) + ) + .fetch()); + + } + +} diff --git a/src/main/java/com/moing/backend/domain/fire/domain/repository/FireRepository.java b/src/main/java/com/moing/backend/domain/fire/domain/repository/FireRepository.java new file mode 100644 index 00000000..6a9b01af --- /dev/null +++ b/src/main/java/com/moing/backend/domain/fire/domain/repository/FireRepository.java @@ -0,0 +1,7 @@ +package com.moing.backend.domain.fire.domain.repository; + +import com.moing.backend.domain.fire.domain.entity.Fire; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface FireRepository extends JpaRepository,FireCustomRepository { +} diff --git a/src/main/java/com/moing/backend/domain/fire/domain/service/FireQueryService.java b/src/main/java/com/moing/backend/domain/fire/domain/service/FireQueryService.java new file mode 100644 index 00000000..abb94e44 --- /dev/null +++ b/src/main/java/com/moing/backend/domain/fire/domain/service/FireQueryService.java @@ -0,0 +1,28 @@ +package com.moing.backend.domain.fire.domain.service; + +import com.moing.backend.domain.fire.domain.entity.Fire; +import com.moing.backend.domain.fire.domain.repository.FireCustomRepository; +import com.moing.backend.domain.fire.domain.repository.FireRepository; +import com.moing.backend.domain.fire.exception.NotFoundFireException; +import com.moing.backend.domain.fire.exception.NotFoundFireReceiversException; +import com.moing.backend.domain.member.domain.entity.Member; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +@RequiredArgsConstructor +public class FireQueryService { + + private final FireRepository fireRepository; + + public boolean hasFireCreatedWithinOneHour(Long throwMemberId, Long receiveMemberId) { + return fireRepository.hasFireCreatedWithinOneHour(throwMemberId,receiveMemberId); + } + + public List getNotYetMissionMember(Long teamId, Long missionId) { + return fireRepository.getFireReceivers(teamId, missionId).orElseThrow(NotFoundFireReceiversException::new); + } + +} diff --git a/src/main/java/com/moing/backend/domain/fire/domain/service/FireSaveService.java b/src/main/java/com/moing/backend/domain/fire/domain/service/FireSaveService.java new file mode 100644 index 00000000..14a94b30 --- /dev/null +++ b/src/main/java/com/moing/backend/domain/fire/domain/service/FireSaveService.java @@ -0,0 +1,20 @@ +package com.moing.backend.domain.fire.domain.service; + +import com.moing.backend.domain.fire.domain.entity.Fire; +import com.moing.backend.domain.fire.domain.repository.FireCustomRepository; +import com.moing.backend.domain.fire.domain.repository.FireRepository; +import com.moing.backend.global.annotation.DomainService; +import lombok.RequiredArgsConstructor; +import org.springframework.transaction.annotation.Transactional; + +@DomainService +@Transactional +@RequiredArgsConstructor +public class FireSaveService { + + private final FireRepository fireRepository; + + public Fire save(Fire fire) { + return fireRepository.save(fire); + } +} diff --git a/src/main/java/com/moing/backend/domain/fire/exception/FireException.java b/src/main/java/com/moing/backend/domain/fire/exception/FireException.java new file mode 100644 index 00000000..9540cd09 --- /dev/null +++ b/src/main/java/com/moing/backend/domain/fire/exception/FireException.java @@ -0,0 +1,13 @@ +package com.moing.backend.domain.fire.exception; + +import com.moing.backend.global.exception.ApplicationException; +import com.moing.backend.global.response.ErrorCode; +import org.springframework.http.HttpStatus; + +public abstract class FireException extends ApplicationException { + + protected FireException(ErrorCode errorCode, HttpStatus httpStatus) { + super(errorCode, httpStatus); + } + +} \ No newline at end of file diff --git a/src/main/java/com/moing/backend/domain/fire/exception/NoAuthThrowFireException.java b/src/main/java/com/moing/backend/domain/fire/exception/NoAuthThrowFireException.java new file mode 100644 index 00000000..fda8174d --- /dev/null +++ b/src/main/java/com/moing/backend/domain/fire/exception/NoAuthThrowFireException.java @@ -0,0 +1,12 @@ +package com.moing.backend.domain.fire.exception; + +import com.moing.backend.global.response.ErrorCode; +import org.springframework.http.HttpStatus; + +public class NoAuthThrowFireException extends FireException { + + public NoAuthThrowFireException() { + super(ErrorCode.NOT_AUTH_FIRE_THROW, + HttpStatus.NOT_FOUND); + } +} diff --git a/src/main/java/com/moing/backend/domain/fire/exception/NotFoundFireException.java b/src/main/java/com/moing/backend/domain/fire/exception/NotFoundFireException.java new file mode 100644 index 00000000..666d85b0 --- /dev/null +++ b/src/main/java/com/moing/backend/domain/fire/exception/NotFoundFireException.java @@ -0,0 +1,12 @@ +package com.moing.backend.domain.fire.exception; + +import com.moing.backend.global.response.ErrorCode; +import org.springframework.http.HttpStatus; + +public class NotFoundFireException extends FireException { + + public NotFoundFireException() { + super(ErrorCode.NOT_FOUND_FIRE, + HttpStatus.NOT_FOUND); + } +} diff --git a/src/main/java/com/moing/backend/domain/fire/exception/NotFoundFireReceiversException.java b/src/main/java/com/moing/backend/domain/fire/exception/NotFoundFireReceiversException.java new file mode 100644 index 00000000..b41c2eb7 --- /dev/null +++ b/src/main/java/com/moing/backend/domain/fire/exception/NotFoundFireReceiversException.java @@ -0,0 +1,12 @@ +package com.moing.backend.domain.fire.exception; + +import com.moing.backend.global.response.ErrorCode; +import org.springframework.http.HttpStatus; + +public class NotFoundFireReceiversException extends FireException { + + public NotFoundFireReceiversException() { + super(ErrorCode.NOT_FOUND_FIRE_RECEIVERS, + HttpStatus.NOT_FOUND); + } +} diff --git a/src/main/java/com/moing/backend/domain/fire/presentation/FireController.java b/src/main/java/com/moing/backend/domain/fire/presentation/FireController.java new file mode 100644 index 00000000..a9210481 --- /dev/null +++ b/src/main/java/com/moing/backend/domain/fire/presentation/FireController.java @@ -0,0 +1,55 @@ +package com.moing.backend.domain.fire.presentation; + +import com.moing.backend.domain.fire.application.dto.res.FireReceiveRes; +import com.moing.backend.domain.fire.application.dto.res.FireThrowRes; +import com.moing.backend.domain.fire.application.service.FireThrowUseCase; +import com.moing.backend.domain.mission.application.dto.res.MissionReadRes; +import com.moing.backend.global.config.security.dto.User; +import com.moing.backend.global.response.SuccessResponse; +import lombok.AllArgsConstructor; +import org.springframework.http.ResponseEntity; +import org.springframework.security.core.annotation.AuthenticationPrincipal; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +import static com.moing.backend.domain.fire.presentation.constant.FireResponseMessage.GET_RECEIVERS_SUCCESS; +import static com.moing.backend.domain.fire.presentation.constant.FireResponseMessage.THROW_FIRE_SUCCESS; +import static com.moing.backend.domain.mission.presentation.constant.MissionResponseMessage.READ_MISSION_SUCCESS; + +@RestController +@AllArgsConstructor +@RequestMapping("/api/{teamId}/missions/{missionId}/fire") +public class FireController { + + private final FireThrowUseCase fireThrowUseCase; + + /** + * 불 던지기 + * [POST] {teamId}/mission/{missionId}/fire/{receiveMemberId} + * 작성자 : 정승연 + */ + + @PostMapping("{receiveMemberId}") + public ResponseEntity> throwFire (@AuthenticationPrincipal User user, @PathVariable("teamId") Long teamId, + @PathVariable("receiveMemberId") Long receiveMemberId) { + return ResponseEntity.ok(SuccessResponse.create(THROW_FIRE_SUCCESS.getMessage(), this.fireThrowUseCase.createFireThrow(user.getSocialId(),receiveMemberId))); + } + + /** + * 불 던질 사람 조회 (receivers) + * [GET] {teamId}/mission/{missionId}/fire + * 작성자 : 정승연 + */ + + @GetMapping("") + public ResponseEntity>> throwFireList (@AuthenticationPrincipal User user, @PathVariable("teamId") Long teamId, + @PathVariable("missionId") Long missionId) { + return ResponseEntity.ok(SuccessResponse.create(GET_RECEIVERS_SUCCESS.getMessage(), this.fireThrowUseCase.getFireReceiveList(user.getSocialId(),teamId,missionId))); + } + + + + + +} diff --git a/src/main/java/com/moing/backend/domain/fire/presentation/constant/FireResponseMessage.java b/src/main/java/com/moing/backend/domain/fire/presentation/constant/FireResponseMessage.java new file mode 100644 index 00000000..8bbb91aa --- /dev/null +++ b/src/main/java/com/moing/backend/domain/fire/presentation/constant/FireResponseMessage.java @@ -0,0 +1,14 @@ +package com.moing.backend.domain.fire.presentation.constant; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +@Getter +@RequiredArgsConstructor +public enum FireResponseMessage { + THROW_FIRE_SUCCESS("불던지기를 완료 했습니다"), + GET_RECEIVERS_SUCCESS("불 던질사람 조회를 완료 했습니다"); + + private final String message; + +} diff --git a/src/main/java/com/moing/backend/domain/missionArchive/presentation/MissionArchiveController.java b/src/main/java/com/moing/backend/domain/missionArchive/presentation/MissionArchiveController.java index f331662d..0fbea4cf 100644 --- a/src/main/java/com/moing/backend/domain/missionArchive/presentation/MissionArchiveController.java +++ b/src/main/java/com/moing/backend/domain/missionArchive/presentation/MissionArchiveController.java @@ -83,7 +83,7 @@ public ResponseEntity>> getOtherPeopleA return ResponseEntity.ok(SuccessResponse.create(READ_TEAM_ARCHIVE_SUCCESS.getMessage(), this.singleMissionArchiveReadUseCase.getPersonalArchive(user.getSocialId(),missionId))); } - /** + /** * 미션 인증 게시물 좋아요 * [GET] {teamId}/m원issions/{missionId}/archive/hearts * 작성자 : 정승연 @@ -99,7 +99,7 @@ public ResponseEntity> pushHeart(@Authen /** * 인증 성공 인원 조회 - * [GET] {teamId}/m원issions/{missionId}/archive/status + * [GET] {teamId}/missions/{missionId}/archive/status * 작성자 : 정승연 **/ diff --git a/src/main/java/com/moing/backend/global/response/ErrorCode.java b/src/main/java/com/moing/backend/global/response/ErrorCode.java index bd2bd522..d3ffc8dd 100644 --- a/src/main/java/com/moing/backend/global/response/ErrorCode.java +++ b/src/main/java/com/moing/backend/global/response/ErrorCode.java @@ -31,6 +31,11 @@ public enum ErrorCode { NOT_FOUND_MISSION_ARCHIVE("MA0001", "아직 미션을 제출하지 않았습니다."), NO_MORE_ARCHIVE_ERROR("MA0001", "지정한 횟수 이상 미션을 인증할 수 없습니다."), + //불던지기 관련 에러 코드 + NOT_FOUND_FIRE("F001","불던지기 현황을 찾을 수 없습니다"), + NOT_FOUND_FIRE_RECEIVERS("F001","불던지기를 받을 사람을 찾을 수 없습니다"), + NOT_AUTH_FIRE_THROW("F002","1시간 이내에 불던지기를 할 수 없습니다"), + //팀 관련 에러 코드 NOT_FOUND_BY_TEAM_ID_ERROR("T0001", "해당 teamId인 팀이 존재하지 않습니다."), NOT_AUTH_BY_TEAM_ERROR("T0002","권한이 없습니다."), diff --git a/src/test/java/com/moing/backend/domain/fire/representation/FireControllerTest.java b/src/test/java/com/moing/backend/domain/fire/representation/FireControllerTest.java new file mode 100644 index 00000000..767f6e4c --- /dev/null +++ b/src/test/java/com/moing/backend/domain/fire/representation/FireControllerTest.java @@ -0,0 +1,133 @@ +package com.moing.backend.domain.fire.representation; + +import com.moing.backend.config.CommonControllerTest; +import com.moing.backend.domain.fire.application.dto.res.FireReceiveRes; +import com.moing.backend.domain.fire.application.dto.res.FireThrowRes; +import com.moing.backend.domain.fire.application.service.FireThrowUseCase; +import com.moing.backend.domain.fire.presentation.FireController; +import com.moing.backend.domain.mission.application.dto.req.MissionReq; +import com.moing.backend.domain.mission.application.dto.res.MissionCreateRes; +import org.assertj.core.util.Lists; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.http.MediaType; +import org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders; +import org.springframework.test.web.servlet.ResultActions; +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; + +import java.util.List; + +import static com.moing.backend.domain.fire.presentation.constant.FireResponseMessage.GET_RECEIVERS_SUCCESS; +import static com.moing.backend.domain.fire.presentation.constant.FireResponseMessage.THROW_FIRE_SUCCESS; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.BDDMockito.given; +import static org.springframework.restdocs.headers.HeaderDocumentation.headerWithName; +import static org.springframework.restdocs.headers.HeaderDocumentation.requestHeaders; +import static org.springframework.restdocs.payload.PayloadDocumentation.*; +import static org.springframework.restdocs.request.RequestDocumentation.parameterWithName; +import static org.springframework.restdocs.request.RequestDocumentation.pathParameters; + +@WebMvcTest(FireController.class) +public class FireControllerTest extends CommonControllerTest { + + @MockBean + private FireThrowUseCase fireThrowUseCase; + + + @Test + public void 불_던질_사람_조회() throws Exception { + //given + + List output = Lists.newArrayList(FireReceiveRes.builder() + .receiveMemberId(1L) + .nickname("receiver 닉네임") + .fireStatus("True/False") + .build()); + + given(fireThrowUseCase.getFireReceiveList(any(),any(),any())).willReturn(output); + + Long teamId = 1L; + Long missionId = 1L; + //when + ResultActions actions = mockMvc.perform(RestDocumentationRequestBuilders. + get("/api/{teamId}/missions/{missionId}/fire",teamId,missionId) + .header("Authorization", "Bearer ACCESS_TOKEN") + .contentType(MediaType.APPLICATION_JSON) + + ); + + //then + actions + .andExpect(MockMvcResultMatchers.status().isOk()) + .andDo( + restDocs.document( + requestHeaders( + headerWithName("Authorization").description("접근 토큰") + ), + pathParameters( + parameterWithName("teamId").description("팀 아이디"), + parameterWithName("missionId").description("미션 아이디") + ), + responseFields( + fieldWithPath("isSuccess").description("true"), + fieldWithPath("message").description(GET_RECEIVERS_SUCCESS.getMessage()), + fieldWithPath("data[].receiveMemberId").description("미션 아이디"), + fieldWithPath("data[].nickname").description("불 받을 사람 "), + fieldWithPath("data[].fireStatus").description("불 던질 수 있는 상태 리턴, 1시간 내 불 던진 내역에 따라 true[True/False]") + + ) + ) + ) + .andReturn(); + + } + + + @Test + public void 불_던지기() throws Exception { + //given + + FireThrowRes output = FireThrowRes.builder() + .receiveMemberId(1L) + .build(); + + given(fireThrowUseCase.createFireThrow(any(),any())).willReturn(output); + + Long teamId = 2L; + Long missionId = 2L; + Long receiveMemberId = 2L; + //when + ResultActions actions = mockMvc.perform(RestDocumentationRequestBuilders. + post("/api/{teamId}/missions/{missionId}/fire/{receiveMemberId}",teamId,missionId,receiveMemberId) + .header("Authorization", "Bearer ACCESS_TOKEN") + .contentType(MediaType.APPLICATION_JSON) + ); + + //then + actions + .andExpect(MockMvcResultMatchers.status().isOk()) + .andDo( + restDocs.document( + requestHeaders( + headerWithName("Authorization").description("접근 토큰") + ), + pathParameters( + parameterWithName("teamId").description("팀 아이디"), + parameterWithName("missionId").description("미션 아이디"), + parameterWithName("receiveMemberId").description("불 받을 사람 아이디") + ), + + responseFields( + fieldWithPath("isSuccess").description("true"), + fieldWithPath("message").description(THROW_FIRE_SUCCESS.getMessage()), + fieldWithPath("data.receiveMemberId").description("미션 아이디") + ) + ) + ) + .andReturn(); + + } + + +}