Compare commits
3 Commits
1aa98f2bcf
...
3cbf52f8b2
Author | SHA1 | Date | |
---|---|---|---|
|
3cbf52f8b2 | ||
|
b67f9f944c | ||
|
bc69acd75d |
81
backend/API/BusinessLogic/RecipeLogic.cs
Normal file
81
backend/API/BusinessLogic/RecipeLogic.cs
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
using API.DBAccess;
|
||||||
|
using API.Models.RecipeModels;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
|
namespace API.BusinessLogic
|
||||||
|
{
|
||||||
|
public class RecipeLogic
|
||||||
|
{
|
||||||
|
private readonly RecipeDBaccess _dbAccess;
|
||||||
|
private readonly IConfiguration _configuration;
|
||||||
|
|
||||||
|
public RecipeLogic(IConfiguration configuration, RecipeDBaccess dbAccess)
|
||||||
|
{
|
||||||
|
_dbAccess = dbAccess;
|
||||||
|
_configuration = configuration;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<IActionResult> GetPrefereredRecipes(int userId)
|
||||||
|
{
|
||||||
|
PrefereredRecipes recipes = await _dbAccess.ReadPrefereredRecipes(userId);
|
||||||
|
if (recipes == null) { recipes = await _dbAccess.CreateMissingLists(userId); }
|
||||||
|
if (recipes == null || recipes.Id == 0) { return new ConflictObjectResult(new { message = "Could not find any recipes" }); }
|
||||||
|
|
||||||
|
return new OkObjectResult(recipes);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<IActionResult> GetRecipe(int recipeId)
|
||||||
|
{
|
||||||
|
var recipe = await _dbAccess.ReadRecipe(recipeId);
|
||||||
|
|
||||||
|
if (recipe == null || recipe.Id == 0) { return new ConflictObjectResult(new { message = "Could not find any recipe" }); }
|
||||||
|
|
||||||
|
return new OkObjectResult(recipe);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<IActionResult> CreateRecipe(Recipe recipe, int prefereredRecipesId)
|
||||||
|
{
|
||||||
|
var prefereredRecipes = await _dbAccess.ReadAllRecipe(prefereredRecipesId);
|
||||||
|
|
||||||
|
foreach (var item in prefereredRecipes.Recipes)
|
||||||
|
{
|
||||||
|
if (item.Name == recipe.Name)
|
||||||
|
{
|
||||||
|
return new ConflictObjectResult(new { message = "Recipe name is already in use." });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return await _dbAccess.CreateRecipe(recipe, prefereredRecipesId);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<IActionResult> EditRecipe(Recipe recipe, int recipeId, int userId)
|
||||||
|
{
|
||||||
|
var prefereredRecipes = await _dbAccess.ReadPrefereredRecipes(userId);
|
||||||
|
var dish = await _dbAccess.ReadRecipe(recipeId);
|
||||||
|
|
||||||
|
foreach (var item in prefereredRecipes.Recipes)
|
||||||
|
{
|
||||||
|
if (item.Name == recipe.Name)
|
||||||
|
{
|
||||||
|
return new ConflictObjectResult(new { message = "Recipe name is already in use." });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
dish.Name = recipe.Name;
|
||||||
|
dish.Description = recipe.Description;
|
||||||
|
dish.Directions = recipe.Directions;
|
||||||
|
dish.Ingredients = recipe.Ingredients;
|
||||||
|
|
||||||
|
return await _dbAccess.UpdateRecipe(dish);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<IActionResult> DeleteRecipe(int recipeId)
|
||||||
|
{
|
||||||
|
var recipe = await _dbAccess.ReadRecipe(recipeId);
|
||||||
|
|
||||||
|
if (recipe != null) { return await _dbAccess.DeleteUser(recipe); }
|
||||||
|
|
||||||
|
return new ConflictObjectResult(new { message = "Invalid user" });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,8 +1,8 @@
|
|||||||
using API.DBAccess;
|
using API.DBAccess;
|
||||||
using API.Models.UserModels;
|
using API.Models.UserModels;
|
||||||
|
using API.Models.RecipeModels;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.IdentityModel.Tokens;
|
using Microsoft.IdentityModel.Tokens;
|
||||||
using Org.BouncyCastle.Asn1.Ocsp;
|
|
||||||
using System.IdentityModel.Tokens.Jwt;
|
using System.IdentityModel.Tokens.Jwt;
|
||||||
using System.Security.Claims;
|
using System.Security.Claims;
|
||||||
using System.Security.Cryptography;
|
using System.Security.Cryptography;
|
||||||
@ -13,10 +13,10 @@ namespace API.BusinessLogic
|
|||||||
{
|
{
|
||||||
public class UserLogic
|
public class UserLogic
|
||||||
{
|
{
|
||||||
private readonly DbAccess _dbAccess;
|
private readonly UserDBAccess _dbAccess;
|
||||||
private readonly IConfiguration _configuration;
|
private readonly IConfiguration _configuration;
|
||||||
|
|
||||||
public UserLogic(IConfiguration configuration, DbAccess dbAccess)
|
public UserLogic(IConfiguration configuration, UserDBAccess dbAccess)
|
||||||
{
|
{
|
||||||
_dbAccess = dbAccess;
|
_dbAccess = dbAccess;
|
||||||
_configuration = configuration;
|
_configuration = configuration;
|
||||||
@ -60,12 +60,16 @@ namespace API.BusinessLogic
|
|||||||
string salt = Guid.NewGuid().ToString();
|
string salt = Guid.NewGuid().ToString();
|
||||||
string hashedPassword = ComputeHash(userDTO.Password, SHA256.Create(), salt);
|
string hashedPassword = ComputeHash(userDTO.Password, SHA256.Create(), salt);
|
||||||
|
|
||||||
|
PrefereredRecipes recipes = new PrefereredRecipes();
|
||||||
|
recipes.Recipes = new List<Recipe>();
|
||||||
|
|
||||||
User user = new User
|
User user = new User
|
||||||
{
|
{
|
||||||
UserName = userDTO.UserName,
|
UserName = userDTO.UserName,
|
||||||
Email = userDTO.Email,
|
Email = userDTO.Email,
|
||||||
Password = hashedPassword,
|
Password = hashedPassword,
|
||||||
Salt = salt,
|
Salt = salt,
|
||||||
|
PrefereredRecipes = recipes,
|
||||||
};
|
};
|
||||||
|
|
||||||
return await _dbAccess.CreateUser(user);
|
return await _dbAccess.CreateUser(user);
|
||||||
|
61
backend/API/Controllers/RecipeController.cs
Normal file
61
backend/API/Controllers/RecipeController.cs
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
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> ReadPrefereredRecipes()
|
||||||
|
{
|
||||||
|
var claims = HttpContext.User.Claims;
|
||||||
|
string userIdString = claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value;
|
||||||
|
int userId = Convert.ToInt32(userIdString);
|
||||||
|
return await _recipeLogic.GetPrefereredRecipes(userId);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Authorize]
|
||||||
|
[HttpGet("get/{recipeId}")]
|
||||||
|
public async Task<IActionResult> ReadRecipe(int recipeId)
|
||||||
|
{
|
||||||
|
return await _recipeLogic.GetRecipe(recipeId);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Authorize]
|
||||||
|
[HttpPost("create/{prefereredRecipesId}")]
|
||||||
|
public async Task<IActionResult> CreateRecipe([FromBody] Recipe recipe, int prefereredRecipesId)
|
||||||
|
{
|
||||||
|
return await _recipeLogic.CreateRecipe(recipe, prefereredRecipesId);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Authorize]
|
||||||
|
[HttpPut("edit/{recipeId}")]
|
||||||
|
public async Task<IActionResult> EditRecipe([FromBody] Recipe 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
79
backend/API/DBAccess/RecipeDBaccess.cs
Normal file
79
backend/API/DBAccess/RecipeDBaccess.cs
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
using API.Models.RecipeModels;
|
||||||
|
using API.Models.UserModels;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace API.DBAccess
|
||||||
|
{
|
||||||
|
public class RecipeDBaccess
|
||||||
|
{
|
||||||
|
private readonly DBContext _context;
|
||||||
|
|
||||||
|
public RecipeDBaccess(DBContext context)
|
||||||
|
{
|
||||||
|
_context = context;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<PrefereredRecipes> ReadPrefereredRecipes(int userId)
|
||||||
|
{
|
||||||
|
var recipes = await _context.Users.Include(u => u.PrefereredRecipes).ThenInclude(p => p.Recipes).FirstOrDefaultAsync(u => u.Id == userId);
|
||||||
|
|
||||||
|
return recipes.PrefereredRecipes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<PrefereredRecipes> ReadAllRecipe(int prefereredRecipesId)
|
||||||
|
{
|
||||||
|
return await _context.PrefereredRecipes.Include(p => p.Recipes).FirstOrDefaultAsync(r => r.Id == prefereredRecipesId);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<Recipe> ReadRecipe(int recipeId)
|
||||||
|
{
|
||||||
|
return await _context.Recipes.Include(r => r.Ingredients).FirstOrDefaultAsync(r => r.Id == recipeId);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<PrefereredRecipes> CreateMissingLists(int userId)
|
||||||
|
{
|
||||||
|
var user = await _context.Users.FirstOrDefaultAsync(u => u.Id == userId);
|
||||||
|
user.PrefereredRecipes = new PrefereredRecipes();
|
||||||
|
user.PrefereredRecipes.Recipes = new List<Recipe>();
|
||||||
|
|
||||||
|
_context.SaveChangesAsync();
|
||||||
|
|
||||||
|
return await ReadPrefereredRecipes(userId);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<IActionResult> CreateRecipe(Recipe recipe, int prefereredRecipeId)
|
||||||
|
{
|
||||||
|
var recipes = await _context.PrefereredRecipes.Include(p => p.Recipes).FirstOrDefaultAsync(p => p.Id == prefereredRecipeId);
|
||||||
|
|
||||||
|
recipes.Recipes.Add(recipe);
|
||||||
|
|
||||||
|
bool saved = await _context.SaveChangesAsync() > 1;
|
||||||
|
|
||||||
|
if (saved) { return new OkObjectResult(saved); }
|
||||||
|
|
||||||
|
return new ConflictObjectResult(new { message = "Could not save to database" });
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<IActionResult> UpdateRecipe(Recipe recipe)
|
||||||
|
{
|
||||||
|
_context.Entry(recipe).State = EntityState.Modified;
|
||||||
|
|
||||||
|
bool saved = await _context.SaveChangesAsync() >= 1;
|
||||||
|
|
||||||
|
if (saved) { return new OkObjectResult(recipe); }
|
||||||
|
|
||||||
|
return new ConflictObjectResult(new { message = "Could not save to database" });
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<IActionResult> DeleteUser(Recipe recipe)
|
||||||
|
{
|
||||||
|
_context.Recipes.Remove(recipe);
|
||||||
|
bool saved = await _context.SaveChangesAsync() >= 0;
|
||||||
|
|
||||||
|
if (saved) { return new OkObjectResult(saved); }
|
||||||
|
|
||||||
|
return new ConflictObjectResult(new { message = "Could not save to database" });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -4,11 +4,11 @@ using Microsoft.EntityFrameworkCore;
|
|||||||
|
|
||||||
namespace API.DBAccess
|
namespace API.DBAccess
|
||||||
{
|
{
|
||||||
public class DbAccess
|
public class UserDBAccess
|
||||||
{
|
{
|
||||||
private readonly DBContext _context;
|
private readonly DBContext _context;
|
||||||
|
|
||||||
public DbAccess(DBContext context)
|
public UserDBAccess(DBContext context)
|
||||||
{
|
{
|
||||||
_context = context;
|
_context = context;
|
||||||
}
|
}
|
||||||
@ -66,7 +66,6 @@ namespace API.DBAccess
|
|||||||
if (saved) { return new OkObjectResult(user); }
|
if (saved) { return new OkObjectResult(user); }
|
||||||
|
|
||||||
return new ConflictObjectResult(new { message = "Could not save to database" });
|
return new ConflictObjectResult(new { message = "Could not save to database" });
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IActionResult> UpdatePassword(User user)
|
public async Task<IActionResult> UpdatePassword(User user)
|
@ -25,8 +25,10 @@ namespace API
|
|||||||
services.AddDbContext<DBContext>(options =>
|
services.AddDbContext<DBContext>(options =>
|
||||||
options.UseMySQL(_configuration.GetConnectionString("Database")));
|
options.UseMySQL(_configuration.GetConnectionString("Database")));
|
||||||
|
|
||||||
services.AddScoped<DbAccess>();
|
services.AddScoped<UserDBAccess>();
|
||||||
services.AddScoped<UserLogic>();
|
services.AddScoped<UserLogic>();
|
||||||
|
services.AddScoped<RecipeDBaccess>();
|
||||||
|
services.AddScoped<RecipeLogic>();
|
||||||
|
|
||||||
services.AddControllers();
|
services.AddControllers();
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user