Skip to content

Commit

Permalink
v3.9.4 (#203)
Browse files Browse the repository at this point in the history
* Refactor - move SimpleEntity into Gameboard.Api.Features.Common.

* Don't bootstrap JobsService in integration test env

* Address an issue where Gameboard was incorrectly evaluating whether a challenge has a deployed gamespace after destroy. Resolves #182.

* Server-side hardening for illegal mime type upload. Removed bare repositories.
Updated recommended extensions.

* Add improved file upload validation. Add editor config to suppress private member warnings.

* Update unit tests

* Remove server-side html escaping of user input (handled client side).
  • Loading branch information
sei-bstein authored Jun 14, 2023
1 parent da39012 commit 4792200
Show file tree
Hide file tree
Showing 38 changed files with 575 additions and 662 deletions.
2 changes: 2 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[*.{cs,vb}]
dotnet_diagnostic.CA1822.severity = none
7 changes: 4 additions & 3 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"recommendations": [
"formulahendry.dotnet-test-explorer",
"ms-azuretools.vscode-docker"
"ms-azuretools.vscode-docker",
"ms-dotnettools.csharp",
"ms-dotnettools.csdevkit"
]
}
}
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"dotnet.defaultSolution": "Gameboard.sln"
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using AutoMapper;
using Gameboard.Api;
using Gameboard.Api.Services;

namespace Gameboard.Api.Tests.Unit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ string userId
A.Fake<IMapper>(),
A.Fake<CoreOptions>(),
A.Fake<IChallengeStore>(),
A.Fake<IChallengeSpecStore>(),
A.Fake<IStore<Data.ChallengeSpec>>(),
fakeGameEngineService,
A.Fake<IGameStore>(),
A.Fake<IGuidService>(),
Expand Down
1 change: 0 additions & 1 deletion src/Gameboard.Api/Data/Entities/Ticket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace Gameboard.Api.Data
Expand Down
147 changes: 74 additions & 73 deletions src/Gameboard.Api/Data/Store/Store[TEntity].cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,108 +6,109 @@
using System.Linq;
using System.Threading.Tasks;
using Gameboard.Api.Data.Abstractions;
using Gameboard.Api.Services;
using Microsoft.EntityFrameworkCore;

namespace Gameboard.Api.Data
namespace Gameboard.Api.Data;

public class Store<TEntity> : IStore<TEntity> where TEntity : class, IEntity
{
public class Store<TEntity> : IStore<TEntity>
where TEntity : class, IEntity
private readonly IGuidService _guids;

public Store(IGuidService guids, GameboardDbContext dbContext)
{
public Store(GameboardDbContext dbContext)
{
DbContext = dbContext;
DbSet = dbContext.Set<TEntity>().AsQueryable();
}
DbContext = dbContext;
DbSet = dbContext.Set<TEntity>().AsQueryable();
_guids = guids;
}

public GameboardDbContext DbContext { get; private set; }
public IQueryable<TEntity> DbSet { get; private set; }
public GameboardDbContext DbContext { get; private set; }
public IQueryable<TEntity> DbSet { get; private set; }

public virtual IQueryable<TEntity> List(string term = null)
{
return DbContext.Set<TEntity>();
}
public virtual IQueryable<TEntity> List(string term = null)
{
return DbContext.Set<TEntity>();
}

public virtual async Task<TEntity> Create(TEntity entity)
{
if (string.IsNullOrWhiteSpace(entity.Id))
entity.Id = Guid.NewGuid().ToString("n");
public virtual async Task<TEntity> Create(TEntity entity)
{
if (string.IsNullOrWhiteSpace(entity.Id))
entity.Id = _guids.GetGuid();

DbContext.Add(entity);
DbContext.Add(entity);
await DbContext.SaveChangesAsync();

await DbContext.SaveChangesAsync();
return entity;
}

return entity;
}
public virtual async Task<IEnumerable<TEntity>> Create(IEnumerable<TEntity> range)
{
foreach (var entity in range)
if (string.IsNullOrWhiteSpace(entity.Id))
entity.Id = Guid.NewGuid().ToString("n");

public virtual async Task<IEnumerable<TEntity>> Create(IEnumerable<TEntity> range)
{
foreach (var entity in range)
if (string.IsNullOrWhiteSpace(entity.Id))
entity.Id = Guid.NewGuid().ToString("n");
DbContext.AddRange(range);

DbContext.AddRange(range);
await DbContext.SaveChangesAsync();

await DbContext.SaveChangesAsync();
return range;
}

return range;
}
public virtual async Task<bool> Exists(string id)
{
return (await List().CountAsync(e => e.Id == id)) > 0;
}

public virtual async Task<bool> Exists(string id)
public virtual Task<TEntity> Retrieve(string id)
=> Retrieve(id, null);

public virtual async Task<TEntity> Retrieve(string id, Func<IQueryable<TEntity>, IQueryable<TEntity>> includes)
{
if (includes != null)
{
return (await List().CountAsync(e => e.Id == id)) > 0;
var query = includes(DbContext.Set<TEntity>());
return await query
.FirstOrDefaultAsync(e => e.Id == id);
}

public virtual Task<TEntity> Retrieve(string id)
=> Retrieve(id, null);
return await DbContext.Set<TEntity>().FindAsync(id);
}

public virtual async Task<TEntity> Retrieve(string id, Func<IQueryable<TEntity>, IQueryable<TEntity>> includes)
{
if (includes != null)
{
var query = includes(DbContext.Set<TEntity>());
return await query
.FirstOrDefaultAsync(e => e.Id == id);
}

return await DbContext.Set<TEntity>().FindAsync(id);
}
public virtual async Task Update(TEntity entity)
{
DbContext.Update(entity);

public virtual async Task Update(TEntity entity)
{
DbContext.Update(entity);
await DbContext.SaveChangesAsync();
}

await DbContext.SaveChangesAsync();
}
public virtual async Task Update(IEnumerable<TEntity> range)
{
DbContext.UpdateRange(range);

public virtual async Task Update(IEnumerable<TEntity> range)
{
DbContext.UpdateRange(range);
await DbContext.SaveChangesAsync();
}

await DbContext.SaveChangesAsync();
}
public virtual async Task Delete(string id)
{
var entity = await DbContext.Set<TEntity>().FindAsync(id);

public virtual async Task Delete(string id)
if (entity is TEntity)
{
var entity = await DbContext.Set<TEntity>().FindAsync(id);

if (entity is TEntity)
{
DbContext.Set<TEntity>().Remove(entity);
DbContext.Set<TEntity>().Remove(entity);

await DbContext.SaveChangesAsync();
}
await DbContext.SaveChangesAsync();
}
}

public virtual async Task<int> CountAsync(Func<IQueryable<TEntity>, IQueryable<TEntity>> queryBuilder = null)
{
var query = DbContext.Set<TEntity>().AsNoTracking();

if (queryBuilder != null)
{
query = queryBuilder(query);
}
public virtual async Task<int> CountAsync(Func<IQueryable<TEntity>, IQueryable<TEntity>> queryBuilder = null)
{
var query = DbContext.Set<TEntity>().AsNoTracking();

return await query.CountAsync();
if (queryBuilder != null)
{
query = queryBuilder(query);
}

return await query.CountAsync();
}
}
4 changes: 2 additions & 2 deletions src/Gameboard.Api/Features/Challenge/ChallengeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ public class ChallengeService : _Service
private readonly IMapper _mapper;
private readonly INowService _now;
private readonly IPlayerStore _playerStore;
private readonly IChallengeSpecStore _specStore;
private readonly IStore<Data.ChallengeSpec> _specStore;

public ChallengeService(
ILogger<ChallengeService> logger,
IMapper mapper,
CoreOptions options,
IChallengeStore store,
IChallengeSpecStore specStore,
IStore<Data.ChallengeSpec> specStore,
IGameEngineService gameEngine,
IGameStore gameStore,
IGuidService guids,
Expand Down
7 changes: 5 additions & 2 deletions src/Gameboard.Api/Features/Challenge/ChallengeStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,24 @@
using Gameboard.Api.Data.Abstractions;
using AutoMapper;
using Microsoft.EntityFrameworkCore;
using Gameboard.Api.Services;
using System;
using Gameboard.Api.Services;

namespace Gameboard.Api.Data;

public class ChallengeStore : Store<Challenge>, IChallengeStore
{
private readonly IGuidService _guids;
private readonly GameboardDbContext _dbContext;
private readonly IMapper _mapper;

public ChallengeStore(
IGuidService guids,
GameboardDbContext dbContext,
IMapper mapper) : base(dbContext)
IMapper mapper) : base(guids, dbContext)
{
_dbContext = dbContext;
_guids = guids;
_mapper = mapper;
}

Expand Down
12 changes: 6 additions & 6 deletions src/Gameboard.Api/Features/ChallengeGate/ChallengeGateService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@

namespace Gameboard.Api.Services
{
public class ChallengeGateService: _Service
public class ChallengeGateService : _Service
{
IChallengeGateStore Store { get; }
IStore<Data.ChallengeGate> Store { get; }

public ChallengeGateService (
public ChallengeGateService(
ILogger<ChallengeGateService> logger,
IMapper mapper,
CoreOptions options,
IChallengeGateStore store
): base(logger, mapper, options)
IStore<Data.ChallengeGate> store
) : base(logger, mapper, options)
{
Store = store;
}
Expand Down Expand Up @@ -69,7 +69,7 @@ public async Task Delete(string id)
internal async Task<ChallengeGate[]> List(string id)
{
if (id.IsEmpty())
return new ChallengeGate[]{};
return new ChallengeGate[] { };

return
await Mapper.ProjectTo<ChallengeGate>(
Expand Down
38 changes: 0 additions & 38 deletions src/Gameboard.Api/Features/ChallengeGate/ChallengeGateStore.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@

using System.Threading.Tasks;
using Gameboard.Api.Data.Abstractions;
using Gameboard.Api.Validators;

namespace Gameboard.Api.ChallengeGates;

public class ChallengeGateValidator : IModelValidator
{
private readonly IChallengeGateStore _store;
private readonly IStore<Data.ChallengeGate> _store;

public ChallengeGateValidator(IChallengeGateStore store)
public ChallengeGateValidator(IStore<Data.ChallengeGate> store)
{
_store = store;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ namespace Gameboard.Api.Services
{
public class ChallengeSpecService : _Service
{
IChallengeSpecStore Store { get; }
IStore<Data.ChallengeSpec> Store { get; }
GameEngineService GameEngine { get; }

public ChallengeSpecService(
ILogger<ChallengeSpecService> logger,
IMapper mapper,
CoreOptions options,
IChallengeSpecStore store,
IStore<Data.ChallengeSpec> store,
GameEngineService gameEngine
) : base(logger, mapper, options)
{
Expand Down
14 changes: 0 additions & 14 deletions src/Gameboard.Api/Features/ChallengeSpec/ChallengeSpecStore.cs

This file was deleted.

Loading

0 comments on commit 4792200

Please sign in to comment.