From da7d1776c4fed5155ad4a3e88d93478575ac2ae0 Mon Sep 17 00:00:00 2001 From: Can Date: Fri, 8 Dec 2023 11:49:02 +0300 Subject: [PATCH] delete game functionality is implemented --- .../gamereview/controller/GameController.java | 9 +++++ .../app/gamereview/service/GameService.java | 33 ++++++++++++------- 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/app/backend/src/main/java/com/app/gamereview/controller/GameController.java b/app/backend/src/main/java/com/app/gamereview/controller/GameController.java index c594758e..ada83eee 100644 --- a/app/backend/src/main/java/com/app/gamereview/controller/GameController.java +++ b/app/backend/src/main/java/com/app/gamereview/controller/GameController.java @@ -95,4 +95,13 @@ public ResponseEntity editGame(@RequestParam String id, Game updatedGame = gameService.editGame(id, updateGameRequestDto); return ResponseEntity.ok(updatedGame); } + + @AuthorizationRequired + @AdminRequired + @DeleteMapping("/delete") + public ResponseEntity deleteGame(@RequestParam String id, @RequestHeader String Authorization, + HttpServletRequest request) { + Boolean isDeleted = gameService.deleteGame(id); + return ResponseEntity.ok(isDeleted); + } } diff --git a/app/backend/src/main/java/com/app/gamereview/service/GameService.java b/app/backend/src/main/java/com/app/gamereview/service/GameService.java index cd9462d9..9116defb 100644 --- a/app/backend/src/main/java/com/app/gamereview/service/GameService.java +++ b/app/backend/src/main/java/com/app/gamereview/service/GameService.java @@ -1,12 +1,8 @@ package com.app.gamereview.service; -import java.util.ArrayList; -import java.util.List; -import java.util.Optional; -import java.util.stream.Collectors; - import com.app.gamereview.dto.request.game.*; -import com.app.gamereview.dto.response.group.GetGroupResponseDto; +import com.app.gamereview.dto.response.game.GameDetailResponseDto; +import com.app.gamereview.dto.response.game.GetGameListResponseDto; import com.app.gamereview.dto.response.tag.AddGameTagResponseDto; import com.app.gamereview.dto.response.tag.GetAllTagsOfGameResponseDto; import com.app.gamereview.enums.ForumType; @@ -14,9 +10,10 @@ import com.app.gamereview.exception.BadRequestException; import com.app.gamereview.exception.ResourceNotFoundException; import com.app.gamereview.model.Forum; -import com.app.gamereview.model.Group; +import com.app.gamereview.model.Game; import com.app.gamereview.model.Tag; import com.app.gamereview.repository.ForumRepository; +import com.app.gamereview.repository.GameRepository; import com.app.gamereview.repository.TagRepository; import org.modelmapper.ModelMapper; import org.modelmapper.PropertyMap; @@ -26,10 +23,10 @@ import org.springframework.data.mongodb.core.query.Query; import org.springframework.stereotype.Service; -import com.app.gamereview.dto.response.game.GetGameListResponseDto; -import com.app.gamereview.model.Game; -import com.app.gamereview.repository.GameRepository; -import com.app.gamereview.dto.response.game.GameDetailResponseDto; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; @Service public class GameService { @@ -345,5 +342,19 @@ public Game editGame(String id, UpdateGameRequestDto request){ return gameToUpdate; } + public Boolean deleteGame(String id){ + Optional findGame = gameRepository.findByIdAndIsDeletedFalse(id); + + if(findGame.isEmpty()){ + throw new ResourceNotFoundException("Game is not found"); + } + + Game gameToDelete = findGame.get(); + gameToDelete.setIsDeleted(true); + gameRepository.save(gameToDelete); + + return true; + } + }