Add services and controllers for games #12

Merged
Egamorf merged 7 commits from release/add-services-controllers into main 2025-04-12 21:34:45 +02:00
11 changed files with 84 additions and 37 deletions
Showing only changes of commit 797484525b - Show all commits

View File

@@ -9,10 +9,10 @@
<PageTitle>@ResourcesKey.GamesIdeas</PageTitle>
<GamesHeader>
<GameHeader>
<GameFilter @bind-DisplayType=DisplayType
@bind-GameFilterParams=GameFilterParams />
</GamesHeader>
</GameHeader>
<div class="container">
<div class="content">

View File

@@ -3,7 +3,7 @@ using GameIdeas.BlazorApp.Shared.Models;
namespace GameIdeas.BlazorApp.Pages.Games;
public partial class GamesBase ()
public partial class GameBase ()
{
private DisplayType DisplayType = DisplayType.List;
private GameFilterParams GameFilterParams = new();

View File

@@ -6,7 +6,7 @@ using Microsoft.AspNetCore.Components;
namespace GameIdeas.BlazorApp.Pages.Games.Header;
public partial class GamesHeader : ComponentBase
public partial class GameHeader : ComponentBase
{
[Parameter] public EventCallback<AddType> AddTypeChanged { get; set; }
[Parameter] public RenderFragment? ChildContent { get; set; }

View File

@@ -13,6 +13,4 @@ public class GameFilterDto
public IEnumerable<int>? DeveloperIds { get; set; }
public IEnumerable<int>? CreationUserIds { get; set; }
public IEnumerable<int>? ModificationUserIds { get; set; }
public int CurrentPage { get; set; }
public int NumberPerPage { get; set; }
}

View File

@@ -0,0 +1,7 @@
namespace GameIdeas.Shared.Dto;
public class PaggingDto
{
public int CurrentPage { get; set; }
public int NumberPerPage { get; set; }
}

View File

@@ -0,0 +1,7 @@
namespace GameIdeas.Shared.Exceptions;
public class NotFoundException : Exception
{
public NotFoundException(string msg, Exception? innerException = null) : base(msg, innerException)
{ }
}

View File

@@ -12,6 +12,6 @@ public class GameController(GameService gameService) : Controller
[HttpGet("Search")]
public async Task<IEnumerable<GameDto>> FetchGames([FromQuery] GameFilterDto filter)
{
return await gameService.SearchGames(filter);
return await gameService.GetGames(filter);
}
}

View File

@@ -1,6 +1,6 @@
using AutoMapper;
using GameIdeas.Shared.Dto;
using GameIdeas.Shared.Enum;
using GameIdeas.Shared.Exceptions;
using GameIdeas.Shared.Model;
using GameIdeas.WebAPI.Context;
using Microsoft.EntityFrameworkCore;
@@ -9,7 +9,7 @@ namespace GameIdeas.WebAPI.Services;
public class GameService(GameIdeasContext context, IMapper mapper)
{
public async Task<IEnumerable<GameDto>> SearchGames(GameFilterDto filter)
public async Task<IEnumerable<GameDto>> GetGames(PaggingDto pagging)
{
var query = context.Games
.Include(g => g.CreationUser)
@@ -21,40 +21,75 @@ public class GameService(GameIdeasContext context, IMapper mapper)
.Include(g => g.GameDevelopers).ThenInclude(p => p.Developer)
.AsQueryable();
ApplyFilter(ref query);
var games = await query
.OrderBy(g => g.Title)
.Skip(filter.CurrentPage * filter.NumberPerPage)
.Take(filter.NumberPerPage)
.Skip(pagging.CurrentPage * pagging.NumberPerPage)
.Take(pagging.NumberPerPage)
.ToListAsync();
List<Game> gamesTest = [ new ()
{
Id = 1,
Title = "Test",
CreationDate = DateTime.Today - TimeSpan.FromHours(-12),
CreationUser = new User() { Id = 1, Username = "Test", Role = (int)Role.Administrator },
CreationUserId = 1,
Description = "Test",
GameDevelopers = [ new() { Developer = new Developer() { Id = 1, Name = "THQNordic"} }],
GamePlatforms = [ new() { Platform = new Platform() { Id = 1, Label = "Steam" }, Url = "steam.com?app=55554131" }],
GameProperties = [ new () { Property = new Property() { Id = 1, Label = "Coop"} }],
GamePublishers = [ new () { Publisher = new Publisher() { Id = 1, Name = "Take-Two"} }],
GameTags = [ new () { Tag = new Tag() { Id = 1, Label = "RPG" } }],
Interest = 5,
ModificationDate = DateTime.Today ,
ModificationUser = new User() { Id = 2, Username = "Test 2", Role = (int)Role.Member },
ModificationUserId = 2,
ReleaseDate = new DateTime(2025, 2, 1),
StorageSpace = 40
} ];
return mapper.Map<IEnumerable<GameDto>>(gamesTest);
return mapper.Map<IEnumerable<GameDto>>(games);
}
private void ApplyFilter(ref IQueryable<Game> query)
public async Task<GameDto> GetGameById(int gameId)
{
var game = await context.Games
.Include(g => g.CreationUser)
.Include(g => g.ModificationUser)
.Include(g => g.GamePlatforms).ThenInclude(p => p.Platform)
.Include(g => g.GameProperties).ThenInclude(p => p.Property)
.Include(g => g.GameTags).ThenInclude(p => p.Tag)
.Include(g => g.GamePublishers).ThenInclude(p => p.Publisher)
.Include(g => g.GameDevelopers).ThenInclude(p => p.Developer)
.FirstOrDefaultAsync(g => g.Id == gameId);
if (game == null)
{
throw new NotFoundException($"[{typeof(Game).Name}] with ID {gameId} has not been found in context");
}
return mapper.Map<GameDto>(game);
}
public async Task<GameDto> CreateGame(GameDto gameDto)
{
var game = mapper.Map<Game>(gameDto);
context.GameDevelopers.AttachRange(game.GameDevelopers);
context.Developers.AttachRange(game.GameDevelopers.Select(gd => gd.Developer));
context.GamePlatforms.AttachRange(game.GamePlatforms);
context.Platforms.AttachRange(game.GamePlatforms.Select(gp => gp.Platform));
context.GameProperties.AttachRange(game.GameProperties);
context.Properties.AttachRange(game.GameProperties.Select(gp => gp.Property));
context.GamePublishers.AttachRange(game.GamePublishers);
context.Publishers.AttachRange(game.GamePublishers.Select(gp => gp.Publisher));
context.GameTags.AttachRange(game.GameTags);
context.Tags.AttachRange(game.GameTags.Select(gt => gt.Tag));
await context.Games.AddAsync(game);
await context.SaveChangesAsync();
return mapper.Map<GameDto>(game);
}
public async Task<GameDto> UpdateGame(GameDto gameDto)
{
var gameToUpdate = mapper.Map<Game>(gameDto);
context.GameDevelopers.AttachRange(gameToUpdate.GameDevelopers);
context.Developers.AttachRange(gameToUpdate.GameDevelopers.Select(gd => gd.Developer));
context.GamePlatforms.AttachRange(gameToUpdate.GamePlatforms);
context.Platforms.AttachRange(gameToUpdate.GamePlatforms.Select(gp => gp.Platform));
context.GameProperties.AttachRange(gameToUpdate.GameProperties);
context.Properties.AttachRange(gameToUpdate.GameProperties.Select(gp => gp.Property));
context.GamePublishers.AttachRange(gameToUpdate.GamePublishers);
context.Publishers.AttachRange(gameToUpdate.GamePublishers.Select(gp => gp.Publisher));
context.GameTags.AttachRange(gameToUpdate.GameTags);
context.Tags.AttachRange(gameToUpdate.GameTags.Select(gt => gt.Tag));
context.Games.Update(gameToUpdate);
await context.SaveChangesAsync();
return mapper.Map<GameDto>(gameToUpdate);
}
}