Skip to content

Commit

Permalink
Merge pull request #855 from bounswe/main
Browse files Browse the repository at this point in the history
Deploying latest changes
  • Loading branch information
halisbal authored Nov 26, 2023
2 parents 5bad6ea + ec95e63 commit 56a2d78
Show file tree
Hide file tree
Showing 14 changed files with 474 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ public ResponseEntity<Achievement> deleteAchievement(@RequestParam String id, @R
return ResponseEntity.ok(achievement);
}

@DeleteMapping("/delete-by-name")
@AuthorizationRequired
@AdminRequired
public ResponseEntity<Achievement> deleteAchievementByName(@RequestParam String achievementName, @RequestParam String gameName,
@RequestHeader String Authorization, HttpServletRequest request) {
Achievement achievement = achievementService.deleteAchievement(achievementName, gameName);
return ResponseEntity.ok(achievement);
}

@GetMapping("/get-game-achievements")
public ResponseEntity<List<Achievement>> getGameAchievements(@RequestParam String gameId) {
List<Achievement> gameAchievements = achievementService.getGameAchievements(gameId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,12 @@ public ResponseEntity<List<GetPostListResponseDto>> getPostList(@Valid @Paramete
return ResponseEntity.ok(posts);
}

@AuthorizationRequired
@GetMapping("/get-post-detail")
public ResponseEntity<GetPostDetailResponseDto> getPostDetail(@RequestParam String id, @RequestHeader String Authorization, HttpServletRequest request) {
User user = (User) request.getAttribute("authenticatedUser");
GetPostDetailResponseDto post = postService.getPostById(id, user);
public ResponseEntity<GetPostDetailResponseDto> getPostDetail(@RequestParam String id, @RequestHeader(name = HttpHeaders.AUTHORIZATION, required = false) String Authorization) {
String email;
if (JwtUtil.validateToken(Authorization)) email = JwtUtil.extractSubject(Authorization);
else email = "";
GetPostDetailResponseDto post = postService.getPostById(id, email);
return ResponseEntity.ok(post);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
package com.app.gamereview.controller;

import com.app.gamereview.dto.request.review.GetAllReviewsFilterRequestDto;
import com.app.gamereview.dto.request.profile.EditProfileRequestDto;
import com.app.gamereview.dto.request.profile.ProfileUpdateGameRequestDto;
import com.app.gamereview.dto.response.profile.GetLastActivitiesResponseDto;
import com.app.gamereview.dto.response.review.GetAllReviewsResponseDto;
import com.app.gamereview.dto.response.profile.ProfilePageResponseDto;
import com.app.gamereview.model.Profile;
import com.app.gamereview.model.User;
import com.app.gamereview.service.PostService;
import com.app.gamereview.service.ProfileService;
import com.app.gamereview.util.JwtUtil;
import com.app.gamereview.util.validation.annotation.AuthorizationRequired;
import jakarta.servlet.http.HttpServletRequest;
import org.springdoc.core.annotations.ParameterObject;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import java.util.List;

Expand All @@ -34,11 +32,46 @@ public ProfileController(ProfileService profileService) {

@AuthorizationRequired
@GetMapping("/get-activites")
public ResponseEntity<List<GetLastActivitiesResponseDto>> getReviews(@RequestHeader String Authorization, HttpServletRequest request) {
public ResponseEntity<List<GetLastActivitiesResponseDto>> getReviews(@RequestHeader String Authorization, HttpServletRequest request) {

User user = (User) request.getAttribute("authenticatedUser");

List<GetLastActivitiesResponseDto> reviews = profileService.getLastActivities(user);
return ResponseEntity.ok(reviews);
}

@PostMapping("/get")
public ResponseEntity<ProfilePageResponseDto> getProfile(@RequestParam String id, @RequestHeader(name = HttpHeaders.AUTHORIZATION, required = false) String Authorization) {
String email;
if (JwtUtil.validateToken(Authorization)) email = JwtUtil.extractSubject(Authorization);
else email = "";

ProfilePageResponseDto profile = profileService.getProfile(id, email);
return ResponseEntity.ok(profile);
}

@AuthorizationRequired
@PostMapping("/edit")
public ResponseEntity<Profile> editProfile(@RequestParam String id, @Valid @RequestBody EditProfileRequestDto profile, @RequestHeader String Authorization, HttpServletRequest request) {
User user = (User) request.getAttribute("authenticatedUser");
Profile editedProfile = profileService.editProfile(id, profile, user);
return ResponseEntity.ok(editedProfile);
}

@AuthorizationRequired
@PostMapping("/add-game")
public ResponseEntity<Profile> addGameToProfile(@RequestParam String id, @Valid @RequestBody ProfileUpdateGameRequestDto dto, @RequestHeader String Authorization, HttpServletRequest request) {
User user = (User) request.getAttribute("authenticatedUser");
Profile editedProfile = profileService.addGameToProfile(id, dto, user);
return ResponseEntity.ok(editedProfile);
}

@AuthorizationRequired
@PostMapping("/remove-game")
public ResponseEntity<Profile> removeGameFromProfile(@RequestParam String id, @Valid @RequestBody ProfileUpdateGameRequestDto dto, @RequestHeader String Authorization, HttpServletRequest request) {
User user = (User) request.getAttribute("authenticatedUser");
Profile editedProfile = profileService.removeGameFromProfile(id, dto, user);
return ResponseEntity.ok(editedProfile);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.app.gamereview.dto.request.profile;

import jakarta.validation.constraints.Size;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class EditProfileRequestDto {

@Size(min = 3, message = "Username must be at least 3 characters long")
@Size(max = 15, message = "Username must be at most 15 characters long")
private String username;

private Boolean isPrivate;

private String profilePhoto;

private String steamProfile;

private String epicGamesProfile;

private String xboxProfile;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.app.gamereview.dto.request.profile;

import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class ProfileUpdateGameRequestDto {
@NotNull(message = "Game Id cannot be null or empty.")
@Pattern(regexp = "^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$",
message = "Game has invalid Id (UUID) format")
private String game;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.app.gamereview.dto.response.profile;

import com.app.gamereview.dto.response.game.GameDetailResponseDto;
import com.app.gamereview.model.Achievement;
import com.app.gamereview.model.Review;
import com.app.gamereview.model.User;
import com.app.gamereview.model.Group;
import lombok.*;

import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public class ProfilePageResponseDto {

private String id;

private User user;

private List<Achievement> achievements;

private int reviewCount;

private int voteCount;

private int commentCount;

private int postCount;

private Boolean isReviewedYet;

private Boolean isVotedYet;

private Boolean isCommentedYet;

private Boolean isPostedYet;

private Boolean isPrivate;

private String profilePhoto;

private List<GameDetailResponseDto> games;

private List<Review> reviews;

private List<Group> groups;

private String steamProfile;

private String epicGamesProfile;

private String xboxProfile;
}
26 changes: 23 additions & 3 deletions app/backend/src/main/java/com/app/gamereview/model/Profile.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,31 @@ public class Profile extends BaseModel {

private Boolean isPostedYet = false;

// TODO add other fields and necessary logic (visit class diagram)
private Boolean isPrivate = false;

public void addAchievement(String achievementId){
if(!achievements.contains(achievementId)){
private String profilePhoto;

private List<String> games = new ArrayList<>(); // ids of games

private String steamProfile;

private String epicGamesProfile;

private String xboxProfile;

public void addAchievement(String achievementId) {
if (!achievements.contains(achievementId)) {
achievements.add(achievementId);
}
}

public void addGame(String gameId) {
if (!games.contains(gameId)) {
games.add(gameId);
}
}

public void removeGame(String gameId) {
games.remove(gameId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@

import com.app.gamereview.model.Group;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query;

import java.util.Optional;
import java.util.List;

public interface GroupRepository extends MongoRepository<Group, String> {
Optional<Group> findByIdAndIsDeletedFalse(String id);

Optional<Group> findByTitleAndIsDeletedFalse(String title);

@Query("{ 'members' : ?0, 'bannedMembers' : { $nin: [?0] }, 'isDeleted' : false }")
List<Group> findUserGroups(String userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@
import com.app.gamereview.model.Review;
import org.springframework.data.mongodb.repository.MongoRepository;

import java.util.List;

public interface ReviewRepository extends MongoRepository<Review, String> {
List<Review> findByReviewedBy(String reviewerId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@
import org.springframework.data.mongodb.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface UserRepository extends MongoRepository<User, String> {

Optional<User> findByUsernameAndIsDeletedFalse(String username);
Optional<User> findByUsernameAndIsDeletedFalse(String username);


Optional<User> findByEmailAndIsDeletedFalse(String email);
Optional<User> findByEmailAndIsDeletedFalse(String email);

Optional<User> findByIdAndIsDeletedFalse(String id);
Optional<User> findByIdAndIsDeletedFalse(String id);

@Query("{ 'email' : ?0 }")
Optional<User> findByEmail(String email);
@Query("{ 'email' : ?0 }")
Optional<User> findByEmail(String email);

Optional<User> findByUsername(String username);
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,38 @@ public Achievement deleteAchievement(String id) {
return achievementToDelete;
}

public Achievement deleteAchievement(String achievementName, String gameName) {
List<Achievement> achievementWithName = achievementRepository.findByTitleAndIsDeletedFalse(achievementName);

if (achievementWithName.isEmpty()) {
throw new ResourceNotFoundException("Achievement with the given name is not found.");
}

Optional<Game> gameWithName = gameRepository.findByGameNameAndIsDeletedFalse(gameName);

if (gameWithName.isEmpty()) {
throw new ResourceNotFoundException("Game with the given name is not found.");
}

Achievement achievementToDelete = null;

for (Achievement achievement : achievementWithName) {
Optional<Game> game = gameRepository.findByIdAndIsDeletedFalse(achievement.getGame());
if (game.isPresent() && game.get().getGameName().equals(gameName)) {
achievementToDelete = achievement;
}
}

if (achievementToDelete == null) {
throw new ResourceNotFoundException("There is no achievement with the given name in the game.");
}

achievementToDelete.setIsDeleted(true);

achievementRepository.save(achievementToDelete);
return achievementToDelete;
}

public List<String> grantAchievement(GrantAchievementRequestDto request) {
Optional<Achievement> achievementOptional = achievementRepository.findByIdAndIsDeletedFalse(request.getAchievementId());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,18 +129,21 @@ private GetPostListResponseDto mapToGetPostListResponseDto(Post post, String log
}


public GetPostDetailResponseDto getPostById(String id, User user) {
public GetPostDetailResponseDto getPostById(String id, String email) {
Optional<Post> post = postRepository.findById(id);
Optional<User> loggedInUser = userRepository.findByEmailAndIsDeletedFalse(email);
String loggedInUserId = loggedInUser.map(User::getId).orElse(null);

if (post.isEmpty()) {
throw new ResourceNotFoundException("The post with the given id was not found");
}
Optional<Forum> forum = forumRepository.findById(post.get().getForum());

if (forum.isPresent()) {
List<String> bannedUsers = forum.get().getBannedUsers();
if (bannedUsers.contains(user.getId())) {
throw new ResourceNotFoundException("You cannot see the post because you are banned.");
if(loggedInUserId != null){
List<String> bannedUsers = forum.get().getBannedUsers();
if (bannedUsers.contains(loggedInUserId)) {
throw new ResourceNotFoundException("You cannot see the post because you are banned.");
}
}
}

Expand Down
Loading

0 comments on commit 56a2d78

Please sign in to comment.