Skip to content

Commit

Permalink
Implemented get-view-model-by-xml route on InvoiceController
Browse files Browse the repository at this point in the history
  • Loading branch information
adilsonresendedev committed Aug 9, 2023
1 parent 67261ba commit 5cca611
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 58 deletions.
20 changes: 8 additions & 12 deletions RetailApp.API/Controllers/InvoiceController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.AspNetCore.Mvc;
using RetailApp.Services;
using RetailApp.Services.Interfaces;
using RetailApp.ViewModels;
using RetailApp.ViewModels.Fiscal;
using System.Xml;

Expand All @@ -17,19 +18,14 @@ public InvoiceController(IInvoiceService invoiceService)
_invoiceService = invoiceService;
}

[HttpGet]
public async Task<IActionResult> GetViewModelByXML()
[HttpPost]
[Route("get-view-model-by-xml")]
[ProducesResponseType(typeof(ResponseViewModel<InvoiceViewModel>), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ResponseViewModel<InvoiceViewModel>), StatusCodes.Status500InternalServerError)]
public async Task<IActionResult> GetViewModelByXML([FromBody] RequestViewModel<string> requestViewModel)
{
XmlDocument xmlDocument = new XmlDocument();
InvoiceViewModel invoiceViewModel = _invoiceService.GetInvoiceViewModelByXML(xmlDocument.OuterXml);
return Ok(invoiceViewModel);
ResponseViewModel<InvoiceViewModel> responseViewModel = _invoiceService.GetInvoiceViewModelByXML(requestViewModel.Data);
return StatusCode((int)responseViewModel.StatusCode, responseViewModel);
}

//[HttpGet]
//public async Task<IActionResult> GetViewModelByXML([FromBody] XmlDocument xmlDocument)
//{
// InvoiceViewModel invoiceViewModel = _invoiceService.GetInvoiceViewModelByXML(xmlDocument.OuterXml);
// return Ok(invoiceViewModel);
//}
}
}
40 changes: 37 additions & 3 deletions RetailApp.Business/InvoiceBusiness.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,51 @@
using RetailApp.Business.Interfaces;
using RetailApp.Utility;
using RetailApp.ViewModels.Address;
using RetailApp.ViewModels.Fiscal;
using RetailApp.ViewModels.Phone;
using RetailApp.ViewModels.Suplier;
using System.Text;

namespace RetailApp.Business
{
public class InvoiceBusiness : IInvoiceBusiness
{
public InvoiceViewModel GetInvoiceViewModelByXML(string xml)
{
InvoiceViewModel invoiceViewModel = new InvoiceViewModel();
invoiceViewModel.SupplierViewModel = new SupplierViewModel
byte[] xmlByte = Convert.FromBase64String(xml);
string xmlString = Encoding.UTF8.GetString(xmlByte);
nfeProc nfeProc = XmlUtility.XmlToClass<nfeProc>(xmlString);
InvoiceViewModel invoiceViewModel = new InvoiceViewModel
{
Name = "Chegou na camada de business!"
SupplierViewModel = new SupplierViewModel
{
CNPJ = nfeProc.NFe.infNFe.emit.CNPJ,
Name = nfeProc.NFe.infNFe.emit.xNome,
ComercialName = nfeProc.NFe.infNFe.emit.xNome,
IE = nfeProc.NFe.infNFe.emit.IE,
IEST = nfeProc.NFe.infNFe.emit.IEST,
CRT = nfeProc.NFe.infNFe.emit.CRT,
addresViewModel = new List<AddresViewModel>
{
new AddresViewModel
{
MainAdress = true,
StreetName = nfeProc.NFe.infNFe.emit.enderEmit.xLgr,
StreetNumber = nfeProc.NFe.infNFe.emit.enderEmit.nro,
StreetAditionalInformation = nfeProc.NFe.infNFe.emit.enderEmit.xCpl,
Neighborhood = nfeProc.NFe.infNFe.emit.enderEmit.xBairro,
ZipCode = nfeProc.NFe.infNFe.emit.enderEmit.CEP
}
},
PhoneViewModel = new List<PhoneViewModel>
{
new PhoneViewModel
{
MainPhone = true,
PhoneNumber = nfeProc.NFe.infNFe.emit.enderEmit.fone
}
}
}
};

return invoiceViewModel;
Expand Down
1 change: 1 addition & 0 deletions RetailApp.Business/RetailApp.Business.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\RetailApp.Utility\RetailApp.Utility.csproj" />
<ProjectReference Include="..\RetailApp.ViewModels\RetailApp.ViewModels.csproj" />
</ItemGroup>

Expand Down
11 changes: 0 additions & 11 deletions RetailApp.DesktopUI/Fiscal/frmInvoiceList.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,8 @@
using RetailApp.ViewModels.Address;
using RetailApp.ViewModels.Phone;
using RetailApp.ViewModels.Suplier;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace RetailApp.DesktopUI.Fiscal
{
Expand Down
10 changes: 5 additions & 5 deletions RetailApp.DesktopUI/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ private void Window_Loaded(object sender, RoutedEventArgs e)

}

private void ImportXML()
{
string xmlContent = FileUtility.GetStringFromXml();
nfeProc nfeProc = XmlUtility.XmlToClass<nfeProc>(xmlContent);
}
//private void ImportXML()
//{
// string xmlContent = FileUtility.GetStringFromXml();
// nfeProc nfeProc = XmlUtility.XmlToClass<nfeProc>(xmlContent);
//}

private void meiFiscal_Click(object sender, RoutedEventArgs e)
{
Expand Down
5 changes: 3 additions & 2 deletions RetailApp.Services/Interfaces/IInvoiceService.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using RetailApp.ViewModels.Fiscal;
using RetailApp.ViewModels;
using RetailApp.ViewModels.Fiscal;

namespace RetailApp.Services.Interfaces
{
public interface IInvoiceService
{
InvoiceViewModel GetInvoiceViewModelByXML(string xml);
ResponseViewModel<InvoiceViewModel> GetInvoiceViewModelByXML(string xml);
}
}
19 changes: 16 additions & 3 deletions RetailApp.Services/InvoiceService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using RetailApp.Business.Interfaces;
using RetailApp.Services.Interfaces;
using RetailApp.ViewModels;
using RetailApp.ViewModels.Fiscal;
using System.Net;

namespace RetailApp.Services
{
Expand All @@ -11,10 +13,21 @@ public InvoiceService(IInvoiceBusiness invoiceBusiness)
{
_invoiceBusiness = invoiceBusiness;
}
public InvoiceViewModel GetInvoiceViewModelByXML(string xml)

public ResponseViewModel<InvoiceViewModel> GetInvoiceViewModelByXML(string xml)
{
InvoiceViewModel invoiceViewModel = _invoiceBusiness.GetInvoiceViewModelByXML(xml);
return invoiceViewModel;
ResponseViewModel<InvoiceViewModel> responseViewModel = new ResponseViewModel<InvoiceViewModel>(HttpStatusCode.OK);
try
{
InvoiceViewModel invoiceViewModel = _invoiceBusiness.GetInvoiceViewModelByXML(xml);
responseViewModel.Data = invoiceViewModel;
}
catch (Exception ex)
{
responseViewModel = new ResponseViewModel<InvoiceViewModel>(ex);
}

return responseViewModel;
}
}
}
32 changes: 16 additions & 16 deletions RetailApp.Utility/FileUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@
{
public static class FileUtility
{
public static string GetStringFromXml()
{
string xmlString = string.Empty;
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Multiselect = false;
openFileDialog.Filter = "XML files (*.xml)|*.xml|All files (*.*)|*.*\";";
DialogResult dialogResult = openFileDialog.ShowDialog();
//public static string GetStringFromXml()
//{
// string xmlString = string.Empty;
// OpenFileDialog openFileDialog = new OpenFileDialog();
// openFileDialog.Multiselect = false;
// openFileDialog.Filter = "XML files (*.xml)|*.xml|All files (*.*)|*.*\";";
// DialogResult dialogResult = openFileDialog.ShowDialog();

if(dialogResult == DialogResult.OK)
{
using (StreamReader streamReader = new StreamReader(openFileDialog.FileName))
{
xmlString = streamReader.ReadToEnd();
}
}
// if(dialogResult == DialogResult.OK)
// {
// using (StreamReader streamReader = new StreamReader(openFileDialog.FileName))
// {
// xmlString = streamReader.ReadToEnd();
// }
// }

return xmlString;
}
// return xmlString;
//}
}
}
11 changes: 5 additions & 6 deletions RetailApp.Utility/RetailApp.Utility.csproj
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0-windows</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
7 changes: 7 additions & 0 deletions RetailApp.ViewModels/RequestViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace RetailApp.ViewModels
{
public class RequestViewModel<T>
{
public T Data { get; set; }
}
}
26 changes: 26 additions & 0 deletions RetailApp.ViewModels/ResponseViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Net;

namespace RetailApp.ViewModels
{
public class ResponseViewModel<T>
{
public ResponseViewModel(HttpStatusCode httpStatusCode)
{
IsSuccess = true;
StatusCode = httpStatusCode;
}

public ResponseViewModel(Exception ex)
{
StatusCode = HttpStatusCode.InternalServerError;
IsSuccess = false;
Message = ex.GetBaseException().Message;
}

public HttpStatusCode StatusCode { get; set; }
public bool IsSuccess { get; set; }
public string Message { get; set; }
public T Data { get; set; }
}
}

0 comments on commit 5cca611

Please sign in to comment.