Get rid of DataQueryObject

This commit is contained in:
CodeLiturgy 2022-08-03 23:01:10 +01:00
parent ee5d91c61d
commit a8e740f4fd
6 changed files with 30 additions and 62 deletions

View File

@ -1,35 +0,0 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace BlueWest.Data
{
public sealed class DataQueryResult<T>
{
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;
}
}
}

View File

@ -15,7 +15,7 @@ namespace BlueWest.Data
[MapFrom(typeof(UserUpdateDto))]
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);

View File

@ -31,10 +31,10 @@ namespace BlueWest.Data
return -1;
}
public DataQueryResult<User> GetUserById(int id)
public User GetUserById(int id)
{
if (Users.ContainsKey(id)) return new DataQueryResult<User>(Users[id], true);
return new DataQueryResult<User>(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<User> UpdateUser(int userId, UserUpdateDto userUpdate)
public User UpdateUser(int userId, UserUpdateDto userUpdate)
{
if (Users.ContainsKey(userId))
{
Users[userId].Update(userUpdate);
return new DataQueryResult<User>(Users[userId], true);
return Users[userId];
}
return new DataQueryResult<User>(null, false);
return new User();
}
internal bool UserHasTransaction(int userId, int transactionId) => HasUser(userId) && Users[userId].FinanceTransactions.Contains(transactionId);
public DataQueryResult<FinanceTransaction> GetTransactionById(int userId, int transactionId)
public FinanceTransaction GetTransactionById(int userId, int transactionId)
{
if(!HasUser(userId)) return new DataQueryResult<FinanceTransaction>(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<FinanceTransaction>(Users[userId].FinanceTransactions[transactionId]);
if (UserHasTransaction(userId, transactionId)) return Users[userId].FinanceTransactions[transactionId];
return new DataQueryResult<FinanceTransaction>(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<FinanceTransaction> 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>(financeTransaction);
return financeTransaction;
}
return new DataQueryResult<FinanceTransaction>(new FinanceTransaction(), false,
"no transaction with the provided id");
return new FinanceTransaction();
}

View File

@ -33,12 +33,11 @@ namespace BlueWest.WebApi.Controllers
[HttpGet("{userId}", Name = nameof(GetUserById))]
public ActionResult GetUserById(int userId)
{
DataQueryResult<User> 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<FinanceTransaction> 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();
}

View File

@ -39,7 +39,7 @@ namespace PerformanceSolution.Data
UserList ??= new UserList(new FastDictionary<int, User>());
}
public static DataQueryResult<FinanceTransaction> 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<User> GetUserById(int userId)
public static User GetUserById(int userId)
{
return UserList.GetUserById(userId);
}