Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/feat/security_filter_login' into…
Browse files Browse the repository at this point in the history
… dev-check
  • Loading branch information
nohy6630 committed Feb 28, 2024
2 parents 91a1c17 + ae3954f commit d9c0a7c
Show file tree
Hide file tree
Showing 69 changed files with 869 additions and 873 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group = 'com.numberone.backend'
version = '0.0.1-SNAPSHOT'
version = '0.0.2-SNAPSHOT'

allprojects {
apply plugin: 'java'
Expand All @@ -19,7 +19,7 @@ allprojects {
}

ext {
set('springCloudVersion', "2021.0.1")
set('springCloudVersion', "2022.0.5")
}

subprojects {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ public class ArticleService {

@Transactional
public UploadArticleResponse uploadArticle(UploadArticleRequest request) {
String principal = SecurityContextProvider.getAuthenticatedUserEmail();
Member owner = memberRepository.findByEmail(principal)
long id = SecurityContextProvider.getAuthenticatedUserId();
Member owner = memberRepository.findById(id)
.orElseThrow(NotFoundMemberException::new);

// 1. 게시글 생성 ( 제목, 내용, 작성자 아이디, 태그)
Expand Down Expand Up @@ -148,8 +148,8 @@ public DeleteArticleResponse deleteArticle(Long articleId) {
}

public GetArticleDetailResponse getArticleDetail(Long articleId) {
String principal = SecurityContextProvider.getAuthenticatedUserEmail();
Member member = memberRepository.findByEmail(principal) // 회원
long id = SecurityContextProvider.getAuthenticatedUserId();
Member member = memberRepository.findById(id) // 회원
.orElseThrow(NotFoundMemberException::new);
Article article = articleRepository.findById(articleId)
.orElseThrow(NotFoundArticleException::new);
Expand Down Expand Up @@ -179,8 +179,8 @@ public GetArticleDetailResponse getArticleDetail(Long articleId) {
}

public Slice<GetArticleListResponse> getArticleListPaging(ArticleSearchParameter param, Pageable pageable) {
String principal = SecurityContextProvider.getAuthenticatedUserEmail();
Member member = memberRepository.findByEmail(principal)
long id = SecurityContextProvider.getAuthenticatedUserId();
Member member = memberRepository.findById(id)
.orElseThrow(NotFoundMemberException::new);
List<Long> memberLikedArticleIdList = articleLikeRepository.findByMember(member)
.stream().map(ArticleLike::getArticleId)
Expand All @@ -207,8 +207,8 @@ public void updateArticleInfo(GetArticleListResponse articleInfo, List<Long> mem

@Transactional
public CreateCommentResponse createComment(Long articleId, CreateCommentRequest request) {
String principal = SecurityContextProvider.getAuthenticatedUserEmail();
Member member = memberRepository.findByEmail(principal)
long id = SecurityContextProvider.getAuthenticatedUserId();
Member member = memberRepository.findById(id)
.orElseThrow(NotFoundMemberException::new);
Article article = articleRepository.findById(articleId)
.orElseThrow(NotFoundArticleException::new);
Expand Down Expand Up @@ -236,8 +236,8 @@ public CreateCommentResponse createComment(Long articleId, CreateCommentRequest

@Transactional
public ModifyArticleResponse modifyArticle(Long articleId, ModifyArticleRequest request) {
String principal = SecurityContextProvider.getAuthenticatedUserEmail();
Member member = memberRepository.findByEmail(principal)
long id = SecurityContextProvider.getAuthenticatedUserId();
Member member = memberRepository.findById(id)
.orElseThrow(NotFoundMemberException::new);
Article article = articleRepository.findById(articleId)
.orElseThrow(NotFoundArticleException::new);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ public CreateChildCommentResponse createChildComment(
Long parentCommentId,
CreateChildCommentRequest request) {

String principal = SecurityContextProvider.getAuthenticatedUserEmail();
Member member = memberRepository.findByEmail(principal)
long principal = SecurityContextProvider.getAuthenticatedUserId();
Member member = memberRepository.findById(principal)
.orElseThrow(NotFoundMemberException::new);
Article article = articleRepository.findById(articleId)
.orElseThrow(NotFoundArticleException::new);
Expand Down Expand Up @@ -78,8 +78,8 @@ public CreateChildCommentResponse createChildComment(
}

public List<GetCommentDto> getCommentsByArticle(Long articleId) {
String principal = SecurityContextProvider.getAuthenticatedUserEmail();
Member member = memberRepository.findByEmail(principal)
long principal = SecurityContextProvider.getAuthenticatedUserId();
Member member = memberRepository.findById(principal)
.orElseThrow(NotFoundMemberException::new);
Article article = articleRepository.findById(articleId)
.orElseThrow(NotFoundArticleException::new);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ public void delete(@PathVariable Long conversationId) {
""")
@PostMapping("/like/{conversationId}")
public void increaseLike(Authentication authentication, @PathVariable Long conversationId) {
conversationService.increaseLike(authentication.getName(), conversationId);
conversationService.increaseLike(conversationId);
}

@Operation(summary = "대화 좋아요 취소하기", description = """
사용자가 대화의 좋아요를 취소할 때 대화 id를 파라미터로 전달해주세요.
""")
@DeleteMapping("/like/{conversationId}")
public void decreaseLike(Authentication authentication, @PathVariable Long conversationId) {
conversationService.decreaseLike(authentication.getName(), conversationId);
public void decreaseLike(@PathVariable Long conversationId) {
conversationService.decreaseLike(conversationId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.numberone.backend.exception.conflict.AlreadyUnLikedException;
import com.numberone.backend.exception.notfound.NotFoundConversationException;
import com.numberone.backend.exception.notfound.NotFoundDisasterException;
import com.numberone.backend.provider.security.SecurityContextProvider;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -33,7 +34,8 @@ public class ConversationService {

@Transactional
public void createConversation(String email, CreateConversationRequest createConversationRequest) {
Member member = memberService.findByEmail(email);
long id = SecurityContextProvider.getAuthenticatedUserId();
Member member = memberService.findById(id);
Disaster disaster = disasterRepository.findById(createConversationRequest.getDisasterId())
.orElseThrow(NotFoundDisasterException::new);
Conversation conversation = Conversation.of(
Expand All @@ -46,7 +48,8 @@ public void createConversation(String email, CreateConversationRequest createCon

@Transactional
public void createChildConversation(String email, CreateChildConversationRequest createConversationRequest, Long conversationId) {
Member member = memberService.findByEmail(email);
long id = SecurityContextProvider.getAuthenticatedUserId();
Member member = memberService.findById(id);
Conversation parent = conversationRepository.findById(conversationId)
.orElseThrow(NotFoundConversationException::new);
Conversation conversation = Conversation.childOf(
Expand All @@ -73,10 +76,11 @@ private boolean checkLike(Member member, Conversation conversation) {
return false;
}

public GetConversationResponse get(String email, Long conversationId) {
public GetConversationResponse get(Long conversationId) {
Conversation conversation = conversationRepository.findById(conversationId)
.orElseThrow(NotFoundConversationException::new);
Member member = memberService.findByEmail(email);
long id = SecurityContextProvider.getAuthenticatedUserId();
Member member = memberService.findById(id);
List<GetConversationResponse> childs = new ArrayList<>();
List<Conversation> childConversations = conversationRepository.findAllByParentOrderByCreatedAt(conversation);
for (Conversation child : childConversations) {
Expand All @@ -94,10 +98,11 @@ public GetConversationResponse get(String email, Long conversationId) {
childs);
}

public GetConversationResponse getExceptChild(String email, Long conversationId) {
public GetConversationResponse getExceptChild(Long conversationId) {
Conversation conversation = conversationRepository.findById(conversationId)
.orElseThrow(NotFoundConversationException::new);
Member member = memberService.findByEmail(email);
long id = SecurityContextProvider.getAuthenticatedUserId();
Member member = memberService.findById(id);
return GetConversationResponse.of(
conversation,
checkLike(member, conversation),
Expand All @@ -106,8 +111,9 @@ public GetConversationResponse getExceptChild(String email, Long conversationId)
}

@Transactional
public void increaseLike(String email, Long conversationId) {
Member member = memberService.findByEmail(email);
public void increaseLike(Long conversationId) {
long id = SecurityContextProvider.getAuthenticatedUserId();
Member member = memberService.findById(id);
Conversation conversation = conversationRepository.findById(conversationId)
.orElseThrow(NotFoundConversationException::new);
conversationLikeRepository.findByMemberAndConversation(member, conversation)
Expand All @@ -120,8 +126,9 @@ public void increaseLike(String email, Long conversationId) {
}

@Transactional
public void decreaseLike(String email, Long conversationId) {
Member member = memberService.findByEmail(email);
public void decreaseLike(Long conversationId) {
long id = SecurityContextProvider.getAuthenticatedUserId();
Member member = memberService.findById(id);
Conversation conversation = conversationRepository.findById(conversationId)
.orElseThrow(NotFoundConversationException::new);
ConversationLike conversationLike = conversationLikeRepository.findByMemberAndConversation(member, conversation)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ public class DisasterController {
유저가 등록한 지역은 유저가 인증을 위해 같이 보내야하는 jwt 토큰으로부터 알아서 추출해서 처리할 것입니다.
""")
@PostMapping("/latest")
public ResponseEntity<LatestDisasterResponse> getLatestDisaster(Authentication authentication, @Valid @RequestBody LatestDisasterRequest latestDisasterRequest) {
return ResponseEntity.ok(disasterService.getLatestDisaster(authentication.getName(), latestDisasterRequest));
public ResponseEntity<LatestDisasterResponse> getLatestDisaster(@Valid @RequestBody LatestDisasterRequest latestDisasterRequest) {
return ResponseEntity.ok(disasterService.getLatestDisaster(latestDisasterRequest));
}

@Operation(summary = "재난상황 커뮤니티 데이터 가져오기", description = """
재난상황 페이지에서 필요한 재난목록과 그와 관련된 대화(댓글)들을 가져옵니다.
""")
@GetMapping("/situation")
public ResponseEntity<SituationHomeResponse> getSituationHome(Authentication authentication) {
return ResponseEntity.ok(disasterService.getSituationHome(authentication.getName()));
public ResponseEntity<SituationHomeResponse> getSituationHome() {
return ResponseEntity.ok(disasterService.getSituationHome());
}

@Operation(summary = "해당 재난과 관련된 모든 커뮤니티 대화 가져오기", description = """
Expand All @@ -47,7 +47,7 @@ public ResponseEntity<SituationHomeResponse> getSituationHome(Authentication aut
커뮤니티-재난상황-댓글더보기 페이지에서 사용하는 API입니다.
""")
@GetMapping("/{sort}/{disasterId}")
public ResponseEntity<SituationDetailResponse> getSituationDetail(Authentication authentication, @PathVariable Long disasterId, @PathVariable String sort) {
return ResponseEntity.ok(disasterService.getSituationDetail(authentication.getName(), disasterId, sort));
public ResponseEntity<SituationDetailResponse> getSituationDetail(@PathVariable Long disasterId, @PathVariable String sort) {
return ResponseEntity.ok(disasterService.getSituationDetail(disasterId, sort));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.numberone.backend.exception.badrequest.BadRequestConversationSortException;
import com.numberone.backend.exception.notfound.NotFoundDisasterException;
import com.numberone.backend.provider.location.LocationProvider;
import com.numberone.backend.provider.security.SecurityContextProvider;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEventPublisher;
Expand All @@ -45,11 +46,12 @@ public class DisasterService {
private final ApplicationEventPublisher eventPublisher;

@Transactional
public LatestDisasterResponse getLatestDisaster(String email, LatestDisasterRequest latestDisasterRequest) {
public LatestDisasterResponse getLatestDisaster(LatestDisasterRequest latestDisasterRequest) {
String address = locationProvider.pos2address(latestDisasterRequest.getLatitude(), latestDisasterRequest.getLongitude());
LocalDateTime time = LocalDateTime.now().minusDays(1);
Set<Disaster> disasters = new HashSet<>(disasterRepository.findDisastersInAddressAfterTime(address, time));
Member member = memberService.findByEmail(email);
long id = SecurityContextProvider.getAuthenticatedUserId();
Member member = memberService.findById(id);
member.updateGps(latestDisasterRequest.getLatitude(), latestDisasterRequest.getLongitude(), address);
String[] locationTokens = member.getLocation().split(" ");
switch (locationTokens.length) {
Expand Down Expand Up @@ -109,9 +111,10 @@ private boolean isValidDisasterType(DisasterType disasterType, List<Notification
return false;
}

public SituationHomeResponse getSituationHome(String email) {
public SituationHomeResponse getSituationHome() {
Set<Disaster> disasters = new HashSet<>();
Member member = memberService.findByEmail(email);
long id = SecurityContextProvider.getAuthenticatedUserId();
Member member = memberService.findById(id);
LocalDateTime time = LocalDateTime.now().minusDays(1);
for (NotificationRegion notificationRegion : member.getNotificationRegions()) {
disasters.addAll(disasterRepository.findDisastersInAddressAfterTime(notificationRegion.getLocation(), time));
Expand All @@ -126,7 +129,7 @@ public SituationHomeResponse getSituationHome(String email) {
conversationCnt += conversationRepository.countByDisaster(disaster);
List<Conversation> conversations = conversationRepository.findAllByDisasterOrderByLikeCntDesc(disaster, PageRequest.of(0, 3));
for (Conversation conversation : conversations) {
conversationResponses.add(conversationService.getExceptChild(email, conversation.getId()));
conversationResponses.add(conversationService.getExceptChild(conversation.getId()));
conversationCnt += conversationRepository.countByParent(conversation);
}
situationResponses.add(SituationResponse.of(disaster, conversationResponses, conversationCnt));
Expand All @@ -135,7 +138,7 @@ public SituationHomeResponse getSituationHome(String email) {
return SituationHomeResponse.of(situationResponses);
}

public SituationDetailResponse getSituationDetail(String email, Long disasterId, String sort) {
public SituationDetailResponse getSituationDetail(Long disasterId, String sort) {
Disaster disaster = disasterRepository.findById(disasterId)
.orElseThrow(NotFoundDisasterException::new);
List<GetConversationResponse> conversationResponses = new ArrayList<>();
Expand All @@ -147,7 +150,7 @@ else if (sort.equals("time"))
else
throw new BadRequestConversationSortException();
for (Conversation conversation : conversations) {
conversationResponses.add(conversationService.get(email, conversation.getId()));
conversationResponses.add(conversationService.get(conversation.getId()));
}
return SituationDetailResponse.of(conversationResponses);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public class FriendshipService {

@Transactional
public InviteFriendResponse inviteFriend(Long memberId) {
String principal = SecurityContextProvider.getAuthenticatedUserEmail();
Member invitedMember = memberRepository.findByEmail(principal)
long id = SecurityContextProvider.getAuthenticatedUserId();
Member invitedMember = memberRepository.findById(id)
.orElseThrow(NotFoundMemberException::new);
Member member = memberRepository.findById(memberId)
.orElseThrow(NotFoundMemberException::new);
Expand All @@ -55,8 +55,8 @@ public InviteFriendResponse inviteFriend(Long memberId) {
}

public List<FriendResponse> getFriends() {
String principal = SecurityContextProvider.getAuthenticatedUserEmail();
Member member = memberRepository.findByEmail(principal)
long id = SecurityContextProvider.getAuthenticatedUserId();
Member member = memberRepository.findById(id)
.orElseThrow(NotFoundMemberException::new);
return member.getFriendships()
.stream().map(friendship -> {
Expand All @@ -67,8 +67,8 @@ public List<FriendResponse> getFriends() {

@Transactional
public FriendResponse deleteFriend(Long memberId) {
String principal = SecurityContextProvider.getAuthenticatedUserEmail();
Member me = memberRepository.findByEmail(principal)
long id = SecurityContextProvider.getAuthenticatedUserId();
Member me = memberRepository.findById(id)
.orElseThrow(NotFoundMemberException::new);
Member friend = memberRepository.findById(memberId)
.orElseThrow(NotFoundMemberException::new);
Expand All @@ -83,8 +83,8 @@ public FriendResponse deleteFriend(Long memberId) {

@Transactional
public SendFcmFriendResponse sendFcmToFriend(Long friendId) {
String principal = SecurityContextProvider.getAuthenticatedUserEmail();
Member member = memberRepository.findByEmail(principal)
long id = SecurityContextProvider.getAuthenticatedUserId();
Member member = memberRepository.findById(id)
.orElseThrow(NotFoundMemberException::new);
Member friend = memberRepository.findById(friendId)
.orElseThrow(NotFoundMemberException::new);
Expand Down
Loading

0 comments on commit d9c0a7c

Please sign in to comment.