Initial commit
This commit is contained in:
100
MealPlan.Api/Controllers/AuthController.cs
Normal file
100
MealPlan.Api/Controllers/AuthController.cs
Normal file
@@ -0,0 +1,100 @@
|
||||
using System.Security.Claims;
|
||||
using MealPlan.Api.DTOs.Auth;
|
||||
using MealPlan.Api.Services;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.RateLimiting;
|
||||
|
||||
namespace MealPlan.Api.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/auth")]
|
||||
public class AuthController : ControllerBase
|
||||
{
|
||||
private readonly IAuthService _authService;
|
||||
|
||||
public AuthController(IAuthService authService) => _authService = authService;
|
||||
|
||||
/// <summary>Creates a new user account and returns an access + refresh token pair.</summary>
|
||||
[HttpPost("register")]
|
||||
[AllowAnonymous]
|
||||
[EnableRateLimiting("login")]
|
||||
[ProducesResponseType(typeof(TokenResponseDto), StatusCodes.Status201Created)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status409Conflict)]
|
||||
public async Task<IActionResult> Register([FromBody] RegisterRequestDto request, CancellationToken ct)
|
||||
{
|
||||
var result = await _authService.RegisterAsync(request, ct);
|
||||
if (!result.Succeeded)
|
||||
{
|
||||
var status = result.Error?.Contains("already exists", StringComparison.OrdinalIgnoreCase) == true
|
||||
? StatusCodes.Status409Conflict
|
||||
: StatusCodes.Status400BadRequest;
|
||||
return StatusCode(status, new { error = result.Error });
|
||||
}
|
||||
|
||||
return StatusCode(StatusCodes.Status201Created, result.Tokens);
|
||||
}
|
||||
|
||||
/// <summary>Authenticates a user and returns an access + refresh token pair.</summary>
|
||||
[HttpPost("login")]
|
||||
[AllowAnonymous]
|
||||
[EnableRateLimiting("login")]
|
||||
[ProducesResponseType(typeof(TokenResponseDto), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
public async Task<IActionResult> Login([FromBody] LoginRequestDto request, CancellationToken ct)
|
||||
{
|
||||
var result = await _authService.LoginAsync(request, ct);
|
||||
if (!result.Succeeded)
|
||||
{
|
||||
return Unauthorized(new { error = result.Error });
|
||||
}
|
||||
|
||||
return Ok(result.Tokens);
|
||||
}
|
||||
|
||||
/// <summary>Rotates a refresh token and issues a fresh token pair.</summary>
|
||||
[HttpPost("refresh")]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(typeof(TokenResponseDto), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
public async Task<IActionResult> Refresh([FromBody] RefreshRequestDto request, CancellationToken ct)
|
||||
{
|
||||
var result = await _authService.RefreshAsync(request.RefreshToken, ct);
|
||||
if (!result.Succeeded)
|
||||
{
|
||||
return Unauthorized(new { error = result.Error });
|
||||
}
|
||||
|
||||
return Ok(result.Tokens);
|
||||
}
|
||||
|
||||
/// <summary>Revokes the supplied refresh token. Always returns 204.</summary>
|
||||
[HttpPost("logout")]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||
public IActionResult Logout([FromBody] RefreshRequestDto request)
|
||||
{
|
||||
_authService.Logout(request.RefreshToken);
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
/// <summary>Returns the currently authenticated user's profile.</summary>
|
||||
[HttpGet("me")]
|
||||
[Authorize]
|
||||
[ProducesResponseType(typeof(UserInfoDto), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
public async Task<IActionResult> Me(CancellationToken ct)
|
||||
{
|
||||
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier)
|
||||
?? User.FindFirstValue("sub");
|
||||
|
||||
if (string.IsNullOrEmpty(userId))
|
||||
{
|
||||
return Unauthorized();
|
||||
}
|
||||
|
||||
var info = await _authService.GetUserInfoAsync(userId, ct);
|
||||
return info is null ? Unauthorized() : Ok(info);
|
||||
}
|
||||
}
|
||||
54
MealPlan.Api/Controllers/RecipesController.cs
Normal file
54
MealPlan.Api/Controllers/RecipesController.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
using MealPlan.Api.DTOs.Recipe;
|
||||
using MealPlan.Api.Services;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace MealPlan.Api.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/recipes")]
|
||||
[Authorize]
|
||||
public class RecipesController : ControllerBase
|
||||
{
|
||||
private readonly IRecipeService _recipeService;
|
||||
|
||||
public RecipesController(IRecipeService recipeService) => _recipeService = recipeService;
|
||||
|
||||
/// <summary>Lists active recipes, optionally filtered by category and/or name search.</summary>
|
||||
[HttpGet]
|
||||
[ProducesResponseType(typeof(IReadOnlyList<RecipeListItemDto>), StatusCodes.Status200OK)]
|
||||
public async Task<IActionResult> GetRecipes(
|
||||
[FromQuery] int? category,
|
||||
[FromQuery] string? search,
|
||||
CancellationToken ct)
|
||||
{
|
||||
if (category is < 0 or > 3)
|
||||
{
|
||||
return BadRequest(new { error = "category must be between 0 and 3." });
|
||||
}
|
||||
|
||||
var recipes = await _recipeService.GetRecipesAsync(category, search, ct);
|
||||
return Ok(recipes);
|
||||
}
|
||||
|
||||
/// <summary>Returns category metadata with active recipe counts.</summary>
|
||||
[HttpGet("categories")]
|
||||
[ProducesResponseType(typeof(IReadOnlyList<CategoryCountDto>), StatusCodes.Status200OK)]
|
||||
public async Task<IActionResult> GetCategories(CancellationToken ct)
|
||||
{
|
||||
var categories = await _recipeService.GetCategoriesAsync(ct);
|
||||
return Ok(categories);
|
||||
}
|
||||
|
||||
/// <summary>Returns a single recipe with ingredients and ordered steps.</summary>
|
||||
[HttpGet("{id:int}")]
|
||||
[ProducesResponseType(typeof(RecipeDetailDto), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<IActionResult> GetRecipe(int id, CancellationToken ct)
|
||||
{
|
||||
var recipe = await _recipeService.GetRecipeByIdAsync(id, ct);
|
||||
return recipe is null
|
||||
? NotFound(new { error = $"Recipe {id} was not found." })
|
||||
: Ok(recipe);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user