diff --git a/backend/API/BusinessLogic/RecipeLogic.cs b/backend/API/BusinessLogic/RecipeLogic.cs index 88b307f..8d02900 100644 --- a/backend/API/BusinessLogic/RecipeLogic.cs +++ b/backend/API/BusinessLogic/RecipeLogic.cs @@ -13,6 +13,11 @@ namespace API.BusinessLogic _dbAccess = dbAccess; } + /// + /// Gets all the recipes from _dbaccess and checks if there are any + /// + /// + /// public async Task GetRecipes(int userId) { var recipes = await _dbAccess.ReadRecipes(userId); @@ -21,6 +26,7 @@ namespace API.BusinessLogic return new OkObjectResult(recipes); } + // public async Task GetRecipe(int recipeId) { var recipe = await _dbAccess.ReadRecipe(recipeId); diff --git a/backend/API/BusinessLogic/UserLogic.cs b/backend/API/BusinessLogic/UserLogic.cs index 1e3604b..c0c2f92 100644 --- a/backend/API/BusinessLogic/UserLogic.cs +++ b/backend/API/BusinessLogic/UserLogic.cs @@ -43,25 +43,20 @@ namespace API.BusinessLogic return new ConflictObjectResult(new { message = "Password is not up to the security standard" }); } - var users = await _dbAccess.ReadAllUsers(); - - foreach (var item in users) + if (await _dbAccess.UserNameInUse(userDTO.UserName)) { - if (item.UserName == userDTO.UserName) - { - return new ConflictObjectResult(new { message = "Username is already in use." }); - } + return new ConflictObjectResult(new { message = "Username is already in use." }); + } - if (item.Email == userDTO.Email) - { - return new ConflictObjectResult(new { message = "Email is already in use." }); - } + if (await _dbAccess.EmailInUse(userDTO.Email)) + { + return new ConflictObjectResult(new { message = "Email is already in use." }); } string salt = Guid.NewGuid().ToString(); string hashedPassword = ComputeHash(userDTO.Password, SHA256.Create(), salt); - + User user = new User { @@ -99,7 +94,6 @@ namespace API.BusinessLogic public async Task EditProfile(UpdateUserDTO userDTO, int userId) { var profile = await _dbAccess.ReadUser(userId); - var users = await _dbAccess.ReadAllUsers(); if (profile == null) { return new ConflictObjectResult(new { message = "User does not exist" }); } @@ -108,19 +102,6 @@ namespace API.BusinessLogic return new ConflictObjectResult(new { message = "Invalid email address" }); } - foreach (var item in users) - { - if (item.UserName == userDTO.UserName) - { - return new ConflictObjectResult(new { message = "Username is already in use." }); - } - - if (item.Email == userDTO.Email) - { - return new ConflictObjectResult(new { message = "Email is already in use." }); - } - } - if (userDTO.Email == "" || userDTO.Email == null) { return new ConflictObjectResult(new { message = "Please enter an email" }); @@ -131,6 +112,19 @@ namespace API.BusinessLogic return new ConflictObjectResult(new { message = "Please enter an userName" }); } + + if (await _dbAccess.UserNameInUse(userDTO.UserName)) + { + return new ConflictObjectResult(new { message = "Username is already in use." }); + } + + if (await _dbAccess.EmailInUse(userDTO.Email)) + { + return new ConflictObjectResult(new { message = "Email is already in use." }); + } + + + profile.Email = userDTO.Email; profile.UserName = userDTO.UserName; diff --git a/backend/API/DBAccess/UserDBAccess.cs b/backend/API/DBAccess/UserDBAccess.cs index dfbc59e..9118433 100644 --- a/backend/API/DBAccess/UserDBAccess.cs +++ b/backend/API/DBAccess/UserDBAccess.cs @@ -23,6 +23,20 @@ namespace API.DBAccess return await _context.Users.ToListAsync(); } + public async Task UserNameInUse(string username) + { + var user = await _context.Users.FirstOrDefaultAsync(u => u.UserName == username); + if (user == null || user.UserName == null || user.UserName != username) { return false; } + return true; + } + + public async Task EmailInUse(string email) + { + var user = await _context.Users.FirstOrDefaultAsync(u => u.UserName == email); + if (user == null || user.Email == null || user.Email != email) { return false; } + return true; + } + public async Task ReadUserByRefreshToken(string refreshToken) { return await _context.Users.FirstOrDefaultAsync(u => u.RefreshToken == refreshToken); diff --git a/backend/API/Services/OpenAiRecipes.cs b/backend/API/Services/OpenAiRecipes.cs index 32d0a5a..2e70375 100644 --- a/backend/API/Services/OpenAiRecipes.cs +++ b/backend/API/Services/OpenAiRecipes.cs @@ -19,6 +19,7 @@ namespace API.Services model: _configuration["OpenAI:Model"], apiKey: _configuration["OpenAI:APIKey"] ); + string allergi = ""; if (recipeDTO.Allergi.Count != 0) @@ -35,6 +36,7 @@ namespace API.Services string jsonStructure = "{ name: string, description: string, directions: [string, string, ...], ingredients: [ { amount: number?, unit: string?, name: string }]}"; + // The messages for chat gpt List messages = [ new SystemChatMessage($"You are a a helpful assistant. You give back {recipeDTO.NumberOfRecipes} recipes on the dish that you have been given. " + $"Answer in the language {recipeDTO.Language}. " + @@ -44,6 +46,7 @@ namespace API.Services new UserChatMessage(recipeDTO.Dish), ]; + // Here we send the mess messages to chatgpt ChatCompletion chat = await client.CompleteChatAsync(messages); return chat;