From 937fbc4bc0793dfaa81a4905435ed9f4e4fa138b Mon Sep 17 00:00:00 2001 From: syneffort Date: Thu, 26 Oct 2023 18:11:58 +0900 Subject: [PATCH] add new project --- MySolution/DBCreateEF/Context/AppDbContext.cs | 22 ++++ MySolution/DBCreateEF/DBCreateEF.csproj | 24 ++++ MySolution/DBCreateEF/Model/Snack.cs | 15 +++ MySolution/DBCreateEF/Program.cs | 17 +++ .../InventoryManagement_MariaDB/Program.cs | 2 + MySolution/MySolution.sln | 12 ++ MySolution/ToDoApp/App.xaml | 9 ++ MySolution/ToDoApp/App.xaml.cs | 17 +++ MySolution/ToDoApp/AssemblyInfo.cs | 10 ++ MySolution/ToDoApp/Context/ToDoContext.cs | 24 ++++ .../ToDoApp/Controller/ToDoController.cs | 111 ++++++++++++++++++ MySolution/ToDoApp/MainWindow.xaml | 12 ++ MySolution/ToDoApp/MainWindow.xaml.cs | 51 ++++++++ MySolution/ToDoApp/Model/Item.cs | 30 +++++ MySolution/ToDoApp/Model/User.cs | 33 ++++++ MySolution/ToDoApp/ToDoApp.csproj | 23 ++++ MySolution/ToDoApp/Utility/Utils.cs | 27 +++++ 17 files changed, 439 insertions(+) create mode 100644 MySolution/DBCreateEF/Context/AppDbContext.cs create mode 100644 MySolution/DBCreateEF/DBCreateEF.csproj create mode 100644 MySolution/DBCreateEF/Model/Snack.cs create mode 100644 MySolution/DBCreateEF/Program.cs create mode 100644 MySolution/ToDoApp/App.xaml create mode 100644 MySolution/ToDoApp/App.xaml.cs create mode 100644 MySolution/ToDoApp/AssemblyInfo.cs create mode 100644 MySolution/ToDoApp/Context/ToDoContext.cs create mode 100644 MySolution/ToDoApp/Controller/ToDoController.cs create mode 100644 MySolution/ToDoApp/MainWindow.xaml create mode 100644 MySolution/ToDoApp/MainWindow.xaml.cs create mode 100644 MySolution/ToDoApp/Model/Item.cs create mode 100644 MySolution/ToDoApp/Model/User.cs create mode 100644 MySolution/ToDoApp/ToDoApp.csproj create mode 100644 MySolution/ToDoApp/Utility/Utils.cs diff --git a/MySolution/DBCreateEF/Context/AppDbContext.cs b/MySolution/DBCreateEF/Context/AppDbContext.cs new file mode 100644 index 0000000..ff98beb --- /dev/null +++ b/MySolution/DBCreateEF/Context/AppDbContext.cs @@ -0,0 +1,22 @@ +using DBCreateEF.Model; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DBCreateEF.Context +{ + internal class AppDbContext : DbContext + { + public DbSet Snacks { get; set; } + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + //optionsBuilder.UseSqlServer(@"Data Source=peacecloud.synology.me,21433;Initial Catalog=Study;User ID=study;Password=Study1234;Encrypt=false"); + optionsBuilder.UseMySQL(@"Server=peacecloud.synology.me;Port=3307;Database=Study;Uid=study;Pwd=Study1234!;"); + base.OnConfiguring(optionsBuilder); + } + } +} diff --git a/MySolution/DBCreateEF/DBCreateEF.csproj b/MySolution/DBCreateEF/DBCreateEF.csproj new file mode 100644 index 0000000..47e732d --- /dev/null +++ b/MySolution/DBCreateEF/DBCreateEF.csproj @@ -0,0 +1,24 @@ + + + + Exe + net6.0 + enable + enable + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + diff --git a/MySolution/DBCreateEF/Model/Snack.cs b/MySolution/DBCreateEF/Model/Snack.cs new file mode 100644 index 0000000..a1a8acf --- /dev/null +++ b/MySolution/DBCreateEF/Model/Snack.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DBCreateEF.Model +{ + internal class Snack + { + public int Id { get; set; } + public string Name { get; set; } + public decimal Price { get; set; } + } +} diff --git a/MySolution/DBCreateEF/Program.cs b/MySolution/DBCreateEF/Program.cs new file mode 100644 index 0000000..6c6e880 --- /dev/null +++ b/MySolution/DBCreateEF/Program.cs @@ -0,0 +1,17 @@ +using DBCreateEF.Context; +using Microsoft.EntityFrameworkCore; + +namespace DBCreateEF +{ + internal class Program + { + static void Main(string[] args) + { + using (var context = new AppDbContext()) + { + context.Database.EnsureCreated(); + //context.Database.Migrate(); + } + } + } +} \ No newline at end of file diff --git a/MySolution/InventoryManagement_MariaDB/Program.cs b/MySolution/InventoryManagement_MariaDB/Program.cs index 755df90..8fba39a 100644 --- a/MySolution/InventoryManagement_MariaDB/Program.cs +++ b/MySolution/InventoryManagement_MariaDB/Program.cs @@ -1,4 +1,5 @@ using InventoryManagement.Components; +using Microsoft.EntityFrameworkCore; namespace InventoryManagement { @@ -9,6 +10,7 @@ namespace InventoryManagement using (var context = new InventoryContext()) { context.Database.EnsureCreated(); + //context.Database.Migrate(); } var InventoryManager = new InventoryManager(); diff --git a/MySolution/MySolution.sln b/MySolution/MySolution.sln index b21c067..a9bc715 100644 --- a/MySolution/MySolution.sln +++ b/MySolution/MySolution.sln @@ -23,6 +23,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EF_ChangeLog", "EF_ChangeLo EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "InventoryManagement_MariaDB", "InventoryManagement_MariaDB\InventoryManagement_MariaDB.csproj", "{EF6B4197-EAF3-4F13-AA42-F25110F82D3E}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DBCreateEF", "DBCreateEF\DBCreateEF.csproj", "{A3960078-A866-414F-B039-FCD4065E8F74}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ToDoApp", "ToDoApp\ToDoApp.csproj", "{D500F21E-3B7B-47DA-9D0C-69EB8A8FA3B1}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -69,6 +73,14 @@ Global {EF6B4197-EAF3-4F13-AA42-F25110F82D3E}.Debug|Any CPU.Build.0 = Debug|Any CPU {EF6B4197-EAF3-4F13-AA42-F25110F82D3E}.Release|Any CPU.ActiveCfg = Release|Any CPU {EF6B4197-EAF3-4F13-AA42-F25110F82D3E}.Release|Any CPU.Build.0 = Release|Any CPU + {A3960078-A866-414F-B039-FCD4065E8F74}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A3960078-A866-414F-B039-FCD4065E8F74}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A3960078-A866-414F-B039-FCD4065E8F74}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A3960078-A866-414F-B039-FCD4065E8F74}.Release|Any CPU.Build.0 = Release|Any CPU + {D500F21E-3B7B-47DA-9D0C-69EB8A8FA3B1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D500F21E-3B7B-47DA-9D0C-69EB8A8FA3B1}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D500F21E-3B7B-47DA-9D0C-69EB8A8FA3B1}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D500F21E-3B7B-47DA-9D0C-69EB8A8FA3B1}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/MySolution/ToDoApp/App.xaml b/MySolution/ToDoApp/App.xaml new file mode 100644 index 0000000..1e276e0 --- /dev/null +++ b/MySolution/ToDoApp/App.xaml @@ -0,0 +1,9 @@ + + + + + diff --git a/MySolution/ToDoApp/App.xaml.cs b/MySolution/ToDoApp/App.xaml.cs new file mode 100644 index 0000000..7e26b4c --- /dev/null +++ b/MySolution/ToDoApp/App.xaml.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Data; +using System.Linq; +using System.Threading.Tasks; +using System.Windows; + +namespace ToDoApp +{ + /// + /// Interaction logic for App.xaml + /// + public partial class App : Application + { + } +} diff --git a/MySolution/ToDoApp/AssemblyInfo.cs b/MySolution/ToDoApp/AssemblyInfo.cs new file mode 100644 index 0000000..8b5504e --- /dev/null +++ b/MySolution/ToDoApp/AssemblyInfo.cs @@ -0,0 +1,10 @@ +using System.Windows; + +[assembly: ThemeInfo( + ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located + //(used if a resource is not found in the page, + // or application resource dictionaries) + ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located + //(used if a resource is not found in the page, + // app, or any theme specific resource dictionaries) +)] diff --git a/MySolution/ToDoApp/Context/ToDoContext.cs b/MySolution/ToDoApp/Context/ToDoContext.cs new file mode 100644 index 0000000..cc61367 --- /dev/null +++ b/MySolution/ToDoApp/Context/ToDoContext.cs @@ -0,0 +1,24 @@ +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ToDoApp.Model; + +namespace ToDoApp.Context +{ + public class ToDoContext : DbContext + { + public DbSet Users { get; set; } + public DbSet Items { get; set; } + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + optionsBuilder.UseMySQL(@"Server=peacecloud.synology.me;Port=3307;Database=Study;Uid=study;Pwd=Study1234!;"); + base.OnConfiguring(optionsBuilder); + } + + + } +} diff --git a/MySolution/ToDoApp/Controller/ToDoController.cs b/MySolution/ToDoApp/Controller/ToDoController.cs new file mode 100644 index 0000000..52bee0b --- /dev/null +++ b/MySolution/ToDoApp/Controller/ToDoController.cs @@ -0,0 +1,111 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ToDoApp.Context; +using ToDoApp.Model; +using ToDoApp.Utility; + +namespace ToDoApp.Controller +{ + public class ToDoController + { + public static ToDoController Instance { get; set; } = new ToDoController(); + + public void EnsureCreated() + { + using (var context = new ToDoContext()) + { + context.Database.EnsureCreated(); + } + } + + public User? FindUser(int id) + { + using (var context = new ToDoContext()) + { + return context.Users.Find(id); + } + } + + public User? FindUser(string username) + { + using (var context = new ToDoContext()) + { + return context.Users.FirstOrDefault(u => u.Name == username); + } + } + + public bool LoginCheckUser(string username, string password) + { + User? user = FindUser(username); + if (user == null) + return false; + + return Utils.VerifyPassword(password, user.Password); + } + + public void CreateUser(User user) + { + using (var context = new ToDoContext()) + { + user.CreateDate = DateTime.Now; + context.Users.Add(user); + context.SaveChanges(); + } + } + + public void DisableUser(int id) + { + using (var context = new ToDoContext()) + { + User? user = context.Users.Find(id); + if (user == null) + return; + + user.Status = UserStatus.Invalid; + context.SaveChanges(); + } + } + + public void DisableUser(string username) + { + using (var context = new ToDoContext()) + { + User? user = context.Users.FirstOrDefault(u => u.Name == username); + if (user == null) + return; + + user.Status = UserStatus.Invalid; + context.SaveChanges(); + } + } + + public void RemoveUser(int id) + { + using (var context = new ToDoContext()) + { + User? user = context.Users.Find(id); + if (user == null) + return; + + context.Users.Remove(user); + context.SaveChanges(); + } + } + + public void RemoveUser(string username) + { + using (var context = new ToDoContext()) + { + User? user = context.Users.FirstOrDefault(u => u.Name == username); + if (user == null) + return; + + context.Users.Remove(user); + context.SaveChanges(); + } + } + } +} diff --git a/MySolution/ToDoApp/MainWindow.xaml b/MySolution/ToDoApp/MainWindow.xaml new file mode 100644 index 0000000..277b2dc --- /dev/null +++ b/MySolution/ToDoApp/MainWindow.xaml @@ -0,0 +1,12 @@ + + + + + diff --git a/MySolution/ToDoApp/MainWindow.xaml.cs b/MySolution/ToDoApp/MainWindow.xaml.cs new file mode 100644 index 0000000..6332a28 --- /dev/null +++ b/MySolution/ToDoApp/MainWindow.xaml.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Navigation; +using System.Windows.Shapes; +using ToDoApp.Context; +using ToDoApp.Controller; +using ToDoApp.Model; + +namespace ToDoApp +{ + /// + /// Interaction logic for MainWindow.xaml + /// + public partial class MainWindow : Window + { + public MainWindow() + { + InitializeComponent(); + + if (!CheckDB()) + { + MessageBox.Show("데이터베이스가 정상적이지 않아 프로그램을 종료합니다."); + Application.Current.Shutdown(); + } + } + + private bool CheckDB() + { + try + { + ToDoController.Instance.EnsureCreated(); + return true; + } + catch (Exception ex) + { + return false; + } + + } + } +} diff --git a/MySolution/ToDoApp/Model/Item.cs b/MySolution/ToDoApp/Model/Item.cs new file mode 100644 index 0000000..530675b --- /dev/null +++ b/MySolution/ToDoApp/Model/Item.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ToDoApp.Model +{ + public class Item + { + public int Id { get; set; } + + [Required] + public int UserId { get; set; } + + [Required] + public string Title { get; set; } + + public string Description { get; set; } + + public DateTime DueDate { get; set; } + + public bool IsCompleted { get; set; } + + public DateTime CreateDate { get; set; } + + public User User { get; set; } + } +} diff --git a/MySolution/ToDoApp/Model/User.cs b/MySolution/ToDoApp/Model/User.cs new file mode 100644 index 0000000..86577df --- /dev/null +++ b/MySolution/ToDoApp/Model/User.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ToDoApp.Model +{ + public enum UserStatus + { + Valid, + Invalid + } + + public class User + { + public int Id { get; set; } + + [Required] + public string Name { get; set; } + + [Required] + public string Password { get; set; } + + [EmailAddress(ErrorMessage = "Invalid email format")] + public string Email { get; set; } + + public DateTime CreateDate { get; set; } + + public UserStatus Status { get; set; } + } +} diff --git a/MySolution/ToDoApp/ToDoApp.csproj b/MySolution/ToDoApp/ToDoApp.csproj new file mode 100644 index 0000000..2ad5d14 --- /dev/null +++ b/MySolution/ToDoApp/ToDoApp.csproj @@ -0,0 +1,23 @@ + + + + WinExe + net6.0-windows + enable + true + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + diff --git a/MySolution/ToDoApp/Utility/Utils.cs b/MySolution/ToDoApp/Utility/Utils.cs new file mode 100644 index 0000000..53be4ba --- /dev/null +++ b/MySolution/ToDoApp/Utility/Utils.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Security.Cryptography; +using System.Text; +using System.Threading.Tasks; + +namespace ToDoApp.Utility +{ + public class Utils + { + public static string HashPassword(string password) + { + using (var sha256 = SHA256.Create()) + { + var hashedBytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(password)); + return BitConverter.ToString(hashedBytes).Replace("-", "").ToLower(); + } + } + + public static bool VerifyPassword(string inputPassword, string hashedPassword) + { + var hashedInput = HashPassword(inputPassword); + return hashedInput.Equals(hashedPassword); + } + } +}