Skip to content

Commit

Permalink
Delete games
Browse files Browse the repository at this point in the history
  • Loading branch information
sergio-octavio committed Feb 21, 2023
1 parent a910afa commit f6358ea
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 7 deletions.
7 changes: 5 additions & 2 deletions backend/src/main/java/app/controller/AddGameController.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.security.Principal;
import java.sql.SQLException;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;
Expand Down Expand Up @@ -77,8 +78,10 @@ public String editGame(Model model, @PathVariable long id) {

@GetMapping("/deleteGame/{id}")
public String deleteGame(Model model, @PathVariable long id) {
gameService.deleteById(id);
return "controlPanel";
Game game = gameService.findById(id).orElseThrow();
game.setDeleted(true);
gameService.save(game);
return "redirect:/controlPanel";
}

@PostMapping("/editGame/{id}")
Expand Down
6 changes: 6 additions & 0 deletions backend/src/main/java/app/model/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class Game {
private String network;
private String hardDrive;
private String soundCard;
private boolean deleted = false;

private int totalRating = 0;

Expand All @@ -58,6 +59,7 @@ public class Game {

@OneToMany(mappedBy = "game", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Review> reviews = new ArrayList<>();


public Game() {

Expand Down Expand Up @@ -94,6 +96,10 @@ public Game(String category, String name, float price, String os, String process
this.soundCard = soundCard;
}

public void setDeleted(boolean deleted){
this.deleted = deleted;
}


public Long getId() {
return this.id;
Expand Down
12 changes: 7 additions & 5 deletions backend/src/main/java/app/repository/GameRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,25 @@
import app.model.Game;

public interface GameRepository extends JpaRepository<Game, Long> {

@Query("SELECT g FROM Game g WHERE g.category = :category AND g.deleted = false")
List<Game> findByCategory(String category);

@Query("SELECT g FROM Game g WHERE LOWER(g.name) LIKE %:name% AND g.category = :category")
@Query("SELECT g FROM Game g WHERE LOWER(g.name) LIKE %:name% AND g.category = :category AND g.deleted = false")
List<Game> findByCategoryAndName(String name, String category, Pageable pageable);

@Query("SELECT g FROM Game g WHERE LOWER(g.name) LIKE %:name%")
@Query("SELECT g FROM Game g WHERE LOWER(g.name) LIKE %:name% AND g.deleted = false")
List<Game> findByName(String name, Pageable pageable);

@Query("SELECT COUNT(g) FROM Game g WHERE LOWER(g.name) LIKE %:name% AND g.category = :category")
@Query("SELECT COUNT(g) FROM Game g WHERE LOWER(g.name) LIKE %:name% AND g.category = :category AND g.deleted = false")
int countByCategoryAndName(String name, String category);

@Query("SELECT COUNT(g) FROM Game g WHERE LOWER(g.name) LIKE %:name%")
@Query("SELECT COUNT(g) FROM Game g WHERE LOWER(g.name) LIKE %:name% AND g.deleted = false")
int countByName(String name);

int countById(long id);

@Query("SELECT g FROM Game g")
@Query("SELECT g FROM Game g WHERE g.deleted = false")
List<Game> findGames(Pageable pageable);
}

Expand Down
7 changes: 7 additions & 0 deletions backend/src/main/resources/templates/product-info.html
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,14 @@ <h1>{{name}}</h1>
</a></div>
{{/admin}}
<br>
{{^deleted}}
<h2><span class="label label-default">${{price}}</span></h2>
{{/deleted}}
{{#deleted}}
<div class="alert" role="alert">
<strong>This game is no longer available</strong>
</div>
{{/deleted}}
{{#logged}}
{{^inCart}}
{{^isBought}}
Expand Down
7 changes: 7 additions & 0 deletions backend/target/classes/templates/product-info.html
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,14 @@ <h1>{{name}}</h1>
</a></div>
{{/admin}}
<br>
{{^deleted}}
<h2><span class="label label-default">${{price}}</span></h2>
{{/deleted}}
{{#deleted}}
<div class="alert" role="alert">
<strong>This game is no longer available</strong>
</div>
{{/deleted}}
{{#logged}}
{{^inCart}}
{{^isBought}}
Expand Down

0 comments on commit f6358ea

Please sign in to comment.