From a8e740f4fd56c09c69480c45ab97f92d1e95331d Mon Sep 17 00:00:00 2001 From: CodeLiturgy Date: Wed, 3 Aug 2022 23:01:10 +0100 Subject: [PATCH] Get rid of DataQueryObject --- BlueWest.Data/BlueWest.Data.csproj | 2 +- BlueWest.Data/DataQueryResult.cs | 35 ------------------- BlueWest.Data/User/User.cs | 9 +++-- BlueWest.Data/UserList.cs | 27 +++++++------- BlueWest.WebApi/Controllers/UserController.cs | 15 ++++---- BlueWest/Data/MemoryData.cs | 4 +-- 6 files changed, 30 insertions(+), 62 deletions(-) delete mode 100644 BlueWest.Data/DataQueryResult.cs diff --git a/BlueWest.Data/BlueWest.Data.csproj b/BlueWest.Data/BlueWest.Data.csproj index ef18cfb..89effba 100644 --- a/BlueWest.Data/BlueWest.Data.csproj +++ b/BlueWest.Data/BlueWest.Data.csproj @@ -13,7 +13,7 @@ - + diff --git a/BlueWest.Data/DataQueryResult.cs b/BlueWest.Data/DataQueryResult.cs deleted file mode 100644 index b9ecc04..0000000 --- a/BlueWest.Data/DataQueryResult.cs +++ /dev/null @@ -1,35 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace BlueWest.Data -{ - public sealed class DataQueryResult - { - public readonly T Value; - public bool HasValue; - public string Message; - - public DataQueryResult(T value, bool success = true, string message = "") - { - Value = value; - HasValue = success; - Message = message; - } - } - internal sealed class DataQueryResult - { - public readonly object Value; - public bool HasValue; - public string Message; - - public DataQueryResult(object value, bool success = true, string message = "") - { - Value = value; - HasValue = success; - Message = message; - } - } - - -} diff --git a/BlueWest.Data/User/User.cs b/BlueWest.Data/User/User.cs index 57b3d7f..2f8fc67 100644 --- a/BlueWest.Data/User/User.cs +++ b/BlueWest.Data/User/User.cs @@ -13,9 +13,9 @@ namespace BlueWest.Data [MessagePackObject] [UseUpdate] [MapFrom(typeof(UserUpdateDto))] - public partial class User + public partial class User { - [Key(1)] public TimeSpan Id { get; } + [Key(1)] public TimeSpan Id { get; } = TimeSpan.Zero; [Key(2)] public string Name { get; set; } @@ -28,6 +28,11 @@ namespace BlueWest.Data FinanceTransactions = financeTransactions; } + public User() + { + + } + public void AddTransaction(FinanceTransaction financeTransaction) { FinanceTransactions.Add(FinanceTransactions.Count + 1, financeTransaction); diff --git a/BlueWest.Data/UserList.cs b/BlueWest.Data/UserList.cs index 3ecc55d..7050ae4 100644 --- a/BlueWest.Data/UserList.cs +++ b/BlueWest.Data/UserList.cs @@ -31,10 +31,10 @@ namespace BlueWest.Data return -1; } - public DataQueryResult GetUserById(int id) + public User GetUserById(int 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."); + if (Users.ContainsKey(id)) return Users[id]; + return new User(); } public bool RemoveUser(int userId) @@ -53,27 +53,27 @@ namespace BlueWest.Data return Users.Contains(userId); } - public DataQueryResult UpdateUser(int userId, UserUpdateDto userUpdate) + public User UpdateUser(int userId, UserUpdateDto userUpdate) { if (Users.ContainsKey(userId)) { Users[userId].Update(userUpdate); - return new DataQueryResult(Users[userId], true); + return Users[userId]; } - return new DataQueryResult(null, false); + return new User(); } internal bool UserHasTransaction(int userId, int transactionId) => HasUser(userId) && Users[userId].FinanceTransactions.Contains(transactionId); - public DataQueryResult GetTransactionById(int userId, int transactionId) + public FinanceTransaction GetTransactionById(int userId, int transactionId) { - if(!HasUser(userId)) return new DataQueryResult(new FinanceTransaction(), false, "Can't find a user with the provided id."); + if(!HasUser(userId)) return new FinanceTransaction(); - if (UserHasTransaction(userId, transactionId)) return new DataQueryResult(Users[userId].FinanceTransactions[transactionId]); + if (UserHasTransaction(userId, transactionId)) return Users[userId].FinanceTransactions[transactionId]; - return new DataQueryResult(new FinanceTransaction(), false, "Can't find a transaction with the provided id."); + return new FinanceTransaction(); } public User AddUser(int userId, UserUpdateDto userUpdateDto) @@ -97,18 +97,17 @@ namespace BlueWest.Data const string NoIdError = "No transaction was found with the specified id"; - public DataQueryResult AddFinanceTransaction(int userId, FinanceTransactionInsertDto financeTransactionDto) + public FinanceTransaction AddFinanceTransaction(int userId, FinanceTransactionInsertDto financeTransactionDto) { if (Users.ContainsKey(userId)) { var now = TimeSpan.FromTicks(DateTime.Now.Ticks); var financeTransaction = new FinanceTransaction(financeTransactionDto, now, Users[userId].Id); Users[userId].AddTransaction(financeTransaction); - return new DataQueryResult(financeTransaction); + return financeTransaction; } - return new DataQueryResult(new FinanceTransaction(), false, - "no transaction with the provided id"); + return new FinanceTransaction(); } diff --git a/BlueWest.WebApi/Controllers/UserController.cs b/BlueWest.WebApi/Controllers/UserController.cs index aafc23b..693f89d 100644 --- a/BlueWest.WebApi/Controllers/UserController.cs +++ b/BlueWest.WebApi/Controllers/UserController.cs @@ -33,12 +33,11 @@ namespace BlueWest.WebApi.Controllers [HttpGet("{userId}", Name = nameof(GetUserById))] public ActionResult GetUserById(int userId) { - DataQueryResult user = MemoryData.GetUserById(userId); + var user = MemoryData.GetUserById(userId); - if (user.HasValue) + if (user.Id != TimeSpan.Zero) { - return Ok(user.Value); - + return Ok(user); } return new NotFoundResult(); @@ -62,9 +61,9 @@ namespace BlueWest.WebApi.Controllers { var user = MemoryData.UserList.GetUserById(userId); - if(!user.HasValue) return new NotFoundResult(); + if(user.Id == TimeSpan.Zero) return new NotFoundResult(); - var transactions = user?.Value.FinanceTransactions; + var transactions = user.FinanceTransactions; FinanceTransaction? transaction = transactions?.Values.ToImmutableArray().FirstOrNull(t => t.CreationDate == transactionId); @@ -117,9 +116,9 @@ namespace BlueWest.WebApi.Controllers [HttpPost("{userId}/transactions")] public ActionResult PostTransaction(int userId, FinanceTransactionInsertDto financeTransaction) { - DataQueryResult result = MemoryData.UserList.AddFinanceTransaction(userId, financeTransaction); + FinanceTransaction result = MemoryData.UserList.AddFinanceTransaction(userId, financeTransaction); - if(result.HasValue) return Ok(result.Value); + if(result.UserId != TimeSpan.Zero) return Ok(result); return new BadRequestResult(); } diff --git a/BlueWest/Data/MemoryData.cs b/BlueWest/Data/MemoryData.cs index 59a166c..1aef72c 100644 --- a/BlueWest/Data/MemoryData.cs +++ b/BlueWest/Data/MemoryData.cs @@ -39,7 +39,7 @@ namespace PerformanceSolution.Data UserList ??= new UserList(new FastDictionary()); } - public static DataQueryResult GetTransactionById(int userId, int transactionId) => UserList.GetTransactionById(userId, transactionId); + public static FinanceTransaction GetTransactionById(int userId, int transactionId) => UserList.GetTransactionById(userId, transactionId); private static void SaveUserList() => SaveUserList(UserList); @@ -67,7 +67,7 @@ namespace PerformanceSolution.Data return new UserList(list); } - public static DataQueryResult GetUserById(int userId) + public static User GetUserById(int userId) { return UserList.GetUserById(userId); }