Skip to content

Commit

Permalink
Merge pull request #14 from theImmortalCoders/issue-8
Browse files Browse the repository at this point in the history
Issue 8
  • Loading branch information
marcinbator authored Aug 17, 2024
2 parents 4cff2e6 + 5feb07b commit fa58898
Show file tree
Hide file tree
Showing 41 changed files with 1,131 additions and 211 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ namespace rag_2_backend.data;
public class DatabaseContext(DbContextOptions<DatabaseContext> options) : DbContext(options)
{
public virtual required DbSet<Game> Games { get; init; }

public virtual required DbSet<RecordedGame> RecordedGames { get; init; }

public virtual required DbSet<User> Users { get; init; }
public virtual required DbSet<AccountConfirmationToken> AccountConfirmationTokens { get; init; }
public virtual required DbSet<BlacklistedJwt> BlacklistedJwts { get; init; }
public virtual required DbSet<PasswordResetToken> PasswordResetTokens { get; init; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ public record ExceptionResponse(HttpStatusCode StatusCode, string Description);

public class ExceptionHandlingMiddleware
{
private readonly RequestDelegate _next;
private readonly ILogger<ExceptionHandlingMiddleware> _logger;
private readonly RequestDelegate _next;

public ExceptionHandlingMiddleware(RequestDelegate next, ILogger<ExceptionHandlingMiddleware> logger)
{
Expand All @@ -31,7 +31,7 @@ private async Task HandleExceptionAsync(HttpContext context, Exception exception
{
_logger.LogError(exception, "An unexpected error occurred.");

ExceptionResponse response = exception switch
var response = exception switch
{
BadHttpRequestException e => new ExceptionResponse(HttpStatusCode.BadRequest, e.Message),
KeyNotFoundException e => new ExceptionResponse(HttpStatusCode.NotFound, e.Message),
Expand Down
10 changes: 5 additions & 5 deletions rag-2-backend/Controllers/GameController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,27 @@ public async Task<IEnumerable<GameResponse>> GetGames()
}

/// <summary>
/// (Admin)
/// (Admin)
/// </summary>
[HttpPost]
[Authorize(Roles = "Admin")]
public void Add([FromBody][Required] GameRequest request)
public void Add([FromBody] [Required] GameRequest request)
{
gameService.AddGame(request);
}

/// <summary>
/// (Admin)
/// (Admin)
/// </summary>
[HttpPut("{id:int}")]
[Authorize(Roles = "Admin")]
public void Edit([FromBody][Required] GameRequest request, int id)
public void Edit([FromBody] [Required] GameRequest request, int id)
{
gameService.EditGame(request, id);
}

/// <summary>
/// (Admin) only if no record is connected
/// (Admin)
/// </summary>
[HttpDelete("{id:int}")]
[Authorize(Roles = "Admin")]
Expand Down
8 changes: 4 additions & 4 deletions rag-2-backend/Controllers/GameRecordController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ namespace rag_2_backend.controllers;
public class GameRecordController(GameRecordService gameRecordService) : ControllerBase
{
[HttpGet]
public async Task<IEnumerable<RecordedGameResponse>> GetRecordsByGame([Required] int gameId)
public List<RecordedGameResponse> GetRecordsByGame([Required] int gameId)
{
return await gameRecordService.GetRecordsByGame(gameId);
return gameRecordService.GetRecordsByGame(gameId);
}

/// <summary>
/// (Authenticated)
/// (Authenticated)
/// </summary>
[HttpPost]
[Authorize]
public void AddGameRecord([FromBody][Required] RecordedGameRequest request)
public void AddGameRecord([FromBody] [Required] RecordedGameRequest request)
{
var email = User.FindFirst(ClaimTypes.Email)?.Value ?? throw new KeyNotFoundException("User not found");

Expand Down
119 changes: 87 additions & 32 deletions rag-2-backend/Controllers/UserController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,93 @@
namespace rag_2_backend.controllers;

[ApiController]
[Route("api/[controller]")]
[Route("api/[controller]/auth")]
public class UserController(UserService userService) : ControllerBase
{
[HttpPost("auth/register")]
public void Register([FromBody][Required] UserRequest userRequest)
{
userService.RegisterUser(userRequest);
}

[HttpPost("auth/login")]
public async Task<string> Login([FromBody][Required] UserRequest loginRequest)
{
return await userService.LoginUser(loginRequest.Email, loginRequest.Password);
}

[HttpPost("auth/logout")]
public void Logout()
{
var email = (User.FindFirst(ClaimTypes.Email)?.Value) ?? throw new UnauthorizedAccessException("Unauthorized");

userService.LogoutUser(email);
}

/// <summary>
/// (Autneticated)
/// </summary>
[HttpGet("me")]
[Authorize]
public async Task<UserResponse> Me()
{
var email = (User.FindFirst(ClaimTypes.Email)?.Value) ?? throw new UnauthorizedAccessException("Unauthorized");

return await userService.GetMe(email);
}
[HttpPost("register")]
public void Register([FromBody] [Required] UserRequest userRequest)
{
userService.RegisterUser(userRequest);
}

[HttpPost("login")]
public string Login([FromBody] [Required] UserLoginRequest loginRequest)
{
return userService.LoginUser(loginRequest.Email, loginRequest.Password);
}

[HttpPost("resend-confirmation-email")]
public void ResendConfirmationEmail([Required] string email)
{
userService.ResendConfirmationEmail(email);
}

[HttpPost("confirm-account")]
public void ConfirmAccount([Required] string token)
{
userService.ConfirmAccount(token);
}

[HttpPost("request-password-reset")]
public void RequestPasswordReset([Required] string email)
{
userService.RequestPasswordReset(email);
}

[HttpPost("reset-password")]
public void ResetPassword([Required] string tokenValue, [Required] string newPassword)
{
userService.ResetPassword(tokenValue, newPassword);
}

/// <summary>
/// (Auth)
/// </summary>
[HttpPost("logout")]
[Authorize]
public void Logout()
{
var header = HttpContext.Request.Headers.Authorization.FirstOrDefault() ??
throw new UnauthorizedAccessException("Unauthorized");

userService.LogoutUser(header);
}

/// <summary>
/// (Auth)
/// </summary>
[HttpGet("me")]
[Authorize]
public UserResponse Me()
{
var email = User.FindFirst(ClaimTypes.Email)?.Value ?? throw new UnauthorizedAccessException("Unauthorized");

return userService.GetMe(email);
}

/// <summary>
/// (Auth)
/// </summary>
[HttpPost("change-password")]
[Authorize]
public void ChangePassword([Required] string oldPassword, [Required] string newPassword)
{
var email = User.FindFirst(ClaimTypes.Email)?.Value ?? throw new UnauthorizedAccessException("Unauthorized");

userService.ChangePassword(email, oldPassword, newPassword);
}

/// <summary>
/// (Auth)
/// </summary>
[HttpPost("delete-account")]
[Authorize]
public void DeleteAccount()
{
var email = User.FindFirst(ClaimTypes.Email)?.Value ?? throw new UnauthorizedAccessException("Unauthorized");
var header = HttpContext.Request.Headers.Authorization.FirstOrDefault() ??
throw new UnauthorizedAccessException("Unauthorized");

userService.DeleteAccount(email, header);
}
}
13 changes: 6 additions & 7 deletions rag-2-backend/DTO/GameResponse.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using rag_2_backend.Models;

namespace rag_2_backend.DTO
namespace rag_2_backend.DTO;

public class GameResponse
{
public class GameResponse
{
public int Id { get; init; }
public required string Name { get; init; }
public GameType GameType { get; init; }
}
public int Id { get; init; }
public required string Name { get; init; }
public GameType GameType { get; init; }
}
9 changes: 4 additions & 5 deletions rag-2-backend/DTO/Mapper/GameMapper.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
namespace rag_2_backend.DTO.Mapper;

using rag_2_backend.DTO;
using rag_2_backend.models.entity;

public class GameMapper
namespace rag_2_backend.DTO.Mapper;

public abstract class GameMapper
{
public static GameResponse Map(Game game)
{
Expand All @@ -14,4 +13,4 @@ public static GameResponse Map(Game game)
GameType = game.GameType
};
}
}
}
9 changes: 4 additions & 5 deletions rag-2-backend/DTO/Mapper/RecordedGameMapper.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
namespace rag_2_backend.DTO.Mapper;

using rag_2_backend.DTO;
using rag_2_backend.models.entity;

public class RecordedGameMapper
namespace rag_2_backend.DTO.Mapper;

public abstract class RecordedGameMapper
{
public static RecordedGameResponse Map(RecordedGame recordedGame)
{
Expand All @@ -15,4 +14,4 @@ public static RecordedGameResponse Map(RecordedGame recordedGame)
Value = recordedGame.Value
};
}
}
}
12 changes: 7 additions & 5 deletions rag-2-backend/DTO/Mapper/UserMapper.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
namespace rag_2_backend.DTO.Mapper;
using rag_2_backend.DTO;
using rag_2_backend.Models.Entity;

public class UserMapper
namespace rag_2_backend.DTO.Mapper;

public abstract class UserMapper
{
public static UserResponse Map(User user)
{
return new UserResponse
{
Id = user.Id,
Email = user.Email,
Role = user.Role
Role = user.Role,
StudyCycleYearA = user.StudyCycleYearA,
StudyCycleYearB = user.StudyCycleYearB
};
}
}
}
2 changes: 1 addition & 1 deletion rag-2-backend/DTO/RecordedGameRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ public class RecordedGameRequest
{
public required int GameId { get; init; }
public required string Value { get; init; }
}
}
3 changes: 2 additions & 1 deletion rag-2-backend/DTO/RecordedGameResponse.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
namespace rag_2_backend.DTO;

public class RecordedGameResponse
{
public int Id { get; set; }
public required UserResponse UserResponse { get; set; }
public required GameResponse GameResponse { get; set; }
public required string Value { get; set; }
}
}
7 changes: 7 additions & 0 deletions rag-2-backend/DTO/UserLoginRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace rag_2_backend.DTO;

public class UserLoginRequest
{
public required string Email { get; set; }
public required string Password { get; set; }
}
4 changes: 3 additions & 1 deletion rag-2-backend/DTO/UserRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ public class UserRequest
{
public required string Email { get; set; }
public required string Password { get; set; }
}
public int StudyCycleYearA { get; init; }
public int StudyCycleYearB { get; init; }
}
5 changes: 3 additions & 2 deletions rag-2-backend/DTO/UserResponse.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.ComponentModel.DataAnnotations;
using rag_2_backend.Models;
using rag_2_backend.Models.Entity;

namespace rag_2_backend.DTO;

Expand All @@ -9,4 +8,6 @@ public class UserResponse
[Key] public required int Id { get; set; }
public required string Email { get; set; }
public Role Role { get; set; }
}
public required int StudyCycleYearA { get; set; }
public required int StudyCycleYearB { get; set; }
}
Loading

0 comments on commit fa58898

Please sign in to comment.