diff --git a/.config/dotnet-tools.json b/.config/dotnet-tools.json index 321b5d6..d715709 100644 --- a/.config/dotnet-tools.json +++ b/.config/dotnet-tools.json @@ -4,10 +4,13 @@ "tools": { "csharpier": { "version": "1.1.2", - "commands": [ - "csharpier" - ], + "commands": ["csharpier"], + "rollForward": false + }, + "dotnet-ef": { + "version": "9.0.10", + "commands": ["dotnet-ef"], "rollForward": false } } -} \ No newline at end of file +} diff --git a/Project.App/Project.Api/.config/dotnet-tools.json b/Project.App/Project.Api/.config/dotnet-tools.json deleted file mode 100644 index d4937e0..0000000 --- a/Project.App/Project.Api/.config/dotnet-tools.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "version": 1, - "isRoot": true, - "tools": { - "dotnet-ef": { - "version": "9.0.10", - "commands": [ - "dotnet-ef" - ], - "rollForward": false - } - } -} \ No newline at end of file diff --git a/Project.App/Project.Api/DTOs/ExampleDTO.cs b/Project.App/Project.Api/DTOs/ExampleDTO.cs index 670caf1..47fd7a9 100644 --- a/Project.App/Project.Api/DTOs/ExampleDTO.cs +++ b/Project.App/Project.Api/DTOs/ExampleDTO.cs @@ -1,3 +1,3 @@ namespace Project.DTOs; -public class ExampleDTO { } +public class ExampleDTO { } diff --git a/Project.App/Project.Api/Data/MappingProfile.cs b/Project.App/Project.Api/Data/MappingProfile.cs index 574d87e..06997de 100644 --- a/Project.App/Project.Api/Data/MappingProfile.cs +++ b/Project.App/Project.Api/Data/MappingProfile.cs @@ -4,6 +4,7 @@ using Project.DTOs; namespace Project.Api.Data; + public class MappingProfile : Profile { public MappingProfile() { } diff --git a/Project.App/Project.Api/Enums/roleEnum.cs b/Project.App/Project.Api/Enums/roleEnum.cs index 674ac5d..9061287 100644 --- a/Project.App/Project.Api/Enums/roleEnum.cs +++ b/Project.App/Project.Api/Enums/roleEnum.cs @@ -1,7 +1,8 @@ namespace Project.Api.Enums; + public enum Role { Admin, Moderator, Player, -} \ No newline at end of file +} diff --git a/Project.App/Project.Api/Enums/statusEnum.cs b/Project.App/Project.Api/Enums/statusEnum.cs index 585c5d7..f680654 100644 --- a/Project.App/Project.Api/Enums/statusEnum.cs +++ b/Project.App/Project.Api/Enums/statusEnum.cs @@ -1,8 +1,9 @@ namespace Project.Api.Enums; + public enum Status { Active, Inactive, Away, Left, -} \ No newline at end of file +} diff --git a/Project.App/Project.Api/Models/Room.cs b/Project.App/Project.Api/Models/Room.cs index 9dcd2c0..3f07b20 100644 --- a/Project.App/Project.Api/Models/Room.cs +++ b/Project.App/Project.Api/Models/Room.cs @@ -1,8 +1,8 @@ - using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace Project.Api.Models; + public class Room { [Key] diff --git a/Project.App/Project.Api/Repositories/HandRepository.cs b/Project.App/Project.Api/Repositories/HandRepository.cs index bc2619b..179f0d8 100644 --- a/Project.App/Project.Api/Repositories/HandRepository.cs +++ b/Project.App/Project.Api/Repositories/HandRepository.cs @@ -14,11 +14,13 @@ namespace Project.Api.Repositories; public class HandRepository : IHandRepository { private readonly AppDbContext _context; + // Constructor public HandRepository(AppDbContext context) { _context = context; } + // Implement the methods // Get a hand by its ID public async Task GetHandAsyncById(Guid handId) @@ -33,6 +35,7 @@ public HandRepository(AppDbContext context) // return hand or throw exception if not found return hand ?? throw new Exception("Hand not found"); } + public async Task> GetHandsByRoomIdAsync(Guid roomId) { // Validate roomId @@ -41,13 +44,14 @@ public async Task> GetHandsByRoomIdAsync(Guid roomId) throw new ArgumentException("Invalid roomId"); } // Retrieve the hands from the database - List hands = await _context.Hands - .Include(h => h.RoomPlayer) + List hands = await _context + .Hands.Include(h => h.RoomPlayer) .Where(h => h.RoomPlayer != null && h.RoomPlayer.RoomId == roomId) .ToListAsync(); // return hands or throw exception if not found return (hands == null || hands.Count == 0) ? throw new Exception("No hands found") : hands; } + public async Task> GetHandsByUserIdAsync(Guid roomId, Guid userId) { // Validate roomId and userId @@ -56,13 +60,18 @@ public async Task> GetHandsByUserIdAsync(Guid roomId, Guid userId) throw new ArgumentException(userId == Guid.Empty ? "Invalid userId" : "Invalid roomId"); } // Retrieve the hands from the database - List hands = await _context.Hands - .Include(h => h.RoomPlayer) - .Where(h => h.RoomPlayer != null && h.RoomPlayer.RoomId == roomId && h.RoomPlayer.UserId == userId) + List hands = await _context + .Hands.Include(h => h.RoomPlayer) + .Where(h => + h.RoomPlayer != null + && h.RoomPlayer.RoomId == roomId + && h.RoomPlayer.UserId == userId + ) .ToListAsync(); // return hands or throw exception if not found return (hands == null || hands.Count == 0) ? throw new Exception("No hands found") : hands; } + // Create a new hand public async Task CreateHandAsync(Hand hand) { @@ -75,14 +84,17 @@ public async Task CreateHandAsync(Hand hand) // Asynchronously add the hand to the context and save changes await _context.Hands.AddAsync(hand); await SaveChangesAsync(); - // return hand + // return hand return hand; } + // Update an existing hand public async Task UpdateHandAsync(Guid handId, Hand hand) { //check if hand does not exist add hand if it does - var existingHand = await _context.Hands.FindAsync(handId) ?? throw new KeyNotFoundException("Hand not found"); + var existingHand = + await _context.Hands.FindAsync(handId) + ?? throw new KeyNotFoundException("Hand not found"); // Update properties existingHand.Order = hand.Order; @@ -96,11 +108,19 @@ public async Task UpdateHandAsync(Guid handId, Hand hand) // return newly updated hand return existingHand; } + // update specific properties of an existing hand - public async Task PatchHandAsync(Guid handId, int? Order = null, string? CardsJson = null, int? Bet = null) + public async Task PatchHandAsync( + Guid handId, + int? Order = null, + string? CardsJson = null, + int? Bet = null + ) { // Check if hand exists and retrieve it - var existingHand = await _context.Hands.FindAsync(handId) ?? throw new KeyNotFoundException("Hand not found"); + var existingHand = + await _context.Hands.FindAsync(handId) + ?? throw new KeyNotFoundException("Hand not found"); // Update properties if provided existingHand.Order = Order ?? existingHand.Order; @@ -114,11 +134,14 @@ public async Task PatchHandAsync(Guid handId, int? Order = null, string? C // Return the updated hand return existingHand; } + // Delete an existing hand public async Task DeleteHandAsync(Guid handId) { // Check if hand exists and retrieve it - var existingHand = await _context.Hands.FindAsync(handId) ?? throw new KeyNotFoundException("Hand not found"); + var existingHand = + await _context.Hands.FindAsync(handId) + ?? throw new KeyNotFoundException("Hand not found"); // Remove the hand from the context and save changes _context.Hands.Remove(existingHand); @@ -127,11 +150,11 @@ public async Task DeleteHandAsync(Guid handId) // Return the deleted hand return existingHand; } + // Save changes to the database public async Task SaveChangesAsync() { // Save changes to the database await _context.SaveChangesAsync(); } - } diff --git a/Project.App/Project.Api/Repositories/Interface/IHandRepository.cs b/Project.App/Project.Api/Repositories/Interface/IHandRepository.cs index 581deb7..3968715 100644 --- a/Project.App/Project.Api/Repositories/Interface/IHandRepository.cs +++ b/Project.App/Project.Api/Repositories/Interface/IHandRepository.cs @@ -8,25 +8,37 @@ using Project.Api.Repositories; namespace Project.Api.Repositories; + public interface IHandRepository { // Define Fields // Get a hand by its ID Task GetHandAsyncById(Guid handId); + // Get all hands in a room Task> GetHandsByRoomIdAsync(Guid roomId); + // Get all hands by a user in a room Task> GetHandsByUserIdAsync(Guid roomId, Guid userId); + // Create a new hand Task CreateHandAsync(Hand hand); + // Update an existing hand Task UpdateHandAsync(Guid handId, Hand hand); + // Partially update an existing hand - Task PatchHandAsync(Guid handId, int? Order = null, string? CardsJson = null, int? Bet = null); + Task PatchHandAsync( + Guid handId, + int? Order = null, + string? CardsJson = null, + int? Bet = null + ); + // Delete a hand Task DeleteHandAsync(Guid handId); + // Save changes to the database Task SaveChangesAsync(); } - diff --git a/Project.App/Project.Api/Repositories/Interface/IRoomPlayerRepository.cs b/Project.App/Project.Api/Repositories/Interface/IRoomPlayerRepository.cs index d3be8fc..515b0ae 100644 --- a/Project.App/Project.Api/Repositories/Interface/IRoomPlayerRepository.cs +++ b/Project.App/Project.Api/Repositories/Interface/IRoomPlayerRepository.cs @@ -2,8 +2,8 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; -using Project.Api.Models; using Project.Api.Enums; +using Project.Api.Models; namespace Project.Api.Repositories.Interface { @@ -25,4 +25,4 @@ public interface IRoomPlayerRepository Task UpdatePlayerStatusAsync(Guid id, Status status); Task UpdatePlayerBalanceAsync(Guid id, long balance); } -} \ No newline at end of file +} diff --git a/Project.App/Project.Api/Repositories/Interface/IRoomRepository.cs b/Project.App/Project.Api/Repositories/Interface/IRoomRepository.cs index daeb15e..1ec5ff0 100644 --- a/Project.App/Project.Api/Repositories/Interface/IRoomRepository.cs +++ b/Project.App/Project.Api/Repositories/Interface/IRoomRepository.cs @@ -1,6 +1,7 @@ using Project.Api.Models; namespace Project.Api.Repositories.Interface; + public interface IRoomRepository { Task GetByIdAsync(Guid id); @@ -13,4 +14,3 @@ public interface IRoomRepository Task DeleteAsync(Guid id); Task ExistsAsync(Guid id); } - diff --git a/Project.App/Project.Api/Repositories/RoomPlayerRepository.cs b/Project.App/Project.Api/Repositories/RoomPlayerRepository.cs index 1f0cbd1..89db15b 100644 --- a/Project.App/Project.Api/Repositories/RoomPlayerRepository.cs +++ b/Project.App/Project.Api/Repositories/RoomPlayerRepository.cs @@ -4,133 +4,145 @@ using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using Project.Api.Data; +using Project.Api.Enums; using Project.Api.Models; using Project.Api.Repositories.Interface; -using Project.Api.Enums; + namespace Project.Api.Repositories; - public class RoomPlayerRepository : IRoomPlayerRepository +public class RoomPlayerRepository : IRoomPlayerRepository +{ + private readonly AppDbContext _context; + + public RoomPlayerRepository(AppDbContext context) { - private readonly AppDbContext _context; - public RoomPlayerRepository(AppDbContext context) - { - _context = context; - } - public async Task GetByIdAsync(Guid id) - { - return await _context.RoomPlayers - .Include(rp => rp.Room) - .Include(rp => rp.User) - .Include(rp => rp.Hands) - .FirstOrDefaultAsync(rp => rp.Id == id); - } - public async Task> GetAllAsync() - { - return await _context.RoomPlayers - .Include(rp => rp.Room) - .Include(rp => rp.User) - .Include(rp => rp.Hands) - .ToListAsync(); - } + _context = context; + } + + public async Task GetByIdAsync(Guid id) + { + return await _context + .RoomPlayers.Include(rp => rp.Room) + .Include(rp => rp.User) + .Include(rp => rp.Hands) + .FirstOrDefaultAsync(rp => rp.Id == id); + } + + public async Task> GetAllAsync() + { + return await _context + .RoomPlayers.Include(rp => rp.Room) + .Include(rp => rp.User) + .Include(rp => rp.Hands) + .ToListAsync(); + } + public async Task> GetByRoomIdAsync(Guid roomId) { - return await _context.RoomPlayers - .Include(rp => rp.Room) + return await _context + .RoomPlayers.Include(rp => rp.Room) .Include(rp => rp.User) .Include(rp => rp.Hands) .Where(rp => rp.RoomId == roomId) .ToListAsync(); } - public async Task> GetByUserIdAsync(Guid userId) - { - return await _context.RoomPlayers - .Include(rp => rp.Room) - .Include(rp => rp.User) - .Include(rp => rp.Hands) - .Where(rp => rp.UserId == userId) - .ToListAsync(); - } - public async Task GetByRoomAndUserAsync(Guid roomId, Guid userId) - { - return await _context.RoomPlayers - .Include(rp => rp.Room) - .Include(rp => rp.User) - .Include(rp => rp.Hands) - .FirstOrDefaultAsync(rp => rp.RoomId == roomId && rp.UserId == userId); - } + public async Task> GetByUserIdAsync(Guid userId) + { + return await _context + .RoomPlayers.Include(rp => rp.Room) + .Include(rp => rp.User) + .Include(rp => rp.Hands) + .Where(rp => rp.UserId == userId) + .ToListAsync(); + } - public async Task> GetActivePlayersInRoomAsync(Guid roomId) - { - return await _context.RoomPlayers - .Include(rp => rp.Room) - .Include(rp => rp.User) - .Include(rp => rp.Hands) - .Where(rp => rp.RoomId == roomId && rp.Status == Status.Active) - .ToListAsync(); - } + public async Task GetByRoomAndUserAsync(Guid roomId, Guid userId) + { + return await _context + .RoomPlayers.Include(rp => rp.Room) + .Include(rp => rp.User) + .Include(rp => rp.Hands) + .FirstOrDefaultAsync(rp => rp.RoomId == roomId && rp.UserId == userId); + } - public async Task CreateAsync(RoomPlayer roomPlayer) - { - _context.RoomPlayers.Add(roomPlayer); - await _context.SaveChangesAsync(); - return roomPlayer; - } + public async Task> GetActivePlayersInRoomAsync(Guid roomId) + { + return await _context + .RoomPlayers.Include(rp => rp.Room) + .Include(rp => rp.User) + .Include(rp => rp.Hands) + .Where(rp => rp.RoomId == roomId && rp.Status == Status.Active) + .ToListAsync(); + } + + public async Task CreateAsync(RoomPlayer roomPlayer) + { + _context.RoomPlayers.Add(roomPlayer); + await _context.SaveChangesAsync(); + return roomPlayer; + } + + public async Task UpdateAsync(RoomPlayer roomPlayer) + { + _context.Entry(roomPlayer).State = EntityState.Modified; + await _context.SaveChangesAsync(); + return roomPlayer; + } + + public async Task DeleteAsync(Guid id) + { + var roomPlayer = await _context.RoomPlayers.FindAsync(id); + if (roomPlayer == null) + return false; + _context.RoomPlayers.Remove(roomPlayer); + await _context.SaveChangesAsync(); + return true; + } + + public async Task ExistsAsync(Guid id) + { + return await _context.RoomPlayers.AnyAsync(rp => rp.Id == id); + } + + public async Task IsPlayerInRoomAsync(Guid roomId, Guid userId) + { + return await _context.RoomPlayers.AnyAsync(rp => + rp.RoomId == roomId && rp.UserId == userId + ); + } + + public async Task GetPlayerCountInRoomAsync(Guid roomId) + { + return await _context.RoomPlayers.CountAsync(rp => rp.RoomId == roomId); + } - public async Task UpdateAsync(RoomPlayer roomPlayer) + public async Task GetRoomHostAsync(Guid roomId) + { + return await _context + .RoomPlayers.Include(rp => rp.Room) + .Include(rp => rp.User) + .Include(rp => rp.Hands) + .FirstOrDefaultAsync(rp => rp.RoomId == roomId && rp.Role == Role.Admin); + } + + public async Task UpdatePlayerStatusAsync(Guid id, Status status) + { + var roomPlayer = await _context.RoomPlayers.FindAsync(id); + if (roomPlayer != null) { - _context.Entry(roomPlayer).State = EntityState.Modified; + roomPlayer.Status = status; await _context.SaveChangesAsync(); - return roomPlayer; } + } - public async Task DeleteAsync(Guid id) + public async Task UpdatePlayerBalanceAsync(Guid id, long balance) + { + var roomPlayer = await _context.RoomPlayers.FindAsync(id); + if (roomPlayer != null) { - var roomPlayer = await _context.RoomPlayers.FindAsync(id); - if (roomPlayer == null) - return false; - _context.RoomPlayers.Remove(roomPlayer); + roomPlayer.Balance = balance; await _context.SaveChangesAsync(); - return true; } - public async Task ExistsAsync(Guid id) - { - return await _context.RoomPlayers.AnyAsync(rp => rp.Id == id); - } - public async Task IsPlayerInRoomAsync(Guid roomId, Guid userId) - { - return await _context.RoomPlayers - .AnyAsync(rp => rp.RoomId == roomId && rp.UserId == userId); - } - public async Task GetPlayerCountInRoomAsync(Guid roomId) - { - return await _context.RoomPlayers - .CountAsync(rp => rp.RoomId == roomId); - } - public async Task GetRoomHostAsync(Guid roomId) - { - return await _context.RoomPlayers - .Include(rp => rp.Room) - .Include(rp => rp.User) - .Include(rp => rp.Hands) - .FirstOrDefaultAsync(rp => rp.RoomId == roomId && rp.Role == Role.Admin); - } - public async Task UpdatePlayerStatusAsync(Guid id, Status status) - { - var roomPlayer = await _context.RoomPlayers.FindAsync(id); - if (roomPlayer != null) - { - roomPlayer.Status = status; - await _context.SaveChangesAsync(); - } - } - public async Task UpdatePlayerBalanceAsync(Guid id, long balance) - { - var roomPlayer = await _context.RoomPlayers.FindAsync(id); - if (roomPlayer != null) - { - roomPlayer.Balance = balance; - await _context.SaveChangesAsync(); - } - } - } \ No newline at end of file + } +} diff --git a/Project.App/Project.Api/Repositories/RoomRepository.cs b/Project.App/Project.Api/Repositories/RoomRepository.cs index 90cc7f3..1a0da52 100644 --- a/Project.App/Project.Api/Repositories/RoomRepository.cs +++ b/Project.App/Project.Api/Repositories/RoomRepository.cs @@ -24,10 +24,7 @@ public RoomRepository(AppDbContext context) public async Task> GetAllAsync() { - return await _context - .Rooms.Include(r => r.Host) - .Include(r => r.RoomPlayers) - .ToListAsync(); + return await _context.Rooms.Include(r => r.Host).Include(r => r.RoomPlayers).ToListAsync(); } public async Task> GetActiveRoomsAsync() @@ -94,4 +91,3 @@ public async Task ExistsAsync(Guid id) return await _context.Rooms.AnyAsync(r => r.Id == id); } } - diff --git a/Project.App/Project.Api/Services/HandService.cs b/Project.App/Project.Api/Services/HandService.cs index 8af2208..2b88cba 100644 --- a/Project.App/Project.Api/Services/HandService.cs +++ b/Project.App/Project.Api/Services/HandService.cs @@ -17,6 +17,7 @@ using Project.Api.Services; namespace Project.Api.Services; + public class HandService : IHandService { // Call the repository @@ -39,6 +40,7 @@ public async Task CreateHandAsync(Hand hand) _logger.LogInformation("Creating a new hand"); return await _Repo.CreateHandAsync(hand); } + // Get all hands in a room public async Task> GetHandsByRoomIdAsync(Guid roomId) { @@ -55,6 +57,7 @@ public async Task> GetHandsByRoomIdAsync(Guid roomId) throw new Exception(e.Message); } } + // Get a hand by its ID public async Task GetHandAsyncByIdAsync(Guid handId) { @@ -71,6 +74,7 @@ public async Task> GetHandsByRoomIdAsync(Guid roomId) throw new Exception(e.Message); } } + // Get all hands by a user in a room public async Task> GetHandsByUserIdAsync(Guid roomId, Guid userId) { @@ -83,13 +87,14 @@ public async Task> GetHandsByUserIdAsync(Guid roomId, Guid userId) catch (Exception e) { // throw an exception and Log it - _logger.LogError(e, $"Error getting all hands for user {userId} in room {roomId}: {e.Message}"); + _logger.LogError( + e, + $"Error getting all hands for user {userId} in room {roomId}: {e.Message}" + ); throw new Exception(e.Message); } - } - // Update an existing hand public async Task UpdateHandAsync(Guid handId, Hand hand) { @@ -106,8 +111,14 @@ public async Task UpdateHandAsync(Guid handId, Hand hand) throw new Exception(e.Message); } } + // Partially update an existing hand - public async Task PatchHandAsync(Guid handId, int? Order = null, string? CardsJson = null, int? Bet = null) + public async Task PatchHandAsync( + Guid handId, + int? Order = null, + string? CardsJson = null, + int? Bet = null + ) { try { @@ -122,6 +133,7 @@ public async Task PatchHandAsync(Guid handId, int? Order = null, string? C throw new Exception(e.Message); } } + // Delete a hand public async Task DeleteHandAsync(Guid handId) { @@ -137,8 +149,5 @@ public async Task DeleteHandAsync(Guid handId) _logger.LogError(e, $"Error deleting hand {handId}: {e.Message}"); throw new Exception(e.Message); } - } - - } diff --git a/Project.App/Project.Api/Services/Interface/IHandService.cs b/Project.App/Project.Api/Services/Interface/IHandService.cs index afca4cf..bc417a4 100644 --- a/Project.App/Project.Api/Services/Interface/IHandService.cs +++ b/Project.App/Project.Api/Services/Interface/IHandService.cs @@ -6,6 +6,7 @@ using Project.Api.Models; namespace Project.Api.Services; + public interface IHandService { // Define Fields @@ -19,7 +20,12 @@ public interface IHandService Task UpdateHandAsync(Guid handId, Hand hand); - Task PatchHandAsync(Guid handId, int? Order = null, string? CardsJson = null, int? Bet = null); + Task PatchHandAsync( + Guid handId, + int? Order = null, + string? CardsJson = null, + int? Bet = null + ); Task DeleteHandAsync(Guid handId); } diff --git a/Project.App/Project.Test/Tests.Repositories/HandRepositoryTest.cs b/Project.App/Project.Test/Tests.Repositories/HandRepositoryTest.cs index dffb77b..1474747 100644 --- a/Project.App/Project.Test/Tests.Repositories/HandRepositoryTest.cs +++ b/Project.App/Project.Test/Tests.Repositories/HandRepositoryTest.cs @@ -1,13 +1,12 @@ +using System.Runtime.InteropServices; using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Query; using Microsoft.Extensions.Logging; using Moq; +using Project.Api.Data; using Project.Api.Models; using Project.Api.Repositories; -using Project.Api.Data; using Xunit; -using Microsoft.EntityFrameworkCore.Query; -using System.Runtime.InteropServices; - namespace Project.Test.Repository { @@ -15,10 +14,8 @@ public class HandRepositoryTest { private readonly Mock _handRepositoryMock; - public HandRepositoryTest() { - _handRepositoryMock = new Mock(); } @@ -38,11 +35,18 @@ public async Task GetHandAsyncById_ValidId_ReturnsHand() var roomPlayerId = Guid.NewGuid(); using var context = new AppDbContext(options); - context.Hands.Add(new Hand { Id = handId, Order = 1, RoomPlayerId = roomPlayerId, CardsJson = "[]", Bet = 0 }); + context.Hands.Add( + new Hand + { + Id = handId, + Order = 1, + RoomPlayerId = roomPlayerId, + CardsJson = "[]", + Bet = 0, + } + ); await context.SaveChangesAsync(); - - // Act var repo = new HandRepository(context); Hand? res = await repo.GetHandAsyncById(context.Hands.First().Id); @@ -54,8 +58,8 @@ public async Task GetHandAsyncById_ValidId_ReturnsHand() Assert.Equal(roomPlayerId, res.RoomPlayerId); Assert.Equal("[]", res.CardsJson); Assert.Equal(0, res.Bet); - } + [Fact] public async Task GetHandAsyncById_InvalidId_ReturnsNull() { @@ -63,7 +67,16 @@ public async Task GetHandAsyncById_InvalidId_ReturnsNull() var options = GetInMemoryDbOptions(); using var context = new AppDbContext(options); - context.Hands.Add(new Hand { Id = Guid.NewGuid(), Order = 1, RoomPlayerId = Guid.NewGuid(), CardsJson = "[]", Bet = 0 }); + context.Hands.Add( + new Hand + { + Id = Guid.NewGuid(), + Order = 1, + RoomPlayerId = Guid.NewGuid(), + CardsJson = "[]", + Bet = 0, + } + ); await context.SaveChangesAsync(); // Act @@ -73,6 +86,7 @@ public async Task GetHandAsyncById_InvalidId_ReturnsNull() // Assert Assert.Null(res); } + [Fact] public async Task GetHandsByRoomIdAsync_ValidRoomId_ReturnsHands() { @@ -90,14 +104,82 @@ public async Task GetHandsByRoomIdAsync_ValidRoomId_ReturnsHands() // Act using var context = new AppDbContext(options); - context.RoomPlayers.Add(new RoomPlayer { Id = roomPlayerId1, RoomId = roomId, UserId = Guid.NewGuid(), Balance = 1000 }); - context.RoomPlayers.Add(new RoomPlayer { Id = roomPlayerId2, RoomId = roomId, UserId = Guid.NewGuid(), Balance = 1000 }); - context.RoomPlayers.Add(new RoomPlayer { Id = roomPlayerId3, RoomId = roomId, UserId = Guid.NewGuid(), Balance = 1000 }); - context.RoomPlayers.Add(new RoomPlayer { Id = roomPlayerId4, RoomId = roomId, UserId = Guid.NewGuid(), Balance = 1000 }); - context.Hands.Add(new Hand { Id = handId1, Order = 1, RoomPlayerId = roomPlayerId1, CardsJson = "[]", Bet = 0 }); - context.Hands.Add(new Hand { Id = handId2, Order = 2, RoomPlayerId = roomPlayerId2, CardsJson = "[]", Bet = 0 }); - context.Hands.Add(new Hand { Id = handId3, Order = 3, RoomPlayerId = roomPlayerId3, CardsJson = "[]", Bet = 0 }); - context.Hands.Add(new Hand { Id = handId4, Order = 4, RoomPlayerId = roomPlayerId4, CardsJson = "[]", Bet = 0 }); // Different RoomPlayer + context.RoomPlayers.Add( + new RoomPlayer + { + Id = roomPlayerId1, + RoomId = roomId, + UserId = Guid.NewGuid(), + Balance = 1000, + } + ); + context.RoomPlayers.Add( + new RoomPlayer + { + Id = roomPlayerId2, + RoomId = roomId, + UserId = Guid.NewGuid(), + Balance = 1000, + } + ); + context.RoomPlayers.Add( + new RoomPlayer + { + Id = roomPlayerId3, + RoomId = roomId, + UserId = Guid.NewGuid(), + Balance = 1000, + } + ); + context.RoomPlayers.Add( + new RoomPlayer + { + Id = roomPlayerId4, + RoomId = roomId, + UserId = Guid.NewGuid(), + Balance = 1000, + } + ); + context.Hands.Add( + new Hand + { + Id = handId1, + Order = 1, + RoomPlayerId = roomPlayerId1, + CardsJson = "[]", + Bet = 0, + } + ); + context.Hands.Add( + new Hand + { + Id = handId2, + Order = 2, + RoomPlayerId = roomPlayerId2, + CardsJson = "[]", + Bet = 0, + } + ); + context.Hands.Add( + new Hand + { + Id = handId3, + Order = 3, + RoomPlayerId = roomPlayerId3, + CardsJson = "[]", + Bet = 0, + } + ); + context.Hands.Add( + new Hand + { + Id = handId4, + Order = 4, + RoomPlayerId = roomPlayerId4, + CardsJson = "[]", + Bet = 0, + } + ); // Different RoomPlayer await context.SaveChangesAsync(); // Assert @@ -130,6 +212,7 @@ await Assert.ThrowsAsync(async () => await repo.GetHandsByRoomIdAsync(Guid.Empty); }); } + [Fact] public async Task CreateHandAsync_ValidHand_ReturnsCreatedHand() { @@ -137,7 +220,14 @@ public async Task CreateHandAsync_ValidHand_ReturnsCreatedHand() var options = GetInMemoryDbOptions(); var handId = Guid.NewGuid(); var roomPlayerId = Guid.NewGuid(); - var newHand = new Hand { Id = handId, Order = 1, RoomPlayerId = roomPlayerId, CardsJson = "[]", Bet = 0 }; + var newHand = new Hand + { + Id = handId, + Order = 1, + RoomPlayerId = roomPlayerId, + CardsJson = "[]", + Bet = 0, + }; using var context = new AppDbContext(options); var repo = new HandRepository(context); @@ -153,6 +243,7 @@ public async Task CreateHandAsync_ValidHand_ReturnsCreatedHand() Assert.Equal("[]", res.CardsJson); Assert.Equal(0, res.Bet); } + [Fact] public async Task CreateHandAsync_NullHand_ThrowsArgumentNullException() { @@ -168,6 +259,7 @@ await Assert.ThrowsAsync(async () => await repo.CreateHandAsync(null!); }); } + [Fact] public async Task UpdateHandAsync_ValidHand_ReturnsUpdatedHand() { @@ -180,10 +272,29 @@ public async Task UpdateHandAsync_ValidHand_ReturnsUpdatedHand() using var context = new AppDbContext(options); // Act - context.Hands.Add(new Hand { Id = handId, Order = 1, RoomPlayerId = roomPlayerId, CardsJson = "[]", Bet = 0 }); + context.Hands.Add( + new Hand + { + Id = handId, + Order = 1, + RoomPlayerId = roomPlayerId, + CardsJson = "[]", + Bet = 0, + } + ); await context.SaveChangesAsync(); var repo = new HandRepository(context); - await repo.UpdateHandAsync(handId, new Hand { Id = handId, Order = 2, RoomPlayerId = roomPlayerId, CardsJson = "[\"AS\"]", Bet = 100 }); + await repo.UpdateHandAsync( + handId, + new Hand + { + Id = handId, + Order = 2, + RoomPlayerId = roomPlayerId, + CardsJson = "[\"AS\"]", + Bet = 100, + } + ); // Assert @@ -207,9 +318,20 @@ public async Task UpdateHandAsync_InvalidHandId_ThrowsKeyNotFoundException() // Act & Assert await Assert.ThrowsAsync(async () => { - await repo.UpdateHandAsync(Guid.NewGuid(), new Hand { Id = Guid.NewGuid(), Order = 1, RoomPlayerId = Guid.NewGuid(), CardsJson = "[]", Bet = 0 }); + await repo.UpdateHandAsync( + Guid.NewGuid(), + new Hand + { + Id = Guid.NewGuid(), + Order = 1, + RoomPlayerId = Guid.NewGuid(), + CardsJson = "[]", + Bet = 0, + } + ); }); } + [Fact] public async Task UpdateHandAsync_NullHand_ThrowsArgumentNullException() { @@ -225,6 +347,7 @@ await Assert.ThrowsAsync(async () => await repo.UpdateHandAsync(Guid.NewGuid(), null!); }); } + [Fact] public async Task PatchHandAsync_ValidHand_ReturnsPatchedHand() { @@ -237,7 +360,16 @@ public async Task PatchHandAsync_ValidHand_ReturnsPatchedHand() using var context = new AppDbContext(options); // Act - context.Hands.Add(new Hand { Id = handId, Order = 1, RoomPlayerId = roomPlayerId, CardsJson = "[]", Bet = 0 }); + context.Hands.Add( + new Hand + { + Id = handId, + Order = 1, + RoomPlayerId = roomPlayerId, + CardsJson = "[]", + Bet = 0, + } + ); await context.SaveChangesAsync(); var repo = new HandRepository(context); Hand res = await repo.PatchHandAsync(handId, Order: 3, Bet: 200); @@ -249,6 +381,7 @@ public async Task PatchHandAsync_ValidHand_ReturnsPatchedHand() Assert.Equal("[]", res.CardsJson); Assert.Equal(200, res.Bet); } + [Fact] public async Task PatchHandAsync_InvalidHandId_ThrowsKeyNotFoundException() { @@ -264,6 +397,7 @@ await Assert.ThrowsAsync(async () => await repo.PatchHandAsync(Guid.NewGuid(), Order: 2); }); } + [Fact] public async Task DeleteHandAsync_ValidHandId_ReturnsDeletedHand() { @@ -276,7 +410,16 @@ public async Task DeleteHandAsync_ValidHandId_ReturnsDeletedHand() using var context = new AppDbContext(options); // Act - context.Hands.Add(new Hand { Id = handId, Order = 1, RoomPlayerId = roomPlayerId, CardsJson = "[]", Bet = 0 }); + context.Hands.Add( + new Hand + { + Id = handId, + Order = 1, + RoomPlayerId = roomPlayerId, + CardsJson = "[]", + Bet = 0, + } + ); await context.SaveChangesAsync(); var repo = new HandRepository(context); Hand res = await repo.DeleteHandAsync(handId); @@ -288,6 +431,7 @@ public async Task DeleteHandAsync_ValidHandId_ReturnsDeletedHand() var deletedHand = await repo.GetHandAsyncById(handId); Assert.Null(deletedHand); } + [Fact] public async Task DeleteHandAsync_InvalidHandId_ThrowsKeyNotFoundException() { @@ -303,11 +447,5 @@ await Assert.ThrowsAsync(async () => await repo.DeleteHandAsync(Guid.NewGuid()); }); } - } - - }; - - -