This commit is contained in:
Rui Sousa 2021-12-07 23:57:27 +00:00
parent ceb451c5f1
commit 3151ab9c56
6 changed files with 49 additions and 67 deletions

View File

@ -7,7 +7,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="MapTo" Version="0.8.13" /> <PackageReference Include="MapTo" Version="0.8.3" />
<PackageReference Include="MessagePack" Version="2.3.85" /> <PackageReference Include="MessagePack" Version="2.3.85" />
<PackageReference Include="Newtonsoft.Json" Version="6.0.4" /> <PackageReference Include="Newtonsoft.Json" Version="6.0.4" />
</ItemGroup> </ItemGroup>

View File

@ -11,7 +11,7 @@ namespace BlueWest.Data
[MapFrom(typeof(UserUpdateDto))] [MapFrom(typeof(UserUpdateDto))]
public partial class User : DataObject public partial class User : DataObject
{ {
[Key(1)]public int Id { get; set; } = -1; [Key(1)]public int Id { get; } = -1;
[Key(2)]public string Name { get; set; } = ""; [Key(2)]public string Name { get; set; } = "";
[Key(3)]public string Address { get; set; } = ""; [Key(3)]public string Address { get; set; } = "";
@ -21,8 +21,7 @@ namespace BlueWest.Data
[Key(6)]public double BTCAmount { get; set; } = 0; [Key(6)]public double BTCAmount { get; set; } = 0;
[Key(7)]public double LTCAmount { get; set; } = 0; [Key(7)]public double LTCAmount { get; set; } = 0;
[IgnoreProperty] [IgnoreProperty] [Key(8)] public List<FinanceTransaction> FinanceTransactions { get; set; }
[Key(8)] public List<FinanceTransaction> FinanceTransactions { get; set; }
public User(int id, string name, string address, string btcAddress, string ltcAddress, double btcAmount, double ltcAmount, List<FinanceTransaction> financeTransactions) public User(int id, string name, string address, string btcAddress, string ltcAddress, double btcAmount, double ltcAmount, List<FinanceTransaction> financeTransactions)
{ {
@ -36,6 +35,11 @@ namespace BlueWest.Data
FinanceTransactions = financeTransactions; FinanceTransactions = financeTransactions;
} }
public void AddTransaction(FinanceTransaction financeTransaction)
{
FinanceTransactions.Add(financeTransaction);
}
} }
} }

View File

@ -15,11 +15,7 @@ namespace BlueWest.Data
public double BTCAmount { get; set; } public double BTCAmount { get; set; }
public double LTCAmount { get; set; } public double LTCAmount { get; set; }
public UserUpdateDto()
{
}
} }
} }

View File

@ -82,7 +82,7 @@ namespace BlueWest.WebApi.Controllers
[HttpPost] [HttpPost]
public ActionResult AddOrModifyUser(UserUpdateDto userUpdateDto) public ActionResult AddOrModifyUser(UserUpdateDto userUpdateDto)
{ {
var user = MemoryData.AddOrModify(userUpdateDto); var user = MemoryData.AddOrModifyUser(userUpdateDto);
return CreatedAtRoute(nameof(GetUserById), new {userId = user.Id}, user); return CreatedAtRoute(nameof(GetUserById), new {userId = user.Id}, user);
} }

View File

@ -18,7 +18,20 @@ namespace BlueWest.WebApi
new EventManager(new FastDictionary<Type, FastList<EventListenerBase>>(10000)); new EventManager(new FastDictionary<Type, FastList<EventListenerBase>>(10000));
private static ThreadServer _threadServer; private static ThreadServer _threadServer;
/*private static CSharpCompilation GenerateCode(string sourceCode)
{
var codeString = SourceText.From(sourceCode);
var parsedSyntaxTree = SyntaxFactory.ParseSyntaxTree(codeString, CSharpParseOptions.Default);
return CSharpCompilation.Create("Hello.dll",
new[] { parsedSyntaxTree },
references: ReferenceAssemblies.Net50, // install jared's Basic.Reference.Assemblies for this, otherwise, you are going to manually add the DLLs you want
options: new CSharpCompilationOptions(OutputKind.ConsoleApplication,
optimizationLevel: OptimizationLevel.Release));
}*/
public static IHost Host1 { get; private set; } public static IHost Host1 { get; private set; }
public static void Main(string[] args) public static void Main(string[] args)

View File

@ -55,8 +55,13 @@ namespace PerformanceSolution.Data
SaveUserList(); SaveUserList();
return true; return true;
} }
public static async Task AddUserAsync(User user) => await Task.Run(() => { AddOrModifyUser(user); });
private static int GetIndexById(int userId) => UserList.Users.FindIndex(row => row.Id == userId);
public static void AddOrModify(User user) private static void SaveUserList() => SaveUserList(UserList);
public static void AddOrModifyUser(User user)
{ {
var index = GetIndexById(user.Id); var index = GetIndexById(user.Id);
if (index != -1) if (index != -1)
@ -71,29 +76,28 @@ namespace PerformanceSolution.Data
} }
} }
public static User AddOrModify(UserUpdateDto userUpdateDto, int userId = -1) public static User? AddOrModifyUser(UserUpdateDto userUpdateDto, int userId = -1)
{ {
var id = userId != -1 ? userId : UserList.Length + 1; var id = userId != -1 ? userId : UserList.Length + 1;
var index = GetIndexById(userId); var index = GetIndexById(userId);
User user = null; User? user;
if (index != -1) if (index != -1)
{ {
user = GetUserById(id); user = GetUserById(id);
if (user == null) return user;
if (user != null) UserList.Users[index] = user;
{
UserList.Users[index] = user; SaveUserList();
SaveUserList();
}
} }
else else
{ {
user = new User(userUpdateDto); user = new User(userUpdateDto);
user.Id = id;
UserList.Users.Add(user); UserList.Users.Add(user);
SaveUserList(); SaveUserList();
@ -104,12 +108,13 @@ namespace PerformanceSolution.Data
public static User? UpdateUser(int userId, UserUpdateDto userUpdate) public static User? UpdateUser(int userId, UserUpdateDto userUpdate)
{ {
var index = GetIndexById(userId); var index = GetIndexById(userId);
if (index == -1) return null; if (index == -1) return null;
var actualUser = GetUserById(userId); var actualUser = GetUserById(userId);
if (actualUser == null) return null;
actualUser.Update(userUpdate); actualUser.Update(userUpdate);
@ -120,56 +125,20 @@ namespace PerformanceSolution.Data
return actualUser; return actualUser;
} }
public static async Task AddUserAsync(User user)
{
await Task.Run(() => { AddOrModify(user); });
}
private static int GetIndexById(int userId)
{
return UserList.Users.FindIndex(row => row.Id == userId);
}
private static void SaveUserList()
{
SaveUserList(UserList);
}
public static FinanceTransaction? AddFinanceTransaction(int userId, FinanceTransaction financeTransaction) public static FinanceTransaction? AddFinanceTransaction(int userId, FinanceTransaction financeTransaction)
{ {
var index = GetIndexById(userId); var user = GetUserById(userId);
if (index < UserList.Users.Count) if (user == null) return null;
{
if (financeTransaction.Id == 0) user.AddTransaction(financeTransaction);
{
financeTransaction.Id = UserList.Users[index].FinanceTransactions.Count;
}
if (financeTransaction.UserId == 0)
{
financeTransaction.UserId = UserList.Users[index].Id;
}
if (UserList.Users[index].FinanceTransactions == null)
{
UserList.Users[index].FinanceTransactions = new List<FinanceTransaction>();
}
UserList.Users[index].FinanceTransactions.Add(financeTransaction);
SaveUserList();
return financeTransaction;
}
return null;
}
public static void GenerateAdminAndSave()
{
SaveUserList(); SaveUserList();
return financeTransaction;
} }
private static UserList GenerateMockData() private static UserList GenerateMockData()