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;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-04 01:01:10 +03:00
|
|
|
|
public User GetUserById(int id)
|
2021-12-10 03:04:48 +03:00
|
|
|
|
{
|
2022-08-04 01:01:10 +03:00
|
|
|
|
if (Users.ContainsKey(id)) return Users[id];
|
|
|
|
|
return new User();
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-04 01:01:10 +03:00
|
|
|
|
public 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);
|
2022-08-04 01:01:10 +03:00
|
|
|
|
return Users[userId];
|
2021-12-26 20:43:27 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-04 01:01:10 +03:00
|
|
|
|
return new User();
|
2021-12-10 03:04:48 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
internal bool UserHasTransaction(int userId, int transactionId) => HasUser(userId) && Users[userId].FinanceTransactions.Contains(transactionId);
|
|
|
|
|
|
2022-08-04 01:01:10 +03:00
|
|
|
|
public FinanceTransaction GetTransactionById(int userId, int transactionId)
|
2021-12-10 03:04:48 +03:00
|
|
|
|
{
|
2022-08-04 01:01:10 +03:00
|
|
|
|
if(!HasUser(userId)) return new FinanceTransaction();
|
2021-12-10 03:04:48 +03:00
|
|
|
|
|
2022-08-04 01:01:10 +03:00
|
|
|
|
if (UserHasTransaction(userId, transactionId)) return Users[userId].FinanceTransactions[transactionId];
|
2021-12-10 03:04:48 +03:00
|
|
|
|
|
2022-08-04 01:01:10 +03:00
|
|
|
|
return new FinanceTransaction();
|
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";
|
|
|
|
|
|
2022-08-04 01:01:10 +03:00
|
|
|
|
public 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);
|
2022-08-04 01:01:10 +03:00
|
|
|
|
return financeTransaction;
|
2021-12-10 03:04:48 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-04 01:01:10 +03:00
|
|
|
|
return new FinanceTransaction();
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|