From 3e090ba964c0111dd3d7ac3e5cea04d459792080 Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Mon, 21 Jan 2019 09:43:20 +0300 Subject: [PATCH] microservice demo: web layer --- .../Resources/ProductManagement/en.json | 10 ++- .../ProductManagementPage.cs | 25 ++++++ .../ProductManagement/Products/Create.cshtml | 19 +++++ .../Products/Create.cshtml.cs | 49 +++++++++++ .../ProductManagement/Products/Edit.cshtml | 18 +++++ .../ProductManagement/Products/Edit.cshtml.cs | 67 +++++++++++++++ .../ProductManagement/Products/Index.cshtml | 42 ++++++++++ .../Products/Index.cshtml.cs | 12 +++ .../Pages/ProductManagement/Products/index.js | 81 +++++++++++++++++++ .../ProductManagementWebAutoMapperProfile.cs | 4 +- 10 files changed, 325 insertions(+), 2 deletions(-) create mode 100644 samples/MicroserviceDemo/modules/product/src/ProductManagement.Web/Pages/ProductManagement/ProductManagementPage.cs create mode 100644 samples/MicroserviceDemo/modules/product/src/ProductManagement.Web/Pages/ProductManagement/Products/Create.cshtml create mode 100644 samples/MicroserviceDemo/modules/product/src/ProductManagement.Web/Pages/ProductManagement/Products/Create.cshtml.cs create mode 100644 samples/MicroserviceDemo/modules/product/src/ProductManagement.Web/Pages/ProductManagement/Products/Edit.cshtml create mode 100644 samples/MicroserviceDemo/modules/product/src/ProductManagement.Web/Pages/ProductManagement/Products/Edit.cshtml.cs create mode 100644 samples/MicroserviceDemo/modules/product/src/ProductManagement.Web/Pages/ProductManagement/Products/Index.cshtml create mode 100644 samples/MicroserviceDemo/modules/product/src/ProductManagement.Web/Pages/ProductManagement/Products/Index.cshtml.cs create mode 100644 samples/MicroserviceDemo/modules/product/src/ProductManagement.Web/Pages/ProductManagement/Products/index.js diff --git a/samples/MicroserviceDemo/modules/product/src/ProductManagement.Web/Localization/Resources/ProductManagement/en.json b/samples/MicroserviceDemo/modules/product/src/ProductManagement.Web/Localization/Resources/ProductManagement/en.json index 012b6138cc..d9e9ea167a 100644 --- a/samples/MicroserviceDemo/modules/product/src/ProductManagement.Web/Localization/Resources/ProductManagement/en.json +++ b/samples/MicroserviceDemo/modules/product/src/ProductManagement.Web/Localization/Resources/ProductManagement/en.json @@ -2,6 +2,14 @@ "culture": "en", "texts": { "Menu:ProductManagement": "Product Management", - "Menu:Products": "Products" + "Menu:Products": "Products", + "ProductManagement": "Product Management", + "CreateANewProduct": "Create A New Product", + "Products": "Products", + "StockCount": "Stock Count", + "Code": "Code", + "Name": "Name", + "Price": "Price", + "ProductDeletionWarningMessage": "Are you sure you want to delete this product?" } } \ No newline at end of file diff --git a/samples/MicroserviceDemo/modules/product/src/ProductManagement.Web/Pages/ProductManagement/ProductManagementPage.cs b/samples/MicroserviceDemo/modules/product/src/ProductManagement.Web/Pages/ProductManagement/ProductManagementPage.cs new file mode 100644 index 0000000000..62077aa82e --- /dev/null +++ b/samples/MicroserviceDemo/modules/product/src/ProductManagement.Web/Pages/ProductManagement/ProductManagementPage.cs @@ -0,0 +1,25 @@ +using Microsoft.AspNetCore.Mvc.Localization; +using Microsoft.AspNetCore.Mvc.Razor.Internal; +using ProductManagement.Localization; +using Volo.Abp.AspNetCore.Mvc.UI.RazorPages; + +namespace ProductManagement.Pages.ProductManagement +{ + public abstract class ProductManagementPage : AbpPage + { + [RazorInject] + public IHtmlLocalizer L { get; set; } + + public const string DefaultTitle = "ProductManagement"; + + public string GetTitle(string title = null) + { + if (string.IsNullOrWhiteSpace(title)) + { + return DefaultTitle; + } + + return title; + } + } +} diff --git a/samples/MicroserviceDemo/modules/product/src/ProductManagement.Web/Pages/ProductManagement/Products/Create.cshtml b/samples/MicroserviceDemo/modules/product/src/ProductManagement.Web/Pages/ProductManagement/Products/Create.cshtml new file mode 100644 index 0000000000..1a72ed4697 --- /dev/null +++ b/samples/MicroserviceDemo/modules/product/src/ProductManagement.Web/Pages/ProductManagement/Products/Create.cshtml @@ -0,0 +1,19 @@ +@page +@using Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Modal +@inherits ProductManagement.Pages.ProductManagement.ProductManagementPage +@model ProductManagement.Pages.ProductManagement.Products.CreateModel +@{ + Layout = null; +} + + + + + + + + + + + + diff --git a/samples/MicroserviceDemo/modules/product/src/ProductManagement.Web/Pages/ProductManagement/Products/Create.cshtml.cs b/samples/MicroserviceDemo/modules/product/src/ProductManagement.Web/Pages/ProductManagement/Products/Create.cshtml.cs new file mode 100644 index 0000000000..e44f18afee --- /dev/null +++ b/samples/MicroserviceDemo/modules/product/src/ProductManagement.Web/Pages/ProductManagement/Products/Create.cshtml.cs @@ -0,0 +1,49 @@ +using System.ComponentModel.DataAnnotations; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using Volo.Abp.AspNetCore.Mvc.UI.RazorPages; + +namespace ProductManagement.Pages.ProductManagement.Products +{ + public class CreateModel : AbpPageModel + { + private readonly IProductAppService _productAppService; + + [BindProperty] + public ProductCreateModalView Product { get; set; } = new ProductCreateModalView(); + + public CreateModel(IProductAppService productAppService) + { + _productAppService = productAppService; + } + + public void OnGet() + { + + } + + public async Task OnPostAsync() + { + var createProductDto = ObjectMapper.Map(Product); + + await _productAppService.CreateAsync(createProductDto); + + return NoContent(); + } + + public class ProductCreateModalView + { + [Required] + [StringLength(ProductConsts.MaxCodeLength)] + public string Code { get; set; } + + [Required] + [StringLength(ProductConsts.MaxNameLength)] + public string Name { get; set; } + + public float Price { get; set; } + + public int StockCount { get; set; } + } + } +} \ No newline at end of file diff --git a/samples/MicroserviceDemo/modules/product/src/ProductManagement.Web/Pages/ProductManagement/Products/Edit.cshtml b/samples/MicroserviceDemo/modules/product/src/ProductManagement.Web/Pages/ProductManagement/Products/Edit.cshtml new file mode 100644 index 0000000000..89c6c1d8a6 --- /dev/null +++ b/samples/MicroserviceDemo/modules/product/src/ProductManagement.Web/Pages/ProductManagement/Products/Edit.cshtml @@ -0,0 +1,18 @@ +@page +@using Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Modal +@inherits ProductManagement.Pages.ProductManagement.ProductManagementPage +@model ProductManagement.Pages.ProductManagement.Products.EditModel +@{ + Layout = null; +} + + + + + + + + + + + \ No newline at end of file diff --git a/samples/MicroserviceDemo/modules/product/src/ProductManagement.Web/Pages/ProductManagement/Products/Edit.cshtml.cs b/samples/MicroserviceDemo/modules/product/src/ProductManagement.Web/Pages/ProductManagement/Products/Edit.cshtml.cs new file mode 100644 index 0000000000..3b864b6c6a --- /dev/null +++ b/samples/MicroserviceDemo/modules/product/src/ProductManagement.Web/Pages/ProductManagement/Products/Edit.cshtml.cs @@ -0,0 +1,67 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.RazorPages; +using Volo.Abp.AspNetCore.Mvc.UI.RazorPages; + +namespace ProductManagement.Pages.ProductManagement.Products +{ + public class EditModel : AbpPageModel + { + private readonly IProductAppService _productAppService; + + [BindProperty] + public ProductEditModalView Product { get; set; } = new ProductEditModalView(); + + public EditModel(IProductAppService productAppService) + { + _productAppService = productAppService; + } + + public void OnGet() + { + } + + public async Task OnGetAsync(Guid productId) + { + var productDto = await _productAppService.GetAsync(productId); + + Product = ObjectMapper.Map(productDto); + + return Page(); + } + + public async Task OnPostAsync() + { + await _productAppService.UpdateAsync(Product.Id, new UpdateProductDto() + { + Code = Product.Code, + Name = Product.Name, + Price = Product.Price, + StockCount = Product.StockCount + }); + } + + public class ProductEditModalView + { + [HiddenInput] + [Required] + public Guid Id { get; set; } + + [Required] + [StringLength(ProductConsts.MaxCodeLength)] + public string Code { get; set; } + + [Required] + [StringLength(ProductConsts.MaxNameLength)] + public string Name { get; set; } + + public float Price { get; set; } + + public int StockCount { get; set; } + } + } +} \ No newline at end of file diff --git a/samples/MicroserviceDemo/modules/product/src/ProductManagement.Web/Pages/ProductManagement/Products/Index.cshtml b/samples/MicroserviceDemo/modules/product/src/ProductManagement.Web/Pages/ProductManagement/Products/Index.cshtml new file mode 100644 index 0000000000..8397164943 --- /dev/null +++ b/samples/MicroserviceDemo/modules/product/src/ProductManagement.Web/Pages/ProductManagement/Products/Index.cshtml @@ -0,0 +1,42 @@ +@page +@using Microsoft.AspNetCore.Authorization +@using ProductManagement +@inherits ProductManagement.Pages.ProductManagement.ProductManagementPage +@model ProductManagement.Pages.ProductManagement.Products.IndexModel +@inject IAuthorizationService Authorization +@{ + ViewBag.PageTitle = "Products"; +} + +@section scripts { + +} + + + + + +

@L["Products"]

+
+ + @if (await Authorization.IsGrantedAsync(ProductManagementPermissions.Products.Create)) + { + + } + +
+
+ + + + + @L["Actions"] + @L["Code"] + @L["Name"] + @L["Price"] + @L["StockCount"] + + + + +
diff --git a/samples/MicroserviceDemo/modules/product/src/ProductManagement.Web/Pages/ProductManagement/Products/Index.cshtml.cs b/samples/MicroserviceDemo/modules/product/src/ProductManagement.Web/Pages/ProductManagement/Products/Index.cshtml.cs new file mode 100644 index 0000000000..1a7c0d9059 --- /dev/null +++ b/samples/MicroserviceDemo/modules/product/src/ProductManagement.Web/Pages/ProductManagement/Products/Index.cshtml.cs @@ -0,0 +1,12 @@ +using System.Threading.Tasks; +using Volo.Abp.AspNetCore.Mvc.UI.RazorPages; + +namespace ProductManagement.Pages.ProductManagement.Products +{ + public class IndexModel : AbpPageModel + { + public async Task OnGetAsync() + { + } + } +} \ No newline at end of file diff --git a/samples/MicroserviceDemo/modules/product/src/ProductManagement.Web/Pages/ProductManagement/Products/index.js b/samples/MicroserviceDemo/modules/product/src/ProductManagement.Web/Pages/ProductManagement/Products/index.js new file mode 100644 index 0000000000..2359310a5e --- /dev/null +++ b/samples/MicroserviceDemo/modules/product/src/ProductManagement.Web/Pages/ProductManagement/Products/index.js @@ -0,0 +1,81 @@ +$(function () { + + var l = abp.localization.getResource('ProductManagement'); + var _createModal = new abp.ModalManager(abp.appPath + 'ProductManagement/Products/Create'); + var _editModal = new abp.ModalManager(abp.appPath + 'ProductManagement/Products/Edit'); + + var _dataTable = $('#ProductsTable').DataTable(abp.libs.datatables.normalizeConfiguration({ + processing: true, + serverSide: true, + paging: true, + searching: false, + autoWidth: false, + scrollCollapse: true, + order: [[1, "desc"]], + ajax: abp.libs.datatables.createAjax(productManagement.products.getListPaged), + columnDefs: [ + { + rowAction: { + items: + [ + { + text: l('Edit'), + visible: function () { + return true; //TODO: Check permission + }, + action: function (data) { + _editModal.open({ + productId: data.record.id + }); + } + }, + { + text: l('Delete'), + visible: function () { + return true; //TODO: Check permission + }, + confirmMessage: function (data) { return l('ProductDeletionWarningMessage'); }, + action: function (data) { + productManagement.products + .delete(data.record.id) + .then(function () { + _dataTable.ajax.reload(); + }); + } + } + ] + } + }, + { + target: 1, + data: "code" + }, + { + target: 2, + data: "name" + }, + { + target: 3, + data: "price" + }, + { + target: 4, + data: "stockCount" + } + ] + })); + + + $("#CreateNewProductButtonId").click(function () { + _createModal.open(); + }); + + _createModal.onClose(function () { + _dataTable.ajax.reload(); + }); + + _editModal.onResult(function () { + _dataTable.ajax.reload(); + }); + +}); \ No newline at end of file diff --git a/samples/MicroserviceDemo/modules/product/src/ProductManagement.Web/ProductManagementWebAutoMapperProfile.cs b/samples/MicroserviceDemo/modules/product/src/ProductManagement.Web/ProductManagementWebAutoMapperProfile.cs index 47656da8cd..43b2be406a 100644 --- a/samples/MicroserviceDemo/modules/product/src/ProductManagement.Web/ProductManagementWebAutoMapperProfile.cs +++ b/samples/MicroserviceDemo/modules/product/src/ProductManagement.Web/ProductManagementWebAutoMapperProfile.cs @@ -1,4 +1,5 @@ using AutoMapper; +using ProductManagement.Pages.ProductManagement.Products; namespace ProductManagement { @@ -6,7 +7,8 @@ namespace ProductManagement { public ProductManagementWebAutoMapperProfile() { - //Create mappings. + CreateMap(); + CreateMap(); } } } \ No newline at end of file