diff --git a/BlueWest.Data/DataQueryResult.cs b/BlueWest.Data/DataQueryResult.cs index 64fc9c7..b9ecc04 100644 --- a/BlueWest.Data/DataQueryResult.cs +++ b/BlueWest.Data/DataQueryResult.cs @@ -4,25 +4,32 @@ using System.Text; namespace BlueWest.Data { - class DataQueryResult { } - - internal sealed class PositiveResult : DataQueryResult + public sealed class DataQueryResult { public readonly T Value; + public bool HasValue; + public string Message; - public PositiveResult(T value) + public DataQueryResult(T value, bool success = true, string message = "") { Value = value; + HasValue = success; + Message = message; } } - - - internal sealed class NegativeResult : DataQueryResult + internal sealed class DataQueryResult { - public readonly string UserMessage; - public NegativeResult(string userMessage) + public readonly object Value; + public bool HasValue; + public string Message; + + public DataQueryResult(object value, bool success = true, string message = "") { - UserMessage = userMessage; + Value = value; + HasValue = success; + Message = message; } } + + } diff --git a/BlueWest.Data/User/User.cs b/BlueWest.Data/User/User.cs index 2062814..3e9e56d 100644 --- a/BlueWest.Data/User/User.cs +++ b/BlueWest.Data/User/User.cs @@ -25,9 +25,9 @@ namespace BlueWest.Data [Key(6)] public double BTCAmount { get; set; } [Key(7)] public double LTCAmount { get; set; } - [Key(8)] public FastDictionary FinanceTransactions { get; set; } + [Key(8)] public FastDictionary FinanceTransactions { get; } - public User(int id, string name, string address, string btcAddress, string ltcAddress, double btcAmount, double ltcAmount, FastList financeTransactions) + public User(int id, string name, string address, string btcAddress, string ltcAddress, double btcAmount, double ltcAmount, FastDictionary financeTransactions) { Id = id; Name = name; @@ -41,11 +41,11 @@ namespace BlueWest.Data public void AddTransaction(FinanceTransaction financeTransaction) { - FinanceTransactions.Add(financeTransaction); + FinanceTransactions.Add(FinanceTransactions.Count + 1, financeTransaction); } public void AddTransaction(FinanceTransactionInsertDto financeTransaction) { - FinanceTransactions.Add(new FinanceTransaction(financeTransaction)); + FinanceTransactions.Add(FinanceTransactions.Count + 1, new FinanceTransaction(financeTransaction, FinanceTransactions.Count + 1)); } internal bool HasTransaction(int transactionId) @@ -53,9 +53,9 @@ namespace BlueWest.Data return FinanceTransactions.Contains(transactionId); } - internal Transaction GetTransactionById(int transactionId) + internal FinanceTransaction GetTransactionById(int transactionId) { - return FinanceTransactions(transactionId); + return FinanceTransactions[transactionId]; } } } diff --git a/BlueWest.Data/UserList.cs b/BlueWest.Data/UserList.cs index a6c9730..48ff02b 100644 --- a/BlueWest.Data/UserList.cs +++ b/BlueWest.Data/UserList.cs @@ -11,7 +11,7 @@ namespace BlueWest.Data { [Key(9)] public readonly FastDictionary Users; - public int Length = 0; + [IgnoreMember] public int Length = 0; public UserList(FastDictionary users) { @@ -30,13 +30,13 @@ namespace BlueWest.Data return -1; } - internal DataQueryResult GetUserById(int id) + public DataQueryResult GetUserById(int id) { - if (Users.ContainsKey(id)) return new PositiveResult(Users[id]); - return new NegativeResult("Can't find a user with the provided id."); + if (Users.ContainsKey(id)) return new DataQueryResult(Users[id], true); + return new DataQueryResult(null, false, "Can't find a user with the provided id."); } - internal bool RemoveUser(int userId) + public bool RemoveUser(int userId) { if (Users.ContainsKey(userId)) { @@ -52,25 +52,30 @@ namespace BlueWest.Data return Users.Contains(userId); } - internal DataQueryResult UpdateUser(int userId, UserUpdateDto userUpdate) + public DataQueryResult UpdateUser(int userId, UserUpdateDto userUpdate) { - Users[userId].Update(userUpdate); - return Users[userId]; + if (Users.ContainsKey(userId)) + { + Users[userId].Update(userUpdate); + return new DataQueryResult(Users[userId], true); + } + + return new DataQueryResult(null, false); } internal bool UserHasTransaction(int userId, int transactionId) => HasUser(userId) && Users[userId].FinanceTransactions.Contains(transactionId); - internal DataQueryResult GetTransactionById(int userId, int transactionId) + public DataQueryResult GetTransactionById(int userId, int transactionId) { - if(!HasUser(userId)) return new NegativeResult("Can't find a user with the provided id."); + if(!HasUser(userId)) return new DataQueryResult(new FinanceTransaction(), false, "Can't find a user with the provided id."); - if (UserHasTransaction(userId, transactionId)) return new PositiveResult(Users[userId].FinanceTransactions[transactionId]); + if (UserHasTransaction(userId, transactionId)) return new DataQueryResult(Users[userId].FinanceTransactions[transactionId]); - return new NegativeResult("No transaction was found with the specified id"); + return new DataQueryResult(new FinanceTransaction(), false, "Can't find a transaction with the provided id."); } - internal User AddUser(int userId, UserUpdateDto userUpdateDto) + public User AddUser(int userId, UserUpdateDto userUpdateDto) { if (Users.ContainsKey(userId)) { @@ -80,7 +85,7 @@ namespace BlueWest.Data var id = userId != -1 ? userId : Length + 1; - var newUser = new User(userUpdateDto, id, new FastList()); + var newUser = new User(userUpdateDto); Users.Add(Length, newUser); @@ -91,28 +96,28 @@ namespace BlueWest.Data const string NoIdError = "No transaction was found with the specified id"; - static NegativeResult NoIdResult = new NegativeResult(NoIdError); - internal DataQueryResult AddFinanceTransaction(int userId, FinanceTransactionInsertDto financeTransactionDto) + public DataQueryResult AddFinanceTransaction(int userId, FinanceTransactionInsertDto financeTransactionDto) { if (Users.ContainsKey(userId)) { - var financeTransaction = new FinanceTransaction(financeTransactionDto); + var financeTransaction = new FinanceTransaction(financeTransactionDto, Users[userId].FinanceTransactions.Count + 1); Users[userId].AddTransaction(financeTransaction); - return new PositiveResult(financeTransaction); + return new DataQueryResult(financeTransaction); } - return NoIdResult; + return new DataQueryResult(new FinanceTransaction(), false, + "no transaction with the provided id"); } - internal FastList GetUserTransactions(int userId) + public ImmutableArray GetUserTransactions(int userId) { if (Users.ContainsKey(userId)) { - return Users[userId].FinanceTransactions; + return Users[userId].FinanceTransactions.Values.ToImmutableArray(); } - return new FastList(); + return new ImmutableArray(); } } diff --git a/BlueWest.WebApi/BlueWest.WebApi.csproj b/BlueWest.WebApi/BlueWest.WebApi.csproj index 6383267..efd5660 100644 --- a/BlueWest.WebApi/BlueWest.WebApi.csproj +++ b/BlueWest.WebApi/BlueWest.WebApi.csproj @@ -1,7 +1,7 @@ - net5.0-windows + net6.0 10 diff --git a/BlueWest.WebApi/Controllers/UserController.cs b/BlueWest.WebApi/Controllers/UserController.cs index 5eef681..35e9741 100644 --- a/BlueWest.WebApi/Controllers/UserController.cs +++ b/BlueWest.WebApi/Controllers/UserController.cs @@ -1,4 +1,5 @@ -using BlueWest.Core.Tests; +using System.Collections.Immutable; +using BlueWest.Collections; using BlueWest.Data; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; @@ -18,7 +19,7 @@ namespace BlueWest.WebApi.Controllers { if (_userList != null) { - return Ok(_userList); + return Ok(_userList.Users.Values.ToImmutableArray()); } @@ -31,9 +32,9 @@ namespace BlueWest.WebApi.Controllers [HttpGet("{userId}", Name = nameof(GetUserById))] public ActionResult GetUserById(int userId) { - User? user = MemoryData.GetUserById(userId); + DataQueryResult user = MemoryData.GetUserById(userId); - if (user != null) + if (user.HasValue) { return Ok(user.Value); @@ -49,7 +50,7 @@ namespace BlueWest.WebApi.Controllers public ActionResult GetTransactions(int userId) { var transactions = MemoryData.GetUserTransactions(userId); - return OK(transactions.ToDictionary()); + return Ok(transactions); } @@ -60,9 +61,11 @@ namespace BlueWest.WebApi.Controllers { var user = MemoryData.UserList.GetUserById(userId); - var transactions = user?.FinanceTransactions; + if(!user.HasValue) return new NotFoundResult(); + + var transactions = user?.Value.FinanceTransactions; - FinanceTransaction? transaction = transactions?.FirstOrNull(t => t.Id == transactionId); + FinanceTransaction? transaction = transactions?.Values.ToImmutableArray().FirstOrNull(t => t.Id == transactionId); if (transaction != null) { @@ -113,9 +116,9 @@ namespace BlueWest.WebApi.Controllers [HttpPost("{userId}/transactions")] public ActionResult PostTransaction(int userId, FinanceTransactionInsertDto financeTransaction) { - FinanceTransaction? result = MemoryData.UserList.AddFinanceTransaction(userId, financeTransaction); + DataQueryResult result = MemoryData.UserList.AddFinanceTransaction(userId, financeTransaction); - if(result != null) return Ok(result); + if(result.HasValue) return Ok(result.Value); return new BadRequestResult(); } diff --git a/BlueWest/BlueWest.csproj b/BlueWest/BlueWest.csproj index d79aeea..010b6bf 100644 --- a/BlueWest/BlueWest.csproj +++ b/BlueWest/BlueWest.csproj @@ -1,7 +1,7 @@ - net5.0 + net6.0 enable disable PerformanceSolution diff --git a/BlueWest/Core/Tests/MappingExtensions.cs b/BlueWest/Core/Tests/MappingExtensions.cs index 961a488..57cac46 100644 --- a/BlueWest/Core/Tests/MappingExtensions.cs +++ b/BlueWest/Core/Tests/MappingExtensions.cs @@ -3,7 +3,6 @@ using System.Collections.Generic; using System.ComponentModel; using System.Dynamic; using System.Linq; -using BlueWest.Core.Tests; using BlueWest.DataAgent; namespace BlueWest.Core diff --git a/BlueWest/Core/Tests/PocoDictionary.cs b/BlueWest/Core/Tests/PocoDictionary.cs index 41055e6..5f28270 100644 --- a/BlueWest/Core/Tests/PocoDictionary.cs +++ b/BlueWest/Core/Tests/PocoDictionary.cs @@ -1,44 +1 @@ -using System; -using System.Collections.Concurrent; -using System.Collections.Generic; -using System.Linq; -using System.Linq.Expressions; -using System.Reflection; - -namespace BlueWest.Core.Tests -{ - public static class PocoToDictionary -{ - private static readonly MethodInfo AddToDicitonaryMethod = typeof(IDictionary).GetMethod("Add"); - private static readonly ConcurrentDictionary>> Converters = new ConcurrentDictionary>>(); - private static readonly ConstructorInfo DictionaryConstructor = typeof(Dictionary).GetConstructors().FirstOrDefault(c => c.IsPublic && !c.GetParameters().Any()); - - public static IDictionary ToDictionary(this object obj) => obj == null ? null : Converters.GetOrAdd(obj.GetType(), o => - { - var outputType = typeof(IDictionary); - var inputType = obj.GetType(); - var inputExpression = Expression.Parameter(typeof(object), "input"); - var typedInputExpression = Expression.Convert(inputExpression, inputType); - var outputVariable = Expression.Variable(outputType, "output"); - var returnTarget = Expression.Label(outputType); - var body = new List - { - Expression.Assign(outputVariable, Expression.New(DictionaryConstructor)) - }; - body.AddRange( - from prop in inputType.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy) - where prop.CanRead && (prop.PropertyType.IsPrimitive || prop.PropertyType == typeof(int)) - let getExpression = Expression.Property(typedInputExpression, prop.GetMethod) - let convertExpression = Expression.Convert(getExpression, typeof(object)) - select Expression.Call(outputVariable, AddToDicitonaryMethod, Expression.Constant(prop.Name), convertExpression)); - body.Add(Expression.Return(returnTarget, outputVariable)); - body.Add(Expression.Label(returnTarget, Expression.Constant(null, outputType))); - - var lambdaExpression = Expression.Lambda>>( - Expression.Block(new[] { outputVariable }, body), - inputExpression); - - return lambdaExpression.Compile(); - })(obj); -} -} \ No newline at end of file + \ No newline at end of file diff --git a/BlueWest/Data/MemoryData.cs b/BlueWest/Data/MemoryData.cs index 15450a7..2a05ad3 100644 --- a/BlueWest/Data/MemoryData.cs +++ b/BlueWest/Data/MemoryData.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Collections.Immutable; using System.Transactions; using BlueWest.Collections; using BlueWest.Data; @@ -20,16 +21,16 @@ namespace PerformanceSolution.Data if (DEBUG) { UserList = GenerateMockData(); - SaveUserList(); + //SaveUserList(); } - LoadUsers(); + //LoadUsers(); } public static void SaveUserList(UserList userList) { - UserList = userList; - SaveLoadManager.Save(UserList, SavePathName); + //UserList = userList; + //SaveLoadManager.Save(UserList, SavePathName); } public static void LoadUsers() @@ -38,7 +39,7 @@ namespace PerformanceSolution.Data UserList ??= new UserList(new FastDictionary()); } - public static Transaction GetTransactionById(int userId, int transactionId) => UserList.GetTransactionById(userId, transactionId); + public static DataQueryResult GetTransactionById(int userId, int transactionId) => UserList.GetTransactionById(userId, transactionId); private static void SaveUserList() => SaveUserList(UserList); @@ -53,10 +54,10 @@ namespace PerformanceSolution.Data { - var transactions = new FastList(); + var transactions = new FastDictionary(); var financeTransaction = new FinanceTransaction(0, 1, FinanceTransactionType.Buy, FinanceSymbol.BTC_EUR, 0, 0, 0.1, DateTime.Now); - transactions.Add(financeTransaction); + transactions.Add(1, financeTransaction); var u = new User(1, "Rui Sousa", "Sagres", "NOADD", "NOADD", 0, 0, transactions); var list = new FastDictionary(10); @@ -65,12 +66,12 @@ namespace PerformanceSolution.Data return new UserList(list); } - public static User? GetUserById(int userId) + public static DataQueryResult GetUserById(int userId) { return UserList.GetUserById(userId); } - public static FastList GetUserTransactions(int userId) + public static ImmutableArray GetUserTransactions(int userId) { return UserList.GetUserTransactions(userId); } diff --git a/MapTo b/MapTo index 9470afb..ffc3dc7 160000 --- a/MapTo +++ b/MapTo @@ -1 +1 @@ -Subproject commit 9470afb737d873f2fbbc631813eadf10032c5c6b +Subproject commit ffc3dc7729c97190849441d73edb2dfc5fe63ecd