diff --git a/HardwareShop/HardwareShopContracts/BindingModels/CommentBindingModel.cs b/HardwareShop/HardwareShopContracts/BindingModels/CommentBindingModel.cs index ad748e1..a759df4 100644 --- a/HardwareShop/HardwareShopContracts/BindingModels/CommentBindingModel.cs +++ b/HardwareShop/HardwareShopContracts/BindingModels/CommentBindingModel.cs @@ -10,5 +10,7 @@ namespace HardwareShopContracts.BindingModels public string Text { get; set; } = string.Empty; public int BuildId { get; set; } + + public int UserId { get; set; } } } diff --git a/HardwareShop/HardwareShopContracts/SearchModels/CommentSearchModel.cs b/HardwareShop/HardwareShopContracts/SearchModels/CommentSearchModel.cs index 78b05be..2c82c3e 100644 --- a/HardwareShop/HardwareShopContracts/SearchModels/CommentSearchModel.cs +++ b/HardwareShop/HardwareShopContracts/SearchModels/CommentSearchModel.cs @@ -8,5 +8,7 @@ namespace HardwareShopContracts.SearchModels public int? Id { get; set; } public int? BuildId { get; set; } + + public int? UserId { get; set; } } } diff --git a/HardwareShop/HardwareShopContracts/ViewModels/CommentViewModel.cs b/HardwareShop/HardwareShopContracts/ViewModels/CommentViewModel.cs index e6f9be8..06e1526 100644 --- a/HardwareShop/HardwareShopContracts/ViewModels/CommentViewModel.cs +++ b/HardwareShop/HardwareShopContracts/ViewModels/CommentViewModel.cs @@ -14,5 +14,10 @@ namespace HardwareShopContracts.ViewModels public string BuildName { get; set; } = string.Empty; public int BuildId { get; set; } + + public int UserId { get; set; } + + [DisplayName("Email клиента")] + public string UserEmail { get; set; } = string.Empty; } } diff --git a/HardwareShop/HardwareShopDataModels/Models/ICommentModel.cs b/HardwareShop/HardwareShopDataModels/Models/ICommentModel.cs index dc59c3f..7897562 100644 --- a/HardwareShop/HardwareShopDataModels/Models/ICommentModel.cs +++ b/HardwareShop/HardwareShopDataModels/Models/ICommentModel.cs @@ -11,5 +11,7 @@ namespace HardwareShopDataModels.Models string Text { get; } int BuildId { get; } + + int UserId { get; } } } diff --git a/HardwareShop/HardwareShopDatabaseImplement/Implements/Worker/BuildStorage.cs b/HardwareShop/HardwareShopDatabaseImplement/Implements/Worker/BuildStorage.cs index 10e0f11..4add52d 100644 --- a/HardwareShop/HardwareShopDatabaseImplement/Implements/Worker/BuildStorage.cs +++ b/HardwareShop/HardwareShopDatabaseImplement/Implements/Worker/BuildStorage.cs @@ -24,7 +24,7 @@ namespace HardwareShopContracts.StoragesContracts public List GetFilteredList(BuildSearchModel model) { - if (model.Id.HasValue) + if (!model.Id.HasValue) { return new(); } diff --git a/HardwareShop/HardwareShopDatabaseImplement/Implements/Worker/CommentStorage.cs b/HardwareShop/HardwareShopDatabaseImplement/Implements/Worker/CommentStorage.cs index 2f92b6f..b03d79c 100644 --- a/HardwareShop/HardwareShopDatabaseImplement/Implements/Worker/CommentStorage.cs +++ b/HardwareShop/HardwareShopDatabaseImplement/Implements/Worker/CommentStorage.cs @@ -14,19 +14,31 @@ namespace HardwareShopContracts.StoragesContracts using var context = new HardwareShopDatabase(); return context.Comments .Include(x => x.Build) + .Include(x => x.User) .Select(x => x.GetViewModel) .ToList(); } public List GetFilteredList(CommentSearchModel model) { - if (model.BuildId.HasValue) + if (!model.Id.HasValue) { return new(); } using var context = new HardwareShopDatabase(); + if (model.UserId.HasValue) + { + return context.Comments + .Include(x => x.Build) + .Include(x => x.User) + .Where(x => x.UserId == model.UserId) + .Select(x => x.GetViewModel) + .ToList(); + } + return context.Comments .Include(x => x.Build) + .Include(x => x.User) .Where(x => x.BuildId == model.BuildId) .Select(x => x.GetViewModel) .ToList(); @@ -34,13 +46,14 @@ namespace HardwareShopContracts.StoragesContracts public CommentViewModel? GetElement(CommentSearchModel model) { - if (model.Id.HasValue) + if (!model.Id.HasValue) { return null; } using var context = new HardwareShopDatabase(); return context.Comments .Include(x => x.Build) + .Include(x => x.User) .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id) ?.GetViewModel; } @@ -61,17 +74,17 @@ namespace HardwareShopContracts.StoragesContracts public CommentViewModel? Update(CommentBindingModel model) { using var context = new HardwareShopDatabase(); - var comment = context.Comments.FirstOrDefault(x => x.Id == model.Id); + var comment = context.Comments + .Include(x => x.Build) + .Include(x => x.User) + .FirstOrDefault(x => x.Id == model.Id); if (comment == null) { return null; } comment.Update(model); context.SaveChanges(); - return context.Comments - .Include(x => x.Build) - .FirstOrDefault(x => x.Id == comment.Id) - ?.GetViewModel; + return comment.GetViewModel; } public CommentViewModel? Delete(CommentBindingModel model) @@ -79,6 +92,7 @@ namespace HardwareShopContracts.StoragesContracts using var context = new HardwareShopDatabase(); var element = context.Comments .Include(x => x.Build) + .Include(x => x.User) .FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { diff --git a/HardwareShop/HardwareShopDatabaseImplement/Models/Worker/Comment.cs b/HardwareShop/HardwareShopDatabaseImplement/Models/Worker/Comment.cs index 5306cbc..d4e0854 100644 --- a/HardwareShop/HardwareShopDatabaseImplement/Models/Worker/Comment.cs +++ b/HardwareShop/HardwareShopDatabaseImplement/Models/Worker/Comment.cs @@ -18,6 +18,11 @@ namespace HardwareShopDatabaseImplement.Models.Worker public virtual Build Build { get; set; } + [Required] + public int UserId { get; set; } + + public virtual User User { get; set; } + public static Comment? Create(CommentBindingModel? model) { if (model == null) @@ -28,7 +33,8 @@ namespace HardwareShopDatabaseImplement.Models.Worker { Id = model.Id, Text = model.Text, - BuildId = model.BuildId + BuildId = model.BuildId, + UserId = model.UserId, }; } public void Update(CommentBindingModel? model) @@ -44,7 +50,9 @@ namespace HardwareShopDatabaseImplement.Models.Worker Id = Id, Text = Text, BuildId = BuildId, - BuildName = Build.BuildName + BuildName = Build.BuildName, + UserId = UserId, + UserEmail = User.Email, }; } }