Some comment and a fix for checking if a username or email is in use
This commit is contained in:
parent
16d1d3459d
commit
b0bb534eff
@ -13,6 +13,11 @@ namespace API.BusinessLogic
|
|||||||
_dbAccess = dbAccess;
|
_dbAccess = dbAccess;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets all the recipes from _dbaccess and checks if there are any
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="userId"></param>
|
||||||
|
/// <returns></returns>
|
||||||
public async Task<IActionResult> GetRecipes(int userId)
|
public async Task<IActionResult> GetRecipes(int userId)
|
||||||
{
|
{
|
||||||
var recipes = await _dbAccess.ReadRecipes(userId);
|
var recipes = await _dbAccess.ReadRecipes(userId);
|
||||||
@ -21,6 +26,7 @@ namespace API.BusinessLogic
|
|||||||
return new OkObjectResult(recipes);
|
return new OkObjectResult(recipes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//
|
||||||
public async Task<IActionResult> GetRecipe(int recipeId)
|
public async Task<IActionResult> GetRecipe(int recipeId)
|
||||||
{
|
{
|
||||||
var recipe = await _dbAccess.ReadRecipe(recipeId);
|
var recipe = await _dbAccess.ReadRecipe(recipeId);
|
||||||
|
@ -43,25 +43,20 @@ namespace API.BusinessLogic
|
|||||||
return new ConflictObjectResult(new { message = "Password is not up to the security standard" });
|
return new ConflictObjectResult(new { message = "Password is not up to the security standard" });
|
||||||
}
|
}
|
||||||
|
|
||||||
var users = await _dbAccess.ReadAllUsers();
|
if (await _dbAccess.UserNameInUse(userDTO.UserName))
|
||||||
|
|
||||||
foreach (var item in users)
|
|
||||||
{
|
{
|
||||||
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)
|
if (await _dbAccess.EmailInUse(userDTO.Email))
|
||||||
{
|
{
|
||||||
return new ConflictObjectResult(new { message = "Email is already in use." });
|
return new ConflictObjectResult(new { message = "Email is already in use." });
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
User user = new User
|
User user = new User
|
||||||
{
|
{
|
||||||
@ -99,7 +94,6 @@ namespace API.BusinessLogic
|
|||||||
public async Task<IActionResult> EditProfile(UpdateUserDTO userDTO, int userId)
|
public async Task<IActionResult> EditProfile(UpdateUserDTO userDTO, int userId)
|
||||||
{
|
{
|
||||||
var profile = await _dbAccess.ReadUser(userId);
|
var profile = await _dbAccess.ReadUser(userId);
|
||||||
var users = await _dbAccess.ReadAllUsers();
|
|
||||||
|
|
||||||
if (profile == null) { return new ConflictObjectResult(new { message = "User does not exist" }); }
|
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" });
|
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)
|
if (userDTO.Email == "" || userDTO.Email == null)
|
||||||
{
|
{
|
||||||
return new ConflictObjectResult(new { message = "Please enter an email" });
|
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" });
|
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.Email = userDTO.Email;
|
||||||
profile.UserName = userDTO.UserName;
|
profile.UserName = userDTO.UserName;
|
||||||
|
|
||||||
|
@ -23,6 +23,20 @@ namespace API.DBAccess
|
|||||||
return await _context.Users.ToListAsync();
|
return await _context.Users.ToListAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<bool> 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<bool> 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<User> ReadUserByRefreshToken(string refreshToken)
|
public async Task<User> ReadUserByRefreshToken(string refreshToken)
|
||||||
{
|
{
|
||||||
return await _context.Users.FirstOrDefaultAsync(u => u.RefreshToken == refreshToken);
|
return await _context.Users.FirstOrDefaultAsync(u => u.RefreshToken == refreshToken);
|
||||||
|
@ -19,6 +19,7 @@ namespace API.Services
|
|||||||
model: _configuration["OpenAI:Model"],
|
model: _configuration["OpenAI:Model"],
|
||||||
apiKey: _configuration["OpenAI:APIKey"]
|
apiKey: _configuration["OpenAI:APIKey"]
|
||||||
);
|
);
|
||||||
|
|
||||||
string allergi = "";
|
string allergi = "";
|
||||||
|
|
||||||
if (recipeDTO.Allergi.Count != 0)
|
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 }]}";
|
string jsonStructure = "{ name: string, description: string, directions: [string, string, ...], ingredients: [ { amount: number?, unit: string?, name: string }]}";
|
||||||
|
|
||||||
|
// The messages for chat gpt
|
||||||
List<ChatMessage> messages = [
|
List<ChatMessage> messages = [
|
||||||
new SystemChatMessage($"You are a a helpful assistant. You give back {recipeDTO.NumberOfRecipes} recipes on the dish that you have been given. " +
|
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}. " +
|
$"Answer in the language {recipeDTO.Language}. " +
|
||||||
@ -44,6 +46,7 @@ namespace API.Services
|
|||||||
new UserChatMessage(recipeDTO.Dish),
|
new UserChatMessage(recipeDTO.Dish),
|
||||||
];
|
];
|
||||||
|
|
||||||
|
// Here we send the mess messages to chatgpt
|
||||||
ChatCompletion chat = await client.CompleteChatAsync(messages);
|
ChatCompletion chat = await client.CompleteChatAsync(messages);
|
||||||
|
|
||||||
return chat;
|
return chat;
|
||||||
|
Loading…
Reference in New Issue
Block a user