From 3151ab9c564241a0a4ef2c08e7edcfc40fb0781a Mon Sep 17 00:00:00 2001 From: Rui Sousa Date: Tue, 7 Dec 2021 23:57:27 +0000 Subject: [PATCH] stuff --- BlueWest.Data/BlueWest.Data.csproj | 2 +- BlueWest.Data/Data/User.cs | 10 ++- BlueWest.Data/Data/UserUpdateDto.cs | 6 +- BlueWest.WebApi/Controllers/UserController.cs | 2 +- BlueWest.WebApi/Program.cs | 13 +++ BlueWest/Data/MemoryData.cs | 83 ++++++------------- 6 files changed, 49 insertions(+), 67 deletions(-) diff --git a/BlueWest.Data/BlueWest.Data.csproj b/BlueWest.Data/BlueWest.Data.csproj index a0d6337..540b8e3 100644 --- a/BlueWest.Data/BlueWest.Data.csproj +++ b/BlueWest.Data/BlueWest.Data.csproj @@ -7,7 +7,7 @@ - + diff --git a/BlueWest.Data/Data/User.cs b/BlueWest.Data/Data/User.cs index 6a9ba8e..f3eb6e2 100644 --- a/BlueWest.Data/Data/User.cs +++ b/BlueWest.Data/Data/User.cs @@ -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 FinanceTransactions { get; set; } + [IgnoreProperty] [Key(8)] public List FinanceTransactions { get; set; } public User(int id, string name, string address, string btcAddress, string ltcAddress, double btcAmount, double ltcAmount, List financeTransactions) { @@ -36,6 +35,11 @@ namespace BlueWest.Data FinanceTransactions = financeTransactions; } + public void AddTransaction(FinanceTransaction financeTransaction) + { + FinanceTransactions.Add(financeTransaction); + } + } } diff --git a/BlueWest.Data/Data/UserUpdateDto.cs b/BlueWest.Data/Data/UserUpdateDto.cs index 818c6a2..4f6f3fe 100644 --- a/BlueWest.Data/Data/UserUpdateDto.cs +++ b/BlueWest.Data/Data/UserUpdateDto.cs @@ -15,11 +15,7 @@ namespace BlueWest.Data public double BTCAmount { get; set; } public double LTCAmount { get; set; } - - public UserUpdateDto() - { - - } + } } \ No newline at end of file diff --git a/BlueWest.WebApi/Controllers/UserController.cs b/BlueWest.WebApi/Controllers/UserController.cs index 420739c..148f58b 100644 --- a/BlueWest.WebApi/Controllers/UserController.cs +++ b/BlueWest.WebApi/Controllers/UserController.cs @@ -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); } diff --git a/BlueWest.WebApi/Program.cs b/BlueWest.WebApi/Program.cs index 4be1a38..98c63ad 100644 --- a/BlueWest.WebApi/Program.cs +++ b/BlueWest.WebApi/Program.cs @@ -18,7 +18,20 @@ namespace BlueWest.WebApi new EventManager(new FastDictionary>(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) diff --git a/BlueWest/Data/MemoryData.cs b/BlueWest/Data/MemoryData.cs index c6173d2..330c76f 100644 --- a/BlueWest/Data/MemoryData.cs +++ b/BlueWest/Data/MemoryData.cs @@ -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(); - } - 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()