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>
<ItemGroup>
<PackageReference Include="MapTo" Version="0.8.13" />
<PackageReference Include="MapTo" Version="0.8.3" />
<PackageReference Include="MessagePack" Version="2.3.85" />
<PackageReference Include="Newtonsoft.Json" Version="6.0.4" />
</ItemGroup>

View File

@ -11,7 +11,7 @@ namespace BlueWest.Data
[MapFrom(typeof(UserUpdateDto))]
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(3)]public string Address { get; set; } = "";
@ -21,8 +21,7 @@ namespace BlueWest.Data
[Key(6)]public double BTCAmount { get; set; } = 0;
[Key(7)]public double LTCAmount { get; set; } = 0;
[IgnoreProperty]
[Key(8)] public List<FinanceTransaction> FinanceTransactions { get; set; }
[IgnoreProperty] [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)
{
@ -36,6 +35,11 @@ namespace BlueWest.Data
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 LTCAmount { get; set; }
public UserUpdateDto()
{
}
}
}

View File

@ -82,7 +82,7 @@ namespace BlueWest.WebApi.Controllers
[HttpPost]
public ActionResult AddOrModifyUser(UserUpdateDto userUpdateDto)
{
var user = MemoryData.AddOrModify(userUpdateDto);
var user = MemoryData.AddOrModifyUser(userUpdateDto);
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));
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 void Main(string[] args)

View File

@ -55,8 +55,13 @@ namespace PerformanceSolution.Data
SaveUserList();
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);
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 index = GetIndexById(userId);
User user = null;
User? user;
if (index != -1)
{
user = GetUserById(id);
if (user == null) return user;
if (user != null)
{
UserList.Users[index] = user;
SaveUserList();
}
UserList.Users[index] = user;
SaveUserList();
}
else
{
user = new User(userUpdateDto);
user.Id = id;
UserList.Users.Add(user);
SaveUserList();
@ -104,12 +108,13 @@ namespace PerformanceSolution.Data
public static User? UpdateUser(int userId, UserUpdateDto userUpdate)
{
var index = GetIndexById(userId);
if (index == -1) return null;
var actualUser = GetUserById(userId);
if (actualUser == null) return null;
actualUser.Update(userUpdate);
@ -120,56 +125,20 @@ namespace PerformanceSolution.Data
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)
{
var index = GetIndexById(userId);
if (index < UserList.Users.Count)
{
if (financeTransaction.Id == 0)
{
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()
{
var user = GetUserById(userId);
if (user == null) return null;
user.AddTransaction(financeTransaction);
SaveUserList();
return financeTransaction;
}
private static UserList GenerateMockData()