Skip to content

Commit

Permalink
Merge pull request #828 from bounswe/backend/group-join-status
Browse files Browse the repository at this point in the history
group/get and get-all join status return
  • Loading branch information
zeynep-baydemir authored Nov 23, 2023
2 parents e165f98 + d97d424 commit 41e09f3
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
import com.app.gamereview.model.Group;
import com.app.gamereview.model.User;
import com.app.gamereview.service.GroupService;
import com.app.gamereview.service.ReviewService;
import com.app.gamereview.util.JwtUtil;
import com.app.gamereview.util.validation.annotation.AdminRequired;
import com.app.gamereview.util.validation.annotation.AuthorizationRequired;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
Expand All @@ -26,9 +25,6 @@
@Validated
public class GroupController {

@Value("${SECRET_KEY}")
private String secret_key = "${SECRET_KEY}";

private final GroupService groupService;

@Autowired
Expand All @@ -40,14 +36,25 @@ public GroupController(

@GetMapping("/get-all")
public ResponseEntity<List<GetGroupResponseDto>> getReviews(
@ParameterObject GetAllGroupsFilterRequestDto filter) {
List<GetGroupResponseDto> groups = groupService.getAllGroups(filter);
@ParameterObject GetAllGroupsFilterRequestDto filter, @RequestHeader(name = HttpHeaders.AUTHORIZATION, required = false) String Authorization) {

String email;
if (JwtUtil.validateToken(Authorization)) email = JwtUtil.extractSubject(Authorization);
else email = "";

List<GetGroupResponseDto> groups = groupService.getAllGroups(filter, email);
return ResponseEntity.ok(groups);
}

@GetMapping("/get")
public ResponseEntity<GetGroupResponseDto> getGroup(@RequestParam String id) {
GetGroupResponseDto group = groupService.getGroupById(id);
public ResponseEntity<GetGroupResponseDto> getGroup(
@RequestParam String id, @RequestHeader(name = HttpHeaders.AUTHORIZATION, required = false) String Authorization) {

String email;
if (JwtUtil.validateToken(Authorization)) email = JwtUtil.extractSubject(Authorization);
else email = "";

GetGroupResponseDto group = groupService.getGroupById(id, email);

return ResponseEntity.ok(group);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
package com.app.gamereview.controller;

import java.util.Date;
import java.util.List;

import com.app.gamereview.dto.request.post.EditPostRequestDto;
import com.app.gamereview.dto.response.comment.GetPostCommentsResponseDto;
import com.app.gamereview.dto.response.post.GetPostDetailResponseDto;
import com.app.gamereview.model.User;
import com.app.gamereview.util.JwtUtil;
import com.app.gamereview.util.validation.annotation.AuthorizationRequired;
import io.jsonwebtoken.*;
import jakarta.servlet.http.HttpServletRequest;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
Expand All @@ -31,8 +29,6 @@
@Validated
public class PostController {

@Value("${SECRET_KEY}")
private String secret_key = "${SECRET_KEY}";

private final PostService postService;

Expand All @@ -44,23 +40,9 @@ public PostController(PostService postService) {
@GetMapping("/get-post-list")
public ResponseEntity<List<GetPostListResponseDto>> getPostList(@Valid @ParameterObject GetPostListFilterRequestDto filter, @RequestHeader(name = HttpHeaders.AUTHORIZATION, required = false) String Authorization) {

String email = "";
try {
Claims claims = Jwts.parser().setSigningKey(secret_key).parseClaimsJws(String.valueOf(Authorization)).getBody();

Date expirationDate = claims.getExpiration();
Date now = new Date();
if (expirationDate.after(now)) {
email = claims.getSubject();
}
}
catch (SignatureException | ExpiredJwtException | IllegalArgumentException | MalformedJwtException e) {
// SignatureException: Token signature is invalid
// ExpiredJwtException: Token has expired
// IllegalArgumentException: Token is not correctly formatted
// MalformedJwtException: Token is not correctly formatted (eg. empty string)
// In any of these cases, the token is considered invalid
}
String email;
if (JwtUtil.validateToken(Authorization)) email = JwtUtil.extractSubject(Authorization);
else email = "";

List<GetPostListResponseDto> posts = postService.getPostList(filter, email);
return ResponseEntity.ok(posts);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,24 @@
import com.app.gamereview.model.Review;
import com.app.gamereview.model.User;
import com.app.gamereview.service.ReviewService;
import com.app.gamereview.util.JwtUtil;
import com.app.gamereview.util.validation.annotation.AuthorizationRequired;
import io.jsonwebtoken.*;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/review")
@Validated
public class ReviewController {

@Value("${SECRET_KEY}")
private String secret_key = "${SECRET_KEY}";

private final ReviewService reviewService;

@Autowired
Expand All @@ -44,23 +39,9 @@ public ResponseEntity<List<GetAllReviewsResponseDto>> getReviews(
@ParameterObject GetAllReviewsFilterRequestDto filter,
@RequestHeader(name = HttpHeaders.AUTHORIZATION, required = false) String Authorization) {

String email = "";
try {
Claims claims = Jwts.parser().setSigningKey(secret_key).parseClaimsJws(String.valueOf(Authorization)).getBody();

Date expirationDate = claims.getExpiration();
Date now = new Date();
if (expirationDate.after(now)) {
email = claims.getSubject();
}
}
catch (SignatureException | ExpiredJwtException | IllegalArgumentException | MalformedJwtException e) {
// SignatureException: Token signature is invalid
// ExpiredJwtException: Token has expired
// IllegalArgumentException: Token is not correctly formatted
// MalformedJwtException: Token is not correctly formatted (eg. empty string)
// In any of these cases, the token is considered invalid
}
String email;
if (JwtUtil.validateToken(Authorization)) email = JwtUtil.extractSubject(Authorization);
else email = "";

List<GetAllReviewsResponseDto> reviews = reviewService.getAllReviews(filter, email);
return ResponseEntity.ok(reviews);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public class GetGroupResponseDto {

private LocalDateTime createdAt;

private Boolean userJoined;

public void populateTag(Tag tag){
this.tags.add(tag);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.app.gamereview.service;

import com.app.gamereview.dto.request.group.*;
import com.app.gamereview.dto.request.review.CreateReviewRequestDto;
import com.app.gamereview.dto.response.group.GetGroupResponseDto;
import com.app.gamereview.dto.response.tag.AddGroupTagResponseDto;
import com.app.gamereview.enums.*;
Expand All @@ -10,7 +9,6 @@
import com.app.gamereview.model.*;
import com.app.gamereview.repository.*;
import com.app.gamereview.util.UtilExtensions;
import com.app.gamereview.util.validation.annotation.ValidMemberPolicy;
import org.modelmapper.ModelMapper;
import org.modelmapper.PropertyMap;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -33,6 +31,8 @@ public class GroupService {

private final TagRepository tagRepository;

private final UserRepository userRepository;

private final MongoTemplate mongoTemplate;

private final ModelMapper modelMapper;
Expand All @@ -43,13 +43,14 @@ public GroupService(
GameRepository gameRepository,
ForumRepository forumRepository,
TagRepository tagRepository,
MongoTemplate mongoTemplate,
UserRepository userRepository, MongoTemplate mongoTemplate,
ModelMapper modelMapper
) {
this.groupRepository = groupRepository;
this.gameRepository = gameRepository;
this.forumRepository = forumRepository;
this.tagRepository = tagRepository;
this.userRepository = userRepository;
this.mongoTemplate = mongoTemplate;
this.modelMapper = modelMapper;

Expand All @@ -69,7 +70,11 @@ protected void configure() {
});
}

public List<GetGroupResponseDto> getAllGroups(GetAllGroupsFilterRequestDto filter){
public List<GetGroupResponseDto> getAllGroups(GetAllGroupsFilterRequestDto filter, String email){

Optional<User> loggedInUser = userRepository.findByEmailAndIsDeletedFalse(email);
String loggedInUserId = loggedInUser.map(User::getId).orElse(null);

Query query = new Query();

// search for title
Expand Down Expand Up @@ -116,13 +121,19 @@ else if (filter.getSortBy().equals(SortType.QUOTA.name())) {
tag.ifPresent(dto::populateTag);
}

dto.setUserJoined(group.getMembers().contains(loggedInUserId));

responseDtos.add(dto);
}

return responseDtos;
}

public GetGroupResponseDto getGroupById(String groupId){
public GetGroupResponseDto getGroupById(String groupId, String email){

Optional<User> loggedInUser = userRepository.findByEmailAndIsDeletedFalse(email);
String loggedInUserId = loggedInUser.map(User::getId).orElse(null);

Optional<Group> isGroupExists = groupRepository.findByIdAndIsDeletedFalse(groupId);

if(isGroupExists.isEmpty()){
Expand All @@ -136,6 +147,8 @@ public GetGroupResponseDto getGroupById(String groupId){
tag.ifPresent(dto::populateTag);
}

dto.setUserJoined(group.getMembers().contains(loggedInUserId));

return dto;
}

Expand Down

0 comments on commit 41e09f3

Please sign in to comment.