Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat(#85): in out api 수정 #92

Merged
merged 5 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/inandout/backend/config/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// TODO: 공유 URL만 경로 모든 권한 허용해주기
// 모든 권한 허용
.requestMatchers("/login", "/", "/join", "/healthcheck", "/regenerate-token", "/find-password", "/logout",
"/auth/verify", "/kakaologin/callback", "/in", "/out", "/myroom", "/others/room").permitAll()
"/auth/verify", "/kakaologin/callback", "/inout", "/myroom", "/others/room").permitAll()
// "ADMIN"이라는 권한을 가진 사용자만 접근 가능
.requestMatchers("/admin").hasRole("ADMIN")
// 로그인 한 사용자만 접근 가능, 즉 Header에 Authorization이 있는 경로만 허용
Expand Down
37 changes: 7 additions & 30 deletions src/main/java/inandout/backend/controller/post/PostController.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
package inandout.backend.controller.post;

import inandout.backend.chat.stomp.StompChatRoomRepository;
import inandout.backend.common.response.BaseResponse;
import inandout.backend.dto.myroom.MyRoomRequestDTO;
import inandout.backend.dto.myroom.MyRoomResponseDTO;
import inandout.backend.dto.myroom.PostResponseDTO;
import inandout.backend.dto.others.InOutRequestDTO;
import inandout.backend.entity.post.Post;
import inandout.backend.dto.post.InOutRequestDTO;
import inandout.backend.dto.post.InOutResponseDTO;
import inandout.backend.service.post.PostService;
import inandout.backend.service.stuff.StuffService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
Expand All @@ -28,30 +21,14 @@ public class PostController {
@Autowired
public StuffService stuffService;

@PostMapping("/in")
public BaseResponse<Integer> inController(@RequestBody InOutRequestDTO inOutRequestDTO) throws Exception {
log.info("in");
@PostMapping("/inout")
public BaseResponse<InOutResponseDTO> inOutController(@RequestBody InOutRequestDTO inOutRequestDTO) throws Exception {
log.info("in/out");

//inout 테이블에 저장
stuffService.saveIn(inOutRequestDTO);
//post 테이블에서 in 증가
Integer newInCount = postService.plusInCount(inOutRequestDTO.getPostId());
InOutResponseDTO inOutResponseDTO = stuffService.saveInOut(inOutRequestDTO);


return new BaseResponse<>(newInCount);
}

@PostMapping("/out")
public BaseResponse<Integer> outController(@RequestBody InOutRequestDTO inOutRequestDTO) throws Exception {
log.info("out");
// HttpSession session = request.getSession();
//inout 테이블에 저장
stuffService.saveOut(inOutRequestDTO);
//post 테이블에서 in 증가
Integer newOutCount = postService.plusOutCount(inOutRequestDTO.getPostId());


return new BaseResponse<>(newOutCount);
return new BaseResponse<>(inOutResponseDTO);
}

}
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package inandout.backend.dto.others;
package inandout.backend.dto.post;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class InOutRequestDTO {
private Integer postId;
private Boolean isMember;
private Integer memberId;
private Boolean in;
private Boolean out;
}
14 changes: 14 additions & 0 deletions src/main/java/inandout/backend/dto/post/InOutResponseDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package inandout.backend.dto.post;

import lombok.Getter;

@Getter
public class InOutResponseDTO {
private int inCount;
private int outCount;

public InOutResponseDTO(Integer inCount, Integer outCount) {
this.inCount = inCount;
this.outCount = outCount;
}
}
8 changes: 8 additions & 0 deletions src/main/java/inandout/backend/entity/post/InOut.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,12 @@ public InOut(boolean isCheckIn, boolean isCheckOut, boolean isMember, Post post,
this.post = post;
this.member = member;
}

public void updateIn(boolean in) {
this.isCheckIn = in;
}

public void updateOut(boolean out) {
this.isCheckOut = out;
}
}
8 changes: 8 additions & 0 deletions src/main/java/inandout/backend/entity/post/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,12 @@ public Post(Member member, String title, String outContent, String inContent, in
// this.chats = chats;
this.chatRoom = chatRoom;
}

public void updateOutCount(int outCount) {
this.outCount = outCount;
}

public void updateInCount(int inCount) {
this.inCount = inCount;
}
}
2 changes: 1 addition & 1 deletion src/main/java/inandout/backend/jwt/JWTFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
String authorization = request.getHeader("Authorization");

if (request.getRequestURI().equals("/myroom") || request.getRequestURI().equals("/others/room")
|| request.getRequestURI().equals("/in") || request.getRequestURI().equals("out")) {
|| request.getRequestURI().equals("/inout")) {
if (authorization == null) {
filterChain.doFilter(request, response);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,19 @@ public void save(InOut inOut) {
em.persist(inOut);
}


public List getIsCheckedInfo(Integer memberId, Integer postId) {

public InOut getIsCheckedInfo(int memberId, int postId) {
// 투표했는지 여부 체크
List resultList= em.createQuery("SELECT io.isCheckIn, io.isCheckOut FROM InOut io WHERE member.id = :member_id AND post.id = :post_id")
.setParameter("member_id", memberId).setParameter("post_id", postId).getResultList();
InOut inout = em.createQuery("select io from InOut io where io.member.id = :member_id and io.post.id = :post_id", InOut.class)
.setParameter("member_id", memberId)
.setParameter("post_id", postId)
.getSingleResult();

return resultList;
return inout;
}

public Boolean getExistMember(int memberId) {
return em.createQuery("select count(io) > 0 from InOut io where io.member.id = :member_id", Boolean.class)
.setParameter("member_id", memberId)
.getSingleResult();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public TokenInfo reissue(String refreshToken) {
// redis에 duration 저장하여 만료시간 설정
// email찾고 accessToken, refreshToken 생성
TokenInfo tokenInfo = jwtUtil.generateToken(email);
redisService.setValues(tokenInfo.getRefreshToken(), email, Duration.ofMillis(refreshTokenValidTime));
redisService.setValues(tokenInfo.getRefreshToken(), email);

log.info(redisService.getEmail(tokenInfo.getRefreshToken()));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,6 @@ public void setValues(String key, String value) {
values.set(key, value);
}

public void setValues(String key, String value, Duration duration) {
System.out.println("RedisService/setValues");
ValueOperations<String, Object> values = redisTemplate.opsForValue();
values.set(key, value, duration);
}

public String getRefreshToken(String email){
return (String) redisTemplate.opsForValue().get(email);
}
Expand Down
33 changes: 21 additions & 12 deletions src/main/java/inandout/backend/service/post/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import inandout.backend.dto.chat.ChatResponseDTO;
import inandout.backend.dto.myroom.PostResponseDTO;
import inandout.backend.entity.post.InOut;
import inandout.backend.entity.post.Post;
import inandout.backend.repository.chat.ChatRepository;
import inandout.backend.repository.post.InOutRepository;
Expand Down Expand Up @@ -49,23 +50,31 @@ public PostResponseDTO getPost(Integer memberId, Integer postId) {
String outContent = post.get().getOutContent();

//in/out 선택했는지 여부 가져오기
List<?> isCheckedInfo = inOutRepository.getIsCheckedInfo(memberId, postId);
InOut isCheckedInfo = inOutRepository.getIsCheckedInfo(memberId, postId);
boolean isCheckedIn = false;
boolean isCheckedOut = false;

for (Object o : isCheckedInfo) {
Object[] result = (Object[]) o;
boolean resultIn = (boolean) result[0];
boolean resultOut = (boolean) result[1];

if (resultIn) {
isCheckedIn = resultIn;
}
if (resultOut) {
isCheckedOut = resultOut;
}
if (isCheckedInfo.isCheckIn()) {
isCheckedIn = true;
}

if (isCheckedInfo.isCheckOut()) {
isCheckedOut = true;
}
//
// for (Object o : isCheckedInfo) {
// Object[] result = (Object[]) o;
// boolean resultIn = (boolean) result[0];
// boolean resultOut = (boolean) result[1];
//
// if (resultIn) {
// isCheckedIn = resultIn;
// }
// if (resultOut) {
// isCheckedOut = resultOut;
// }
// }

// 상위 5개 가져오기(임의)
List<ChatResponseDTO> chatResponseDTOS = chatService.getPostChat(memberId, postId);
List<ChatResponseDTO> chatResponseDTOList;
Expand Down
131 changes: 80 additions & 51 deletions src/main/java/inandout/backend/service/stuff/StuffService.java
Original file line number Diff line number Diff line change
@@ -1,77 +1,106 @@
package inandout.backend.service.stuff;

import inandout.backend.common.exception.MemberException;
import inandout.backend.dto.others.InOutRequestDTO;
import inandout.backend.dto.post.InOutRequestDTO;
import inandout.backend.dto.post.InOutResponseDTO;
import inandout.backend.entity.member.Member;
import inandout.backend.entity.post.InOut;
import inandout.backend.entity.post.Post;
import inandout.backend.repository.login.MemberRepository;
import inandout.backend.repository.post.InOutRepository;
import inandout.backend.repository.post.PostRepository;
import inandout.backend.validator.MemberValidator;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;
import java.util.Optional;

import static inandout.backend.common.response.status.BaseExceptionResponseStatus.NOT_FOUND_MEMBER;

@Service
@RequiredArgsConstructor
@Transactional
public class StuffService {
@Autowired
public InOutRepository inOutRepository;
@Autowired
public PostRepository postRepository;
@Autowired
public MemberRepository memberRepository;
private final InOutRepository inOutRepository;
private final PostRepository postRepository;
private final MemberRepository memberRepository;

public void saveIn(InOutRequestDTO inOutRequestDTO) throws Exception {
System.out.println("saveIn");
System.out.println(inOutRequestDTO.getIsMember());
public InOutResponseDTO saveInOut(InOutRequestDTO inOutRequestDTO) {
Post post = postRepository.getPostByPostId(inOutRequestDTO.getPostId());

Optional<Member> member;
if (inOutRequestDTO.getIsMember()) { // 회원이 누른거면
System.out.println("회원임");
try {
member = memberRepository.findById(inOutRequestDTO.getMemberId());
} catch (Exception e) {
member = null;
throw new MemberException(NOT_FOUND_MEMBER);
Member member = memberRepository.findById(inOutRequestDTO.getMemberId()).get();

// 회원이면 이전에 어떤걸 선택했었는지 확인, 비회원이면 로직 건너 뜀
if (inOutRequestDTO.getIsMember() && inOutRepository.getExistMember(inOutRequestDTO.getMemberId())) {
// 이전에 선택한 정보가 있으면 InOut DB에 있는 정보 update
InOut inOut = inOutRepository.getIsCheckedInfo(inOutRequestDTO.getMemberId(), inOutRequestDTO.getPostId());
if (!inOut.isCheckIn() && !inOut.isCheckOut()) {
if (inOutRequestDTO.getIn() && !inOutRequestDTO.getOut()) {
// false false → true false → in +1
inOut.updateIn(true);
inOut.updateOut(false);
post.updateInCount(post.getInCount()+1);
} else if (!inOutRequestDTO.getIn() && inOutRequestDTO.getOut()) {
// false false → false true → out +1
inOut.updateIn(false);
inOut.updateOut(true);
post.updateOutCount(post.getOutCount()+1);
}
} else if (inOut.isCheckIn() && !inOut.isCheckOut()) {
if (!inOutRequestDTO.getIn() && !inOutRequestDTO.getOut()) {
// true false → false false → in -1
inOut.updateIn(false);
inOut.updateOut(false);
post.updateInCount(post.getInCount()-1);
} else if (!inOutRequestDTO.getIn() && inOutRequestDTO.getOut()) {
// true false → false true → in -1, out +1
inOut.updateIn(false);
inOut.updateOut(true);
post.updateInCount(post.getInCount()-1);
post.updateOutCount(post.getOutCount()+1);
}
} else if (!inOut.isCheckIn()) {
if (!inOutRequestDTO.getIn() && !inOutRequestDTO.getOut()) {
// false true → false false 이면 → out -1
inOut.updateIn(false);
inOut.updateOut(false);
post.updateOutCount(post.getOutCount()-1);
} else if (inOutRequestDTO.getIn() && !inOutRequestDTO.getOut()) {
// false true → true false 이면 → out -1, in +1
inOut.updateIn(true);
inOut.updateOut(false);
post.updateOutCount(post.getOutCount()-1);
post.updateInCount(post.getInCount()+1);
}
}

}else{
member = null;
}

InOut inOut = new InOut(true, false, inOutRequestDTO.getIsMember(), post, member.get());

inOutRepository.save(inOut);

}

public void saveOut(InOutRequestDTO inOutRequestDTO) throws Exception {
System.out.println("saveOut");
System.out.println(inOutRequestDTO.getIsMember());
Post post = postRepository.getPostByPostId(inOutRequestDTO.getPostId());

Optional<Member> member;
if (inOutRequestDTO.getIsMember()) { // 회원이 누른거면
System.out.println("회원임");
try {
member = memberRepository.findById(inOutRequestDTO.getMemberId());
} catch (Exception e) {
member = null;
throw new MemberException(NOT_FOUND_MEMBER);
} else if (inOutRequestDTO.getIsMember() && !inOutRepository.getExistMember(inOutRequestDTO.getMemberId())) {
// 이전에 선택한 정보가 없으면 새로 넣음
if (inOutRequestDTO.getIn() && !inOutRequestDTO.getOut()) {
// false false → true false → in +1
InOut newInOut = new InOut(true, false, inOutRequestDTO.getIsMember(), post, member);
inOutRepository.save(newInOut);
post.updateInCount(post.getInCount()+1);
} else if (!inOutRequestDTO.getIn() && inOutRequestDTO.getOut()) {
// false false → false true → out +1
InOut newInOut = new InOut(false, true, inOutRequestDTO.getIsMember(), post, member);
inOutRepository.save(newInOut);
post.updateOutCount(post.getOutCount()+1);
}

}else{
member = null;
} else if (!inOutRequestDTO.getIsMember()){
// 비회원일때
// if (inOutRequestDTO.getIn() && !inOutRequestDTO.getOut()) {
// // false false → true false → in +1
// post.updateInCount(post.getInCount()+1);
// } else if (!inOutRequestDTO.getIn() && inOutRequestDTO.getOut()) {
// // false false → false true → out +1
// post.updateOutCount(post.getOutCount()+1);
// } else if (inOutRequestDTO.getIn() && !inOutRequestDTO.getOut()) {
// post.updateOutCount(post.getOutCount()+1);
// }
}

InOut inOut = new InOut(false, true, inOutRequestDTO.getIsMember(), post, member.get());

inOutRepository.save(inOut);

return new InOutResponseDTO(post.getInCount(), post.getOutCount());
}

}
Loading