Skip to content

Commit

Permalink
Merge pull request #4 from davidtimovski/dev
Browse files Browse the repository at this point in the history
Upgrade to .NET 8
  • Loading branch information
davidtimovski authored Jan 13, 2024
2 parents 30a9f2c + 94a46c7 commit 3ce9a49
Show file tree
Hide file tree
Showing 15 changed files with 123 additions and 137 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace TeamSketch.Benchmarks
{
[SimpleJob(launchCount: 5, warmupCount: 5, targetCount: 5)]
[SimpleJob(launchCount: 5, warmupCount: 5)]
[MemoryDiagnoser]
public class PayloadConverterPointBenchmarks
{
Expand All @@ -23,7 +23,7 @@ public PayloadConverterPointBenchmarks()
public void PointToBytes() => PayloadConverter.ToBytes(x1, y1, ThicknessEnum.SemiThin, ColorsEnum.Blue);
}

[SimpleJob(launchCount: 5, warmupCount: 5, targetCount: 5)]
[SimpleJob(launchCount: 5, warmupCount: 5)]
[MemoryDiagnoser]
public class PayloadConverterLineBenchmarks
{
Expand Down
2 changes: 1 addition & 1 deletion src/TeamSketch.Benchmarks/Benchmarks/RendererBenchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace TeamSketch.Benchmarks
{
[SimpleJob(launchCount: 5, warmupCount: 5, targetCount: 5)]
[SimpleJob(launchCount: 5, warmupCount: 5)]
[MemoryDiagnoser]
public class RendererBenchmarks
{
Expand Down
28 changes: 15 additions & 13 deletions src/TeamSketch.Benchmarks/TeamSketch.Benchmarks.csproj
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.2" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.12" />
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.1.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.1.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\TeamSketch\TeamSketch.csproj" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\TeamSketch\TeamSketch.csproj" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion src/TeamSketch.Common/TeamSketch.Common.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/TeamSketch.Web/Config/DatabaseSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

public class DatabaseSettings
{
public string? ConnectionString { get; set; }
public required string ConnectionString { get; init; }
}
31 changes: 15 additions & 16 deletions src/TeamSketch.Web/Controllers/LiveViewController.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
using Microsoft.AspNetCore.Mvc;
using TeamSketch.Web.Services;

namespace TeamSketch.Web.Controllers
namespace TeamSketch.Web.Controllers;

[Route("api/[controller]")]
[ApiController]
public class LiveViewController : ControllerBase
{
[Route("api/[controller]")]
[ApiController]
public class LiveViewController : ControllerBase
{
private readonly ILiveViewService _liveLiveService;
private readonly ILiveViewService _liveLiveService;

public LiveViewController(ILiveViewService liveViewService)
{
_liveLiveService = liveViewService;
}
public LiveViewController(ILiveViewService liveViewService)
{
_liveLiveService = liveViewService;
}

[HttpGet]
public IActionResult Get()
{
var locations = _liveLiveService.GetDistinctLocations();
return Ok(locations);
}
[HttpGet]
public IActionResult Get()
{
var locations = _liveLiveService.GetDistinctLocations();
return Ok(locations);
}
}
79 changes: 39 additions & 40 deletions src/TeamSketch.Web/Controllers/RoomsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,56 @@
using TeamSketch.Common.ApiModels;
using TeamSketch.Web.Persistence;

namespace TeamSketch.Web.Controllers
namespace TeamSketch.Web.Controllers;

[Route("api/[controller]")]
[ApiController]
public class RoomsController : ControllerBase
{
[Route("api/[controller]")]
[ApiController]
public class RoomsController : ControllerBase
private readonly IRepository _repository;

public RoomsController(IRepository repository)
{
private readonly IRepository _repository;
_repository = repository;
}

public RoomsController(IRepository repository)
[HttpGet("{room}/validate-join/{nickname}")]
public async Task<IActionResult> ValidateJoin(string room, string nickname)
{
if (string.IsNullOrEmpty(room) || string.IsNullOrEmpty(nickname))
{
_repository = repository;
return BadRequest();
}

[HttpGet("{room}/validate-join/{nickname}")]
public async Task<IActionResult> ValidateJoin(string room, string nickname)
var exists = await _repository.RoomExistsAsync(room);
if (!exists)
{
if (string.IsNullOrEmpty(room) || string.IsNullOrEmpty(nickname))
{
return BadRequest();
}

var exists = await _repository.RoomExistsAsync(room);
if (!exists)
{
return Ok(new JoinRoomValidationResult { RoomExists = false });
}

var participantsInRoom = await _repository.GetActiveParticipantsInRoomAsync(room);
if (participantsInRoom.Count > 4)
{
return Ok(new JoinRoomValidationResult { RoomExists = true, RoomIsFull = true });
}

if (participantsInRoom.Contains(nickname))
{
return Ok(new JoinRoomValidationResult { RoomExists = true, RoomIsFull = false, NicknameIsTaken = true });
}

return Ok(new JoinRoomValidationResult { RoomExists = true });
return Ok(new JoinRoomValidationResult { RoomExists = false });
}

[HttpGet("{room}/participants")]
public async Task<IActionResult> GetParticipantsInRoom(string room)
var participantsInRoom = await _repository.GetActiveParticipantsInRoomAsync(room);
if (participantsInRoom.Count > 4)
{
if (string.IsNullOrEmpty(room))
{
return BadRequest();
}
return Ok(new JoinRoomValidationResult { RoomExists = true, RoomIsFull = true });
}

var participantsInRoom = await _repository.GetActiveParticipantsInRoomAsync(room);
return Ok(participantsInRoom);
if (participantsInRoom.Contains(nickname))
{
return Ok(new JoinRoomValidationResult { RoomExists = true, RoomIsFull = false, NicknameIsTaken = true });
}

return Ok(new JoinRoomValidationResult { RoomExists = true });
}

[HttpGet("{room}/participants")]
public async Task<IActionResult> GetParticipantsInRoom(string room)
{
if (string.IsNullOrEmpty(room))
{
return BadRequest();
}

var participantsInRoom = await _repository.GetActiveParticipantsInRoomAsync(room);
return Ok(participantsInRoom);
}
}
10 changes: 2 additions & 8 deletions src/TeamSketch.Web/Pages/Index.cshtml.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace TeamSketch.Web.Pages
{
public class IndexModel : PageModel
public class IndexModel(IConfiguration configuration) : PageModel
{
public IndexModel(IConfiguration configuration)
{
BaseUrl = configuration["BaseUrl"];
}

public string BaseUrl { get; private set; }
public string BaseUrl { get; private set; } = configuration["BaseUrl"]!;
}
}
15 changes: 7 additions & 8 deletions src/TeamSketch.Web/Persistence/Models/ConnectionRoom.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
namespace TeamSketch.Web.Persistence
namespace TeamSketch.Web.Persistence;

public class ConnectionRoom
{
public class ConnectionRoom
{
public int ConnectionId { get; set; }
public string Nickname { get; set; }
public int RoomId { get; set; }
public string Room { get; set; }
}
public required int ConnectionId { get; init; }
public required string Nickname { get; init; }
public required int RoomId { get; init; }
public required string Room { get; init; }
}
11 changes: 5 additions & 6 deletions src/TeamSketch.Web/Persistence/Models/EventType.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
namespace TeamSketch.Web.Persistence
namespace TeamSketch.Web.Persistence;

public enum EventType
{
public enum EventType
{
Joined,
Disconnected
}
Joined,
Disconnected
}
11 changes: 3 additions & 8 deletions src/TeamSketch.Web/Persistence/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,9 @@ public interface IRepository
Task DisconnectAllAsync();
}

public class Repository : IRepository
public class Repository(IOptions<DatabaseSettings> databaseSettings) : IRepository
{
private readonly string? _connectionString;

public Repository(IOptions<DatabaseSettings> databaseSettings)
{
_connectionString = databaseSettings.Value.ConnectionString;
}
private readonly string _connectionString = databaseSettings.Value.ConnectionString;

public async Task<bool> RoomExistsAsync(string room)
{
Expand Down Expand Up @@ -126,7 +121,7 @@ public async Task DisconnectAllAsync()
await conn.ExecuteScalarAsync<int>(@"UPDATE connections SET is_connected = FALSE, modified = @modified", new { modified = DateTime.UtcNow });
}

private IDbConnection OpenConnection()
private NpgsqlConnection OpenConnection()
{
var conn = new NpgsqlConnection(_connectionString);
conn.Open();
Expand Down
2 changes: 1 addition & 1 deletion src/TeamSketch.Web/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

app.Lifetime.ApplicationStarted.Register(async () =>
{
var repository = app.Services.GetService<IRepository>();
var repository = app.Services.GetRequiredService<IRepository>();
await repository.DisconnectAllAsync();
});

Expand Down
8 changes: 4 additions & 4 deletions src/TeamSketch.Web/TeamSketch.Web.csproj
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Dapper" Version="2.0.123" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Protocols.MessagePack" Version="7.0.0" />
<PackageReference Include="Npgsql" Version="7.0.0" />
<PackageReference Include="Dapper" Version="2.1.28" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Protocols.MessagePack" Version="8.0.1" />
<PackageReference Include="Npgsql" Version="8.0.1" />
</ItemGroup>

<ItemGroup>
Expand Down
39 changes: 19 additions & 20 deletions src/TeamSketch.Web/Utils/RoomNameGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
using System.Security.Cryptography;
using System.Text;

namespace TeamSketch.Web.Utils
namespace TeamSketch.Web.Utils;

public static class RoomNameGenerator
{
public static class RoomNameGenerator
{
private static readonly char[] chars = "abcdefghkmnprstuvwxyz123456789".ToCharArray();
private const int Length = 7;
private static readonly char[] chars = "abcdefghkmnprstuvwxyz123456789".ToCharArray();
private const int Length = 7;

public static string Generate()
public static string Generate()
{
var data = new byte[4 * Length];
using (var crypto = RandomNumberGenerator.Create())
{
var data = new byte[4 * Length];
using (var crypto = RandomNumberGenerator.Create())
{
crypto.GetBytes(data);
}

var result = new StringBuilder(Length);
for (int i = 0; i < Length; i++)
{
var random = BitConverter.ToUInt32(data, i * 4);
var index = random % chars.Length;
crypto.GetBytes(data);
}

result.Append(chars[index]);
}
var result = new StringBuilder(Length);
for (int i = 0; i < Length; i++)
{
var random = BitConverter.ToUInt32(data, i * 4);
var index = random % chars.Length;

return result.ToString();
result.Append(chars[index]);
}

return result.ToString();
}
}
Loading

0 comments on commit 3ce9a49

Please sign in to comment.