From 4eafcacf2403bfd7310d5bab07253db26a260c3f Mon Sep 17 00:00:00 2001 From: SharifovDeveloper Date: Tue, 2 Apr 2024 13:32:02 +0500 Subject: [PATCH 1/9] Added AuthenticationController --- .../Controllers/AuthenticationController.cs | 143 ++++++++++++++++++ 1 file changed, 143 insertions(+) create mode 100644 Inflow.Api/Controllers/AuthenticationController.cs diff --git a/Inflow.Api/Controllers/AuthenticationController.cs b/Inflow.Api/Controllers/AuthenticationController.cs new file mode 100644 index 0000000..211bef0 --- /dev/null +++ b/Inflow.Api/Controllers/AuthenticationController.cs @@ -0,0 +1,143 @@ +using Inflow.Api.Constants; +using Inflow.Api.LoginModels; +using Inflow.Domain.Entities; +using Inflow.Domain.Intefaces.Services; +using Inflow.Infrastructure; +using Microsoft.AspNetCore.Mvc; +using Microsoft.IdentityModel.Tokens; +using System.IdentityModel.Tokens.Jwt; +using System.Security.Claims; +using System.Text; + +namespace Inflow.Api.Controllers +{ + + [Route("api/auth")] + [ApiController] + public class AuthenticationController : ControllerBase + { + private readonly InflowDbContext _context; + private readonly IEmailSender _emailSender; + public AuthenticationController(InflowDbContext context, IEmailSender emailSender) + { + _context = context; + _emailSender = emailSender; + } + + [HttpPost("login")] + public ActionResult Login(LoginRequest request) + { + var user = Authenticate(request.Login, request.Password); + + if (user is null) + { + return Unauthorized(); + } + + if (!FindUser(request.Login, request.Password)) + { + return Unauthorized(); + } + + var securityKey = new SymmetricSecurityKey( + Encoding.UTF8.GetBytes("anvarSekretKalitSozMalades")); + var signingCredentials = new SigningCredentials(securityKey, + SecurityAlgorithms.HmacSha256); + + var claimsForToken = new List(); + claimsForToken.Add(new Claim("sub", user.Phone)); + claimsForToken.Add(new Claim("name", user.Name)); + + var jwtSecurityToken = new JwtSecurityToken( + "anvar-api", + "anvar-mobile", + claimsForToken, + DateTime.UtcNow, + DateTime.UtcNow.AddDays(5), + signingCredentials); + + var token = new JwtSecurityTokenHandler() + .WriteToken(jwtSecurityToken); + + return Ok(token); + } + + [HttpPost("register")] + public ActionResult Register(RegisterRequest request) + { + var existingUser = FindUser(request.Login); + if (existingUser != null) + { + return Conflict("User with this login already exists."); + } + + var user = new User + { + Login = request.Login, + Password = request.Password, + Name = request.FullName, + Phone = request.Phone + }; + + _context.Users.Add(user); + + _context.SaveChanges(); + + _emailSender.SendEmail(request.Login, EmailConfigurations.Subject, EmailConfigurations.RegisterBody.Replace("{recipientName}", request.FullName)); + + var securityKey = new SymmetricSecurityKey( + Encoding.UTF8.GetBytes("anvarSekretKalitSozMalades")); + var signingCredentials = new SigningCredentials(securityKey, + SecurityAlgorithms.HmacSha256); + + var claimsForToken = new List(); + claimsForToken.Add(new Claim("sub", user.Phone)); + claimsForToken.Add(new Claim("name", user.Name)); + + var jwtSecurityToken = new JwtSecurityToken( + "anvar-api", + "anvar-mobile", + claimsForToken, + DateTime.UtcNow, + DateTime.UtcNow.AddDays(30), + signingCredentials); + + var token = new JwtSecurityTokenHandler() + .WriteToken(jwtSecurityToken); + + return Ok(token); + } + + private bool FindUser(string login, string password) + { + var user = _context.Users.FirstOrDefault(u => u.Login == login); + + if (user is null || user.Password != password) + { + return false; + } + + //Send email afte Login + _emailSender.SendEmail(user.Login, EmailConfigurations.Subject, EmailConfigurations.LoginBody.Replace("{recipientName}", user.Name)); + + return true; + } + + private User FindUser(string login) + { + return _context.Users.FirstOrDefault(u => u.Login == login); + } + + static User Authenticate(string login, string password) + { + return new User() + { + Login = login, + Password = password, + Name = "Anvar", + Phone = "124123" + }; + } + } +} + From 0324fc08aadcc609c5f7bc24ba6beb5d257873a0 Mon Sep 17 00:00:00 2001 From: SharifovDeveloper Date: Tue, 2 Apr 2024 13:35:49 +0500 Subject: [PATCH 2/9] Updated CategoriesController --- .../Controllers/CategoriesController.cs | 121 ++++++++++++------ Inflow.Api/Inflow.Api.csproj | 1 + 2 files changed, 85 insertions(+), 37 deletions(-) diff --git a/Inflow.Api/Controllers/CategoriesController.cs b/Inflow.Api/Controllers/CategoriesController.cs index 52757cb..fe929ad 100644 --- a/Inflow.Api/Controllers/CategoriesController.cs +++ b/Inflow.Api/Controllers/CategoriesController.cs @@ -1,12 +1,18 @@ -using Inflow.Domain.DTOs.Category; +using ClosedXML.Excel; +using Inflow.Domain.DTOs.Category; using Inflow.Domain.DTOs.Product; using Inflow.Domain.Interfaces.Services; using Inflow.Domain.ResourceParameters; using Inflow.ResourceParameters; using Microsoft.AspNetCore.Mvc; +using Syncfusion.Drawing; +using Syncfusion.Pdf; +using Syncfusion.Pdf.Grid; +using System.Data; namespace Inflow.Controllers { + [Route("api/categories")] [ApiController] //[Authorize] @@ -38,34 +44,41 @@ public ActionResult Get(int id) return Ok(category); } - //[HttpGet("export")] - //public ActionResult ExportCustomers() - //{ - // var category = _categoryService.GetAllCategories(); + [HttpGet("export/xls")] + public ActionResult ExportCustomers() + { + var categories = _categoryService.GetAllCategories(); + byte[] data = GenerateExcle(categories); + + return File(data, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Categories.xlsx"); + } + + [HttpGet("export/pdf")] + public IActionResult CreatePDFDocument() + { + PdfDocument document = new PdfDocument(); + PdfPage page = document.Pages.Add(); + + PdfGrid pdfGrid = new PdfGrid(); - // using XLWorkbook wb = new XLWorkbook(); - // var sheet1 = wb.AddWorksheet(GetCategoriesDataTable(category), "Categories"); + var categories = _categoryService.GetAllCategories(); + List data = ConvertCategoriesToData(categories); - // sheet1.Column(1).Style.Font.FontColor = XLColor.Red; + pdfGrid.DataSource = data; - // sheet1.Columns(2, 4).Style.Font.FontColor = XLColor.Blue; + pdfGrid.ApplyBuiltinStyle(PdfGridBuiltinStyle.GridTable4Accent1); - // sheet1.Row(1).CellsUsed().Style.Fill.BackgroundColor = XLColor.Black; - // //sheet1.Row(1).Cells(1,3).Style.Fill.BackgroundColor = XLColor.Yellow; - // sheet1.Row(1).Style.Font.FontColor = XLColor.White; + pdfGrid.Draw(page, new PointF(10, 10)); - // sheet1.Row(1).Style.Font.Bold = true; - // sheet1.Row(1).Style.Font.Shadow = true; - // sheet1.Row(1).Style.Font.Underline = XLFontUnderlineValues.Single; - // sheet1.Row(1).Style.Font.VerticalAlignment = XLFontVerticalTextAlignmentValues.Superscript; - // sheet1.Row(1).Style.Font.Italic = true; + MemoryStream stream = new MemoryStream(); + document.Save(stream); + stream.Position = 0; - // sheet1.Rows(2, 3).Style.Font.FontColor = XLColor.AshGrey; + string contentType = "application/pdf"; + string fileName = "categories.pdf"; - // using MemoryStream ms = new MemoryStream(); - // wb.SaveAs(ms); - // return File(ms.ToArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Categories.xlsx"); - //} + return File(stream, contentType, fileName); + } [HttpGet("{id}/products")] public ActionResult GetProductsByCategoryId( @@ -106,21 +119,55 @@ public ActionResult Delete(int id) return NoContent(); } + private static byte[] GenerateExcle(IEnumerable categoryDtos) + { + using XLWorkbook wb = new(); + var sheet1 = wb.AddWorksheet(GetCategoriesDataTable(categoryDtos), "Categories"); + + sheet1.Columns(1, 3).Style.Font.FontColor = XLColor.Black; + sheet1.Row(1).CellsUsed().Style.Fill.BackgroundColor = XLColor.Black; + sheet1.Row(1).Style.Font.FontColor = XLColor.White; + + sheet1.Column(1).Width = 5; + sheet1.Columns(2, 3).Width = 12; + + sheet1.Row(1).Style.Font.FontSize = 15; + sheet1.Row(1).Style.Font.Bold = true; + sheet1.Row(1).Style.Font.Shadow = true; + sheet1.Row(1).Style.Font.VerticalAlignment = XLFontVerticalTextAlignmentValues.Superscript; + sheet1.Row(1).Style.Font.Italic = false; + + using MemoryStream ms = new(); + wb.SaveAs(ms); + + return ms.ToArray(); + } + private List ConvertCategoriesToData(IEnumerable categories) + { + List data = new List(); + + foreach (var category in categories) + { + data.Add(new { ID = category.Id, category.Name, category.NumberOfProduct }); + } + + return data; + } + private static DataTable GetCategoriesDataTable(IEnumerable categories) + { + DataTable table = new DataTable(); + table.TableName = "Categories Data"; + table.Columns.Add("Id", typeof(int)); + table.Columns.Add("Name", typeof(string)); + table.Columns.Add("Number of Products", typeof(int)); + + foreach (var category in categories) + { + table.Rows.Add(category.Id, category.Name, category.NumberOfProduct); + } + + return table; + } - //private DataTable GetCategoriesDataTable(IEnumerable categories) - //{ - // DataTable table = new DataTable(); - // table.TableName = "Categories Data"; - // table.Columns.Add("Id", typeof(int)); - // table.Columns.Add("Name", typeof(string)); - // table.Columns.Add("NumberOfProducts", typeof(string)); - - // foreach (var category in categories) - // { - // table.Rows.Add(category.Id, category.Name); - // } - - // return table; - //} } } diff --git a/Inflow.Api/Inflow.Api.csproj b/Inflow.Api/Inflow.Api.csproj index f564aaa..13fd991 100644 --- a/Inflow.Api/Inflow.Api.csproj +++ b/Inflow.Api/Inflow.Api.csproj @@ -19,6 +19,7 @@ + From 9329124f9762023512d0c31745b6910af8c1f5c8 Mon Sep 17 00:00:00 2001 From: SharifovDeveloper Date: Tue, 2 Apr 2024 13:36:13 +0500 Subject: [PATCH 3/9] Corrected --- Inflow.Api/Controllers/CategoriesController.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Inflow.Api/Controllers/CategoriesController.cs b/Inflow.Api/Controllers/CategoriesController.cs index fe929ad..5507167 100644 --- a/Inflow.Api/Controllers/CategoriesController.cs +++ b/Inflow.Api/Controllers/CategoriesController.cs @@ -148,7 +148,7 @@ private List ConvertCategoriesToData(IEnumerable categories foreach (var category in categories) { - data.Add(new { ID = category.Id, category.Name, category.NumberOfProduct }); + data.Add(new { ID = category.Id, category.Name, category.NumberOfProducts }); } return data; @@ -163,7 +163,7 @@ private static DataTable GetCategoriesDataTable(IEnumerable categor foreach (var category in categories) { - table.Rows.Add(category.Id, category.Name, category.NumberOfProduct); + table.Rows.Add(category.Id, category.Name, category.NumberOfProducts); } return table; From 193e22bfb4e38fc29e8b15fe4490602eb7b1de00 Mon Sep 17 00:00:00 2001 From: SharifovDeveloper Date: Tue, 2 Apr 2024 13:37:36 +0500 Subject: [PATCH 4/9] Updated CustomersController --- Inflow.Api/Controllers/CustomersController.cs | 82 ++++++++++++++----- 1 file changed, 63 insertions(+), 19 deletions(-) diff --git a/Inflow.Api/Controllers/CustomersController.cs b/Inflow.Api/Controllers/CustomersController.cs index 6366404..2ffff02 100644 --- a/Inflow.Api/Controllers/CustomersController.cs +++ b/Inflow.Api/Controllers/CustomersController.cs @@ -3,6 +3,9 @@ using Inflow.Domain.Interfaces.Services; using Inflow.Domain.ResourceParameters; using Microsoft.AspNetCore.Mvc; +using Syncfusion.Drawing; +using Syncfusion.Pdf; +using Syncfusion.Pdf.Grid; using System.Data; namespace Inflow.Controllers @@ -41,33 +44,40 @@ public ActionResult Get(int id) return Ok(customer); } - [HttpGet("export")] + [HttpGet("export/xls")] public ActionResult ExportCustomers() { var customers = _customerService.GetCustomers(); + byte[] data = GenerateExcle(customers); - using XLWorkbook wb = new XLWorkbook(); - var sheet1 = wb.AddWorksheet(GetCustomersDataTable(customers), "Customers"); + return File(data, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Customers.xlsx"); + } + [HttpGet("export/pdf")] + public IActionResult CreatePDFDocument() + { + PdfDocument document = new PdfDocument(); + PdfPage page = document.Pages.Add(); - sheet1.Column(1).Style.Font.FontColor = XLColor.Red; + PdfGrid pdfGrid = new PdfGrid(); - sheet1.Columns(2, 4).Style.Font.FontColor = XLColor.Blue; + var customers = _customerService.GetCustomers(); - sheet1.Row(1).CellsUsed().Style.Fill.BackgroundColor = XLColor.Black; - //sheet1.Row(1).Cells(1,3).Style.Fill.BackgroundColor = XLColor.Yellow; - sheet1.Row(1).Style.Font.FontColor = XLColor.White; + List data = ConvertCustomerToData(customers); - sheet1.Row(1).Style.Font.Bold = true; - sheet1.Row(1).Style.Font.Shadow = true; - sheet1.Row(1).Style.Font.Underline = XLFontUnderlineValues.Single; - sheet1.Row(1).Style.Font.VerticalAlignment = XLFontVerticalTextAlignmentValues.Superscript; - sheet1.Row(1).Style.Font.Italic = true; + pdfGrid.DataSource = data; - sheet1.Rows(2, 3).Style.Font.FontColor = XLColor.AshGrey; + pdfGrid.ApplyBuiltinStyle(PdfGridBuiltinStyle.GridTable4Accent1); - using MemoryStream ms = new MemoryStream(); - wb.SaveAs(ms); - return File(ms.ToArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Customers.xlsx"); + pdfGrid.Draw(page, new PointF(10, 10)); + + MemoryStream stream = new MemoryStream(); + document.Save(stream); + stream.Position = 0; + + string contentType = "application/pdf"; + string fileName = "customers.pdf"; + + return File(stream, contentType, fileName); } [HttpPost] @@ -100,13 +110,47 @@ public ActionResult Delete(int id) return NoContent(); } - private DataTable GetCustomersDataTable(IEnumerable customers) + private List ConvertCustomerToData(IEnumerable customerDtos) + { + List data = new List(); + + foreach (var customer in customerDtos) + { + data.Add(new { ID = customer.Id, customer.FullName, customer.PhoneNumber }); + } + + return data; + } + private static byte[] GenerateExcle(IEnumerable customerDtos) + { + using XLWorkbook wb = new(); + var sheet1 = wb.AddWorksheet(GetCustomersDataTable(customerDtos), "Categories"); + + sheet1.Columns(1, 3).Style.Font.FontColor = XLColor.Black; + sheet1.Row(1).CellsUsed().Style.Fill.BackgroundColor = XLColor.Black; + sheet1.Row(1).Style.Font.FontColor = XLColor.White; + + sheet1.Column(1).Width = 5; + sheet1.Columns(2, 3).Width = 18; + + sheet1.Row(1).Style.Font.FontSize = 15; + sheet1.Row(1).Style.Font.Bold = true; + sheet1.Row(1).Style.Font.Shadow = true; + sheet1.Row(1).Style.Font.VerticalAlignment = XLFontVerticalTextAlignmentValues.Superscript; + sheet1.Row(1).Style.Font.Italic = false; + + using MemoryStream ms = new(); + wb.SaveAs(ms); + + return ms.ToArray(); + } + private static DataTable GetCustomersDataTable(IEnumerable customers) { DataTable table = new DataTable(); table.TableName = "Customers Data"; table.Columns.Add("Id", typeof(int)); table.Columns.Add("Name", typeof(string)); - table.Columns.Add("Phone", typeof(string)); + table.Columns.Add("Phone Number", typeof(string)); foreach (var customer in customers) { From 4efad19fca0443f68b2b95f6e4af7d74e84adbad Mon Sep 17 00:00:00 2001 From: SharifovDeveloper Date: Tue, 2 Apr 2024 13:38:52 +0500 Subject: [PATCH 5/9] Added MailController --- Inflow.Api/Controllers/MailController.cs | 30 ++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Inflow.Api/Controllers/MailController.cs diff --git a/Inflow.Api/Controllers/MailController.cs b/Inflow.Api/Controllers/MailController.cs new file mode 100644 index 0000000..bcddcb4 --- /dev/null +++ b/Inflow.Api/Controllers/MailController.cs @@ -0,0 +1,30 @@ +using Inflow.Api.Constants; +using Inflow.Domain.Intefaces.Services; +using Microsoft.AspNetCore.Mvc; + +namespace Inflow.Api.Controllers +{ + [Route("api/mail")] + [ApiController] + public class MailController : Controller + { + private readonly IEmailSender _emailSender; + + public MailController() { } + public MailController(IEmailSender emailSender) + { + _emailSender = emailSender ?? throw new ArgumentNullException(nameof(emailSender)); + } + + [HttpPost("register")] + public async Task SendRegisterEmail(string receiverEmail, string? name) + { + string subject = EmailConfigurations.Subject; + string emailBody = EmailConfigurations.RegisterBody.Replace("{recipientName}", name); + + await _emailSender.SendEmail(receiverEmail, subject, emailBody); + + return Ok(); + } + } +} From 016c91b8ab3c8edc013385ca7d07e66dcc5e2351 Mon Sep 17 00:00:00 2001 From: SharifovDeveloper Date: Tue, 2 Apr 2024 13:56:32 +0500 Subject: [PATCH 6/9] Updated ProductControllers and installed NewtonsoftJson ng and Updated ProductDto --- Inflow.Api/Controllers/ProductsController.cs | 139 ++++++++++-------- Inflow.Api/Inflow.Api.csproj | 1 + Inflow.Domain/DTOs/Product/ProductDto.cs | 2 + Inflow.Domain/Inflow.Domain.csproj | 1 + .../Inflow.Infrastructure.csproj | 1 + Inflow.Service/Inflow.Service.csproj | 4 + 6 files changed, 83 insertions(+), 65 deletions(-) diff --git a/Inflow.Api/Controllers/ProductsController.cs b/Inflow.Api/Controllers/ProductsController.cs index 2c7c0c9..59788e2 100644 --- a/Inflow.Api/Controllers/ProductsController.cs +++ b/Inflow.Api/Controllers/ProductsController.cs @@ -6,12 +6,11 @@ using Inflow.ResourceParameters; using Microsoft.AspNetCore.JsonPatch; using Microsoft.AspNetCore.Mvc; -using PdfSharpCore.Drawing; -using PdfSharpCore.Pdf; +using Syncfusion.Drawing; +using Syncfusion.Pdf; +using Syncfusion.Pdf.Grid; using System.Data; -// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 - namespace Inflow.Controllers { [Route("api/products")] @@ -28,7 +27,6 @@ public ProductsController(IProductService productService, IMapper mapper) _mapper = mapper; } - // GET: api/ [HttpGet] public ActionResult> GetProductsAsync( [FromQuery] ProductResourceParameters productResourceParameters) @@ -38,68 +36,42 @@ public ActionResult> GetProductsAsync( return Ok(products); } - [HttpGet("export")] + [HttpGet("export/xls")] public ActionResult ExportProducts() { - var category = _productService.GetAllProducts(); - - using XLWorkbook wb = new XLWorkbook(); - var sheet1 = wb.AddWorksheet(GetProductsDataTable(category), "Products"); - - sheet1.Column(1).Style.Font.FontColor = XLColor.Red; - - sheet1.Columns(2, 4).Style.Font.FontColor = XLColor.Blue; - - sheet1.Row(1).CellsUsed().Style.Fill.BackgroundColor = XLColor.Black; - //sheet1.Row(1).Cells(1,3).Style.Fill.BackgroundColor = XLColor.Yellow; - sheet1.Row(1).Style.Font.FontColor = XLColor.White; - - sheet1.Row(1).Style.Font.Bold = true; - sheet1.Row(1).Style.Font.Shadow = true; - sheet1.Row(1).Style.Font.Underline = XLFontUnderlineValues.Single; - sheet1.Row(1).Style.Font.VerticalAlignment = XLFontVerticalTextAlignmentValues.Superscript; - sheet1.Row(1).Style.Font.Italic = true; - - //sheet1.Rows(2, 3).Style.Font.FontColor = XLColor.AshGrey; + var products = _productService.GetAllProducts(); + byte[] data = GenerateExcle(products); - using MemoryStream ms = new MemoryStream(); - wb.SaveAs(ms); - return File(ms.ToArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Products.xlsx"); + return File(data, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Products.xls"); } - [HttpGet("exportPDF")] - public ActionResult ExportProductsPDF() + [HttpGet("export/pdf")] + public IActionResult CreatePDFDocument() { + PdfDocument document = new PdfDocument(); + PdfPage page = document.Pages.Add(); + + PdfGrid pdfGrid = new PdfGrid(); var products = _productService.GetAllProducts(); - var dataTable = GetProductsDataTable(products); - // Создание PDF-документа - using PdfDocument pdf = new PdfDocument(); - PdfPage page = pdf.AddPage(); - XGraphics gfx = XGraphics.FromPdfPage(page); - XFont font = new XFont("Arial", 12, XFontStyle.Regular); + List data = ConvertCategoriesToData(products); - // Начало новой страницы PDF - gfx.DrawString("Продукты PDF", font, XBrushes.Black, new XRect(0, 0, page.Width.Point, page.Height.Point), XStringFormats.TopCenter); + pdfGrid.DataSource = data; - // Добавление данных о продуктах в PDF - int yPosition = 40; - foreach (DataRow row in dataTable.Rows) - { - string productInfo = $"Id: {row["Id"]}, Name: {row["Name"]}, Description: {row["Description"]}, SalePrice: {row["SalePrice"]}, SupplyPrice: {row["SupplyPrice"]}, ExpireDate: {row["ExpireDate"]}, CategoryName: {(row["CategoryName"] != DBNull.Value ? row["CategoryName"] : "")}"; - gfx.DrawString(productInfo, font, XBrushes.Black, new XRect(0, yPosition, page.Width.Point, page.Height.Point), XStringFormats.TopLeft); - yPosition += 20; - } + pdfGrid.ApplyBuiltinStyle(PdfGridBuiltinStyle.GridTable4Accent1); + + pdfGrid.Draw(page, new PointF(15, 15)); - // Сохранение PDF в MemoryStream и отправка его как файл ответа - using MemoryStream pdfStream = new MemoryStream(); - pdf.Save(pdfStream, false); + MemoryStream stream = new MemoryStream(); + document.Save(stream); + stream.Position = 0; - // Возврат PDF-файла - return File(pdfStream.ToArray(), "application/pdf", "Products.pdf"); + string contentType = "application/pdf"; + string fileName = "products.pdf"; + + return File(stream, contentType, fileName); } - // GET api//5 [HttpGet("{id}", Name = "GetProductById")] public ActionResult Get(int id) { @@ -113,7 +85,6 @@ public ActionResult Get(int id) return Ok(product); } - // POST api/ [HttpPost] public ActionResult Post([FromBody] ProductForCreateDto product) { @@ -122,7 +93,6 @@ public ActionResult Post([FromBody] ProductForCreateDto product) return CreatedAtAction(nameof(Get), new { id = createdProduct.Id }, createdProduct); } - // PUT api//5 [HttpPut("{id}")] public ActionResult Put(int id, [FromBody] ProductForUpdateDto product) { @@ -157,7 +127,7 @@ public ActionResult PartiallyUpdateProduct( CategoryId = product.Category.Id, }; - jsonPatch.ApplyTo(productToPatch, (Microsoft.AspNetCore.JsonPatch.Adapters.IObjectAdapter)ModelState); + jsonPatch.ApplyTo(productToPatch, ModelState); if (!ModelState.IsValid) { @@ -178,24 +148,25 @@ public ActionResult PartiallyUpdateProduct( return Ok(productToPatch); } - // DELETE api//5 [HttpDelete("{id}")] public void Delete(int id) { _productService.DeleteProduct(id); } - private DataTable GetProductsDataTable(IEnumerable productDtos) + private static DataTable GetProductsDataTable(IEnumerable productDtos) { - DataTable table = new DataTable(); - table.TableName = "Products Data"; + DataTable table = new() + { + TableName = "Products" + }; table.Columns.Add("Id", typeof(int)); table.Columns.Add("Name", typeof(string)); table.Columns.Add("Description", typeof(string)); - table.Columns.Add("SalePrice", typeof(decimal)); - table.Columns.Add("SupplyPrice", typeof(decimal)); - table.Columns.Add("ExpireDate", typeof(DateTime)); - table.Columns.Add("CategoryName", typeof(string)); + table.Columns.Add("Sale Price", typeof(decimal)); + table.Columns.Add("Supply Price", typeof(decimal)); + table.Columns.Add("Expire Date", typeof(DateTime)); + table.Columns.Add("Category", typeof(string)); foreach (var product in productDtos) { @@ -205,10 +176,48 @@ private DataTable GetProductsDataTable(IEnumerable productDtos) product.SalePrice, product.SupplyPrice, product.ExpireDate, - product.Category != null ? product.Category.Name : null); + product.Category?.Name); } return table; } + private static byte[] GenerateExcle(IEnumerable productDto) + { + using XLWorkbook wb = new(); + var sheet1 = wb.AddWorksheet(GetProductsDataTable(productDto), "Products"); + + sheet1.Columns(1, 3).Style.Font.FontColor = XLColor.Black; + sheet1.Columns(4, 5).Style.Font.FontColor = XLColor.Blue; + sheet1.Columns(6, 7).Style.Font.FontColor = XLColor.Black; + sheet1.Row(1).CellsUsed().Style.Fill.BackgroundColor = XLColor.Black; + sheet1.Row(1).Style.Font.FontColor = XLColor.White; + + sheet1.Column(1).Width = 10; + sheet1.Columns(2, 3).Width = 25; + sheet1.Columns(4, 5).Width = 15; + sheet1.Columns(6, 7).Width = 20; + sheet1.Row(1).Style.Font.FontSize = 16; + + sheet1.Row(1).Style.Font.Bold = true; + sheet1.Row(1).Style.Font.Shadow = true; + sheet1.Row(1).Style.Font.VerticalAlignment = XLFontVerticalTextAlignmentValues.Superscript; + sheet1.Row(1).Style.Font.Italic = false; + + using MemoryStream ms = new(); + wb.SaveAs(ms); + + return ms.ToArray(); + } + private List ConvertCategoriesToData(IEnumerable products) + { + List data = new List(); + + foreach (var product in products) + { + data.Add(new { ID = product.Id, product.Name, product.Description, product.ExpireDate, product.SalePrice, product.SupplyPrice, product.QuantityInStock }); + } + + return data; + } } } diff --git a/Inflow.Api/Inflow.Api.csproj b/Inflow.Api/Inflow.Api.csproj index 13fd991..9f7780d 100644 --- a/Inflow.Api/Inflow.Api.csproj +++ b/Inflow.Api/Inflow.Api.csproj @@ -11,6 +11,7 @@ + diff --git a/Inflow.Domain/DTOs/Product/ProductDto.cs b/Inflow.Domain/DTOs/Product/ProductDto.cs index d7f72d9..ca5d4ba 100644 --- a/Inflow.Domain/DTOs/Product/ProductDto.cs +++ b/Inflow.Domain/DTOs/Product/ProductDto.cs @@ -12,6 +12,8 @@ public record ProductDto public decimal SalePrice { get; init; } public decimal SupplyPrice { get; init; } public DateTime ExpireDate { get; init; } + public int QuantityInStock { get; set; } + public int LowQuantityAmount { get; set; } public CategoryDto Category { get; init; } public ICollection SaleItems { get; init; } public ICollection SupplyItems { get; init; } diff --git a/Inflow.Domain/Inflow.Domain.csproj b/Inflow.Domain/Inflow.Domain.csproj index fe0b795..4f1f464 100644 --- a/Inflow.Domain/Inflow.Domain.csproj +++ b/Inflow.Domain/Inflow.Domain.csproj @@ -8,6 +8,7 @@ + diff --git a/Inflow.Infrastructure/Inflow.Infrastructure.csproj b/Inflow.Infrastructure/Inflow.Infrastructure.csproj index 60117f9..71cc139 100644 --- a/Inflow.Infrastructure/Inflow.Infrastructure.csproj +++ b/Inflow.Infrastructure/Inflow.Infrastructure.csproj @@ -7,6 +7,7 @@ + diff --git a/Inflow.Service/Inflow.Service.csproj b/Inflow.Service/Inflow.Service.csproj index fa71b7a..d16b751 100644 --- a/Inflow.Service/Inflow.Service.csproj +++ b/Inflow.Service/Inflow.Service.csproj @@ -6,4 +6,8 @@ enable + + + + From 79b18d683ae5b7b0cb4b74b77d9c361bf8a4967e Mon Sep 17 00:00:00 2001 From: SharifovDeveloper Date: Tue, 2 Apr 2024 13:57:49 +0500 Subject: [PATCH 7/9] Updated SaleItemsController --- Inflow.Api/Controllers/SaleItemsController.cs | 52 ++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/Inflow.Api/Controllers/SaleItemsController.cs b/Inflow.Api/Controllers/SaleItemsController.cs index 185e2de..06e8751 100644 --- a/Inflow.Api/Controllers/SaleItemsController.cs +++ b/Inflow.Api/Controllers/SaleItemsController.cs @@ -1,8 +1,10 @@ -using Inflow.Domain.DTOs.SaleItem; +using ClosedXML.Excel; +using Inflow.Domain.DTOs.SaleItem; using Inflow.Domain.DTOsSaleItem; using Inflow.Domain.Interfaces.Services; using Inflow.Domain.ResourceParameters; using Microsoft.AspNetCore.Mvc; +using System.Data; namespace Inflow.Controllers { @@ -32,6 +34,34 @@ public ActionResult> GetSalesSaleItems(int salesId) var salesSaleItems = _saleItemService.GetSalesSaleItems(salesId); return Ok(salesSaleItems); } + [HttpGet("export/{saleId}")] + public ActionResult ExportSaleItems(int saleId) + { + var saleItems = _saleItemService.GetSalesSaleItems(saleId); + + using XLWorkbook wb = new XLWorkbook(); + var sheet1 = wb.AddWorksheet(GetSaleItemssDataTable(saleItems), "SaleItems"); + + sheet1.Column(1).Style.Font.FontColor = XLColor.Red; + + sheet1.Columns(2, 4).Style.Font.FontColor = XLColor.Blue; + + sheet1.Row(1).CellsUsed().Style.Fill.BackgroundColor = XLColor.Black; + //sheet1.Row(1).Cells(1,3).Style.Fill.BackgroundColor = XLColor.Yellow; + sheet1.Row(1).Style.Font.FontColor = XLColor.White; + + sheet1.Row(1).Style.Font.Bold = true; + sheet1.Row(1).Style.Font.Shadow = true; + sheet1.Row(1).Style.Font.Underline = XLFontUnderlineValues.Single; + sheet1.Row(1).Style.Font.VerticalAlignment = XLFontVerticalTextAlignmentValues.Superscript; + sheet1.Row(1).Style.Font.Italic = true; + + sheet1.Rows(2, 3).Style.Font.FontColor = XLColor.AshGrey; + + using MemoryStream ms = new MemoryStream(); + wb.SaveAs(ms); + return File(ms.ToArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "SaleItems.xlsx"); + } [HttpGet("{id}", Name = "GetSaleItemById")] public ActionResult Get(int id) @@ -75,5 +105,25 @@ public ActionResult Delete(int id) return NoContent(); } + private DataTable GetSaleItemssDataTable(IEnumerable saleItemDtos) + { + DataTable table = new DataTable(); + table.TableName = "Sales Data"; + table.Columns.Add("ProductName", typeof(string)); + table.Columns.Add("Quantity", typeof(int)); + table.Columns.Add("UnitPrice", typeof(decimal)); + table.Columns.Add("TotalDue", typeof(decimal)); + + foreach (var saleitem in saleItemDtos) + { + table.Rows.Add( + saleitem.ProductName, + saleitem.Quantity, + saleitem.UnitPrice, + saleitem.TotalDue); + } + + return table; + } } } From bd9fd3711489368aeafeb91a752bcfb5c6b490fb Mon Sep 17 00:00:00 2001 From: SharifovDeveloper Date: Tue, 2 Apr 2024 14:00:07 +0500 Subject: [PATCH 8/9] Updated SuppliersController --- Inflow.Api/Controllers/SuppliersController.cs | 89 ++++++++++++++----- 1 file changed, 67 insertions(+), 22 deletions(-) diff --git a/Inflow.Api/Controllers/SuppliersController.cs b/Inflow.Api/Controllers/SuppliersController.cs index 817e236..6c3fc4f 100644 --- a/Inflow.Api/Controllers/SuppliersController.cs +++ b/Inflow.Api/Controllers/SuppliersController.cs @@ -3,6 +3,9 @@ using Inflow.Domain.Interfaces.Services; using Inflow.Domain.ResourceParameters; using Microsoft.AspNetCore.Mvc; +using Syncfusion.Drawing; +using Syncfusion.Pdf; +using Syncfusion.Pdf.Grid; using System.Data; namespace Inflow.Controllers @@ -27,33 +30,40 @@ public ActionResult> GetSuppliersAsync( return Ok(suppliers); } - [HttpGet("export")] + [HttpGet("export/xls")] public ActionResult ExportSuppliers() { - var category = _supplierService.GetAllSuppliers(); + var suppliers = _supplierService.GetAllSuppliers(); + byte[] data = GenerateExcle(suppliers); - using XLWorkbook wb = new XLWorkbook(); - var sheet1 = wb.AddWorksheet(GetSuppliersTable(category), "Suppliers"); + return File(data, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Suppliers.xlsx"); + } + [HttpGet("export/pdf")] + public IActionResult CreatePDFDocument() + { + PdfDocument document = new PdfDocument(); + PdfPage page = document.Pages.Add(); - sheet1.Column(1).Style.Font.FontColor = XLColor.Red; + PdfGrid pdfGrid = new PdfGrid(); - sheet1.Columns(2, 4).Style.Font.FontColor = XLColor.Blue; + var suppliers = _supplierService.GetAllSuppliers(); - sheet1.Row(1).CellsUsed().Style.Fill.BackgroundColor = XLColor.Black; - //sheet1.Row(1).Cells(1,3).Style.Fill.BackgroundColor = XLColor.Yellow; - sheet1.Row(1).Style.Font.FontColor = XLColor.White; + List data = ConvertSuppliersToData(suppliers); - sheet1.Row(1).Style.Font.Bold = true; - sheet1.Row(1).Style.Font.Shadow = true; - sheet1.Row(1).Style.Font.Underline = XLFontUnderlineValues.Single; - sheet1.Row(1).Style.Font.VerticalAlignment = XLFontVerticalTextAlignmentValues.Superscript; - sheet1.Row(1).Style.Font.Italic = true; + pdfGrid.DataSource = data; - sheet1.Rows(2, 3).Style.Font.FontColor = XLColor.AshGrey; + pdfGrid.ApplyBuiltinStyle(PdfGridBuiltinStyle.GridTable4Accent1); - using MemoryStream ms = new MemoryStream(); - wb.SaveAs(ms); - return File(ms.ToArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Suppliers.xlsx"); + pdfGrid.Draw(page, new PointF(10, 10)); + + MemoryStream stream = new MemoryStream(); + document.Save(stream); + stream.Position = 0; + + string contentType = "application/pdf"; + string fileName = "suppliers.pdf"; + + return File(stream, contentType, fileName); } [HttpGet("{id}", Name = "GetSupplierById")] @@ -98,15 +108,50 @@ public ActionResult Delete(int id) return NoContent(); } + private List ConvertSuppliersToData(IEnumerable supplierDtos) + { + List data = new List(); + + foreach (var supplier in supplierDtos) + { + data.Add(new { ID = supplier.Id, supplier.FirstName, supplier.LastName, supplier.Company, supplier.PhoneNumber }); + } + + return data; + } + private static byte[] GenerateExcle(IEnumerable supplierDtos) + { + using XLWorkbook wb = new(); + var sheet1 = wb.AddWorksheet(GetSuppliersTable(supplierDtos), "Suppliers"); + + sheet1.Columns(1, 3).Style.Font.FontColor = XLColor.Black; + sheet1.Row(1).CellsUsed().Style.Fill.BackgroundColor = XLColor.Black; + sheet1.Row(1).Style.Font.FontColor = XLColor.White; + + sheet1.Column(1).Width = 5; + sheet1.Columns(2, 3).Width = 12; + sheet1.Column(4).Width = 20; + sheet1.Column(5).Width = 28; - private DataTable GetSuppliersTable(IEnumerable supplierDtos) + sheet1.Row(1).Style.Font.FontSize = 15; + sheet1.Row(1).Style.Font.Bold = true; + sheet1.Row(1).Style.Font.Shadow = true; + sheet1.Row(1).Style.Font.VerticalAlignment = XLFontVerticalTextAlignmentValues.Superscript; + sheet1.Row(1).Style.Font.Italic = false; + + using MemoryStream ms = new(); + wb.SaveAs(ms); + + return ms.ToArray(); + } + private static DataTable GetSuppliersTable(IEnumerable supplierDtos) { DataTable table = new DataTable(); table.TableName = "Suppliers Data"; table.Columns.Add("Id", typeof(int)); - table.Columns.Add("FirstName", typeof(string)); - table.Columns.Add("LastName", typeof(string)); - table.Columns.Add("PhoneNumber", typeof(string)); + table.Columns.Add("First Name", typeof(string)); + table.Columns.Add("Last Name", typeof(string)); + table.Columns.Add("Phone Number", typeof(string)); table.Columns.Add("Company", typeof(string)); foreach (var supplier in supplierDtos) From 6bfe6a88345e6f2b43d27a1b90a8fc363ab41466 Mon Sep 17 00:00:00 2001 From: SharifovDeveloper Date: Tue, 2 Apr 2024 14:02:17 +0500 Subject: [PATCH 9/9] Updated SupplierController and SupplyDto --- Inflow.Api/Controllers/SuppliesController.cs | 2 +- Inflow.Domain/DTOs/Supply/SupplyDto.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Inflow.Api/Controllers/SuppliesController.cs b/Inflow.Api/Controllers/SuppliesController.cs index 62d5d21..e1b1543 100644 --- a/Inflow.Api/Controllers/SuppliesController.cs +++ b/Inflow.Api/Controllers/SuppliesController.cs @@ -113,7 +113,7 @@ private DataTable GetSuppliesDataTable(IEnumerable supplyDtos) table.Rows.Add(supply.Id, supply.TotalDue, supply.SupplyDate, - supply.Supplier); + supply.SupplierId); } return table; diff --git a/Inflow.Domain/DTOs/Supply/SupplyDto.cs b/Inflow.Domain/DTOs/Supply/SupplyDto.cs index 8b33b1f..b1549a0 100644 --- a/Inflow.Domain/DTOs/Supply/SupplyDto.cs +++ b/Inflow.Domain/DTOs/Supply/SupplyDto.cs @@ -7,6 +7,6 @@ public record SupplyDto( int Id, DateTime SupplyDate, decimal TotalDue, - SupplierDto Supplier, + int SupplierId, ICollection SupplyItems); }