From a97cdab9b011648ba7bb9ef9e0bd1c2df50dac27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Sabol?= Date: Wed, 23 Nov 2022 12:56:01 +0100 Subject: [PATCH] started working on feature user --- .../SwipedRecipeDataAccessTest.cs | 20 +--- .../RecipeDataTest/UserDataAccessTest.cs | 31 ++++++ .../RecipesData/Database/IUserAccess.cs | 17 +++ .../Database/UserDatabaseAccess.cs | 104 ++++++++++++++++++ .../RecipesData/Model/User.cs | 6 - 5 files changed, 153 insertions(+), 25 deletions(-) create mode 100644 backend/UCN-Semester-3-project/RecipeDataTest/UserDataAccessTest.cs create mode 100644 backend/UCN-Semester-3-project/RecipesData/Database/IUserAccess.cs create mode 100644 backend/UCN-Semester-3-project/RecipesData/Database/UserDatabaseAccess.cs diff --git a/backend/UCN-Semester-3-project/RecipeDataTest/SwipedRecipeDataAccessTest.cs b/backend/UCN-Semester-3-project/RecipeDataTest/SwipedRecipeDataAccessTest.cs index 1086cee..22103ba 100644 --- a/backend/UCN-Semester-3-project/RecipeDataTest/SwipedRecipeDataAccessTest.cs +++ b/backend/UCN-Semester-3-project/RecipeDataTest/SwipedRecipeDataAccessTest.cs @@ -17,25 +17,7 @@ public SwipedRecipeDataAccessTest(ITestOutputHelper output) //IRecipeAccess recipeAccess = new RecipeDatabaseAccess(_connectionString); } - [Fact] - public void TestGetSRById() - { - Guid recipeId = new Guid("04c74da5-3035-4a4a-b732-77b40fa4ab17"); - SwipedRecipe retrievedSRecipe = _swipedRecipeAccess.GetSwipedRecipeById(recipeId); - Assert.Equal(recipeId, retrievedSRecipe.RecipeId); - } - - [Fact] - public void CreateSR() - { - Guid recipeId = new Guid("b4ea22a7-8fea-4e27-b359-7dd2ce8da8ae"); - User author = new User(Guid.Parse("00000000-0000-0000-0000-000000000000"), "mail", "mark", "mark", "pass", "street", Role.USER); - SwipedRecipe swipedRecipe = new SwipedRecipe(author.UserId, recipeId, true); - _swipedRecipeAccess.CreateSwipedRecipe(swipedRecipe); - //SwipedRecipe retrievedSRecipe = _swipedRecipeAccess.GetSRById(author.UserId); - Assert.Equal(recipeId, swipedRecipe.RecipeId); - _swipedRecipeAccess.DeleteSR(recipeId); - } + // TODO: Do the tests } diff --git a/backend/UCN-Semester-3-project/RecipeDataTest/UserDataAccessTest.cs b/backend/UCN-Semester-3-project/RecipeDataTest/UserDataAccessTest.cs new file mode 100644 index 0000000..3e8befc --- /dev/null +++ b/backend/UCN-Semester-3-project/RecipeDataTest/UserDataAccessTest.cs @@ -0,0 +1,31 @@ +using Xunit.Abstractions; +using RecipesData.Database; +using RecipesData.Model; + +namespace RecipeDataTest +{ + public class UserDataAccessTest + { + + private readonly ITestOutputHelper extraOutput; + readonly private IUserAccess _userAccess; + readonly string _connectionString = "data Source=foodpanda.dev,1400; Database=ucn; User Id=dev;Password=dev;"; + public UserDataAccessTest(ITestOutputHelper output) + { + this.extraOutput = output; + _userAccess = new UserDatabaseAccess(_connectionString); + //IRecipeAccess recipeAccess = new RecipeDatabaseAccess(_connectionString); + } + + + [Fact] + public void TestGetUserById() + { + Guid id = new Guid("00000000-0000-0000-0000-000000000000"); + User user = null; + user = _userAccess.GetUserById(id); + Assert.NotNull(user); + } + + } +} \ No newline at end of file diff --git a/backend/UCN-Semester-3-project/RecipesData/Database/IUserAccess.cs b/backend/UCN-Semester-3-project/RecipesData/Database/IUserAccess.cs new file mode 100644 index 0000000..f508942 --- /dev/null +++ b/backend/UCN-Semester-3-project/RecipesData/Database/IUserAccess.cs @@ -0,0 +1,17 @@ +using RecipesData.Model; + +namespace RecipesData.Database +{ + public interface IUserAccess + { + User GetUserById(Guid id); + + List GetUsers(); + + Guid CreateUser(User user); + + bool UpdateUser(User user); + + bool DeleteUser(Guid id); + } +} diff --git a/backend/UCN-Semester-3-project/RecipesData/Database/UserDatabaseAccess.cs b/backend/UCN-Semester-3-project/RecipesData/Database/UserDatabaseAccess.cs new file mode 100644 index 0000000..6899e50 --- /dev/null +++ b/backend/UCN-Semester-3-project/RecipesData/Database/UserDatabaseAccess.cs @@ -0,0 +1,104 @@ +using Microsoft.Extensions.Configuration; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using RecipesData.Model; +using System.Data.SqlClient; +using System.Data; + +namespace RecipesData.Database +{ + public class UserDatabaseAccess : IUserAccess + { + readonly string _connectionString; + + public UserDatabaseAccess(IConfiguration configuration) + { + _connectionString = configuration.GetConnectionString("UcnConnection"); + } + + public UserDatabaseAccess(string connetionstring) + { + _connectionString = connetionstring; + } + + public Guid CreateUser(User user) + { + throw new NotImplementedException(); + } + + public bool DeleteUser(Guid id) + { + throw new NotImplementedException(); + } + + public User GetUserById(Guid id) + { + string guidString = id.ToString(); + User user = new User(); + using (SqlConnection connection = new SqlConnection(_connectionString)) + { + connection.Open(); + using (SqlCommand command = connection.CreateCommand()) + { + command.CommandText = "select * from [user] where userId =@userId"; + command.Parameters.AddWithValue("@userId", guidString); + using (SqlDataReader reader = command.ExecuteReader()) + { + if (reader.Read()) + { + user = BuildObject(reader); + } + reader.Close(); + } + } + return user; + } + } + + public List GetUsers() + { + List foundUsers; + User readUser; + + string queryString = "SELECT userId FROM user"; + using (SqlConnection con = new SqlConnection(_connectionString)) + using (SqlCommand readCommand = new SqlCommand(queryString, con)) + { + con.Open(); + // Execute read + SqlDataReader userReader = readCommand.ExecuteReader(); + // Collect data + foundUsers = new List(); + while (userReader.Read()) + { + readUser = GetUserById(Guid.Parse(userReader.GetString(userReader.GetOrdinal("userID")))); + foundUsers.Add(readUser); + } + + con.Close(); + } + return foundUsers; + } + + public bool UpdateUser(User user) + { + throw new NotImplementedException(); + } + + // private + private User BuildObject(SqlDataReader reader) + { + User user = new User(); + user.UserId = Guid.Parse(reader.GetString(reader.GetOrdinal("userId"))); + user.Email = reader.GetString(reader.GetOrdinal("email")); + user.FirstName = reader.GetString(reader.GetOrdinal("firstName")); + user.LastName = reader.GetString(reader.GetOrdinal("lastName")); + user.Password = reader.GetString(reader.GetOrdinal("password")); + user.Address = reader.GetString(reader.GetOrdinal("address")); + return user; + } + } +} diff --git a/backend/UCN-Semester-3-project/RecipesData/Model/User.cs b/backend/UCN-Semester-3-project/RecipesData/Model/User.cs index b3f94eb..2c45c5b 100644 --- a/backend/UCN-Semester-3-project/RecipesData/Model/User.cs +++ b/backend/UCN-Semester-3-project/RecipesData/Model/User.cs @@ -1,9 +1,3 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - namespace RecipesData.Model { public enum Role