Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

started working on feature user #56

Merged
merged 1 commit into from
Nov 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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


}
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}

}
}
17 changes: 17 additions & 0 deletions backend/UCN-Semester-3-project/RecipesData/Database/IUserAccess.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using RecipesData.Model;

namespace RecipesData.Database
{
public interface IUserAccess
{
User GetUserById(Guid id);

List<User> GetUsers();

Guid CreateUser(User user);

bool UpdateUser(User user);

bool DeleteUser(Guid id);
}
}
Original file line number Diff line number Diff line change
@@ -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<User> GetUsers()
{
List<User> 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<User>();
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;
}
}
}
6 changes: 0 additions & 6 deletions backend/UCN-Semester-3-project/RecipesData/Model/User.cs
Original file line number Diff line number Diff line change
@@ -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
Expand Down