From c1c2c3a9f4509bf9c4b3135a8a70a96354f0dd32 Mon Sep 17 00:00:00 2001 From: Ismailov_Rovshan Date: Wed, 6 Sep 2023 21:16:28 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A4=D0=BE=D1=80=D0=BC=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BlogDatabaseImplement/BlogDatabase.cs | 19 +- .../BlogDatabaseImplement.csproj | 1 + .../Implements/CategoryStorage.cs | 91 +++++++ .../Implements/MessageStorage.cs | 165 +++++++++++++ .../Implements/RoleStorage.cs | 106 ++++++++ .../Implements/TopicStorage.cs | 95 ++++++++ .../Implements/UserStorage.cs | 115 +++++++++ .../BlogDatabaseImplement/Model/Category.cs | 54 +++++ .../BlogDatabaseImplement/Model/Message.cs | 69 ++++++ .../BlogDatabaseImplement/Model/Role.cs | 52 ++++ .../BlogDatabaseImplement/Model/Topic.cs | 62 +++++ .../BlogDatabaseImplement/Model/User.cs | 82 +++++++ .../BlogViewModel/BlogViewModel.csproj | 1 + .../BlogViewModel/FormAddTopic.Designer.cs | 88 +++++++ BlogDataModels/BlogViewModel/FormAddTopic.cs | 67 +++++ .../BlogViewModel/FormAddTopic.resx | 60 +++++ .../BlogViewModel/FormAddTopics.Designer.cs | 134 ++++++++++ BlogDataModels/BlogViewModel/FormAddTopics.cs | 117 +++++++++ .../BlogViewModel/FormAddTopics.resx | 60 +++++ .../BlogViewModel/FormCategories.Designer.cs | 128 ++++++++++ .../BlogViewModel/FormCategories.cs | 129 ++++++++++ .../BlogViewModel/FormCategories.resx | 60 +++++ .../BlogViewModel/FormCategory.Designer.cs | 85 +++++++ BlogDataModels/BlogViewModel/FormCategory.cs | 88 +++++++ .../BlogViewModel/FormCategory.resx | 60 +++++ .../FormCreateMessage.Designer.cs | 156 ++++++++++++ .../BlogViewModel/FormCreateMessage.cs | 139 +++++++++++ .../BlogViewModel/FormCreateMessage.resx | 60 +++++ .../BlogViewModel/FormMain.Designer.cs | 163 +++++++++++++ BlogDataModels/BlogViewModel/FormMain.cs | 147 +++++++++++ BlogDataModels/BlogViewModel/FormMain.resx | 63 +++++ .../BlogViewModel/FormRole.Designer.cs | 102 ++++++++ BlogDataModels/BlogViewModel/FormRole.cs | 102 ++++++++ BlogDataModels/BlogViewModel/FormRole.resx | 60 +++++ .../BlogViewModel/FormRoles.Designer.cs | 115 +++++++++ BlogDataModels/BlogViewModel/FormRoles.cs | 111 +++++++++ BlogDataModels/BlogViewModel/FormRoles.resx | 60 +++++ .../BlogViewModel/FormTests.Designer.cs | 229 ++++++++++++++++++ BlogDataModels/BlogViewModel/FormTests.cs | 75 ++++++ BlogDataModels/BlogViewModel/FormTests.resx | 60 +++++ .../BlogViewModel/FormUser.Designer.cs | 184 ++++++++++++++ BlogDataModels/BlogViewModel/FormUser.cs | 138 +++++++++++ BlogDataModels/BlogViewModel/FormUser.resx | 60 +++++ .../BlogViewModel/FormUsers.Designer.cs | 115 +++++++++ BlogDataModels/BlogViewModel/FormUsers.cs | 113 +++++++++ BlogDataModels/BlogViewModel/FormUsers.resx | 60 +++++ BlogDataModels/BlogViewModel/Forum.csproj | 25 ++ BlogDataModels/BlogViewModel/Program.cs | 43 ++-- 48 files changed, 4338 insertions(+), 30 deletions(-) create mode 100644 BlogDataModels/BlogDatabaseImplement/Implements/CategoryStorage.cs create mode 100644 BlogDataModels/BlogDatabaseImplement/Implements/MessageStorage.cs create mode 100644 BlogDataModels/BlogDatabaseImplement/Implements/RoleStorage.cs create mode 100644 BlogDataModels/BlogDatabaseImplement/Implements/TopicStorage.cs create mode 100644 BlogDataModels/BlogDatabaseImplement/Implements/UserStorage.cs create mode 100644 BlogDataModels/BlogDatabaseImplement/Model/Category.cs create mode 100644 BlogDataModels/BlogDatabaseImplement/Model/Message.cs create mode 100644 BlogDataModels/BlogDatabaseImplement/Model/Role.cs create mode 100644 BlogDataModels/BlogDatabaseImplement/Model/Topic.cs create mode 100644 BlogDataModels/BlogDatabaseImplement/Model/User.cs create mode 100644 BlogDataModels/BlogViewModel/FormAddTopic.Designer.cs create mode 100644 BlogDataModels/BlogViewModel/FormAddTopic.cs create mode 100644 BlogDataModels/BlogViewModel/FormAddTopic.resx create mode 100644 BlogDataModels/BlogViewModel/FormAddTopics.Designer.cs create mode 100644 BlogDataModels/BlogViewModel/FormAddTopics.cs create mode 100644 BlogDataModels/BlogViewModel/FormAddTopics.resx create mode 100644 BlogDataModels/BlogViewModel/FormCategories.Designer.cs create mode 100644 BlogDataModels/BlogViewModel/FormCategories.cs create mode 100644 BlogDataModels/BlogViewModel/FormCategories.resx create mode 100644 BlogDataModels/BlogViewModel/FormCategory.Designer.cs create mode 100644 BlogDataModels/BlogViewModel/FormCategory.cs create mode 100644 BlogDataModels/BlogViewModel/FormCategory.resx create mode 100644 BlogDataModels/BlogViewModel/FormCreateMessage.Designer.cs create mode 100644 BlogDataModels/BlogViewModel/FormCreateMessage.cs create mode 100644 BlogDataModels/BlogViewModel/FormCreateMessage.resx create mode 100644 BlogDataModels/BlogViewModel/FormMain.Designer.cs create mode 100644 BlogDataModels/BlogViewModel/FormMain.cs create mode 100644 BlogDataModels/BlogViewModel/FormMain.resx create mode 100644 BlogDataModels/BlogViewModel/FormRole.Designer.cs create mode 100644 BlogDataModels/BlogViewModel/FormRole.cs create mode 100644 BlogDataModels/BlogViewModel/FormRole.resx create mode 100644 BlogDataModels/BlogViewModel/FormRoles.Designer.cs create mode 100644 BlogDataModels/BlogViewModel/FormRoles.cs create mode 100644 BlogDataModels/BlogViewModel/FormRoles.resx create mode 100644 BlogDataModels/BlogViewModel/FormTests.Designer.cs create mode 100644 BlogDataModels/BlogViewModel/FormTests.cs create mode 100644 BlogDataModels/BlogViewModel/FormTests.resx create mode 100644 BlogDataModels/BlogViewModel/FormUser.Designer.cs create mode 100644 BlogDataModels/BlogViewModel/FormUser.cs create mode 100644 BlogDataModels/BlogViewModel/FormUser.resx create mode 100644 BlogDataModels/BlogViewModel/FormUsers.Designer.cs create mode 100644 BlogDataModels/BlogViewModel/FormUsers.cs create mode 100644 BlogDataModels/BlogViewModel/FormUsers.resx create mode 100644 BlogDataModels/BlogViewModel/Forum.csproj diff --git a/BlogDataModels/BlogDatabaseImplement/BlogDatabase.cs b/BlogDataModels/BlogDatabaseImplement/BlogDatabase.cs index e048a7f..250db81 100644 --- a/BlogDataModels/BlogDatabaseImplement/BlogDatabase.cs +++ b/BlogDataModels/BlogDatabaseImplement/BlogDatabase.cs @@ -1,11 +1,11 @@ using Microsoft.EntityFrameworkCore; -using BlogDatabaseImplement.Model; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.ComponentModel; +using BlogDatabase.Models; namespace BlogDatabaseImplement { @@ -15,21 +15,14 @@ namespace BlogDatabaseImplement { if (optionsBuilder.IsConfigured == false) { - optionsBuilder.UseNpgsql(@" - Host=localhost; - Port=5432; - Database=BlogDB; - Username=postgres; - Password=postgres"); + optionsBuilder.UseNpgsql("Host=localhost;Port=5432;Database=ForumSubd;Username=postgres;Password=postgres"); } - base.OnConfiguring(optionsBuilder); } - public virtual DbSet Tags { set; get; } + public virtual DbSet Roles { set; get; } public virtual DbSet Users { set; get; } - public virtual DbSet News { set; get; } - public virtual DbSet Comments { set; get; } - public virtual DbSet NewsTags { set; get; } - + public virtual DbSet Topics { set; get; } + public virtual DbSet Categories { set; get; } + public virtual DbSet Messages { set; get; } } } diff --git a/BlogDataModels/BlogDatabaseImplement/BlogDatabaseImplement.csproj b/BlogDataModels/BlogDatabaseImplement/BlogDatabaseImplement.csproj index a629fbf..b8c5aab 100644 --- a/BlogDataModels/BlogDatabaseImplement/BlogDatabaseImplement.csproj +++ b/BlogDataModels/BlogDatabaseImplement/BlogDatabaseImplement.csproj @@ -18,6 +18,7 @@ + diff --git a/BlogDataModels/BlogDatabaseImplement/Implements/CategoryStorage.cs b/BlogDataModels/BlogDatabaseImplement/Implements/CategoryStorage.cs new file mode 100644 index 0000000..366ea38 --- /dev/null +++ b/BlogDataModels/BlogDatabaseImplement/Implements/CategoryStorage.cs @@ -0,0 +1,91 @@ +using ForumContracts.BindingModels; +using ForumContracts.SearchModels; +using ForumContracts.StoragesContracts; +using ForumContracts.ViewModels; +using BlogDatabase.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlogDatabase.Implements +{ + public class CategoryStorage : ICategoryStorage + { + public CategoryViewModel? Delete(CategoryBindingModel model) + { + using var context = new BlogDatabase(); + var element = context.Categories.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Categories.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + public CategoryViewModel? GetElement(CategorySearchModel model) + { + if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue) + { + return null; + } + using var context = new BlogDatabase(); + return context.Categories + .FirstOrDefault(x => + (!string.IsNullOrEmpty(model.Name) && x.Name == + model.Name) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public List GetFilteredList(CategorySearchModel model) + { + if (string.IsNullOrEmpty(model.Name)) + { + return new(); + } + using var context = new BlogDatabase(); + return context.Categories + .Where(x => x.Name.Contains(model.Name)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new BlogDatabase(); + return context.Categories + .Select(x => x.GetViewModel) + .ToList(); + } + + public CategoryViewModel? Insert(CategoryBindingModel model) + { + var newCategory = Category.Create(model); + if (newCategory == null) + { + return null; + } + using var context = new BlogDatabase(); + context.Categories.Add(newCategory); + context.SaveChanges(); + return newCategory.GetViewModel; + } + + public CategoryViewModel? Update(CategoryBindingModel model) + { + using var context = new BlogDatabase(); + var component = context.Categories.FirstOrDefault(x => x.Id == model.Id); + if (component == null) + { + return null; + } + component.Update(model); + context.SaveChanges(); + return component.GetViewModel; + } + } +} diff --git a/BlogDataModels/BlogDatabaseImplement/Implements/MessageStorage.cs b/BlogDataModels/BlogDatabaseImplement/Implements/MessageStorage.cs new file mode 100644 index 0000000..9096be8 --- /dev/null +++ b/BlogDataModels/BlogDatabaseImplement/Implements/MessageStorage.cs @@ -0,0 +1,165 @@ +using ForumContracts.BindingModels; +using ForumContracts.SearchModels; +using ForumContracts.StoragesContracts; +using ForumContracts.ViewModels; +using BlogDatabase.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlogDatabase.Implements +{ + public class MessageStorage : IMessageStorage + { + public MessageViewModel? Delete(MessageBindingModel model) + { + using var context = new BlogDatabase(); + var element = context.Messages.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Messages.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + public MessageViewModel? GetElement(MessageSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new BlogDatabase(); + return context.Messages + .Include(x => x.User) + .Include(x => x.Topic) + .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id).GetViewModel; + } + + public List GetFilteredList(MessageSearchModel model) + { + using var context = new BlogDatabase(); + return context.Messages + .Include(x => x.User) + .Include(x => x.Topic) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new BlogDatabase(); + return context.Messages + .Include(x => x.User) + .Include(x => x.Topic) + .Select(x => x.GetViewModel) + .ToList(); + } + + public MessageViewModel? Insert(MessageBindingModel model) + { + var newMessage = Message.Create(model); + if (newMessage == null) + { + return null; + } + using var context = new BlogDatabase(); + context.Messages.Add(newMessage); + context.SaveChanges(); + return newMessage.GetViewModel; + } + + public string TestInsertList(int num, List users, List topics) + { + Random rnd = new Random(); + using var context = new BlogDatabase(); + for (int i = 0; i < num; ++i) + { + var model = new MessageBindingModel + { + Id = 0, + Text = "hello", + Date = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc), + UserId = users[rnd.Next(users.Count)].Id, + TopicId = topics[rnd.Next(topics.Count)].Id, + }; + context.Messages.Add(Message.Create(model)); + } + Stopwatch stopwatch = new(); + + stopwatch.Start(); + + context.SaveChanges(); + + stopwatch.Stop(); + return stopwatch.ElapsedMilliseconds.ToString(); + } + + public string TestJoinReadList(int num) + { + using var context = new BlogDatabase(); + Stopwatch stopwatch = new(); + + stopwatch.Start(); + + var list = context.Messages + .Include(x => x.User) + .Include(x => x.Topic) + .Join(context.Users, + r => r.UserId, + c => c.Id, + (r, c) => new { Message = r, User = c }) + .Join(context.Topics, + rc => rc.Message.TopicId, + car => car.Id, + (rc, topic) => new { rc.Message, rc.User, Topic = topic }) + .Select(rc => new + { + Message = rc.Message.GetViewModel, + User = rc.User.GetViewModel, + Topic = rc.Topic.GetViewModel, + }) + .Take(num) + .ToList(); + stopwatch.Stop(); + + return stopwatch.ElapsedMilliseconds.ToString(); + } + + public string TestReadList(int num) + { + using var context = new BlogDatabase(); + Stopwatch stopwatch = new(); + + stopwatch.Start(); + + List list = context.Messages + .Include(x => x.User) + .Include(x => x.Topic) + .Take(num) + .Select(x => x.GetViewModel) + .ToList(); + stopwatch.Stop(); + + return stopwatch.ElapsedMilliseconds.ToString(); + } + + public MessageViewModel? Update(MessageBindingModel model) + { + using var context = new BlogDatabase(); + var component = context.Messages.FirstOrDefault(x => x.Id == model.Id); + if (component == null) + { + return null; + } + component.Update(model); + context.SaveChanges(); + return component.GetViewModel; + } + } +} diff --git a/BlogDataModels/BlogDatabaseImplement/Implements/RoleStorage.cs b/BlogDataModels/BlogDatabaseImplement/Implements/RoleStorage.cs new file mode 100644 index 0000000..57337ec --- /dev/null +++ b/BlogDataModels/BlogDatabaseImplement/Implements/RoleStorage.cs @@ -0,0 +1,106 @@ +using ForumContracts.BindingModels; +using ForumContracts.SearchModels; +using ForumContracts.StoragesContracts; +using ForumContracts.ViewModels; +using BlogDatabase.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlogDatabase.Implements +{ + public class RoleStorage : IRoleStorage + { + public void RoleInsertList(int num) + { + Random rnd = new Random(); + using var context = new BlogDatabase(); + for (int i = 0; i < num; ++i) + { + var model = new RoleBindingModel + { + Id = 0, + Name = rnd.Next(1000).ToString(), + }; + context.Roles.Add(Role.Create(model)); + } + context.SaveChanges(); + } + public RoleViewModel? Delete(RoleBindingModel model) + { + using var context = new BlogDatabase(); + var element = context.Roles.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Roles.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + public RoleViewModel? GetElement(RoleSearchModel model) + { + if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue) + { + return null; + } + using var context = new BlogDatabase(); + return context.Roles + .FirstOrDefault(x => + (!string.IsNullOrEmpty(model.Name) && x.Name == + model.Name) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public List GetFilteredList(RoleSearchModel model) + { + if (string.IsNullOrEmpty(model.Name)) + { + return new(); + } + using var context = new BlogDatabase(); + return context.Roles + .Where(x => x.Name.Contains(model.Name)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new BlogDatabase(); + return context.Roles + .Select(x => x.GetViewModel) + .ToList(); + } + + public RoleViewModel? Insert(RoleBindingModel model) + { + var newRole = Role.Create(model); + if (newRole == null) + { + return null; + } + using var context = new BlogDatabase(); + context.Roles.Add(newRole); + context.SaveChanges(); + return newRole.GetViewModel; + } + + public RoleViewModel? Update(RoleBindingModel model) + { + using var context = new BlogDatabase(); + var component = context.Roles.FirstOrDefault(x => x.Id == model.Id); + if (component == null) + { + return null; + } + component.Update(model); + context.SaveChanges(); + return component.GetViewModel; + } + } +} diff --git a/BlogDataModels/BlogDatabaseImplement/Implements/TopicStorage.cs b/BlogDataModels/BlogDatabaseImplement/Implements/TopicStorage.cs new file mode 100644 index 0000000..2e2c988 --- /dev/null +++ b/BlogDataModels/BlogDatabaseImplement/Implements/TopicStorage.cs @@ -0,0 +1,95 @@ +using ForumContracts.BindingModels; +using ForumContracts.SearchModels; +using ForumContracts.StoragesContracts; +using ForumContracts.ViewModels; +using BlogDatabase.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlogDatabase.Implements +{ + public class TopicStorage : ITopicStorage + { + public TopicViewModel? Delete(TopicBindingModel model) + { + using var context = new BlogDatabase(); + var element = context.Topics.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Topics.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + public TopicViewModel? GetElement(TopicSearchModel model) + { + if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue) + { + return null; + } + using var context = new BlogDatabase(); + return context.Topics + .FirstOrDefault(x => + (!string.IsNullOrEmpty(model.Name) && x.Name == + model.Name) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public List GetFilteredList(TopicSearchModel model) + { + using var context = new BlogDatabase(); + if (model.CategoryId.HasValue) + { + return context.Topics + .Where(x => x.CategoryId == model.CategoryId) + .Select(x => x.GetViewModel) + .ToList(); + } + return context.Topics + .Where(x => x.Name.Contains(model.Name)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new BlogDatabase(); + return context.Topics + .Select(x => x.GetViewModel) + .ToList(); + } + + public TopicViewModel? Insert(TopicBindingModel model) + { + var newTopic = Topic.Create(model); + if (newTopic == null) + { + return null; + } + using var context = new BlogDatabase(); + context.Topics.Add(newTopic); + context.SaveChanges(); + return newTopic.GetViewModel; + } + + public TopicViewModel? Update(TopicBindingModel model) + { + using var context = new BlogDatabase(); + var component = context.Topics.FirstOrDefault(x => x.Id == model.Id); + if (component == null) + { + return null; + } + component.Update(model); + context.SaveChanges(); + return component.GetViewModel; + } + } +} diff --git a/BlogDataModels/BlogDatabaseImplement/Implements/UserStorage.cs b/BlogDataModels/BlogDatabaseImplement/Implements/UserStorage.cs new file mode 100644 index 0000000..2990b9a --- /dev/null +++ b/BlogDataModels/BlogDatabaseImplement/Implements/UserStorage.cs @@ -0,0 +1,115 @@ +using ForumContracts.BindingModels; +using ForumContracts.SearchModels; +using ForumContracts.StoragesContracts; +using ForumContracts.ViewModels; +using BlogDatabase.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.ConstrainedExecution; +using System.Text; +using System.Threading.Tasks; + +namespace BlogDatabase.Implements +{ + public class UserStorage : IUserStorage + { + public UserViewModel? Delete(UserBindingModel model) + { + using var context = new BlogDatabase(); + var element = context.Users.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Users.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + public UserViewModel? GetElement(UserSearchModel model) + { + if (string.IsNullOrEmpty(model.Username) && !model.Id.HasValue && string.IsNullOrEmpty(model.Email)) + { + return null; + } + using var context = new BlogDatabase(); + return context.Users + .Include(x => x.Role) + .FirstOrDefault(x => + (!string.IsNullOrEmpty(model.Username) && x.Username == + model.Username) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public List GetFilteredList(UserSearchModel model) + { + if (string.IsNullOrEmpty(model.Username) || string.IsNullOrEmpty(model.Email)) + { + return new(); + } + using var context = new BlogDatabase(); + return context.Users + .Include(x => x.Role) + .Where(x => x.Username.Contains(model.Username)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new BlogDatabase(); + return context.Users + .Include(x => x.Role) + .Select(x => x.GetViewModel) + .ToList(); + } + + public UserViewModel? Insert(UserBindingModel model) + { + var newUser = User.Create(model); + if (newUser == null) + { + return null; + } + using var context = new BlogDatabase(); + context.Users.Add(newUser); + context.SaveChanges(); + return newUser.GetViewModel; + } + + public UserViewModel? Update(UserBindingModel model) + { + using var context = new BlogDatabase(); + var component = context.Users.FirstOrDefault(x => x.Id == model.Id); + if (component == null) + { + return null; + } + component.Update(model); + context.SaveChanges(); + return component.GetViewModel; + } + + public void UserInsertList(int num, List roles) + { + Random rnd = new Random(); + using var context = new BlogDatabase(); + for (int i = 0; i < num; ++i) + { + var model = new UserBindingModel + { + Id = 0, + Username = rnd.Next(1000).ToString(), + Email = rnd.Next(1000).ToString(), + Password = rnd.Next(1000).ToString(), + RoleId = roles[rnd.Next(roles.Count)].Id, + }; + context.Users.Add(User.Create(model)); + } + context.SaveChanges(); + } + } +} diff --git a/BlogDataModels/BlogDatabaseImplement/Model/Category.cs b/BlogDataModels/BlogDatabaseImplement/Model/Category.cs new file mode 100644 index 0000000..2a76f5a --- /dev/null +++ b/BlogDataModels/BlogDatabaseImplement/Model/Category.cs @@ -0,0 +1,54 @@ +using ForumContracts.BindingModels; +using ForumContracts.ViewModels; +using ForumDataModels; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Runtime.ConstrainedExecution; +using System.Text; +using System.Threading.Tasks; + +namespace BlogDatabase.Models +{ + public class Category : ICategoryModel + { + [Required] + public string Name { get; set; } = string.Empty; + + public int Id { get; private set; } + + [Required] + public int UserId { get; set; } + + [ForeignKey("TopicId")] + List Topics { get; set; } = new(); + + public static Category? Create(CategoryBindingModel model) + { + if (model == null) + { + return null; + } + return new Category() + { + Id = model.Id, + Name = model.Name, + }; + } + public void Update(CategoryBindingModel model) + { + if (model == null) + { + return; + } + Name = model.Name; + } + public CategoryViewModel GetViewModel => new() + { + Id = Id, + Name = Name, + }; + } +} diff --git a/BlogDataModels/BlogDatabaseImplement/Model/Message.cs b/BlogDataModels/BlogDatabaseImplement/Model/Message.cs new file mode 100644 index 0000000..182cff9 --- /dev/null +++ b/BlogDataModels/BlogDatabaseImplement/Model/Message.cs @@ -0,0 +1,69 @@ +using ForumContracts.BindingModels; +using ForumContracts.ViewModels; +using ForumDataModels; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace BlogDatabase.Models +{ + public class Message : IMessageModel + { + [Required] + public string Text { get; set; } = string.Empty; + + [Required] + public DateTime Date { get; set; } = DateTime.SpecifyKind(DateTime.MinValue, DateTimeKind.Utc); + + public int Id { get; private set; } + + [Required] + public int UserId { get; set; } + + [Required] + public int TopicId { get; set; } + + public virtual User? User { get; set; } + + public virtual Topic? Topic { get; set; } + + public static Message? Create(MessageBindingModel model) + { + if (model == null) + { + return null; + } + return new Message() + { + Id = model.Id, + Text = model.Text, + Date = model.Date, + UserId = model.UserId, + TopicId = model.TopicId, + }; + } + public void Update(MessageBindingModel model) + { + if (model == null) + { + return; + } + Text = model.Text; + Date = model.Date; + } + public MessageViewModel GetViewModel => new() + { + Id = Id, + Text = Text, + Date = Date, + TopicId = TopicId, + UserId = UserId, + Username = User == null ? string.Empty : User.Username, + TopicName = Topic == null ? string.Empty : Topic.Name, + }; + } +} diff --git a/BlogDataModels/BlogDatabaseImplement/Model/Role.cs b/BlogDataModels/BlogDatabaseImplement/Model/Role.cs new file mode 100644 index 0000000..211a5cd --- /dev/null +++ b/BlogDataModels/BlogDatabaseImplement/Model/Role.cs @@ -0,0 +1,52 @@ +using ForumContracts.BindingModels; +using ForumContracts.ViewModels; +using ForumDataModels; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Net; +using System.Text; +using System.Threading.Tasks; + +namespace BlogDatabase.Models +{ + public class Role : IRoleModel + { + [Required] + public string Name { get; set; } = string.Empty; + + public int Id { get; private set; } + + [ForeignKey("UserId")] + List Users { get; set; } = new(); + + public static Role? Create(RoleBindingModel model) + { + if (model == null) + { + return null; + } + return new Role() + { + Id = model.Id, + Name = model.Name, + }; + } + + public void Update(RoleBindingModel model) + { + if (model == null) + { + return; + } + Name = model.Name; + } + public RoleViewModel GetViewModel => new() + { + Id = Id, + Name = Name, + }; + } +} diff --git a/BlogDataModels/BlogDatabaseImplement/Model/Topic.cs b/BlogDataModels/BlogDatabaseImplement/Model/Topic.cs new file mode 100644 index 0000000..d3cf68c --- /dev/null +++ b/BlogDataModels/BlogDatabaseImplement/Model/Topic.cs @@ -0,0 +1,62 @@ +using ForumContracts.BindingModels; +using ForumContracts.ViewModels; +using ForumDataModels; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using static System.Net.Mime.MediaTypeNames; + +namespace BlogDatabase.Models +{ + public class Topic : ITopicModel + { + [Required] + public string Name { get; set; } = string.Empty; + + public int Id { get; private set; } + + [Required] + public int UserId { get; set; } + + [Required] + public int CategoryId { get; set; } + + [ForeignKey("MessageId")] + List Messages { get; set; } = new(); + + public virtual Category? Category { get; set; } + + public static Topic? Create(TopicBindingModel model) + { + if (model == null) + { + return null; + } + return new Topic() + { + Id = model.Id, + Name = model.Name, + CategoryId = model.CategoryId, + }; + } + public void Update(TopicBindingModel model) + { + if (model == null) + { + return; + } + model.Id = Id; + model.Name = Name; + } + public TopicViewModel GetViewModel => new() + { + Id = Id, + Name = Name, + CategoryId = CategoryId, + }; + } +} diff --git a/BlogDataModels/BlogDatabaseImplement/Model/User.cs b/BlogDataModels/BlogDatabaseImplement/Model/User.cs new file mode 100644 index 0000000..c8a9d96 --- /dev/null +++ b/BlogDataModels/BlogDatabaseImplement/Model/User.cs @@ -0,0 +1,82 @@ +using ForumContracts.BindingModels; +using ForumContracts.ViewModels; +using ForumDataModels; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Drawing; +using System.Linq; +using System.Reflection; +using System.Runtime.ConstrainedExecution; +using System.Text; +using System.Threading.Tasks; + +namespace BlogDatabase.Models +{ + public class User : IUserModel + { + [Required] + public string Username { get; set; } = string.Empty; + [Required] + public string Email { get; set; } = string.Empty; + [Required] + public string Password { get; set; } = string.Empty; + [Required] + public DateTime RegistrationDate { get; set; } + + public int Id { get; set; } + + [Required] + public int RoleId { get; set; } + + [ForeignKey("CategoryId")] + List Categories { get; set; } = new(); + + [ForeignKey("TopicId")] + List Topics { get; set; } = new(); + + [ForeignKey("MessageId")] + List Messages { get; set; } = new(); + + public virtual Role Role { get; set; } + + public static User? Create(UserBindingModel model) + { + if (model == null) + { + return null; + } + return new User() + { + Id = model.Id, + Username = model.Username, + Email = model.Email, + Password = model.Password, + RegistrationDate = model.RegistrationDate, + RoleId = model.RoleId, + }; + } + public void Update(UserBindingModel model) + { + if (model == null) + { + return; + } + Username = model.Username; + Email = model.Email; + Password = model.Password; + RegistrationDate = model.RegistrationDate; + } + public UserViewModel GetViewModel => new() + { + Id = Id, + Username = Username, + Email = Email, + Password = Password, + RegistrationDate = RegistrationDate, + RoleId = RoleId, + RoleName = Role == null ? string.Empty : Role.Name, + }; + } +} diff --git a/BlogDataModels/BlogViewModel/BlogViewModel.csproj b/BlogDataModels/BlogViewModel/BlogViewModel.csproj index b1c5e67..2d239b4 100644 --- a/BlogDataModels/BlogViewModel/BlogViewModel.csproj +++ b/BlogDataModels/BlogViewModel/BlogViewModel.csproj @@ -19,6 +19,7 @@ + diff --git a/BlogDataModels/BlogViewModel/FormAddTopic.Designer.cs b/BlogDataModels/BlogViewModel/FormAddTopic.Designer.cs new file mode 100644 index 0000000..ff1e157 --- /dev/null +++ b/BlogDataModels/BlogViewModel/FormAddTopic.Designer.cs @@ -0,0 +1,88 @@ +namespace Forum +{ + partial class FormAddTopic + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.textBoxTopic = new System.Windows.Forms.TextBox(); + this.buttonSave = new System.Windows.Forms.Button(); + this.buttonCancel = new System.Windows.Forms.Button(); + this.SuspendLayout(); + // + // textBoxTopic + // + this.textBoxTopic.Location = new System.Drawing.Point(18, 9); + this.textBoxTopic.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.textBoxTopic.Name = "textBoxTopic"; + this.textBoxTopic.Size = new System.Drawing.Size(191, 23); + this.textBoxTopic.TabIndex = 0; + // + // buttonSave + // + this.buttonSave.Location = new System.Drawing.Point(18, 34); + this.buttonSave.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.buttonSave.Name = "buttonSave"; + this.buttonSave.Size = new System.Drawing.Size(82, 22); + this.buttonSave.TabIndex = 1; + this.buttonSave.Text = "Сохранить"; + this.buttonSave.UseVisualStyleBackColor = true; + this.buttonSave.Click += new System.EventHandler(this.buttonSave_Click); + // + // buttonCancel + // + this.buttonCancel.Location = new System.Drawing.Point(126, 34); + this.buttonCancel.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.buttonCancel.Name = "buttonCancel"; + this.buttonCancel.Size = new System.Drawing.Size(82, 22); + this.buttonCancel.TabIndex = 2; + this.buttonCancel.Text = "Отмена"; + this.buttonCancel.UseVisualStyleBackColor = true; + this.buttonCancel.Click += new System.EventHandler(this.buttonCancel_Click); + // + // FormAddTopic + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(220, 63); + this.Controls.Add(this.buttonCancel); + this.Controls.Add(this.buttonSave); + this.Controls.Add(this.textBoxTopic); + this.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.Name = "FormAddTopic"; + this.Text = "Тема"; + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private TextBox textBoxTopic; + private Button buttonSave; + private Button buttonCancel; + } +} \ No newline at end of file diff --git a/BlogDataModels/BlogViewModel/FormAddTopic.cs b/BlogDataModels/BlogViewModel/FormAddTopic.cs new file mode 100644 index 0000000..8d6158a --- /dev/null +++ b/BlogDataModels/BlogViewModel/FormAddTopic.cs @@ -0,0 +1,67 @@ +using ForumContracts.BindingModels; +using ForumContracts.BusinessLogicContracts; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace Forum +{ + public partial class FormAddTopic : Form + { + private readonly ITopicLogic _topicLogic; + private readonly ICategoryLogic _categoryLogic; + + private int? _id; + public int Id { set { _id = value; } } + public FormAddTopic(ITopicLogic topicLogic, ICategoryLogic categoryLogic) + { + InitializeComponent(); + _topicLogic = topicLogic; + _categoryLogic = categoryLogic; + } + + private void buttonSave_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(textBoxTopic.Text)) + { + MessageBox.Show("Название темы не может быть пустой", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning); + return; + } + try + { + var model = new TopicBindingModel + { + Name = textBoxTopic.Text, + CategoryId = _id ?? 0, + }; + var operationResult = _topicLogic.Create(model); + + if (!operationResult) + { + throw new Exception("Ошибка при сохранеии."); + } + + MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); + DialogResult = DialogResult.OK; + + Close(); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void buttonCancel_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); + } + } +} diff --git a/BlogDataModels/BlogViewModel/FormAddTopic.resx b/BlogDataModels/BlogViewModel/FormAddTopic.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/BlogDataModels/BlogViewModel/FormAddTopic.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/BlogDataModels/BlogViewModel/FormAddTopics.Designer.cs b/BlogDataModels/BlogViewModel/FormAddTopics.Designer.cs new file mode 100644 index 0000000..3c91076 --- /dev/null +++ b/BlogDataModels/BlogViewModel/FormAddTopics.Designer.cs @@ -0,0 +1,134 @@ +namespace Forum +{ + partial class FormAddTopics + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.dataGridView = new System.Windows.Forms.DataGridView(); + this.textBoxCategory = new System.Windows.Forms.TextBox(); + this.buttonCreate = new System.Windows.Forms.Button(); + this.buttonChange = new System.Windows.Forms.Button(); + this.buttonDelete = new System.Windows.Forms.Button(); + this.buttonUpdate = new System.Windows.Forms.Button(); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); + this.SuspendLayout(); + // + // dataGridView + // + this.dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; + this.dataGridView.Location = new System.Drawing.Point(10, 40); + this.dataGridView.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.dataGridView.Name = "dataGridView"; + this.dataGridView.RowHeadersWidth = 51; + this.dataGridView.RowTemplate.Height = 29; + this.dataGridView.Size = new System.Drawing.Size(357, 141); + this.dataGridView.TabIndex = 0; + // + // textBoxCategory + // + this.textBoxCategory.Location = new System.Drawing.Point(10, 9); + this.textBoxCategory.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.textBoxCategory.Name = "textBoxCategory"; + this.textBoxCategory.ReadOnly = true; + this.textBoxCategory.Size = new System.Drawing.Size(110, 23); + this.textBoxCategory.TabIndex = 1; + // + // buttonCreate + // + this.buttonCreate.Location = new System.Drawing.Point(381, 40); + this.buttonCreate.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.buttonCreate.Name = "buttonCreate"; + this.buttonCreate.Size = new System.Drawing.Size(82, 22); + this.buttonCreate.TabIndex = 2; + this.buttonCreate.Text = "Создать"; + this.buttonCreate.UseVisualStyleBackColor = true; + this.buttonCreate.Click += new System.EventHandler(this.buttonCreate_Click); + // + // buttonChange + // + this.buttonChange.Location = new System.Drawing.Point(381, 67); + this.buttonChange.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.buttonChange.Name = "buttonChange"; + this.buttonChange.Size = new System.Drawing.Size(82, 22); + this.buttonChange.TabIndex = 3; + this.buttonChange.Text = "Изменить"; + this.buttonChange.UseVisualStyleBackColor = true; + this.buttonChange.Click += new System.EventHandler(this.buttonChange_Click); + // + // buttonDelete + // + this.buttonDelete.Location = new System.Drawing.Point(381, 93); + this.buttonDelete.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.buttonDelete.Name = "buttonDelete"; + this.buttonDelete.Size = new System.Drawing.Size(82, 22); + this.buttonDelete.TabIndex = 4; + this.buttonDelete.Text = "Удалить"; + this.buttonDelete.UseVisualStyleBackColor = true; + this.buttonDelete.Click += new System.EventHandler(this.buttonDelete_Click); + // + // buttonUpdate + // + this.buttonUpdate.Location = new System.Drawing.Point(381, 119); + this.buttonUpdate.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.buttonUpdate.Name = "buttonUpdate"; + this.buttonUpdate.Size = new System.Drawing.Size(82, 22); + this.buttonUpdate.TabIndex = 5; + this.buttonUpdate.Text = "Обновить"; + this.buttonUpdate.UseVisualStyleBackColor = true; + this.buttonUpdate.Click += new System.EventHandler(this.buttonUpdate_Click); + // + // FormAddTopics + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(469, 190); + this.Controls.Add(this.buttonUpdate); + this.Controls.Add(this.buttonDelete); + this.Controls.Add(this.buttonChange); + this.Controls.Add(this.buttonCreate); + this.Controls.Add(this.textBoxCategory); + this.Controls.Add(this.dataGridView); + this.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.Name = "FormAddTopics"; + this.Text = "Добавление тем"; + this.Load += new System.EventHandler(this.FormAddTopic_Load); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit(); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private DataGridView dataGridView; + private TextBox textBoxCategory; + private Button buttonCreate; + private Button buttonChange; + private Button buttonDelete; + private Button buttonUpdate; + } +} \ No newline at end of file diff --git a/BlogDataModels/BlogViewModel/FormAddTopics.cs b/BlogDataModels/BlogViewModel/FormAddTopics.cs new file mode 100644 index 0000000..43347fd --- /dev/null +++ b/BlogDataModels/BlogViewModel/FormAddTopics.cs @@ -0,0 +1,117 @@ +using ForumContracts.BindingModels; +using ForumContracts.BusinessLogicContracts; +using ForumContracts.SearchModels; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace Forum +{ + public partial class FormAddTopics : Form + { + private readonly ICategoryLogic _categoryLogic; + private readonly ITopicLogic _topicLogic; + private int? _id; + public int Id { set { _id = value; } } + public FormAddTopics(ITopicLogic topicLogic, ICategoryLogic categoryLogic) + { + InitializeComponent(); + _topicLogic = topicLogic; + _categoryLogic = categoryLogic; + } + + private void FormAddTopic_Load(object sender, EventArgs e) + { + LoadData(); + try + { + var view = _categoryLogic.ReadElement(new CategorySearchModel { Id = _id.Value }); + + if (view != null) + { + textBoxCategory.Text = view.Name; + } + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void buttonCreate_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormAddTopic)); + + if (service is FormAddTopic form) + { + form.Id = _id ?? 0; + if (form.ShowDialog() == DialogResult.OK) + { + LoadData(); + } + } + } + + private void LoadData() + { + try + { + var list = _topicLogic.ReadList(new TopicSearchModel { CategoryId = _id }); + + if (list != null) + { + dataGridView.DataSource = list; + dataGridView.Columns["Id"].Visible = false; + dataGridView.Columns["CategoryId"].Visible = false; + dataGridView.Columns["Name"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + } + } + catch (Exception) + { } + } + + private void buttonChange_Click(object sender, EventArgs e) + { + + } + + private void buttonDelete_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + if (MessageBox.Show("Удалить запись?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + { + int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + + try + { + if (!_topicLogic.Delete(new TopicBindingModel + { + Id = id + })) + { + throw new Exception("Ошибка при удалении."); + } + + LoadData(); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + } + + private void buttonUpdate_Click(object sender, EventArgs e) + { + LoadData(); + } + } +} diff --git a/BlogDataModels/BlogViewModel/FormAddTopics.resx b/BlogDataModels/BlogViewModel/FormAddTopics.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/BlogDataModels/BlogViewModel/FormAddTopics.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/BlogDataModels/BlogViewModel/FormCategories.Designer.cs b/BlogDataModels/BlogViewModel/FormCategories.Designer.cs new file mode 100644 index 0000000..6eb83b5 --- /dev/null +++ b/BlogDataModels/BlogViewModel/FormCategories.Designer.cs @@ -0,0 +1,128 @@ +namespace Forum +{ + partial class FormCategories + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.dataGridView = new System.Windows.Forms.DataGridView(); + this.buttonCreate = new System.Windows.Forms.Button(); + this.buttonChange = new System.Windows.Forms.Button(); + this.buttonDelete = new System.Windows.Forms.Button(); + this.buttonUpdate = new System.Windows.Forms.Button(); + this.buttonAddTopicInCategory = new System.Windows.Forms.Button(); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); + this.SuspendLayout(); + // + // dataGridView + // + this.dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; + this.dataGridView.Location = new System.Drawing.Point(12, 12); + this.dataGridView.Name = "dataGridView"; + this.dataGridView.RowHeadersWidth = 51; + this.dataGridView.RowTemplate.Height = 29; + this.dataGridView.Size = new System.Drawing.Size(300, 188); + this.dataGridView.TabIndex = 0; + // + // buttonCreate + // + this.buttonCreate.Location = new System.Drawing.Point(330, 12); + this.buttonCreate.Name = "buttonCreate"; + this.buttonCreate.Size = new System.Drawing.Size(94, 29); + this.buttonCreate.TabIndex = 1; + this.buttonCreate.Text = "Создать"; + this.buttonCreate.UseVisualStyleBackColor = true; + this.buttonCreate.Click += new System.EventHandler(this.buttonCreate_Click); + // + // buttonChange + // + this.buttonChange.Location = new System.Drawing.Point(330, 47); + this.buttonChange.Name = "buttonChange"; + this.buttonChange.Size = new System.Drawing.Size(94, 29); + this.buttonChange.TabIndex = 2; + this.buttonChange.Text = "Изменить"; + this.buttonChange.UseVisualStyleBackColor = true; + this.buttonChange.Click += new System.EventHandler(this.buttonChange_Click); + // + // buttonDelete + // + this.buttonDelete.Location = new System.Drawing.Point(330, 82); + this.buttonDelete.Name = "buttonDelete"; + this.buttonDelete.Size = new System.Drawing.Size(94, 29); + this.buttonDelete.TabIndex = 3; + this.buttonDelete.Text = "Удалить"; + this.buttonDelete.UseVisualStyleBackColor = true; + this.buttonDelete.Click += new System.EventHandler(this.buttonDelete_Click); + // + // buttonUpdate + // + this.buttonUpdate.Location = new System.Drawing.Point(330, 117); + this.buttonUpdate.Name = "buttonUpdate"; + this.buttonUpdate.Size = new System.Drawing.Size(94, 29); + this.buttonUpdate.TabIndex = 4; + this.buttonUpdate.Text = "Обновить"; + this.buttonUpdate.UseVisualStyleBackColor = true; + this.buttonUpdate.Click += new System.EventHandler(this.buttonUpdate_Click); + // + // buttonAddTopicInCategory + // + this.buttonAddTopicInCategory.Location = new System.Drawing.Point(330, 152); + this.buttonAddTopicInCategory.Name = "buttonAddTopicInCategory"; + this.buttonAddTopicInCategory.Size = new System.Drawing.Size(94, 54); + this.buttonAddTopicInCategory.TabIndex = 5; + this.buttonAddTopicInCategory.Text = "Добавить темы"; + this.buttonAddTopicInCategory.UseVisualStyleBackColor = true; + this.buttonAddTopicInCategory.Click += new System.EventHandler(this.buttonAddTopicInCategory_Click); + // + // FormCategories + // + this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(438, 211); + this.Controls.Add(this.buttonAddTopicInCategory); + this.Controls.Add(this.buttonUpdate); + this.Controls.Add(this.buttonDelete); + this.Controls.Add(this.buttonChange); + this.Controls.Add(this.buttonCreate); + this.Controls.Add(this.dataGridView); + this.Name = "FormCategories"; + this.Text = "Категории"; + this.Load += new System.EventHandler(this.FormCategories_Load); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit(); + this.ResumeLayout(false); + + } + + #endregion + + private DataGridView dataGridView; + private Button buttonCreate; + private Button buttonChange; + private Button buttonDelete; + private Button buttonUpdate; + private Button buttonAddTopicInCategory; + } +} \ No newline at end of file diff --git a/BlogDataModels/BlogViewModel/FormCategories.cs b/BlogDataModels/BlogViewModel/FormCategories.cs new file mode 100644 index 0000000..afcee17 --- /dev/null +++ b/BlogDataModels/BlogViewModel/FormCategories.cs @@ -0,0 +1,129 @@ +using ForumContracts.BindingModels; +using ForumContracts.BusinessLogicContracts; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace Forum +{ + public partial class FormCategories : Form + { + private readonly ICategoryLogic _categoryLogic; + public FormCategories(ICategoryLogic categoryLogic) + { + InitializeComponent(); + _categoryLogic = categoryLogic; + } + + + private void buttonCreate_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormCategory)); + + if (service is FormCategory form) + { + if (form.ShowDialog() == DialogResult.OK) + { + LoadData(); + } + } + } + + private void LoadData() + { + try + { + var list = _categoryLogic.ReadList(null); + + if (list != null) + { + dataGridView.DataSource = list; + dataGridView.Columns["Id"].Visible = false; + dataGridView.Columns["Name"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + } + } + catch (Exception) + { } + } + + private void FormCategories_Load(object sender, EventArgs e) + { + LoadData(); + } + + private void buttonDelete_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + if (MessageBox.Show("Удалить запись?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + { + int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + + try + { + if (!_categoryLogic.Delete(new CategoryBindingModel + { + Id = id + })) + { + throw new Exception("Ошибка при удалении."); + } + + LoadData(); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + } + + private void buttonChange_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + var service = Program.ServiceProvider?.GetService(typeof(FormCategory)); + + if (service is FormCategory form) + { + form.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + + if (form.ShowDialog() == DialogResult.OK) + { + LoadData(); + } + } + } + } + + private void buttonUpdate_Click(object sender, EventArgs e) + { + LoadData(); + } + + private void buttonAddTopicInCategory_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + var service = Program.ServiceProvider?.GetService(typeof(FormAddTopics)); + + if (service is FormAddTopics form) + { + form.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + + if (form.ShowDialog() == DialogResult.OK) + { + LoadData(); + } + } + } + } + } +} diff --git a/BlogDataModels/BlogViewModel/FormCategories.resx b/BlogDataModels/BlogViewModel/FormCategories.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/BlogDataModels/BlogViewModel/FormCategories.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/BlogDataModels/BlogViewModel/FormCategory.Designer.cs b/BlogDataModels/BlogViewModel/FormCategory.Designer.cs new file mode 100644 index 0000000..b27e7bc --- /dev/null +++ b/BlogDataModels/BlogViewModel/FormCategory.Designer.cs @@ -0,0 +1,85 @@ +namespace Forum +{ + partial class FormCategory + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.textBoxCategory = new System.Windows.Forms.TextBox(); + this.buttonSave = new System.Windows.Forms.Button(); + this.buttonCancel = new System.Windows.Forms.Button(); + this.SuspendLayout(); + // + // textBoxCategory + // + this.textBoxCategory.Location = new System.Drawing.Point(12, 12); + this.textBoxCategory.Name = "textBoxCategory"; + this.textBoxCategory.Size = new System.Drawing.Size(194, 27); + this.textBoxCategory.TabIndex = 0; + // + // buttonSave + // + this.buttonSave.Location = new System.Drawing.Point(12, 45); + this.buttonSave.Name = "buttonSave"; + this.buttonSave.Size = new System.Drawing.Size(94, 29); + this.buttonSave.TabIndex = 1; + this.buttonSave.Text = "Сохранить"; + this.buttonSave.UseVisualStyleBackColor = true; + this.buttonSave.Click += new System.EventHandler(this.buttonSave_Click); + // + // buttonCancel + // + this.buttonCancel.Location = new System.Drawing.Point(112, 45); + this.buttonCancel.Name = "buttonCancel"; + this.buttonCancel.Size = new System.Drawing.Size(94, 29); + this.buttonCancel.TabIndex = 2; + this.buttonCancel.Text = "Отмена"; + this.buttonCancel.UseVisualStyleBackColor = true; + this.buttonCancel.Click += new System.EventHandler(this.buttonCancel_Click); + // + // FormCategory + // + this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(222, 82); + this.Controls.Add(this.buttonCancel); + this.Controls.Add(this.buttonSave); + this.Controls.Add(this.textBoxCategory); + this.Name = "FormCategory"; + this.Text = "Категория"; + this.Click += new System.EventHandler(this.FormCategory_Load); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private TextBox textBoxCategory; + private Button buttonSave; + private Button buttonCancel; + } +} \ No newline at end of file diff --git a/BlogDataModels/BlogViewModel/FormCategory.cs b/BlogDataModels/BlogViewModel/FormCategory.cs new file mode 100644 index 0000000..d436f7e --- /dev/null +++ b/BlogDataModels/BlogViewModel/FormCategory.cs @@ -0,0 +1,88 @@ +using ForumContracts.BindingModels; +using ForumContracts.BusinessLogicContracts; +using ForumContracts.SearchModels; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace Forum +{ + public partial class FormCategory : Form + { + private readonly ICategoryLogic _categoryLogic; + + private int? _id; + public int Id { set { _id = value; } } + public FormCategory(ICategoryLogic categoryLogic) + { + InitializeComponent(); + _categoryLogic = categoryLogic; + + } + + private void buttonSave_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(textBoxCategory.Text)) + { + MessageBox.Show("Введите название роли", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning); + return; + } + try + { + var model = new CategoryBindingModel + { + Id = _id ?? 0, + Name = textBoxCategory.Text + }; + + var operationResult = _id.HasValue ? _categoryLogic.Update(model) : _categoryLogic.Create(model); + + if (!operationResult) + { + throw new Exception("Ошибка при сохранеии."); + } + + MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); + DialogResult = DialogResult.OK; + + Close(); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void buttonCancel_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); + } + + private void FormCategory_Load(object sender, EventArgs e) + { + if (_id.HasValue) + { + try + { + var view = _categoryLogic.ReadElement(new CategorySearchModel { Id = _id.Value }); + + if (view != null) + { + textBoxCategory.Text = view.Name; + } + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + } +} diff --git a/BlogDataModels/BlogViewModel/FormCategory.resx b/BlogDataModels/BlogViewModel/FormCategory.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/BlogDataModels/BlogViewModel/FormCategory.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/BlogDataModels/BlogViewModel/FormCreateMessage.Designer.cs b/BlogDataModels/BlogViewModel/FormCreateMessage.Designer.cs new file mode 100644 index 0000000..420c8b6 --- /dev/null +++ b/BlogDataModels/BlogViewModel/FormCreateMessage.Designer.cs @@ -0,0 +1,156 @@ +namespace Forum +{ + partial class FormCreateMessage + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.comboBoxCategory = new System.Windows.Forms.ComboBox(); + this.comboBoxTopic = new System.Windows.Forms.ComboBox(); + this.label1 = new System.Windows.Forms.Label(); + this.label2 = new System.Windows.Forms.Label(); + this.comboBoxUser = new System.Windows.Forms.ComboBox(); + this.label3 = new System.Windows.Forms.Label(); + this.textBoxMessage = new System.Windows.Forms.TextBox(); + this.buttonSave = new System.Windows.Forms.Button(); + this.buttonCancel = new System.Windows.Forms.Button(); + this.SuspendLayout(); + // + // comboBoxCategory + // + this.comboBoxCategory.FormattingEnabled = true; + this.comboBoxCategory.Location = new System.Drawing.Point(141, 6); + this.comboBoxCategory.Name = "comboBoxCategory"; + this.comboBoxCategory.Size = new System.Drawing.Size(147, 23); + this.comboBoxCategory.TabIndex = 0; + this.comboBoxCategory.SelectedIndexChanged += new System.EventHandler(this.comboBoxCategory_SelectedIndexChanged); + // + // comboBoxTopic + // + this.comboBoxTopic.FormattingEnabled = true; + this.comboBoxTopic.Location = new System.Drawing.Point(141, 35); + this.comboBoxTopic.Name = "comboBoxTopic"; + this.comboBoxTopic.Size = new System.Drawing.Size(147, 23); + this.comboBoxTopic.TabIndex = 1; + // + // label1 + // + this.label1.AutoSize = true; + this.label1.Location = new System.Drawing.Point(12, 9); + this.label1.Name = "label1"; + this.label1.Size = new System.Drawing.Size(123, 15); + this.label1.TabIndex = 2; + this.label1.Text = "Выберите категорию"; + // + // label2 + // + this.label2.AutoSize = true; + this.label2.Location = new System.Drawing.Point(12, 38); + this.label2.Name = "label2"; + this.label2.Size = new System.Drawing.Size(90, 15); + this.label2.TabIndex = 3; + this.label2.Text = "Выберите тему"; + // + // comboBoxUser + // + this.comboBoxUser.FormattingEnabled = true; + this.comboBoxUser.Location = new System.Drawing.Point(141, 64); + this.comboBoxUser.Name = "comboBoxUser"; + this.comboBoxUser.Size = new System.Drawing.Size(147, 23); + this.comboBoxUser.TabIndex = 4; + // + // label3 + // + this.label3.AutoSize = true; + this.label3.Location = new System.Drawing.Point(12, 67); + this.label3.Name = "label3"; + this.label3.Size = new System.Drawing.Size(84, 15); + this.label3.TabIndex = 5; + this.label3.Text = "Пользователь"; + // + // textBoxMessage + // + this.textBoxMessage.Location = new System.Drawing.Point(12, 93); + this.textBoxMessage.Multiline = true; + this.textBoxMessage.Name = "textBoxMessage"; + this.textBoxMessage.Size = new System.Drawing.Size(276, 70); + this.textBoxMessage.TabIndex = 6; + // + // buttonSave + // + this.buttonSave.Location = new System.Drawing.Point(132, 169); + this.buttonSave.Name = "buttonSave"; + this.buttonSave.Size = new System.Drawing.Size(75, 23); + this.buttonSave.TabIndex = 7; + this.buttonSave.Text = "Сохранить"; + this.buttonSave.UseVisualStyleBackColor = true; + this.buttonSave.Click += new System.EventHandler(this.buttonSave_Click); + // + // buttonCancel + // + this.buttonCancel.Location = new System.Drawing.Point(213, 169); + this.buttonCancel.Name = "buttonCancel"; + this.buttonCancel.Size = new System.Drawing.Size(75, 23); + this.buttonCancel.TabIndex = 8; + this.buttonCancel.Text = "Отмена"; + this.buttonCancel.UseVisualStyleBackColor = true; + this.buttonCancel.Click += new System.EventHandler(this.buttonCancel_Click); + // + // FormCreateMessage + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(296, 198); + this.Controls.Add(this.buttonCancel); + this.Controls.Add(this.buttonSave); + this.Controls.Add(this.textBoxMessage); + this.Controls.Add(this.label3); + this.Controls.Add(this.comboBoxUser); + this.Controls.Add(this.label2); + this.Controls.Add(this.label1); + this.Controls.Add(this.comboBoxTopic); + this.Controls.Add(this.comboBoxCategory); + this.Name = "FormCreateMessage"; + this.Text = "Написать сообщение"; + this.Load += new System.EventHandler(this.FormCreateMessage_Load); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private ComboBox comboBoxCategory; + private ComboBox comboBoxTopic; + private Label label1; + private Label label2; + private ComboBox comboBoxUser; + private Label label3; + private TextBox textBoxMessage; + private Button buttonSave; + private Button buttonCancel; + } +} \ No newline at end of file diff --git a/BlogDataModels/BlogViewModel/FormCreateMessage.cs b/BlogDataModels/BlogViewModel/FormCreateMessage.cs new file mode 100644 index 0000000..d1e43a3 --- /dev/null +++ b/BlogDataModels/BlogViewModel/FormCreateMessage.cs @@ -0,0 +1,139 @@ +using ForumBusinessLogic; +using ForumContracts.BindingModels; +using ForumContracts.BusinessLogicContracts; +using ForumContracts.SearchModels; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace Forum +{ + public partial class FormCreateMessage : Form + { + private readonly IMessageLogic _messageLogic; + private readonly ICategoryLogic _categoryLogic; + private readonly ITopicLogic _topicLogic; + private readonly IUserLogic _userLogic; + + private int? _id; + public int Id { set { _id = value; } } + public FormCreateMessage(ICategoryLogic categoryLogic, ITopicLogic topicLogic, IUserLogic userLogic, IMessageLogic messageLogic) + { + InitializeComponent(); + _categoryLogic = categoryLogic; + _topicLogic = topicLogic; + _userLogic = userLogic; + _messageLogic = messageLogic; + } + + private void buttonSave_Click(object sender, EventArgs e) + { + if (comboBoxCategory.SelectedValue == null) + { + MessageBox.Show("Выберите категорию", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (comboBoxTopic.SelectedValue == null) + { + MessageBox.Show("Выберите тему", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (comboBoxUser.SelectedValue == null) + { + MessageBox.Show("Выберите пользователя", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (string.IsNullOrEmpty(textBoxMessage.Text)) + { + MessageBox.Show("Напишите сообщение", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + try + { + var model = new MessageBindingModel + { + Id = _id ?? 0, + Date = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc), + Text = textBoxMessage.Text, + TopicId = Convert.ToInt32(comboBoxTopic.SelectedValue), + UserId = Convert.ToInt32(comboBoxUser.SelectedValue), + }; + var operationResult = _id.HasValue ? _messageLogic.Update(model) : _messageLogic.Create(model); + if (!operationResult) + { + throw new Exception("Ошибка при создании сообщения."); + } + MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); + DialogResult = DialogResult.OK; + Close(); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void buttonCancel_Click(object sender, EventArgs e) + { + + } + + private void FormCreateMessage_Load(object sender, EventArgs e) + { + LoadData(); + } + + private void LoadData() + { + try + { + var user = _userLogic.ReadList(null); + if (user != null) + { + comboBoxUser.DisplayMember = "Username"; + comboBoxUser.ValueMember = "Id"; + comboBoxUser.DataSource = user; + comboBoxUser.SelectedItem = null; + } + var category = _categoryLogic.ReadList(null); + if (category != null) + { + comboBoxCategory.DisplayMember = "Name"; + comboBoxCategory.ValueMember = "Id"; + comboBoxCategory.DataSource = category; + comboBoxCategory.SelectedItem = null; + } + + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void comboBoxCategory_SelectedIndexChanged(object sender, EventArgs e) + { + try + { + var topics = _topicLogic.ReadList(new TopicSearchModel { CategoryId = Convert.ToInt32(comboBoxCategory.SelectedValue), }); + if (topics != null) + { + comboBoxTopic.DisplayMember = "Name"; + comboBoxTopic.ValueMember = "Id"; + comboBoxTopic.DataSource = topics; + comboBoxTopic.SelectedItem = null; + } + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } +} diff --git a/BlogDataModels/BlogViewModel/FormCreateMessage.resx b/BlogDataModels/BlogViewModel/FormCreateMessage.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/BlogDataModels/BlogViewModel/FormCreateMessage.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/BlogDataModels/BlogViewModel/FormMain.Designer.cs b/BlogDataModels/BlogViewModel/FormMain.Designer.cs new file mode 100644 index 0000000..917641f --- /dev/null +++ b/BlogDataModels/BlogViewModel/FormMain.Designer.cs @@ -0,0 +1,163 @@ +namespace Forum +{ + partial class FormMain + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.dataGridView = new System.Windows.Forms.DataGridView(); + this.menuStrip1 = new System.Windows.Forms.MenuStrip(); + this.ролиToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.пользователиToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.категорииToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.buttonCreate = new System.Windows.Forms.Button(); + this.buttonDelete = new System.Windows.Forms.Button(); + this.buttonUpdate = new System.Windows.Forms.Button(); + this.тестыToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); + this.menuStrip1.SuspendLayout(); + this.SuspendLayout(); + // + // dataGridView + // + this.dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; + this.dataGridView.Location = new System.Drawing.Point(10, 32); + this.dataGridView.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.dataGridView.Name = "dataGridView"; + this.dataGridView.RowHeadersWidth = 51; + this.dataGridView.RowTemplate.Height = 29; + this.dataGridView.Size = new System.Drawing.Size(554, 296); + this.dataGridView.TabIndex = 0; + // + // menuStrip1 + // + this.menuStrip1.ImageScalingSize = new System.Drawing.Size(20, 20); + this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.ролиToolStripMenuItem, + this.пользователиToolStripMenuItem, + this.категорииToolStripMenuItem, + this.тестыToolStripMenuItem}); + this.menuStrip1.Location = new System.Drawing.Point(0, 0); + this.menuStrip1.Name = "menuStrip1"; + this.menuStrip1.Padding = new System.Windows.Forms.Padding(5, 2, 0, 2); + this.menuStrip1.Size = new System.Drawing.Size(653, 24); + this.menuStrip1.TabIndex = 1; + this.menuStrip1.Text = "menuStrip1"; + // + // ролиToolStripMenuItem + // + this.ролиToolStripMenuItem.Name = "ролиToolStripMenuItem"; + this.ролиToolStripMenuItem.Size = new System.Drawing.Size(47, 20); + this.ролиToolStripMenuItem.Text = "Роли"; + this.ролиToolStripMenuItem.Click += new System.EventHandler(this.ролиToolStripMenuItem_Click); + // + // пользователиToolStripMenuItem + // + this.пользователиToolStripMenuItem.Name = "пользователиToolStripMenuItem"; + this.пользователиToolStripMenuItem.Size = new System.Drawing.Size(97, 20); + this.пользователиToolStripMenuItem.Text = "Пользователи"; + this.пользователиToolStripMenuItem.Click += new System.EventHandler(this.пользователиToolStripMenuItem_Click); + // + // категорииToolStripMenuItem + // + this.категорииToolStripMenuItem.Name = "категорииToolStripMenuItem"; + this.категорииToolStripMenuItem.Size = new System.Drawing.Size(76, 20); + this.категорииToolStripMenuItem.Text = "Категории"; + this.категорииToolStripMenuItem.Click += new System.EventHandler(this.категорииToolStripMenuItem_Click); + // + // buttonCreate + // + this.buttonCreate.Location = new System.Drawing.Point(570, 32); + this.buttonCreate.Name = "buttonCreate"; + this.buttonCreate.Size = new System.Drawing.Size(75, 23); + this.buttonCreate.TabIndex = 2; + this.buttonCreate.Text = "Создать"; + this.buttonCreate.UseVisualStyleBackColor = true; + this.buttonCreate.Click += new System.EventHandler(this.buttonCreate_Click); + // + // buttonDelete + // + this.buttonDelete.Location = new System.Drawing.Point(570, 61); + this.buttonDelete.Name = "buttonDelete"; + this.buttonDelete.Size = new System.Drawing.Size(75, 23); + this.buttonDelete.TabIndex = 4; + this.buttonDelete.Text = "Удалить"; + this.buttonDelete.UseVisualStyleBackColor = true; + this.buttonDelete.Click += new System.EventHandler(this.buttonDelete_Click); + // + // buttonUpdate + // + this.buttonUpdate.Location = new System.Drawing.Point(570, 90); + this.buttonUpdate.Name = "buttonUpdate"; + this.buttonUpdate.Size = new System.Drawing.Size(75, 23); + this.buttonUpdate.TabIndex = 5; + this.buttonUpdate.Text = "Обновить"; + this.buttonUpdate.UseVisualStyleBackColor = true; + this.buttonUpdate.Click += new System.EventHandler(this.buttonUpdate_Click); + // + // тестыToolStripMenuItem + // + this.тестыToolStripMenuItem.Name = "тестыToolStripMenuItem"; + this.тестыToolStripMenuItem.Size = new System.Drawing.Size(51, 20); + this.тестыToolStripMenuItem.Text = "Тесты"; + this.тестыToolStripMenuItem.Click += new System.EventHandler(this.тестыToolStripMenuItem_Click); + // + // FormMain + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(653, 338); + this.Controls.Add(this.buttonUpdate); + this.Controls.Add(this.buttonDelete); + this.Controls.Add(this.buttonCreate); + this.Controls.Add(this.dataGridView); + this.Controls.Add(this.menuStrip1); + this.MainMenuStrip = this.menuStrip1; + this.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.Name = "FormMain"; + this.Text = "Форум"; + this.Load += new System.EventHandler(this.FormMain_Load); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit(); + this.menuStrip1.ResumeLayout(false); + this.menuStrip1.PerformLayout(); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private DataGridView dataGridView; + private MenuStrip menuStrip1; + private ToolStripMenuItem ролиToolStripMenuItem; + private ToolStripMenuItem пользователиToolStripMenuItem; + private ToolStripMenuItem категорииToolStripMenuItem; + private Button buttonCreate; + private Button buttonDelete; + private Button buttonUpdate; + private ToolStripMenuItem тестыToolStripMenuItem; + } +} \ No newline at end of file diff --git a/BlogDataModels/BlogViewModel/FormMain.cs b/BlogDataModels/BlogViewModel/FormMain.cs new file mode 100644 index 0000000..f131a73 --- /dev/null +++ b/BlogDataModels/BlogViewModel/FormMain.cs @@ -0,0 +1,147 @@ +using ForumBusinessLogic; +using ForumContracts.BindingModels; +using ForumContracts.BusinessLogicContracts; +using System.Windows.Forms; + +namespace Forum +{ + public partial class FormMain : Form + { + private readonly IMessageLogic _messageLogic; + public FormMain(IMessageLogic messageLogic) + { + InitializeComponent(); + _messageLogic = messageLogic; + } + + private void ToolStripMenuItem_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormRoles)); + + if (service is FormRoles form) + { + form.ShowDialog(); + } + } + + private void ToolStripMenuItem_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormUsers)); + + if (service is FormUsers form) + { + form.ShowDialog(); + } + } + + private void ToolStripMenuItem_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormCategories)); + + if (service is FormCategories form) + { + form.ShowDialog(); + } + } + + private void buttonCreate_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormCreateMessage)); + + if (service is FormCreateMessage form) + { + form.ShowDialog(); + LoadData(); + } + } + + private void FormMain_Load(object sender, EventArgs e) + { + LoadData(); + } + + private void LoadData() + { + try + { + var list = _messageLogic.ReadList(null); + if (list != null) + { + dataGridView.DataSource = list; + dataGridView.Columns["Id"].Visible = false; + dataGridView.Columns["UserId"].Visible = false; + dataGridView.Columns["TopicId"].Visible = false; + dataGridView.Columns["Username"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + dataGridView.Columns["TopicName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + dataGridView.Columns["Text"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + dataGridView.Columns["Date"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + } + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void buttonDelete_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + if (MessageBox.Show(" ?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + { + int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + + try + { + if (!_messageLogic.Delete(new MessageBindingModel + { + Id = id + })) + { + throw new Exception(" ."); + } + + LoadData(); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + } + + private void buttonUpdate_Click(object sender, EventArgs e) + { + LoadData(); + } + + private void buttonChange_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + var service = Program.ServiceProvider?.GetService(typeof(FormCreateMessage)); + + if (service is FormCreateMessage form) + { + form.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + + if (form.ShowDialog() == DialogResult.OK) + { + LoadData(); + } + } + } + } + + private void ToolStripMenuItem_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormTests)); + + if (service is FormTests form) + { + form.ShowDialog(); + } + } + } +} \ No newline at end of file diff --git a/BlogDataModels/BlogViewModel/FormMain.resx b/BlogDataModels/BlogViewModel/FormMain.resx new file mode 100644 index 0000000..938108a --- /dev/null +++ b/BlogDataModels/BlogViewModel/FormMain.resx @@ -0,0 +1,63 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + 17, 17 + + \ No newline at end of file diff --git a/BlogDataModels/BlogViewModel/FormRole.Designer.cs b/BlogDataModels/BlogViewModel/FormRole.Designer.cs new file mode 100644 index 0000000..0f47b2b --- /dev/null +++ b/BlogDataModels/BlogViewModel/FormRole.Designer.cs @@ -0,0 +1,102 @@ +namespace Forum +{ + partial class FormRole + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.textBoxRoleName = new System.Windows.Forms.TextBox(); + this.buttonSave = new System.Windows.Forms.Button(); + this.buttonCancel = new System.Windows.Forms.Button(); + this.buttonAddRoles = new System.Windows.Forms.Button(); + this.SuspendLayout(); + // + // textBoxRoleName + // + this.textBoxRoleName.Location = new System.Drawing.Point(10, 9); + this.textBoxRoleName.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.textBoxRoleName.Name = "textBoxRoleName"; + this.textBoxRoleName.Size = new System.Drawing.Size(218, 23); + this.textBoxRoleName.TabIndex = 0; + // + // buttonSave + // + this.buttonSave.Location = new System.Drawing.Point(10, 34); + this.buttonSave.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.buttonSave.Name = "buttonSave"; + this.buttonSave.Size = new System.Drawing.Size(82, 22); + this.buttonSave.TabIndex = 1; + this.buttonSave.Text = "Сохранить"; + this.buttonSave.UseVisualStyleBackColor = true; + this.buttonSave.Click += new System.EventHandler(this.buttonSave_Click); + // + // buttonCancel + // + this.buttonCancel.Location = new System.Drawing.Point(10, 63); + this.buttonCancel.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.buttonCancel.Name = "buttonCancel"; + this.buttonCancel.Size = new System.Drawing.Size(82, 22); + this.buttonCancel.TabIndex = 2; + this.buttonCancel.Text = "Отмена"; + this.buttonCancel.UseVisualStyleBackColor = true; + this.buttonCancel.Click += new System.EventHandler(this.buttonCancel_Click); + // + // buttonAddRoles + // + this.buttonAddRoles.Location = new System.Drawing.Point(153, 34); + this.buttonAddRoles.Name = "buttonAddRoles"; + this.buttonAddRoles.Size = new System.Drawing.Size(75, 54); + this.buttonAddRoles.TabIndex = 3; + this.buttonAddRoles.Text = "Добавить 1000 ролей"; + this.buttonAddRoles.UseVisualStyleBackColor = true; + this.buttonAddRoles.Click += new System.EventHandler(this.buttonAddRoles_Click); + // + // FormRole + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(234, 96); + this.Controls.Add(this.buttonAddRoles); + this.Controls.Add(this.buttonCancel); + this.Controls.Add(this.buttonSave); + this.Controls.Add(this.textBoxRoleName); + this.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.Name = "FormRole"; + this.Text = "Роль"; + this.Load += new System.EventHandler(this.FormRole_Load); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private TextBox textBoxRoleName; + private Button buttonSave; + private Button buttonCancel; + private Button buttonAddRoles; + } +} \ No newline at end of file diff --git a/BlogDataModels/BlogViewModel/FormRole.cs b/BlogDataModels/BlogViewModel/FormRole.cs new file mode 100644 index 0000000..c68b74e --- /dev/null +++ b/BlogDataModels/BlogViewModel/FormRole.cs @@ -0,0 +1,102 @@ +using ForumContracts.BindingModels; +using ForumContracts.BusinessLogicContracts; +using ForumContracts.SearchModels; +using Microsoft.EntityFrameworkCore.Diagnostics; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace Forum +{ + public partial class FormRole : Form + { + private readonly IRoleLogic _roleLogic; + + private int? _id; + public int Id { set { _id = value; } } + public FormRole(IRoleLogic roleLogic) + { + InitializeComponent(); + _roleLogic = roleLogic; + + } + + private void buttonSave_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(textBoxRoleName.Text)) + { + MessageBox.Show("Введите название роли", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning); + return; + } + try + { + var model = new RoleBindingModel + { + Id = _id ?? 0, + Name = textBoxRoleName.Text + }; + + var operationResult = _id.HasValue ? _roleLogic.Update(model) : _roleLogic.Create(model); + + if (!operationResult) + { + throw new Exception("Ошибка при сохранеии."); + } + + MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); + DialogResult = DialogResult.OK; + + Close(); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void buttonCancel_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); + } + + private void FormRole_Load(object sender, EventArgs e) + { + if (_id.HasValue) + { + try + { + var view = _roleLogic.ReadElement(new RoleSearchModel { Id = _id.Value }); + + if (view != null) + { + textBoxRoleName.Text = view.Name; + } + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + + private void buttonAddRoles_Click(object sender, EventArgs e) + { + try + { + _roleLogic.RoleInsertList(1000); + FormRole_Load(sender, e); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } +} diff --git a/BlogDataModels/BlogViewModel/FormRole.resx b/BlogDataModels/BlogViewModel/FormRole.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/BlogDataModels/BlogViewModel/FormRole.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/BlogDataModels/BlogViewModel/FormRoles.Designer.cs b/BlogDataModels/BlogViewModel/FormRoles.Designer.cs new file mode 100644 index 0000000..a0fbea4 --- /dev/null +++ b/BlogDataModels/BlogViewModel/FormRoles.Designer.cs @@ -0,0 +1,115 @@ +namespace Forum +{ + partial class FormRoles + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.dataGridView = new System.Windows.Forms.DataGridView(); + this.buttonCreate = new System.Windows.Forms.Button(); + this.buttonDelete = new System.Windows.Forms.Button(); + this.buttonChange = new System.Windows.Forms.Button(); + this.buttonUpdate = new System.Windows.Forms.Button(); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); + this.SuspendLayout(); + // + // dataGridView + // + this.dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; + this.dataGridView.Location = new System.Drawing.Point(12, 12); + this.dataGridView.Name = "dataGridView"; + this.dataGridView.RowHeadersWidth = 51; + this.dataGridView.RowTemplate.Height = 29; + this.dataGridView.Size = new System.Drawing.Size(371, 268); + this.dataGridView.TabIndex = 0; + // + // buttonCreate + // + this.buttonCreate.Location = new System.Drawing.Point(389, 12); + this.buttonCreate.Name = "buttonCreate"; + this.buttonCreate.Size = new System.Drawing.Size(94, 29); + this.buttonCreate.TabIndex = 1; + this.buttonCreate.Text = "Создать"; + this.buttonCreate.UseVisualStyleBackColor = true; + this.buttonCreate.Click += new System.EventHandler(this.buttonCreate_Click); + // + // buttonDelete + // + this.buttonDelete.Location = new System.Drawing.Point(389, 47); + this.buttonDelete.Name = "buttonDelete"; + this.buttonDelete.Size = new System.Drawing.Size(94, 29); + this.buttonDelete.TabIndex = 2; + this.buttonDelete.Text = "Удалить"; + this.buttonDelete.UseVisualStyleBackColor = true; + this.buttonDelete.Click += new System.EventHandler(this.buttonDelete_Click); + // + // buttonChange + // + this.buttonChange.Location = new System.Drawing.Point(389, 82); + this.buttonChange.Name = "buttonChange"; + this.buttonChange.Size = new System.Drawing.Size(94, 29); + this.buttonChange.TabIndex = 3; + this.buttonChange.Text = "Изменить"; + this.buttonChange.UseVisualStyleBackColor = true; + this.buttonChange.Click += new System.EventHandler(this.buttonChange_Click); + // + // buttonUpdate + // + this.buttonUpdate.Location = new System.Drawing.Point(389, 117); + this.buttonUpdate.Name = "buttonUpdate"; + this.buttonUpdate.Size = new System.Drawing.Size(94, 29); + this.buttonUpdate.TabIndex = 4; + this.buttonUpdate.Text = "Обновить"; + this.buttonUpdate.UseVisualStyleBackColor = true; + this.buttonUpdate.Click += new System.EventHandler(this.buttonUpdate_Click); + // + // FormRoles + // + this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(495, 292); + this.Controls.Add(this.buttonUpdate); + this.Controls.Add(this.buttonChange); + this.Controls.Add(this.buttonDelete); + this.Controls.Add(this.buttonCreate); + this.Controls.Add(this.dataGridView); + this.Name = "FormRoles"; + this.Text = "Роли"; + this.Load += new System.EventHandler(this.FormRoles_Load); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit(); + this.ResumeLayout(false); + + } + + #endregion + + private DataGridView dataGridView; + private Button buttonCreate; + private Button buttonDelete; + private Button buttonChange; + private Button buttonUpdate; + } +} \ No newline at end of file diff --git a/BlogDataModels/BlogViewModel/FormRoles.cs b/BlogDataModels/BlogViewModel/FormRoles.cs new file mode 100644 index 0000000..7c969c1 --- /dev/null +++ b/BlogDataModels/BlogViewModel/FormRoles.cs @@ -0,0 +1,111 @@ +using ForumContracts.BindingModels; +using ForumContracts.BusinessLogicContracts; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace Forum +{ + public partial class FormRoles : Form + { + private readonly IRoleLogic _roleLogic; + public FormRoles(IRoleLogic roleLogic) + { + InitializeComponent(); + _roleLogic = roleLogic; + } + + + private void buttonCreate_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormRole)); + + if (service is FormRole form) + { + if (form.ShowDialog() == DialogResult.OK) + { + LoadData(); + } + } + } + + private void LoadData() + { + try + { + var list = _roleLogic.ReadList(null); + + if (list != null) + { + dataGridView.DataSource = list; + dataGridView.Columns["Id"].Visible = false; + dataGridView.Columns["Name"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + } + } + catch (Exception) + { } + } + + private void FormRoles_Load(object sender, EventArgs e) + { + LoadData(); + } + + private void buttonDelete_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + if (MessageBox.Show("Удалить запись?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + { + int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + + try + { + if (!_roleLogic.Delete(new RoleBindingModel + { + Id = id + })) + { + throw new Exception("Ошибка при удалении."); + } + + LoadData(); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + } + + private void buttonChange_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + var service = Program.ServiceProvider?.GetService(typeof(FormRole)); + + if (service is FormRole form) + { + form.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + + if (form.ShowDialog() == DialogResult.OK) + { + LoadData(); + } + } + } + } + + private void buttonUpdate_Click(object sender, EventArgs e) + { + LoadData(); + } + } +} diff --git a/BlogDataModels/BlogViewModel/FormRoles.resx b/BlogDataModels/BlogViewModel/FormRoles.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/BlogDataModels/BlogViewModel/FormRoles.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/BlogDataModels/BlogViewModel/FormTests.Designer.cs b/BlogDataModels/BlogViewModel/FormTests.Designer.cs new file mode 100644 index 0000000..5252f3a --- /dev/null +++ b/BlogDataModels/BlogViewModel/FormTests.Designer.cs @@ -0,0 +1,229 @@ +namespace Forum +{ + partial class FormTests + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.buttonInsertTest = new System.Windows.Forms.Button(); + this.label1 = new System.Windows.Forms.Label(); + this.label2 = new System.Windows.Forms.Label(); + this.textBoxInsertTime = new System.Windows.Forms.TextBox(); + this.buttonReadTest = new System.Windows.Forms.Button(); + this.textBoxReadTime = new System.Windows.Forms.TextBox(); + this.label3 = new System.Windows.Forms.Label(); + this.label4 = new System.Windows.Forms.Label(); + this.numericUpDownInsert = new System.Windows.Forms.NumericUpDown(); + this.numericUpDownRead = new System.Windows.Forms.NumericUpDown(); + this.buttonJoinQuery = new System.Windows.Forms.Button(); + this.numericUpDownJoin = new System.Windows.Forms.NumericUpDown(); + this.textBoxJoin = new System.Windows.Forms.TextBox(); + this.label5 = new System.Windows.Forms.Label(); + this.label6 = new System.Windows.Forms.Label(); + ((System.ComponentModel.ISupportInitialize)(this.numericUpDownInsert)).BeginInit(); + ((System.ComponentModel.ISupportInitialize)(this.numericUpDownRead)).BeginInit(); + ((System.ComponentModel.ISupportInitialize)(this.numericUpDownJoin)).BeginInit(); + this.SuspendLayout(); + // + // buttonInsertTest + // + this.buttonInsertTest.Location = new System.Drawing.Point(12, 12); + this.buttonInsertTest.Name = "buttonInsertTest"; + this.buttonInsertTest.Size = new System.Drawing.Size(86, 61); + this.buttonInsertTest.TabIndex = 0; + this.buttonInsertTest.Text = "Тест вставки сообщений"; + this.buttonInsertTest.UseVisualStyleBackColor = true; + this.buttonInsertTest.Click += new System.EventHandler(this.buttonInsertTest_Click); + // + // label1 + // + this.label1.AutoSize = true; + this.label1.Location = new System.Drawing.Point(104, 12); + this.label1.Name = "label1"; + this.label1.Size = new System.Drawing.Size(156, 15); + this.label1.TabIndex = 1; + this.label1.Text = "Введите кол-во элементов:"; + // + // label2 + // + this.label2.AutoSize = true; + this.label2.Location = new System.Drawing.Point(104, 53); + this.label2.Name = "label2"; + this.label2.Size = new System.Drawing.Size(146, 15); + this.label2.TabIndex = 3; + this.label2.Text = "Итоговое время запроса:"; + // + // textBoxInsertTime + // + this.textBoxInsertTime.Location = new System.Drawing.Point(266, 50); + this.textBoxInsertTime.Name = "textBoxInsertTime"; + this.textBoxInsertTime.ReadOnly = true; + this.textBoxInsertTime.Size = new System.Drawing.Size(100, 23); + this.textBoxInsertTime.TabIndex = 4; + // + // buttonReadTest + // + this.buttonReadTest.Location = new System.Drawing.Point(12, 106); + this.buttonReadTest.Name = "buttonReadTest"; + this.buttonReadTest.Size = new System.Drawing.Size(86, 56); + this.buttonReadTest.TabIndex = 5; + this.buttonReadTest.Text = "Тест чтения сообщений"; + this.buttonReadTest.UseVisualStyleBackColor = true; + this.buttonReadTest.Click += new System.EventHandler(this.buttonReadTest_Click); + // + // textBoxReadTime + // + this.textBoxReadTime.Location = new System.Drawing.Point(266, 144); + this.textBoxReadTime.Name = "textBoxReadTime"; + this.textBoxReadTime.ReadOnly = true; + this.textBoxReadTime.Size = new System.Drawing.Size(100, 23); + this.textBoxReadTime.TabIndex = 9; + // + // label3 + // + this.label3.AutoSize = true; + this.label3.Location = new System.Drawing.Point(104, 147); + this.label3.Name = "label3"; + this.label3.Size = new System.Drawing.Size(146, 15); + this.label3.TabIndex = 8; + this.label3.Text = "Итоговое время запроса:"; + // + // label4 + // + this.label4.AutoSize = true; + this.label4.Location = new System.Drawing.Point(104, 106); + this.label4.Name = "label4"; + this.label4.Size = new System.Drawing.Size(156, 15); + this.label4.TabIndex = 6; + this.label4.Text = "Введите кол-во элементов:"; + // + // numericUpDownInsert + // + this.numericUpDownInsert.Location = new System.Drawing.Point(266, 10); + this.numericUpDownInsert.Name = "numericUpDownInsert"; + this.numericUpDownInsert.Size = new System.Drawing.Size(100, 23); + this.numericUpDownInsert.TabIndex = 10; + // + // numericUpDownRead + // + this.numericUpDownRead.Location = new System.Drawing.Point(266, 104); + this.numericUpDownRead.Name = "numericUpDownRead"; + this.numericUpDownRead.Size = new System.Drawing.Size(100, 23); + this.numericUpDownRead.TabIndex = 11; + // + // buttonJoinQuery + // + this.buttonJoinQuery.Location = new System.Drawing.Point(12, 197); + this.buttonJoinQuery.Name = "buttonJoinQuery"; + this.buttonJoinQuery.Size = new System.Drawing.Size(86, 74); + this.buttonJoinQuery.TabIndex = 12; + this.buttonJoinQuery.Text = "Тест сложного чтения (Join)"; + this.buttonJoinQuery.UseVisualStyleBackColor = true; + this.buttonJoinQuery.Click += new System.EventHandler(this.buttonJoinQuery_Click); + // + // numericUpDownJoin + // + this.numericUpDownJoin.Location = new System.Drawing.Point(266, 195); + this.numericUpDownJoin.Name = "numericUpDownJoin"; + this.numericUpDownJoin.Size = new System.Drawing.Size(100, 23); + this.numericUpDownJoin.TabIndex = 16; + // + // textBoxJoin + // + this.textBoxJoin.Location = new System.Drawing.Point(266, 235); + this.textBoxJoin.Name = "textBoxJoin"; + this.textBoxJoin.ReadOnly = true; + this.textBoxJoin.Size = new System.Drawing.Size(100, 23); + this.textBoxJoin.TabIndex = 15; + // + // label5 + // + this.label5.AutoSize = true; + this.label5.Location = new System.Drawing.Point(104, 238); + this.label5.Name = "label5"; + this.label5.Size = new System.Drawing.Size(146, 15); + this.label5.TabIndex = 14; + this.label5.Text = "Итоговое время запроса:"; + // + // label6 + // + this.label6.AutoSize = true; + this.label6.Location = new System.Drawing.Point(104, 197); + this.label6.Name = "label6"; + this.label6.Size = new System.Drawing.Size(156, 15); + this.label6.TabIndex = 13; + this.label6.Text = "Введите кол-во элементов:"; + // + // FormTests + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(383, 286); + this.Controls.Add(this.numericUpDownJoin); + this.Controls.Add(this.textBoxJoin); + this.Controls.Add(this.label5); + this.Controls.Add(this.label6); + this.Controls.Add(this.buttonJoinQuery); + this.Controls.Add(this.numericUpDownRead); + this.Controls.Add(this.numericUpDownInsert); + this.Controls.Add(this.textBoxReadTime); + this.Controls.Add(this.label3); + this.Controls.Add(this.label4); + this.Controls.Add(this.buttonReadTest); + this.Controls.Add(this.textBoxInsertTime); + this.Controls.Add(this.label2); + this.Controls.Add(this.label1); + this.Controls.Add(this.buttonInsertTest); + this.Name = "FormTests"; + this.Text = "Тесты запросов к бд"; + ((System.ComponentModel.ISupportInitialize)(this.numericUpDownInsert)).EndInit(); + ((System.ComponentModel.ISupportInitialize)(this.numericUpDownRead)).EndInit(); + ((System.ComponentModel.ISupportInitialize)(this.numericUpDownJoin)).EndInit(); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private Button buttonInsertTest; + private Label label1; + private Label label2; + private TextBox textBoxInsertTime; + private Button buttonReadTest; + private TextBox textBoxReadTime; + private Label label3; + private Label label4; + private NumericUpDown numericUpDownInsert; + private NumericUpDown numericUpDownRead; + private Button buttonJoinQuery; + private NumericUpDown numericUpDownJoin; + private TextBox textBoxJoin; + private Label label5; + private Label label6; + } +} \ No newline at end of file diff --git a/BlogDataModels/BlogViewModel/FormTests.cs b/BlogDataModels/BlogViewModel/FormTests.cs new file mode 100644 index 0000000..f182f51 --- /dev/null +++ b/BlogDataModels/BlogViewModel/FormTests.cs @@ -0,0 +1,75 @@ +using ForumContracts.BusinessLogicContracts; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace Forum +{ + public partial class FormTests : Form + { + private readonly IMessageLogic _messageLogic; + private readonly IUserLogic _userLogic; + private readonly ITopicLogic _topicLogic; + public FormTests(IUserLogic userLogic, ITopicLogic topicLogic, IMessageLogic messageLogic) + { + InitializeComponent(); + _userLogic = userLogic; + _topicLogic = topicLogic; + _messageLogic = messageLogic; + numericUpDownInsert.Minimum = 0; + numericUpDownInsert.Maximum = 1000000; + numericUpDownRead.Minimum = 0; + numericUpDownRead.Maximum = 1000000; + numericUpDownJoin.Minimum = 0; + numericUpDownJoin.Maximum = 1000000; + } + + private void buttonInsertTest_Click(object sender, EventArgs e) + { + try + { + var result = _messageLogic.TestInsertList(Convert.ToInt32(numericUpDownInsert.Value), + _userLogic.ReadList(null) ?? new(), + _topicLogic.ReadList(null) ?? new()); + + textBoxInsertTime.Text = result; + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void buttonReadTest_Click(object sender, EventArgs e) + { + try + { + var result = _messageLogic.TestReadList(Convert.ToInt32(numericUpDownInsert.Value)); + textBoxReadTime.Text = result; + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void buttonJoinQuery_Click(object sender, EventArgs e) + { + try + { + var result = _messageLogic.TestJoinReadList(Convert.ToInt32(numericUpDownJoin.Value)); + textBoxJoin.Text = result; + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } +} diff --git a/BlogDataModels/BlogViewModel/FormTests.resx b/BlogDataModels/BlogViewModel/FormTests.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/BlogDataModels/BlogViewModel/FormTests.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/BlogDataModels/BlogViewModel/FormUser.Designer.cs b/BlogDataModels/BlogViewModel/FormUser.Designer.cs new file mode 100644 index 0000000..5b62c92 --- /dev/null +++ b/BlogDataModels/BlogViewModel/FormUser.Designer.cs @@ -0,0 +1,184 @@ +namespace Forum +{ + partial class FormUser + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.textBoxUsername = new System.Windows.Forms.TextBox(); + this.textBoxLogin = new System.Windows.Forms.TextBox(); + this.textBoxPassword = new System.Windows.Forms.TextBox(); + this.label1 = new System.Windows.Forms.Label(); + this.label2 = new System.Windows.Forms.Label(); + this.label3 = new System.Windows.Forms.Label(); + this.buttonSave = new System.Windows.Forms.Button(); + this.buttonCancel = new System.Windows.Forms.Button(); + this.comboBoxSelectRole = new System.Windows.Forms.ComboBox(); + this.label4 = new System.Windows.Forms.Label(); + this.buttonAddUsers = new System.Windows.Forms.Button(); + this.SuspendLayout(); + // + // textBoxUsername + // + this.textBoxUsername.Location = new System.Drawing.Point(80, 9); + this.textBoxUsername.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.textBoxUsername.Name = "textBoxUsername"; + this.textBoxUsername.Size = new System.Drawing.Size(133, 23); + this.textBoxUsername.TabIndex = 0; + // + // textBoxLogin + // + this.textBoxLogin.Location = new System.Drawing.Point(80, 34); + this.textBoxLogin.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.textBoxLogin.Name = "textBoxLogin"; + this.textBoxLogin.Size = new System.Drawing.Size(133, 23); + this.textBoxLogin.TabIndex = 1; + // + // textBoxPassword + // + this.textBoxPassword.Location = new System.Drawing.Point(80, 58); + this.textBoxPassword.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.textBoxPassword.Name = "textBoxPassword"; + this.textBoxPassword.Size = new System.Drawing.Size(133, 23); + this.textBoxPassword.TabIndex = 2; + // + // label1 + // + this.label1.AutoSize = true; + this.label1.Location = new System.Drawing.Point(10, 14); + this.label1.Name = "label1"; + this.label1.Size = new System.Drawing.Size(60, 15); + this.label1.TabIndex = 3; + this.label1.Text = "Username"; + // + // label2 + // + this.label2.AutoSize = true; + this.label2.Location = new System.Drawing.Point(10, 39); + this.label2.Name = "label2"; + this.label2.Size = new System.Drawing.Size(37, 15); + this.label2.TabIndex = 4; + this.label2.Text = "Login"; + // + // label3 + // + this.label3.AutoSize = true; + this.label3.Location = new System.Drawing.Point(10, 64); + this.label3.Name = "label3"; + this.label3.Size = new System.Drawing.Size(57, 15); + this.label3.TabIndex = 5; + this.label3.Text = "Password"; + // + // buttonSave + // + this.buttonSave.Location = new System.Drawing.Point(219, 11); + this.buttonSave.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.buttonSave.Name = "buttonSave"; + this.buttonSave.Size = new System.Drawing.Size(82, 22); + this.buttonSave.TabIndex = 6; + this.buttonSave.Text = "Сохранить"; + this.buttonSave.UseVisualStyleBackColor = true; + this.buttonSave.Click += new System.EventHandler(this.buttonSave_Click); + // + // buttonCancel + // + this.buttonCancel.Location = new System.Drawing.Point(219, 37); + this.buttonCancel.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.buttonCancel.Name = "buttonCancel"; + this.buttonCancel.Size = new System.Drawing.Size(82, 22); + this.buttonCancel.TabIndex = 7; + this.buttonCancel.Text = "Отмена"; + this.buttonCancel.UseVisualStyleBackColor = true; + this.buttonCancel.Click += new System.EventHandler(this.buttonCancel_Click); + // + // comboBoxSelectRole + // + this.comboBoxSelectRole.FormattingEnabled = true; + this.comboBoxSelectRole.Location = new System.Drawing.Point(80, 88); + this.comboBoxSelectRole.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.comboBoxSelectRole.Name = "comboBoxSelectRole"; + this.comboBoxSelectRole.Size = new System.Drawing.Size(133, 23); + this.comboBoxSelectRole.TabIndex = 8; + // + // label4 + // + this.label4.AutoSize = true; + this.label4.Location = new System.Drawing.Point(10, 90); + this.label4.Name = "label4"; + this.label4.Size = new System.Drawing.Size(30, 15); + this.label4.TabIndex = 9; + this.label4.Text = "Role"; + // + // buttonAddUsers + // + this.buttonAddUsers.Location = new System.Drawing.Point(219, 64); + this.buttonAddUsers.Name = "buttonAddUsers"; + this.buttonAddUsers.Size = new System.Drawing.Size(82, 77); + this.buttonAddUsers.TabIndex = 10; + this.buttonAddUsers.Text = "Добавить 1000 пользователей"; + this.buttonAddUsers.UseVisualStyleBackColor = true; + this.buttonAddUsers.Click += new System.EventHandler(this.buttonAddUsers_Click); + // + // FormUser + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(310, 145); + this.Controls.Add(this.buttonAddUsers); + this.Controls.Add(this.label4); + this.Controls.Add(this.comboBoxSelectRole); + this.Controls.Add(this.buttonCancel); + this.Controls.Add(this.buttonSave); + this.Controls.Add(this.label3); + this.Controls.Add(this.label2); + this.Controls.Add(this.label1); + this.Controls.Add(this.textBoxPassword); + this.Controls.Add(this.textBoxLogin); + this.Controls.Add(this.textBoxUsername); + this.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.Name = "FormUser"; + this.Text = "Пользователь"; + this.Load += new System.EventHandler(this.FormUser_Load); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private TextBox textBoxUsername; + private TextBox textBoxLogin; + private TextBox textBoxPassword; + private Label label1; + private Label label2; + private Label label3; + private Button buttonSave; + private Button buttonCancel; + private ComboBox comboBoxSelectRole; + private Label label4; + private Button buttonAddUsers; + } +} \ No newline at end of file diff --git a/BlogDataModels/BlogViewModel/FormUser.cs b/BlogDataModels/BlogViewModel/FormUser.cs new file mode 100644 index 0000000..c543243 --- /dev/null +++ b/BlogDataModels/BlogViewModel/FormUser.cs @@ -0,0 +1,138 @@ +using ForumContracts.BindingModels; +using ForumContracts.BusinessLogicContracts; +using ForumContracts.SearchModels; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace Forum +{ + public partial class FormUser : Form + { + private readonly IUserLogic _userLogic; + private readonly IRoleLogic _roleLogic; + + private int? _id; + public int Id { set { _id = value; } } + public FormUser(IRoleLogic roleLogic, IUserLogic userLogic) + { + InitializeComponent(); + _roleLogic = roleLogic; + _userLogic = userLogic; + } + + private void buttonSave_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(textBoxUsername.Text)) + { + MessageBox.Show("Заполните поле Username", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (string.IsNullOrEmpty(textBoxLogin.Text)) + { + MessageBox.Show("Заполните поле Login", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (string.IsNullOrEmpty(textBoxPassword.Text)) + { + MessageBox.Show("Заполните поле Password", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (comboBoxSelectRole.SelectedValue == null) + { + MessageBox.Show("Выберите Role", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + try + { + var model = new UserBindingModel + { + Id = _id ?? 0, + Username = textBoxUsername.Text, + Email = textBoxLogin.Text, + Password = textBoxPassword.Text, + RegistrationDate = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc), + RoleId = Convert.ToInt32(comboBoxSelectRole.SelectedValue), + }; + var operationResult = _id.HasValue ? _userLogic.Update(model) : _userLogic.Create(model); + if (!operationResult) + { + throw new Exception("Ошибка при создании пользователя."); + } + MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); + DialogResult = DialogResult.OK; + Close(); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void buttonCancel_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); + } + + private void FormUser_Load(object sender, EventArgs e) + { + LoadData(); + if (_id.HasValue) + { + try + { + var view = _userLogic.ReadElement(new UserSearchModel { Id = _id.Value }); + + if (view != null) + { + textBoxLogin.Text = view.Email; + textBoxPassword.Text = view.Password; + textBoxUsername.Text = view.Username; + } + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + + private void LoadData() + { + try + { + var role = _roleLogic.ReadList(null); + if (role != null) + { + comboBoxSelectRole.DisplayMember = "Name"; + comboBoxSelectRole.ValueMember = "Id"; + comboBoxSelectRole.DataSource = role; + comboBoxSelectRole.SelectedItem = null; + } + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void buttonAddUsers_Click(object sender, EventArgs e) + { + try + { + _userLogic.UserInsertList(1000, _roleLogic.ReadList(null)); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } +} diff --git a/BlogDataModels/BlogViewModel/FormUser.resx b/BlogDataModels/BlogViewModel/FormUser.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/BlogDataModels/BlogViewModel/FormUser.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/BlogDataModels/BlogViewModel/FormUsers.Designer.cs b/BlogDataModels/BlogViewModel/FormUsers.Designer.cs new file mode 100644 index 0000000..c4fcc3b --- /dev/null +++ b/BlogDataModels/BlogViewModel/FormUsers.Designer.cs @@ -0,0 +1,115 @@ +namespace Forum +{ + partial class FormUsers + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.dataGridView = new System.Windows.Forms.DataGridView(); + this.buttonCreate = new System.Windows.Forms.Button(); + this.buttonChange = new System.Windows.Forms.Button(); + this.buttonDelete = new System.Windows.Forms.Button(); + this.buttonUpdate = new System.Windows.Forms.Button(); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); + this.SuspendLayout(); + // + // dataGridView + // + this.dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; + this.dataGridView.Location = new System.Drawing.Point(12, 12); + this.dataGridView.Name = "dataGridView"; + this.dataGridView.RowHeadersWidth = 51; + this.dataGridView.RowTemplate.Height = 29; + this.dataGridView.Size = new System.Drawing.Size(676, 291); + this.dataGridView.TabIndex = 0; + // + // buttonCreate + // + this.buttonCreate.Location = new System.Drawing.Point(694, 22); + this.buttonCreate.Name = "buttonCreate"; + this.buttonCreate.Size = new System.Drawing.Size(94, 29); + this.buttonCreate.TabIndex = 1; + this.buttonCreate.Text = "Добавить"; + this.buttonCreate.UseVisualStyleBackColor = true; + this.buttonCreate.Click += new System.EventHandler(this.buttonCreate_Click); + // + // buttonChange + // + this.buttonChange.Location = new System.Drawing.Point(694, 57); + this.buttonChange.Name = "buttonChange"; + this.buttonChange.Size = new System.Drawing.Size(94, 29); + this.buttonChange.TabIndex = 2; + this.buttonChange.Text = "Изменить"; + this.buttonChange.UseVisualStyleBackColor = true; + this.buttonChange.Click += new System.EventHandler(this.buttonChange_Click); + // + // buttonDelete + // + this.buttonDelete.Location = new System.Drawing.Point(694, 92); + this.buttonDelete.Name = "buttonDelete"; + this.buttonDelete.Size = new System.Drawing.Size(94, 29); + this.buttonDelete.TabIndex = 3; + this.buttonDelete.Text = "Удалить"; + this.buttonDelete.UseVisualStyleBackColor = true; + this.buttonDelete.Click += new System.EventHandler(this.buttonDelete_Click); + // + // buttonUpdate + // + this.buttonUpdate.Location = new System.Drawing.Point(694, 127); + this.buttonUpdate.Name = "buttonUpdate"; + this.buttonUpdate.Size = new System.Drawing.Size(94, 29); + this.buttonUpdate.TabIndex = 4; + this.buttonUpdate.Text = "Обновить"; + this.buttonUpdate.UseVisualStyleBackColor = true; + this.buttonUpdate.Click += new System.EventHandler(this.buttonUpdate_Click); + // + // FormUsers + // + this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(800, 316); + this.Controls.Add(this.buttonUpdate); + this.Controls.Add(this.buttonDelete); + this.Controls.Add(this.buttonChange); + this.Controls.Add(this.buttonCreate); + this.Controls.Add(this.dataGridView); + this.Name = "FormUsers"; + this.Text = "Пользователи"; + this.Load += new System.EventHandler(this.FormUsers_Load); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit(); + this.ResumeLayout(false); + + } + + #endregion + + private DataGridView dataGridView; + private Button buttonCreate; + private Button buttonChange; + private Button buttonDelete; + private Button buttonUpdate; + } +} \ No newline at end of file diff --git a/BlogDataModels/BlogViewModel/FormUsers.cs b/BlogDataModels/BlogViewModel/FormUsers.cs new file mode 100644 index 0000000..c229f7c --- /dev/null +++ b/BlogDataModels/BlogViewModel/FormUsers.cs @@ -0,0 +1,113 @@ +using ForumBusinessLogic; +using ForumContracts.BindingModels; +using ForumContracts.BusinessLogicContracts; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace Forum +{ + public partial class FormUsers : Form + { + private readonly IUserLogic _userLogic; + public FormUsers(IUserLogic userLogic) + { + InitializeComponent(); + _userLogic = userLogic; + } + + private void FormUsers_Load(object sender, EventArgs e) + { + LoadData(); + } + + private void LoadData() + { + try + { + var list = _userLogic.ReadList(null); + if (list != null) + { + dataGridView.DataSource = list; + dataGridView.Columns["Id"].Visible = false; + dataGridView.Columns["RoleId"].Visible = false; + dataGridView.Columns["Username"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + dataGridView.Columns["Email"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + dataGridView.Columns["Password"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + dataGridView.Columns["RoleName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + } + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void buttonCreate_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormUser)); + if (service is FormUser form) + { + form.ShowDialog(); + LoadData(); + } + } + + private void buttonChange_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + var service = Program.ServiceProvider?.GetService(typeof(FormUser)); + + if (service is FormUser form) + { + form.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + + if (form.ShowDialog() == DialogResult.OK) + { + LoadData(); + } + } + } + } + + private void buttonDelete_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + if (MessageBox.Show("Удалить запись?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + { + int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + + try + { + if (!_userLogic.Delete(new UserBindingModel + { + Id = id + })) + { + throw new Exception("Ошибка при удалении."); + } + + LoadData(); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + } + + private void buttonUpdate_Click(object sender, EventArgs e) + { + LoadData(); + } + } +} diff --git a/BlogDataModels/BlogViewModel/FormUsers.resx b/BlogDataModels/BlogViewModel/FormUsers.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/BlogDataModels/BlogViewModel/FormUsers.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/BlogDataModels/BlogViewModel/Forum.csproj b/BlogDataModels/BlogViewModel/Forum.csproj new file mode 100644 index 0000000..e1d3cf6 --- /dev/null +++ b/BlogDataModels/BlogViewModel/Forum.csproj @@ -0,0 +1,25 @@ + + + + WinExe + net6.0-windows + enable + true + enable + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + + \ No newline at end of file diff --git a/BlogDataModels/BlogViewModel/Program.cs b/BlogDataModels/BlogViewModel/Program.cs index 7a0530d..6c8bfaa 100644 --- a/BlogDataModels/BlogViewModel/Program.cs +++ b/BlogDataModels/BlogViewModel/Program.cs @@ -1,9 +1,11 @@ -using BlogContracts +using ForumBusinessLogic; +using ForumContracts.BusinessLogicContracts; +using ForumContracts.StoragesContracts; +using BlogDatabase.Implements; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; -using NLog.Extensions.Logging; +using System; -namespace BlogViewModel +namespace Forum { internal static class Program { @@ -21,26 +23,33 @@ namespace BlogViewModel var services = new ServiceCollection(); ConfigureServices(services); _serviceProvider = services.BuildServiceProvider(); - Application.Run(_serviceProvider.GetRequiredService()); } private static void ConfigureServices(ServiceCollection services) { - services.AddLogging(option => - { - option.SetMinimumLevel(LogLevel.Information); - option.AddNLog("nlog.config"); - }); - - - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - - services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); services.AddTransient(); + + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); } } } \ No newline at end of file