Files
RaidBuilderAPI/src/main/java/com/mattrixwv/raidbuilder/service/GameService.java
2025-03-06 22:32:04 -05:00

131 lines
3.9 KiB
Java

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;
//Related Services
private final GameCalendarEventService gameCalendarEventService;
private final GameClassService gameClassService;
private final GamePermissionService gamePermissionService;
private final RaidGroupService raidGroupService;
//Values
@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();
}
}
gameCalendarEventService.deleteByGameId(gameId);
gameClassService.deleteByGameId(gameId);
gamePermissionService.deleteByGameId(gameId);
raidGroupService.deleteByGameId(gameId);
gameRepository.flush();
gameRepository.deleteById(gameId);
}
}
//Read
public Game getGameById(UUID gameId){
return gameRepository.findById(gameId).orElse(null);
}
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);
}
}