Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions CrudWebAPIAspCore/CrudWebAPIAspCore.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28010.2003
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CrudWebAPIAspCore", "CrudWebAPIAspCore\CrudWebAPIAspCore.csproj", "{115F6831-3D7D-4693-A401-966502729348}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{115F6831-3D7D-4693-A401-966502729348}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{115F6831-3D7D-4693-A401-966502729348}.Debug|Any CPU.Build.0 = Debug|Any CPU
{115F6831-3D7D-4693-A401-966502729348}.Release|Any CPU.ActiveCfg = Release|Any CPU
{115F6831-3D7D-4693-A401-966502729348}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {EC5671AA-11DD-4CC1-B698-B1760E6B0BBC}
EndGlobalSection
EndGlobal
110 changes: 110 additions & 0 deletions CrudWebAPIAspCore/CrudWebAPIAspCore/Controllers/NFControllers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using CrudWebAPIAspCore.Model;
using CrudWebAPIAspCore.Service;

namespace CrudWebAPIAspCore.Controllers
{
[Route("NotasFiscais")]
public class NFControllers :Controller
{
private readonly INFService service;
public NFControllers(INFService service)
{
this.service = service;
}
[HttpGet]
public IActionResult Get()
{
List<NotaFiscal> model = service.GetNotaFiscal();
var outputModel = ToOutputModelList(model);
return Ok(outputModel);
}

[HttpGet("{notafiscalid}", Name = "GetFilme")]
public IActionResult Get(int notafiscalid)
{
var model = service.GetNotaFiscal(notafiscalid);
if (model == null)
return NotFound();

var outputModel = ToOutPutModel(model);
return Ok(outputModel);
}
[HttpPost]
public IActionResult Create([FromBody]NFInput inputModel)
{
if (inputModel == null)
return BadRequest();
var model = ToDomainModel(inputModel);
service.AddNotaFiscal(model);
var outputModel = ToOutPutModel(model);
return CreatedAtRoute("GetFilme", new { notafiscalid = outputModel.notaFiscalId }, outputModel);
}
[HttpPut("{notafiscalid}")]
public IActionResult Update(int notafiscalid, [FromBody]NFInput inputmodel )
{
if (inputmodel == null || notafiscalid != inputmodel.notaFiscalId)
return BadRequest();
if (!service.NotaFiscalExiste(notafiscalid))
return NotFound();
var model = ToDomainModel(inputmodel);
service.UpdateNotaFiscal(model);
return NoContent();

}
[HttpDelete("{notafiscalid}")]
public IActionResult Delete(int notafiscalid)
{
if (!service.NotaFiscalExiste(notafiscalid))
return NotFound();
service.DeletaNotaFiscal(notafiscalid);
return NoContent();
}
//Mapeamentos: modelo de envia/receber dados via API
private NFOutput ToOutPutModel(NotaFiscal model)
{
return new NFOutput
{
notaFiscalId = model.notaFiscalId,
numeroNf = model.numeroNf,
cnpjEmissorNf = model.cnpjEmissorNf,
valorTotal = model.valorTotal,
dataNf = model.dataNf,
cnpjDestinatarioNf= model.cnpjDestinatarioNf
};
}
private List<NFOutput> ToOutputModelList(List<NotaFiscal> model)
{
return model.Select(item => ToOutPutModel(item)).ToList();
}
private NotaFiscal ToDomainModel(NFInput inputModel)
{
return new NotaFiscal
{
notaFiscalId = inputModel.notaFiscalId,
numeroNf = inputModel.numeroNf,
dataNf = inputModel.dataNf,
valorTotal = inputModel.valorTotal,
cnpjDestinatarioNf = inputModel.cnpjDestinatarioNf,
cnpjEmissorNf = inputModel.cnpjEmissorNf
};
}
private NFInput ToInputModel(NotaFiscal model)
{
return new NFInput
{
notaFiscalId = model.notaFiscalId,
numeroNf = model.numeroNf,
dataNf = model.dataNf,
valorTotal = model.valorTotal,
cnpjDestinatarioNf = model.cnpjDestinatarioNf,
cnpjEmissorNf = model.cnpjEmissorNf
};
}
}
}

15 changes: 15 additions & 0 deletions CrudWebAPIAspCore/CrudWebAPIAspCore/CrudWebAPIAspCore.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DebuggerFlavor>ProjectDebugger</DebuggerFlavor>
</PropertyGroup>
<PropertyGroup>
<ActiveDebugProfile>IIS Express</ActiveDebugProfile>
</PropertyGroup>
</Project>
17 changes: 17 additions & 0 deletions CrudWebAPIAspCore/CrudWebAPIAspCore/Model/NFInput.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CrudWebAPIAspCore.Model
{
public class NFInput
{
public int notaFiscalId { get; set; }
public int numeroNf { get; set; }
public float valorTotal { get; set; }
public DateTime dataNf { get; set; }
public string cnpjEmissorNf { get; set; }
public string cnpjDestinatarioNf { get; set; }
}
}
17 changes: 17 additions & 0 deletions CrudWebAPIAspCore/CrudWebAPIAspCore/Model/NFOutput.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CrudWebAPIAspCore.Model
{
public class NFOutput
{
public int notaFiscalId { get; set; }
public int numeroNf { get; set; }
public float valorTotal { get; set; }
public DateTime dataNf { get; set; }
public string cnpjEmissorNf { get; set; }
public string cnpjDestinatarioNf { get; set; }
}
}
18 changes: 18 additions & 0 deletions CrudWebAPIAspCore/CrudWebAPIAspCore/Model/NotaFiscal.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CrudWebAPIAspCore.Model
{
public class NotaFiscal
{
public int notaFiscalId { get; set; }
public int numeroNf { get; set; }
public float valorTotal { get; set; }
public DateTime dataNf { get; set; }
public string cnpjEmissorNf { get; set; }
public string cnpjDestinatarioNf { get; set; }

}
}
24 changes: 24 additions & 0 deletions CrudWebAPIAspCore/CrudWebAPIAspCore/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace CrudWebAPIAspCore
{
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
}
27 changes: 27 additions & 0 deletions CrudWebAPIAspCore/CrudWebAPIAspCore/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:56857",
"sslPort": 44332
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"CrudWebAPIAspCore": {
"commandName": "Project",
"launchBrowser": true,
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
19 changes: 19 additions & 0 deletions CrudWebAPIAspCore/CrudWebAPIAspCore/Service/INFService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CrudWebAPIAspCore.Model;

namespace CrudWebAPIAspCore.Service
{
public interface INFService
{
List<NotaFiscal> GetNotaFiscal();
NotaFiscal GetNotaFiscal(int notafiscalid);
void AddNotaFiscal(NotaFiscal item);
void UpdateNotaFiscal(NotaFiscal item);
void DeletaNotaFiscal(int notafiscalid);
bool NotaFiscalExiste(int notafiscalid);

}
}
53 changes: 53 additions & 0 deletions CrudWebAPIAspCore/CrudWebAPIAspCore/Service/NFService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CrudWebAPIAspCore.Model;



namespace CrudWebAPIAspCore.Service
{
public class NFService: INFService
{
private readonly List<NotaFiscal> notaFiscals;
public NFService()
{
this.notaFiscals = new List<NotaFiscal>
{
new NotaFiscal{notaFiscalId= 1, numeroNf = 1, dataNf = DateTime.Today.AddDays(1), valorTotal = 310.00f, cnpjDestinatarioNf = "30.126.492/0001-27", cnpjEmissorNf = "05.918.500/0001-20"},
new NotaFiscal{notaFiscalId= 2, numeroNf = 2, dataNf = DateTime.Today.AddDays(2), valorTotal = 311.00f, cnpjDestinatarioNf = "30.126.492/0001-27", cnpjEmissorNf = "05.918.500/0001-20"},
new NotaFiscal{notaFiscalId= 3, numeroNf = 3, dataNf = DateTime.Today.AddDays(3), valorTotal = 312.00f, cnpjDestinatarioNf = "30.126.492/0001-27", cnpjEmissorNf = "05.918.500/0001-20"} };
}
public void AddNotaFiscal(NotaFiscal item) {
this.notaFiscals.Add(item);
}
public void DeletaNotaFiscal(int notafiscalid)
{
var model = this.notaFiscals.Where(m => m.notaFiscalId == notafiscalid).FirstOrDefault();
this.notaFiscals.Remove(model);
}
public bool NotaFiscalExiste(int notafiscalid)
{
return this.notaFiscals.Any(m => m.notaFiscalId == notafiscalid);
}
public NotaFiscal GetNotaFiscal(int notafiscalid)
{
return this.notaFiscals.Where(m => m.notaFiscalId == notafiscalid).FirstOrDefault();
}
public List<NotaFiscal> GetNotaFiscal()
{
return this.notaFiscals.ToList();
}
public void UpdateNotaFiscal(NotaFiscal item)
{
var model = this.notaFiscals.Where(m => m.notaFiscalId == item.notaFiscalId).FirstOrDefault();
model.dataNf = item.dataNf;
model.cnpjDestinatarioNf = item.cnpjDestinatarioNf;
model.cnpjEmissorNf = model.cnpjEmissorNf;
model.valorTotal = model.valorTotal;
model.numeroNf = model.numeroNf;
}
}
}

49 changes: 49 additions & 0 deletions CrudWebAPIAspCore/CrudWebAPIAspCore/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using CrudWebAPIAspCore.Model;
using CrudWebAPIAspCore.Service;
using Microsoft.AspNetCore.Diagnostics;

namespace CrudWebAPIAspCore
{
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<INFService, NFService>();
services.AddMvc();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

app.UseExceptionHandler(configure =>
{
configure.Run(async context =>
{
var ex = context.Features
.Get<IExceptionHandlerFeature>()
.Error;

context.Response.StatusCode = 500;
await context.Response.WriteAsync($"{ex.Message}");
});
});
app.UseMvcWithDefaultRoute();

}
}
}
Loading