-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #56 from MaartenVercammen/feature/user
started working on feature user
- Loading branch information
Showing
5 changed files
with
153 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
backend/UCN-Semester-3-project/RecipeDataTest/UserDataAccessTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
17
backend/UCN-Semester-3-project/RecipesData/Database/IUserAccess.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
104 changes: 104 additions & 0 deletions
104
backend/UCN-Semester-3-project/RecipesData/Database/UserDatabaseAccess.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters