62 lines
1.9 KiB
C#
62 lines
1.9 KiB
C#
using API.BusinessLogic;
|
|
using API.Models.RecipeModels;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using System.Security.Claims;
|
|
|
|
namespace API.Controllers
|
|
{
|
|
[ApiController]
|
|
[Route("api/[controller]")]
|
|
public class RecipeController :Controller
|
|
{
|
|
private readonly RecipeLogic _recipeLogic;
|
|
|
|
public RecipeController(RecipeLogic recipeLogic)
|
|
{
|
|
_recipeLogic = recipeLogic;
|
|
}
|
|
|
|
[Authorize]
|
|
[HttpGet("getall")]
|
|
public async Task<IActionResult> ReadRecipes()
|
|
{
|
|
var claims = HttpContext.User.Claims;
|
|
string userIdString = claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value;
|
|
int userId = Convert.ToInt32(userIdString);
|
|
return await _recipeLogic.GetRecipes(userId);
|
|
}
|
|
|
|
[Authorize]
|
|
[HttpGet("get/{recipeId}")]
|
|
public async Task<IActionResult> ReadRecipe(int recipeId)
|
|
{
|
|
return await _recipeLogic.GetRecipe(recipeId);
|
|
}
|
|
|
|
[Authorize]
|
|
[HttpPost("create/{RecipesId}")]
|
|
public async Task<IActionResult> CreateRecipe([FromBody] RecipeDTO recipe, int RecipesId)
|
|
{
|
|
return await _recipeLogic.CreateRecipe(recipe, RecipesId);
|
|
}
|
|
|
|
[Authorize]
|
|
[HttpPut("edit/{recipeId}")]
|
|
public async Task<IActionResult> EditRecipe([FromBody] RecipeDTO recipe, int recipeId)
|
|
{
|
|
var claims = HttpContext.User.Claims;
|
|
string userIdString = claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value;
|
|
int userId = Convert.ToInt32(userIdString);
|
|
return await _recipeLogic.EditRecipe(recipe, recipeId, userId);
|
|
}
|
|
|
|
[Authorize]
|
|
[HttpDelete("delete/{recipeId}")]
|
|
public async Task<IActionResult> DeleteRecipe(int recipeId)
|
|
{
|
|
return await _recipeLogic.DeleteRecipe(recipeId);
|
|
}
|
|
}
|
|
}
|