2022-08-01 00:09:39 +03:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2021-12-10 03:04:48 +03:00
|
|
|
|
using System.Collections.Immutable;
|
|
|
|
|
using System.Transactions;
|
|
|
|
|
using BlueWest.Collections;
|
|
|
|
|
using MessagePack;
|
|
|
|
|
|
|
|
|
|
namespace BlueWest.Data
|
|
|
|
|
{
|
|
|
|
|
[MessagePackObject]
|
|
|
|
|
public class UserList: DataObject
|
|
|
|
|
{
|
|
|
|
|
[Key(9)] public readonly FastDictionary<int, User> Users;
|
|
|
|
|
|
2021-12-26 20:43:27 +03:00
|
|
|
|
[IgnoreMember] public int Length = 0;
|
2021-12-10 03:04:48 +03:00
|
|
|
|
|
|
|
|
|
public UserList(FastDictionary<int, User> users)
|
|
|
|
|
{
|
|
|
|
|
Users = users;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int GetIndexById(int userId)
|
|
|
|
|
{
|
|
|
|
|
var keys = Users.Keys;
|
|
|
|
|
|
|
|
|
|
foreach (var key in keys)
|
|
|
|
|
{
|
|
|
|
|
if (key == userId) return key;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-26 20:43:27 +03:00
|
|
|
|
public DataQueryResult<User> GetUserById(int id)
|
2021-12-10 03:04:48 +03:00
|
|
|
|
{
|
2021-12-26 20:43:27 +03:00
|
|
|
|
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.");
|
2021-12-10 03:04:48 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-26 20:43:27 +03:00
|
|
|
|
public bool RemoveUser(int userId)
|
2021-12-10 03:04:48 +03:00
|
|
|
|
{
|
|
|
|
|
if (Users.ContainsKey(userId))
|
|
|
|
|
{
|
|
|
|
|
Users.Remove(userId);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal bool HasUser(int userId)
|
|
|
|
|
{
|
|
|
|
|
return Users.Contains(userId);
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-26 20:43:27 +03:00
|
|
|
|
public DataQueryResult<User> UpdateUser(int userId, UserUpdateDto userUpdate)
|
2021-12-10 03:04:48 +03:00
|
|
|
|
{
|
2021-12-26 20:43:27 +03:00
|
|
|
|
if (Users.ContainsKey(userId))
|
|
|
|
|
{
|
|
|
|
|
Users[userId].Update(userUpdate);
|
|
|
|
|
return new DataQueryResult<User>(Users[userId], true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new DataQueryResult<User>(null, false);
|
2021-12-10 03:04:48 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
internal bool UserHasTransaction(int userId, int transactionId) => HasUser(userId) && Users[userId].FinanceTransactions.Contains(transactionId);
|
|
|
|
|
|
2021-12-26 20:43:27 +03:00
|
|
|
|
public DataQueryResult<FinanceTransaction> GetTransactionById(int userId, int transactionId)
|
2021-12-10 03:04:48 +03:00
|
|
|
|
{
|
2021-12-26 20:43:27 +03:00
|
|
|
|
if(!HasUser(userId)) return new DataQueryResult<FinanceTransaction>(new FinanceTransaction(), false, "Can't find a user with the provided id.");
|
2021-12-10 03:04:48 +03:00
|
|
|
|
|
2021-12-26 20:43:27 +03:00
|
|
|
|
if (UserHasTransaction(userId, transactionId)) return new DataQueryResult<FinanceTransaction>(Users[userId].FinanceTransactions[transactionId]);
|
2021-12-10 03:04:48 +03:00
|
|
|
|
|
2021-12-26 20:43:27 +03:00
|
|
|
|
return new DataQueryResult<FinanceTransaction>(new FinanceTransaction(), false, "Can't find a transaction with the provided id.");
|
2021-12-10 03:04:48 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-26 20:43:27 +03:00
|
|
|
|
public User AddUser(int userId, UserUpdateDto userUpdateDto)
|
2021-12-10 03:04:48 +03:00
|
|
|
|
{
|
|
|
|
|
if (Users.ContainsKey(userId))
|
|
|
|
|
{
|
|
|
|
|
Users[userId].Update(userUpdateDto);
|
|
|
|
|
return Users[userId];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var id = userId != -1 ? userId : Length + 1;
|
|
|
|
|
|
2022-08-04 00:52:49 +03:00
|
|
|
|
var newUser = new User(userUpdateDto, DateTime.Now.TimeOfDay, new FastDictionary<int, FinanceTransaction>());
|
2021-12-10 03:04:48 +03:00
|
|
|
|
|
|
|
|
|
Users.Add(Length, newUser);
|
|
|
|
|
|
|
|
|
|
Length++;
|
|
|
|
|
|
|
|
|
|
return newUser;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const string NoIdError = "No transaction was found with the specified id";
|
|
|
|
|
|
2021-12-26 20:43:27 +03:00
|
|
|
|
public DataQueryResult<FinanceTransaction> AddFinanceTransaction(int userId, FinanceTransactionInsertDto financeTransactionDto)
|
2021-12-10 03:04:48 +03:00
|
|
|
|
{
|
|
|
|
|
if (Users.ContainsKey(userId))
|
|
|
|
|
{
|
2022-08-01 00:09:39 +03:00
|
|
|
|
var now = TimeSpan.FromTicks(DateTime.Now.Ticks);
|
|
|
|
|
var financeTransaction = new FinanceTransaction(financeTransactionDto, now, Users[userId].Id);
|
2021-12-10 03:04:48 +03:00
|
|
|
|
Users[userId].AddTransaction(financeTransaction);
|
2021-12-26 20:43:27 +03:00
|
|
|
|
return new DataQueryResult<FinanceTransaction>(financeTransaction);
|
2021-12-10 03:04:48 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-26 20:43:27 +03:00
|
|
|
|
return new DataQueryResult<FinanceTransaction>(new FinanceTransaction(), false,
|
|
|
|
|
"no transaction with the provided id");
|
2021-12-10 03:04:48 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-12-26 20:43:27 +03:00
|
|
|
|
public ImmutableArray<FinanceTransaction> GetUserTransactions(int userId)
|
2021-12-10 03:04:48 +03:00
|
|
|
|
{
|
|
|
|
|
if (Users.ContainsKey(userId))
|
|
|
|
|
{
|
2021-12-26 20:43:27 +03:00
|
|
|
|
return Users[userId].FinanceTransactions.Values.ToImmutableArray();
|
2021-12-10 03:04:48 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-26 20:43:27 +03:00
|
|
|
|
return new ImmutableArray<FinanceTransaction>();
|
2021-12-10 03:04:48 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|