Skip to content

Commit

Permalink
Merge pull request #37 from Modagbul/develop
Browse files Browse the repository at this point in the history
merge from develop to main
  • Loading branch information
minsu20 authored Oct 17, 2023
2 parents ed66604 + ef8fbb3 commit dc0926f
Show file tree
Hide file tree
Showing 21 changed files with 565 additions and 2 deletions.
16 changes: 16 additions & 0 deletions src/docs/asciidoc/Fire-API.adoc
Original file line number Diff line number Diff line change
@@ -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']

---

2 changes: 2 additions & 0 deletions src/docs/asciidoc/api.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ include::MissionArchive-API.adoc[]

include::MissionBoard-API.adoc[]

include::Fire-API.adoc[]

include::Board-API.adoc[]

include::BoardComment_API.adoc[]
Expand Down
Original file line number Diff line number Diff line change
@@ -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";
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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<FireReceiveRes> mapToFireReceiversList(List<Member> members) {
List<FireReceiveRes> 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();
}
}
Original file line number Diff line number Diff line change
@@ -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<FireReceiveRes> getFireReceiveList(String userId,Long teamId, Long missionId) {
Long memberId = memberGetService.getMemberBySocialId(userId).getMemberId();

List<FireReceiveRes> fireReceiveRes = FireMapper.mapToFireReceiversList(fireQueryService.getNotYetMissionMember(teamId, missionId));
fireReceiveRes.forEach(
res -> res.updateFireStatus(fireQueryService.hasFireCreatedWithinOneHour(memberId,res.getReceiveMemberId())
));

return fireReceiveRes;
}
}
Original file line number Diff line number Diff line change
@@ -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;

}
Original file line number Diff line number Diff line change
@@ -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<List<Member>> getFireReceivers(Long teamId, Long missionId);

}
Original file line number Diff line number Diff line change
@@ -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<List<Member>> 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());

}

}
Original file line number Diff line number Diff line change
@@ -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<Fire,Long>,FireCustomRepository {
}
Original file line number Diff line number Diff line change
@@ -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<Member> getNotYetMissionMember(Long teamId, Long missionId) {
return fireRepository.getFireReceivers(teamId, missionId).orElseThrow(NotFoundFireReceiversException::new);
}

}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Loading

0 comments on commit dc0926f

Please sign in to comment.