Games tab on admin page working

This commit is contained in:
2025-03-04 21:14:22 -05:00
parent dd4480cf4e
commit f528cbaa2b
25 changed files with 631 additions and 25 deletions

View File

@@ -0,0 +1,113 @@
package com.mattrixwv.raidbuilder.service;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import com.mattrixwv.raidbuilder.entity.Game;
import com.mattrixwv.raidbuilder.repository.game.GameRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
@RequiredArgsConstructor
public class GameService{
private final GameRepository gameRepository;
@Value("${uploadFileDirectory}")
private String uploadFileDirectory;
//Write
public Game createGame(Game game, MultipartFile file){
if(file != null){
String fileName = UUID.randomUUID().toString() + "--" + file.getOriginalFilename();
Path filePath = Paths.get(uploadFileDirectory + "/gameIcons").resolve(fileName);
try{
file.transferTo(filePath);
game.setGameIcon(fileName);
}
catch(Exception error){
log.error("Error uploading file: " + error.getMessage(), error);
throw new RuntimeException("Error uploading file: " + error.getMessage(), error);
}
}
return gameRepository.save(game);
}
public Game updateGame(Game game, MultipartFile file){
Game existingGame = gameRepository.findById(game.getGameId()).orElse(null);
//Delete the old file if one exists
if((existingGame != null) && (existingGame.getGameIcon() != null) && (game.getGameIcon() == null)){
log.debug("Deleting old file: {}", existingGame.getGameIcon());
File existingFile = new File(uploadFileDirectory + "/gameIcons/" + existingGame.getGameIcon());
if(existingFile.exists()){
existingFile.delete();
}
}
if(file != null){
//Upload the new file
String fileName = UUID.randomUUID().toString() + "--" + file.getOriginalFilename();
Path filePath = Paths.get(uploadFileDirectory + "/gameIcons").resolve(fileName);
try{
file.transferTo(filePath);
game.setGameIcon(fileName);
}
catch(Exception error){
log.error("Error uploading file: " + error.getMessage(), error);
throw new RuntimeException("Error uploading file: " + error.getMessage(), error);
}
}
return gameRepository.save(game);
}
public void deleteById(UUID gameId){
Game game = gameRepository.findById(gameId).orElse(null);
if(game != null){
if(game.getGameIcon() != null){
File existingFile = new File(uploadFileDirectory + "/gameIcons/" + game.getGameIcon());
if(existingFile.exists()){
existingFile.delete();
}
}
gameRepository.deleteById(gameId);
}
}
//Read
public List<Game> getGames(int page, int pageSize){
return gameRepository.findAll(PageRequest.of(page, pageSize, Sort.by("gameName").ascending())).getContent();
}
public List<Game> getGames(int page, int pageSize, String searchTerm){
return gameRepository.findAllByGameNameContainingIgnoreCase(
searchTerm,
PageRequest.of(page, pageSize, Sort.by("gameName").ascending())
);
}
public long getGamesCount(){
return gameRepository.count();
}
public long getGamesCount(String searchTerm){
return gameRepository.countAllByGameNameContainingIgnoreCase(searchTerm);
}
}