Add Create and Update
Some checks failed
Game Ideas build for PR / build_blazor_app (pull_request) Failing after 1m3s
Some checks failed
Game Ideas build for PR / build_blazor_app (pull_request) Failing after 1m3s
This commit is contained in:
@@ -9,10 +9,10 @@
|
|||||||
|
|
||||||
<PageTitle>@ResourcesKey.GamesIdeas</PageTitle>
|
<PageTitle>@ResourcesKey.GamesIdeas</PageTitle>
|
||||||
|
|
||||||
<GamesHeader>
|
<GameHeader>
|
||||||
<GameFilter @bind-DisplayType=DisplayType
|
<GameFilter @bind-DisplayType=DisplayType
|
||||||
@bind-GameFilterParams=GameFilterParams />
|
@bind-GameFilterParams=GameFilterParams />
|
||||||
</GamesHeader>
|
</GameHeader>
|
||||||
|
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<div class="content">
|
<div class="content">
|
||||||
@@ -3,7 +3,7 @@ using GameIdeas.BlazorApp.Shared.Models;
|
|||||||
|
|
||||||
namespace GameIdeas.BlazorApp.Pages.Games;
|
namespace GameIdeas.BlazorApp.Pages.Games;
|
||||||
|
|
||||||
public partial class GamesBase ()
|
public partial class GameBase ()
|
||||||
{
|
{
|
||||||
private DisplayType DisplayType = DisplayType.List;
|
private DisplayType DisplayType = DisplayType.List;
|
||||||
private GameFilterParams GameFilterParams = new();
|
private GameFilterParams GameFilterParams = new();
|
||||||
@@ -6,7 +6,7 @@ using Microsoft.AspNetCore.Components;
|
|||||||
|
|
||||||
namespace GameIdeas.BlazorApp.Pages.Games.Header;
|
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 EventCallback<AddType> AddTypeChanged { get; set; }
|
||||||
[Parameter] public RenderFragment? ChildContent { get; set; }
|
[Parameter] public RenderFragment? ChildContent { get; set; }
|
||||||
@@ -13,6 +13,4 @@ public class GameFilterDto
|
|||||||
public IEnumerable<int>? DeveloperIds { get; set; }
|
public IEnumerable<int>? DeveloperIds { get; set; }
|
||||||
public IEnumerable<int>? CreationUserIds { get; set; }
|
public IEnumerable<int>? CreationUserIds { get; set; }
|
||||||
public IEnumerable<int>? ModificationUserIds { get; set; }
|
public IEnumerable<int>? ModificationUserIds { get; set; }
|
||||||
public int CurrentPage { get; set; }
|
|
||||||
public int NumberPerPage { get; set; }
|
|
||||||
}
|
}
|
||||||
|
|||||||
7
src/GameIdeas/GameIdeas.Shared/Dto/PaggingDto.cs
Normal file
7
src/GameIdeas/GameIdeas.Shared/Dto/PaggingDto.cs
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
namespace GameIdeas.Shared.Dto;
|
||||||
|
|
||||||
|
public class PaggingDto
|
||||||
|
{
|
||||||
|
public int CurrentPage { get; set; }
|
||||||
|
public int NumberPerPage { get; set; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
namespace GameIdeas.Shared.Exceptions;
|
||||||
|
|
||||||
|
public class NotFoundException : Exception
|
||||||
|
{
|
||||||
|
public NotFoundException(string msg, Exception? innerException = null) : base(msg, innerException)
|
||||||
|
{ }
|
||||||
|
}
|
||||||
@@ -12,6 +12,6 @@ public class GameController(GameService gameService) : Controller
|
|||||||
[HttpGet("Search")]
|
[HttpGet("Search")]
|
||||||
public async Task<IEnumerable<GameDto>> FetchGames([FromQuery] GameFilterDto filter)
|
public async Task<IEnumerable<GameDto>> FetchGames([FromQuery] GameFilterDto filter)
|
||||||
{
|
{
|
||||||
return await gameService.SearchGames(filter);
|
return await gameService.GetGames(filter);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
using AutoMapper;
|
using AutoMapper;
|
||||||
using GameIdeas.Shared.Dto;
|
using GameIdeas.Shared.Dto;
|
||||||
using GameIdeas.Shared.Enum;
|
using GameIdeas.Shared.Exceptions;
|
||||||
using GameIdeas.Shared.Model;
|
using GameIdeas.Shared.Model;
|
||||||
using GameIdeas.WebAPI.Context;
|
using GameIdeas.WebAPI.Context;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
@@ -9,7 +9,7 @@ namespace GameIdeas.WebAPI.Services;
|
|||||||
|
|
||||||
public class GameService(GameIdeasContext context, IMapper mapper)
|
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
|
var query = context.Games
|
||||||
.Include(g => g.CreationUser)
|
.Include(g => g.CreationUser)
|
||||||
@@ -21,40 +21,75 @@ public class GameService(GameIdeasContext context, IMapper mapper)
|
|||||||
.Include(g => g.GameDevelopers).ThenInclude(p => p.Developer)
|
.Include(g => g.GameDevelopers).ThenInclude(p => p.Developer)
|
||||||
.AsQueryable();
|
.AsQueryable();
|
||||||
|
|
||||||
ApplyFilter(ref query);
|
|
||||||
|
|
||||||
var games = await query
|
var games = await query
|
||||||
.OrderBy(g => g.Title)
|
.OrderBy(g => g.Title)
|
||||||
.Skip(filter.CurrentPage * filter.NumberPerPage)
|
.Skip(pagging.CurrentPage * pagging.NumberPerPage)
|
||||||
.Take(filter.NumberPerPage)
|
.Take(pagging.NumberPerPage)
|
||||||
.ToListAsync();
|
.ToListAsync();
|
||||||
|
|
||||||
List<Game> gamesTest = [ new ()
|
return mapper.Map<IEnumerable<GameDto>>(games);
|
||||||
{
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user