From 4e935df31150783d218e807ac6ed1a389b0b98cb Mon Sep 17 00:00:00 2001 From: Shady Nagy Date: Tue, 28 Jul 2020 23:00:32 +0200 Subject: [PATCH] Image added (#434) * Image added * ImageMaximumBytes * FileController remove Authorized * ApplicationCore.Constants.AuthorizationConstants.AUTH_KEY * SavePicture in the interface. * IFileSystem in Core * WebFileSystem in Infrastructure * PictureUri removed from UpdateCatalogItemRequest * Modal scroll fix --- .../Constants/AuthorizationConstants.cs | 1 + src/ApplicationCore/Entities/CatalogItem.cs | 13 +- src/ApplicationCore/Interfaces/IFileSystem.cs | 9 + src/BlazorAdmin/BlazorAdmin.csproj | 1 + src/BlazorAdmin/JavaScript/Css.cs | 25 +++ .../JavaScript/JSInteropConstants.cs | 2 + .../Pages/CatalogItemPage/Create.razor | 168 ++++++++++++------ .../Pages/CatalogItemPage/Delete.razor | 103 ++++++----- .../Pages/CatalogItemPage/Details.razor | 23 ++- .../Pages/CatalogItemPage/Edit.razor | 161 +++++++++++------ .../Pages/CatalogItemPage/List.razor | 4 +- .../CatalogItemServices/CatalogItem.cs | 58 +++++- .../Create.CreateCatalogItemRequest.cs | 3 + src/BlazorAdmin/_Imports.razor | 1 + src/BlazorAdmin/wwwroot/css/admin.css | 4 + src/BlazorShared/Authorization/Constants.cs | 3 + src/Infrastructure/Data/FileItem.cs | 12 ++ src/Infrastructure/Infrastructure.csproj | 1 + src/Infrastructure/Services/WebFileSystem.cs | 83 +++++++++ .../Create.CreateCatalogItemRequest.cs | 2 + src/PublicApi/CatalogItemEndpoints/Create.cs | 22 ++- .../Update.UpdateCatalogItemRequest.cs | 3 +- src/PublicApi/CatalogItemEndpoints/Update.cs | 21 ++- src/PublicApi/ImageValidators.cs | 25 +++ src/PublicApi/PublicApi.csproj | 2 +- src/PublicApi/Startup.cs | 2 + src/Web/Controllers/FileController.cs | 38 ++++ src/Web/Pages/Admin/Index.cshtml | 10 +- src/Web/ViewModels/File/FileViewModel.cs | 14 ++ 29 files changed, 636 insertions(+), 178 deletions(-) create mode 100644 src/ApplicationCore/Interfaces/IFileSystem.cs create mode 100644 src/BlazorAdmin/JavaScript/Css.cs create mode 100644 src/Infrastructure/Data/FileItem.cs create mode 100644 src/Infrastructure/Services/WebFileSystem.cs create mode 100644 src/PublicApi/ImageValidators.cs create mode 100644 src/Web/Controllers/FileController.cs create mode 100644 src/Web/ViewModels/File/FileViewModel.cs diff --git a/src/ApplicationCore/Constants/AuthorizationConstants.cs b/src/ApplicationCore/Constants/AuthorizationConstants.cs index 6c48ff2..0235217 100644 --- a/src/ApplicationCore/Constants/AuthorizationConstants.cs +++ b/src/ApplicationCore/Constants/AuthorizationConstants.cs @@ -2,6 +2,7 @@ { public class AuthorizationConstants { + public const string AUTH_KEY = "AuthKeyOfDoomThatMustBeAMinimumNumberOfBytes"; // TODO: Don't use this in production public const string DEFAULT_PASSWORD = "Pass@word1"; diff --git a/src/ApplicationCore/Entities/CatalogItem.cs b/src/ApplicationCore/Entities/CatalogItem.cs index 7fe0149..82f681a 100644 --- a/src/ApplicationCore/Entities/CatalogItem.cs +++ b/src/ApplicationCore/Entities/CatalogItem.cs @@ -1,4 +1,5 @@ -using Ardalis.GuardClauses; +using System; +using Ardalis.GuardClauses; using Microsoft.eShopWeb.ApplicationCore.Interfaces; using System.Collections.Generic; @@ -54,5 +55,15 @@ namespace Microsoft.eShopWeb.ApplicationCore.Entities Guard.Against.Zero(catalogTypeId, nameof(catalogTypeId)); CatalogTypeId = catalogTypeId; } + + public void UpdatePictureUri(string pictureName) + { + if (string.IsNullOrEmpty(pictureName)) + { + PictureUri = string.Empty; + return; + } + PictureUri = $"images\\products\\{pictureName}?{new DateTime().Ticks}"; + } } } \ No newline at end of file diff --git a/src/ApplicationCore/Interfaces/IFileSystem.cs b/src/ApplicationCore/Interfaces/IFileSystem.cs new file mode 100644 index 0000000..38be286 --- /dev/null +++ b/src/ApplicationCore/Interfaces/IFileSystem.cs @@ -0,0 +1,9 @@ +using System.Threading.Tasks; + +namespace Microsoft.eShopWeb.ApplicationCore.Interfaces +{ + public interface IFileSystem + { + Task SavePicture(string pictureName, string pictureBase64); + } +} diff --git a/src/BlazorAdmin/BlazorAdmin.csproj b/src/BlazorAdmin/BlazorAdmin.csproj index 9867426..0193727 100644 --- a/src/BlazorAdmin/BlazorAdmin.csproj +++ b/src/BlazorAdmin/BlazorAdmin.csproj @@ -7,6 +7,7 @@ + diff --git a/src/BlazorAdmin/JavaScript/Css.cs b/src/BlazorAdmin/JavaScript/Css.cs new file mode 100644 index 0000000..cf59391 --- /dev/null +++ b/src/BlazorAdmin/JavaScript/Css.cs @@ -0,0 +1,25 @@ +using System.Threading.Tasks; +using Microsoft.JSInterop; + +namespace BlazorAdmin.JavaScript +{ + public class Css + { + private readonly IJSRuntime _jsRuntime; + + public Css(IJSRuntime jsRuntime) + { + _jsRuntime = jsRuntime; + } + + public async Task ShowBodyOverflow() + { + await _jsRuntime.InvokeAsync(JSInteropConstants.ShowBodyOverflow); + } + + public async Task HideBodyOverflow() + { + return await _jsRuntime.InvokeAsync(JSInteropConstants.HideBodyOverflow); + } + } +} diff --git a/src/BlazorAdmin/JavaScript/JSInteropConstants.cs b/src/BlazorAdmin/JavaScript/JSInteropConstants.cs index 0211517..3f1ae39 100644 --- a/src/BlazorAdmin/JavaScript/JSInteropConstants.cs +++ b/src/BlazorAdmin/JavaScript/JSInteropConstants.cs @@ -10,5 +10,7 @@ namespace BlazorAdmin.JavaScript public static string DeleteCookie => "deleteCookie"; public static string GetCookie => "getCookie"; public static string RouteOutside => "routeOutside"; + public static string HideBodyOverflow => "hideBodyOverflow"; + public static string ShowBodyOverflow => "showBodyOverflow"; } } diff --git a/src/BlazorAdmin/Pages/CatalogItemPage/Create.razor b/src/BlazorAdmin/Pages/CatalogItemPage/Create.razor index adecc76..101f7fd 100644 --- a/src/BlazorAdmin/Pages/CatalogItemPage/Create.razor +++ b/src/BlazorAdmin/Pages/CatalogItemPage/Create.razor @@ -1,5 +1,6 @@ @inject ILogger Logger @inject AuthService Auth +@inject IJSRuntime JSRuntime @inherits BlazorAdmin.Helpers.BlazorComponent @@ -25,60 +26,80 @@ } else { -
- -
- - -
-
+
+
+ @if (HasPicture) + { + + } +
+
+ +
+ + +
+
-
- -
- - -
-
+
+ +
+ + +
+
+
+
+
+ +
+ + @foreach (var brand in Brands) + { + + } + + +
+
-
- -
- - @foreach (var brand in Brands) - { - - } - - -
-
+
+ +
+ + @foreach (var type in Types) + { + + } + + +
+
-
- -
- - @foreach (var type in Types) - { - - } - - -
-
+
+ +
+ + +
+
-
- -
- - -
-
- -
- -
- Uploading images not allowed for this version. +
+ +
+
+ +
+
+ @if (HasPicture) + { + + } +
+ @_badFileMessage +
+
+
} @@ -111,35 +132,68 @@ [Parameter] public EventCallback OnCloseClick { get; set; } + private string LoadPicture => string.IsNullOrEmpty(_item.PictureBase64) ? string.Empty : $"data:image/png;base64, {_item.PictureBase64}"; + private bool HasPicture => !string.IsNullOrEmpty(_item.PictureBase64); + private string _badFileMessage = string.Empty; private string _modalDisplay = "none;"; private string _modalClass = ""; private bool _showCreateModal = false; - private readonly CreateCatalogItemRequest _item = new CreateCatalogItemRequest(); + private CreateCatalogItemRequest _item = new CreateCatalogItemRequest(); private async Task CreateClick() { await new BlazorAdmin.Services.CatalogItemServices.Create(Auth).HandleAsync(_item); await OnCloseClick.InvokeAsync(null); - Close(); + await Close(); } - public void Open() + public async Task Open() { Logger.LogInformation("Now loading... /Catalog/Create"); - _item.CatalogTypeId = Types.First().Id; - _item.CatalogBrandId = Brands.First().Id; + await new Css(JSRuntime).HideBodyOverflow(); + + _item = new CreateCatalogItemRequest + { + CatalogTypeId = Types.First().Id, + CatalogBrandId = Brands.First().Id + }; _modalDisplay = "block;"; _modalClass = "Show"; _showCreateModal = true; + + StateHasChanged(); } - public void Close() + private async Task Close() { + await new Css(JSRuntime).ShowBodyOverflow(); _modalDisplay = "none"; _modalClass = ""; _showCreateModal = false; - OnCloseClick.InvokeAsync(null); + await OnCloseClick.InvokeAsync(null); + } + + private async Task AddFile(IFileListEntry[] files) + { + _badFileMessage = string.Empty; + + var file = files.FirstOrDefault(); + _item.PictureName = file?.Name; + _item.PictureBase64 = await CatalogItem.DataToBase64(file); + + _badFileMessage = CatalogItem.IsValidImage(_item.PictureName, _item.PictureBase64); + if (!string.IsNullOrEmpty(_badFileMessage)) + { + _item.PictureName = null; + _item.PictureBase64 = null; + } + } + + private void RemoveImage() + { + _item.PictureName = null; + _item.PictureBase64 = null; } } diff --git a/src/BlazorAdmin/Pages/CatalogItemPage/Delete.razor b/src/BlazorAdmin/Pages/CatalogItemPage/Delete.razor index 7104ef6..dc6d424 100644 --- a/src/BlazorAdmin/Pages/CatalogItemPage/Delete.razor +++ b/src/BlazorAdmin/Pages/CatalogItemPage/Delete.razor @@ -1,5 +1,6 @@ @inject ILogger Logger @inject AuthService Auth +@inject IJSRuntime JSRuntime @inherits BlazorAdmin.Helpers.BlazorComponent @@ -15,60 +16,62 @@