diff --git a/Application/App/NotaFiscalApp.cs b/Application/App/NotaFiscalApp.cs new file mode 100644 index 0000000..7005561 --- /dev/null +++ b/Application/App/NotaFiscalApp.cs @@ -0,0 +1,55 @@ +using System.Collections.Generic; +using Application.Interfaces; +using Application.Model; +using AutoMapper; +using Domain.Entities; +using Domain.Interfaces.Services; + +namespace Application.App +{ + public class NotaFiscalApp : INotaFiscalApp + { + private readonly INotaFiscalService _notaFiscalService; + + public NotaFiscalApp(INotaFiscalService notaFiscalService) + { + this._notaFiscalService = notaFiscalService; + } + + public IEnumerable FindAll() + { + return Mapper.Map>(this._notaFiscalService.FindAll()); + } + + public NotaFiscalVW FindById(int id) + { + return Mapper.Map(this._notaFiscalService.FindById(id)); ; + } + + public NotaFiscalVW FindByNumeroNF(long numNF) + { + return Mapper.Map(this._notaFiscalService.FindByNumeroNF(numNF)); ; + } + + public void Persist(NotaFiscalVW notaFiscal) + { + NotaFiscal lNotaFiscal = Mapper.Map(notaFiscal); + + this._notaFiscalService.Persist(lNotaFiscal); + } + + + public void Update(NotaFiscalVW notaFiscal) + { + NotaFiscal lNotaFiscal = Mapper.Map(notaFiscal); + + this._notaFiscalService.Update(lNotaFiscal); + } + + public void Remove(int id) + { + this._notaFiscalService.Remove(id); + } + + } +} diff --git a/Application/Application.csproj b/Application/Application.csproj index 08f729f..6b09401 100644 --- a/Application/Application.csproj +++ b/Application/Application.csproj @@ -40,7 +40,12 @@ + + + + + @@ -56,11 +61,6 @@ Domain - - - - - - + \ No newline at end of file diff --git a/Application/AutoMapper/AutoMapperConfig.cs b/Application/AutoMapper/AutoMapperConfig.cs index 27f0f07..88a1514 100644 --- a/Application/AutoMapper/AutoMapperConfig.cs +++ b/Application/AutoMapper/AutoMapperConfig.cs @@ -1,15 +1,23 @@ -using AutoMapper; +using Application.AutoMapper.MappingProfile; +using AutoMapper; namespace Application.AutoMapper { public static class AutoMapperConfig { + public static Mapper _mapper; + public static void RegisterMappings() { Mapper.Initialize(x => { - + x.AddProfile(); + x.AddProfile(); }); } + + public static void Reset() { + Mapper.Reset(); + } } } diff --git a/Application/AutoMapper/MappingProfile/DomainToViewModelMappingProfile.cs b/Application/AutoMapper/MappingProfile/DomainToViewModelMappingProfile.cs new file mode 100644 index 0000000..74c87e9 --- /dev/null +++ b/Application/AutoMapper/MappingProfile/DomainToViewModelMappingProfile.cs @@ -0,0 +1,15 @@ +using Application.Model; +using AutoMapper; +using Domain.Entities; + +namespace Application.AutoMapper.MappingProfile +{ + public class DomainToViewModelMappingProfile : Profile + { + public DomainToViewModelMappingProfile() : base("DomainToViewModelMappings") + { + CreateMap(); + } + + } +} diff --git a/Application/AutoMapper/MappingProfile/ViewModelToDomainMappingProfile.cs b/Application/AutoMapper/MappingProfile/ViewModelToDomainMappingProfile.cs new file mode 100644 index 0000000..9c27a02 --- /dev/null +++ b/Application/AutoMapper/MappingProfile/ViewModelToDomainMappingProfile.cs @@ -0,0 +1,14 @@ +using Application.Model; +using AutoMapper; +using Domain.Entities; + +namespace Application.AutoMapper.MappingProfile +{ + public class ViewModelToDomainMappingProfile : Profile + { + public ViewModelToDomainMappingProfile() : base("ViewModelToDomainMappings") + { + CreateMap(); + } + } +} diff --git a/Application/Interfaces/INotaFiscalApp.cs b/Application/Interfaces/INotaFiscalApp.cs new file mode 100644 index 0000000..9b0cd5c --- /dev/null +++ b/Application/Interfaces/INotaFiscalApp.cs @@ -0,0 +1,20 @@ +using Application.Model; +using System.Collections.Generic; + +namespace Application.Interfaces +{ + public interface INotaFiscalApp + { + void Persist(NotaFiscalVW notaFiscal); + + NotaFiscalVW FindById(int id); + + NotaFiscalVW FindByNumeroNF(long numNF); + + IEnumerable FindAll(); + + void Update(NotaFiscalVW notaFiscal); + + void Remove(int id); + } +} diff --git a/Application/Model/NotaFiscalVW.cs b/Application/Model/NotaFiscalVW.cs new file mode 100644 index 0000000..9011659 --- /dev/null +++ b/Application/Model/NotaFiscalVW.cs @@ -0,0 +1,14 @@ +using System; + +namespace Application.Model +{ + public class NotaFiscalVW + { + public int notaFiscalId { get; set; } + public long numeroNf { get; set; } + public decimal valorTotal { get; set; } + public DateTime dataNf { get; set; } + public long cnpjEmissorNf { get; set; } + public long cnpjDestinatarioNf { get; set; } + } +} diff --git a/Data/Context/SystemContext.cs b/Data/Context/SystemContext.cs index dc1a84c..e20059d 100644 --- a/Data/Context/SystemContext.cs +++ b/Data/Context/SystemContext.cs @@ -1,20 +1,22 @@  namespace Data.Context { - using System; + using Domain.Entities; using System.Data.Entity; using System.Data.Entity.ModelConfiguration.Conventions; - using System.Web; public class SystemContext : DbContext { + public SystemContext() : base("DevPartner") + { + + } public static SystemContext Create() { return new SystemContext(); } - protected override void OnModelCreating(DbModelBuilder modelBuilder) { @@ -22,9 +24,10 @@ protected override void OnModelCreating(DbModelBuilder modelBuilder) modelBuilder.Conventions.Remove(); modelBuilder.Conventions.Remove(); - base.OnModelCreating(modelBuilder); } + public virtual DbSet NotaFiscal { get; set; } + } } diff --git a/Data/Data.csproj b/Data/Data.csproj index 1b2810f..7fad711 100644 --- a/Data/Data.csproj +++ b/Data/Data.csproj @@ -33,6 +33,7 @@ + @@ -64,7 +65,6 @@ - \ No newline at end of file diff --git a/Data/Migrations/Configuration.cs b/Data/Migrations/Configuration.cs index e2521e0..3072005 100644 --- a/Data/Migrations/Configuration.cs +++ b/Data/Migrations/Configuration.cs @@ -1,15 +1,12 @@ namespace Data.Migrations { - using System; - using System.Data.Entity; using System.Data.Entity.Migrations; - using System.Linq; internal sealed class Configuration : DbMigrationsConfiguration { public Configuration() { - AutomaticMigrationsEnabled = false; + AutomaticMigrationsEnabled = true; } protected override void Seed(Data.Context.SystemContext context) diff --git a/Data/Repositories/NotaFiscalRepository.cs b/Data/Repositories/NotaFiscalRepository.cs new file mode 100644 index 0000000..59e529e --- /dev/null +++ b/Data/Repositories/NotaFiscalRepository.cs @@ -0,0 +1,54 @@ +using Data.Context; +using System.Collections.Generic; +using System.Data.Entity; +using System.Linq; + +using Domain.Entities; +using Domain.Interfaces.Repository; + +namespace Data.Repositories +{ + public class NotaFiscalRepository : INotaFiscalRepository + { + private SystemContext _context = new SystemContext(); + + public void Persist(NotaFiscal notaFiscal) + { + _context.NotaFiscal.Add(notaFiscal); + _context.SaveChanges(); + } + + public NotaFiscal FindById(int id) + { + return _context.NotaFiscal.Find(id); + } + + public NotaFiscal FindByNumeroNF(long numNF) { + return _context.NotaFiscal + .Where(nf=> nf.numeroNf == numNF) + .FirstOrDefault(); + } + + public IEnumerable FindAll() + { + IEnumerable NotasFiscais = _context.NotaFiscal; + return NotasFiscais; + } + + public void Update(NotaFiscal notaFiscal) + { + _context.Entry(notaFiscal).State = EntityState.Modified; + _context.SaveChanges(); + } + + public void Remove(int id) + { + NotaFiscal lNotaFiscal = new NotaFiscal() { notaFiscalId = id }; + _context.NotaFiscal.Attach(lNotaFiscal); + _context.Entry(lNotaFiscal).State = EntityState.Deleted; + _context.SaveChanges(); + + } + + } +} diff --git a/Domain/Domain.csproj b/Domain/Domain.csproj index 7b9594f..0beef22 100644 --- a/Domain/Domain.csproj +++ b/Domain/Domain.csproj @@ -52,14 +52,14 @@ + + + + - - - - diff --git a/Domain/Entities/NotaFiscal.cs b/Domain/Entities/NotaFiscal.cs new file mode 100644 index 0000000..0aabc67 --- /dev/null +++ b/Domain/Entities/NotaFiscal.cs @@ -0,0 +1,20 @@ +using System; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace Domain.Entities +{ + public class NotaFiscal + { + [Key] + public int notaFiscalId {get;set;} + + [Index("IX_NumeroNf", 1, IsUnique = true)] + public long numeroNf {get;set;} + public decimal valorTotal {get;set;} + public DateTime dataNf {get;set;} + public long cnpjEmissorNf {get;set;} + public long cnpjDestinatarioNf {get;set;} + + } +} diff --git a/Domain/Interfaces/Repository/INotaFiscalRepository.cs b/Domain/Interfaces/Repository/INotaFiscalRepository.cs new file mode 100644 index 0000000..998c1ca --- /dev/null +++ b/Domain/Interfaces/Repository/INotaFiscalRepository.cs @@ -0,0 +1,20 @@ +using Domain.Entities; +using System.Collections.Generic; + +namespace Domain.Interfaces.Repository +{ + public interface INotaFiscalRepository + { + void Persist(NotaFiscal notaFiscal); + + IEnumerable FindAll(); + + NotaFiscal FindById(int id); + + NotaFiscal FindByNumeroNF(long numNF); + + void Update(NotaFiscal notaFiscal); + + void Remove(int id); + } +} diff --git a/Domain/Interfaces/Services/INotaFiscalService.cs b/Domain/Interfaces/Services/INotaFiscalService.cs new file mode 100644 index 0000000..df3b130 --- /dev/null +++ b/Domain/Interfaces/Services/INotaFiscalService.cs @@ -0,0 +1,20 @@ +using Domain.Entities; +using System.Collections.Generic; + +namespace Domain.Interfaces.Services +{ + public interface INotaFiscalService + { + void Persist(NotaFiscal notaFiscal); + + IEnumerable FindAll(); + + NotaFiscal FindById(int id); + + NotaFiscal FindByNumeroNF(long numNF); + + void Update(NotaFiscal notaFiscal); + + void Remove(int id); + } +} diff --git a/Domain/Services/NotaFiscalService.cs b/Domain/Services/NotaFiscalService.cs new file mode 100644 index 0000000..427e3da --- /dev/null +++ b/Domain/Services/NotaFiscalService.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using Domain.Entities; +using Domain.Interfaces.Repository; +using Domain.Interfaces.Services; + +namespace Domain.Services +{ + public class NotaFiscalService : INotaFiscalService + { + private readonly INotaFiscalRepository _notaFiscalRepository; + + public NotaFiscalService(INotaFiscalRepository notaFiscalRepository) + { + this._notaFiscalRepository = notaFiscalRepository; + } + + public IEnumerable FindAll() + { + return this._notaFiscalRepository.FindAll(); + } + + public NotaFiscal FindById(int id) + { + return this._notaFiscalRepository.FindById(id); + } + + public NotaFiscal FindByNumeroNF(long numNF) { + return this._notaFiscalRepository.FindByNumeroNF(numNF); + } + + public void Persist(NotaFiscal notaFiscal) + { + NotaFiscal lNotaFiscal = this._notaFiscalRepository.FindByNumeroNF(notaFiscal.numeroNf); + + if (lNotaFiscal != null) + throw new Exception("Nota fiscal já cadastrada no sistema!"); + + this._notaFiscalRepository.Persist(notaFiscal); + } + + + public void Update(NotaFiscal notaFiscal) + { + this._notaFiscalRepository.Update(notaFiscal); + } + + public void Remove(int id) + { + this._notaFiscalRepository.Remove(id); + } + + + + } +} diff --git a/IoC/BootStrapper.cs b/IoC/BootStrapper.cs index 3d92096..d9d1cc5 100644 --- a/IoC/BootStrapper.cs +++ b/IoC/BootStrapper.cs @@ -1,4 +1,10 @@ -using SimpleInjector; +using Application.App; +using Application.Interfaces; +using Data.Repositories; +using Domain.Interfaces.Repository; +using Domain.Interfaces.Services; +using Domain.Services; +using SimpleInjector; namespace Devpartner.Infra.CrossCutting.IoC { @@ -7,6 +13,17 @@ public static class BootStrapper public static void RegisterServices(Container container) { + #region Application + container.Register(); + #endregion + + #region Domain Services + container.Register(); + #endregion + + #region infrastructure Repositories + container.Register(); + #endregion } } diff --git a/UnitTest/Api/ApiTeste.cs b/UnitTest/Api/ApiTeste.cs new file mode 100644 index 0000000..c0d14e1 --- /dev/null +++ b/UnitTest/Api/ApiTeste.cs @@ -0,0 +1,192 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Moq; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web.Http.Results; + +using Application.Interfaces; +using Application.Model; +using WebAPI.Areas.NotaFiscal.Controllers; +using System.Net.Http; +using System.Web.Http; +using System.Net; + +namespace UnitTest.Api +{ + [TestClass] + public class ApiTeste + { + + #region Unit Test Methods + [TestMethod] + public void ObterTodos() + { + Mock lMock = new Mock(); + + lMock.Setup(x => x.FindAll()).Returns(new List { new NotaFiscalVW { cnpjDestinatarioNf = 55555 } }); + + var lNotaFiscalController = this.CreateController(lMock); + + var response = lNotaFiscalController.ObterTodos(); + + // Asserts + Assert.IsNotNull(response); + + Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); + + var lNotasFiscais = this.GetObjectContent>(response); + + Assert.IsNotNull(lNotasFiscais); + + Assert.AreEqual(1, lNotasFiscais.Count); + } + + [TestMethod] + public void ObterPorId() + { + Mock lMock = new Mock(); + + int id = 55555; + int numNF = 123; + + lMock.Setup(x => x.FindById(id)).Returns(new NotaFiscalVW { notaFiscalId = id, numeroNf = numNF}); + + var lNotaFiscalController = this.CreateController(lMock); + + var response = lNotaFiscalController.ObterPorId(id); + + // Asserts + Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); + + var lNotaFiscal = this.GetObjectContent(response); + + Assert.IsNotNull(lNotaFiscal); + + Assert.AreEqual(numNF, lNotaFiscal.numeroNf); + } + + [TestMethod] + public void ObterPorNumeroNF() + { + Mock lMock = new Mock(); + + int id = 13432; + long numNF = 123; + decimal valor = 1050; + + lMock.Setup(x => x.FindByNumeroNF(numNF)).Returns(new NotaFiscalVW {dataNf = DateTime.Now, notaFiscalId = id, valorTotal = valor, numeroNf = numNF }); + + var lNotaFiscalController = this.CreateController(lMock); + + var response = lNotaFiscalController.ObterPorNumeroNF(numNF); + + // Asserts + Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); + + var lNotaFiscal = this.GetObjectContent(response); + + Assert.IsNotNull(lNotaFiscal); + + Assert.AreEqual(numNF, lNotaFiscal.numeroNf); + } + + [TestMethod] + public void Salvar() + { + Mock lMock = new Mock(); + + var lNotaFiscal = new NotaFiscalVW + { + dataNf = DateTime.Now, + numeroNf = 9999999, + cnpjEmissorNf = 1234, + cnpjDestinatarioNf = 5678, + valorTotal = 100.00M + }; + + lMock.Setup(x => x.Persist(lNotaFiscal)); + + var notaFiscalController = this.CreateController(lMock); + + var response = notaFiscalController.Salvar(lNotaFiscal); + + // Asserts + Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); + } + + [TestMethod] + public void Atualizar() + { + Mock lMock = new Mock(); + + var lNotaFiscal = new NotaFiscalVW + { + notaFiscalId = 55555, + dataNf = DateTime.Now, + numeroNf = 9999999, + cnpjEmissorNf = 1234, + cnpjDestinatarioNf = 5678, + valorTotal = 100.00M + }; + + lMock.Setup(x => x.Update(lNotaFiscal)); + + var notaFiscalController = this.CreateController(lMock); + + var response = notaFiscalController.Atualizar(lNotaFiscal); + + // Asserts + Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); + } + + [TestMethod] + public void Remover() + { + Mock lMock = new Mock(); + + int id = 5555; + + lMock.Setup(x => x.Remove(id)); + + var notaFiscalController = this.CreateController(lMock); + + var response = notaFiscalController.Remover(id); + + // Asserts + Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); + } + #endregion + + #region Utilities Methods + + /// + /// Devolve uma instancia do controllador NotaFiscalController + /// + public NotaFiscalController CreateController(Mock mock) { + + var lNotaFiscalController = new NotaFiscalController(mock.Object) + { + Request = new HttpRequestMessage(), + Configuration = new HttpConfiguration() + }; + + return lNotaFiscalController; + } + + /// + /// Extrai o conteúdo da resposta no tipo Definido + /// + /// Tipo esperado do conteudo da resposta + /// + /// TDestino + public TDestino GetObjectContent(HttpResponseMessage response) where TDestino : class { + + var retorno = (( response.Content as ObjectContent ).Value as TDestino); + + return retorno; + } + #endregion + + } +} diff --git a/UnitTest/Application/ApplicationTeste.cs b/UnitTest/Application/ApplicationTeste.cs new file mode 100644 index 0000000..0290ccb --- /dev/null +++ b/UnitTest/Application/ApplicationTeste.cs @@ -0,0 +1,179 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Application.App; +using Application.AutoMapper; +using Application.Model; +using Domain.Entities; +using Domain.Interfaces.Services; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Moq; + +namespace UnitTest.Application +{ + [TestClass] + public class ApplicationTeste + { + #region Unit Test Methods + [TestMethod] + public void ObterTodos() + { + AutoMapperConfig.Reset(); + AutoMapperConfig.RegisterMappings(); + + Mock lMock = new Mock(); + + lMock.Setup(x => x.FindAll()).Returns(new List { new NotaFiscal { cnpjDestinatarioNf = 55555 } }); + + var notaFiscalApp = new NotaFiscalApp(lMock.Object); + + var lNotasFiscais = notaFiscalApp.FindAll(); + + Assert.IsNotNull(lNotasFiscais); + Assert.AreEqual(1, lNotasFiscais.ToList().Count); + } + + [TestMethod] + public void ObterPorId() + { + AutoMapperConfig.Reset(); + AutoMapperConfig.RegisterMappings(); + + Mock lMock = new Mock(); + + int id = 55555; + long numNF = 123; + + lMock.Setup(x => x.FindById(id)).Returns(new NotaFiscal { notaFiscalId = id, numeroNf = numNF, dataNf = DateTime.Now }); + + var lNotaFiscalApp = new NotaFiscalApp(lMock.Object); + + var lNotaFiscal = lNotaFiscalApp.FindById(id); + + Assert.IsNotNull(lNotaFiscal); + + Assert.AreEqual(numNF, lNotaFiscal.numeroNf); + + } + + [TestMethod] + public void ObterPorNumeroNF() + { + AutoMapperConfig.Reset(); + AutoMapperConfig.RegisterMappings(); + + Mock lMock = new Mock(); + + int id = 13432; + long numNF = 123; + decimal valor = 1050; + + lMock.Setup(x => x.FindByNumeroNF(numNF)).Returns(new NotaFiscal { dataNf = DateTime.Now, notaFiscalId = id, valorTotal = valor, numeroNf = numNF }); + + var lNotaFiscalApp = new NotaFiscalApp(lMock.Object); + + var lNotaFiscal = lNotaFiscalApp.FindByNumeroNF(numNF); + + Assert.IsNotNull(lNotaFiscal); + + Assert.AreEqual(numNF, lNotaFiscal.numeroNf); + + } + + [TestMethod] + public void Salvar() + { + AutoMapperConfig.Reset(); + AutoMapperConfig.RegisterMappings(); + + Mock lMock = new Mock(); + + // VW + var lNotaFiscalVW = new NotaFiscalVW + { + dataNf = DateTime.Now, + numeroNf = 9999999, + cnpjEmissorNf = 1234, + cnpjDestinatarioNf = 5678, + valorTotal = 100.00M + }; + + // Entity + var lNotaFiscal = new NotaFiscal + { + dataNf = DateTime.Now, + numeroNf = 9999999, + cnpjEmissorNf = 1234, + cnpjDestinatarioNf = 5678, + valorTotal = 100.00M + }; + + lMock.Setup(x => x.Persist(lNotaFiscal)); + + var lNotaFiscalApp = new NotaFiscalApp(lMock.Object); + + lNotaFiscalApp.Persist(lNotaFiscalVW); + + } + + [TestMethod] + public void Atualizar() + { + AutoMapperConfig.Reset(); + AutoMapperConfig.RegisterMappings(); + + Mock lMock = new Mock(); + + // VW + var lNotaFiscalVW = new NotaFiscalVW + { + dataNf = DateTime.Now, + numeroNf = 9999999, + cnpjEmissorNf = 1234, + cnpjDestinatarioNf = 5678, + valorTotal = 100.00M + }; + + // Entity + var lNotaFiscal = new NotaFiscal + { + dataNf = DateTime.Now, + numeroNf = 9999999, + cnpjEmissorNf = 1234, + cnpjDestinatarioNf = 5678, + valorTotal = 100.00M + }; + + lMock.Setup(x => x.Update(lNotaFiscal)); + + var lNotaFiscalApp = new NotaFiscalApp(lMock.Object); + + lNotaFiscalApp.Update(lNotaFiscalVW); + + } + + [TestMethod] + public void Remover() + { + AutoMapperConfig.Reset(); + AutoMapperConfig.RegisterMappings(); + + Mock lMock = new Mock(); + + int id = 5555; + + lMock.Setup(x => x.Remove(id)); + + var lNotaFiscalApp = new NotaFiscalApp(lMock.Object); + + lNotaFiscalApp.Remove(id); + } + #endregion + + #region Utilities Methods + + + #endregion + + } +} diff --git a/UnitTest/Domain/DomainTeste.cs b/UnitTest/Domain/DomainTeste.cs new file mode 100644 index 0000000..59095d2 --- /dev/null +++ b/UnitTest/Domain/DomainTeste.cs @@ -0,0 +1,140 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Domain.Entities; +using Domain.Interfaces.Repository; +using Domain.Interfaces.Services; +using Domain.Services; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Moq; + +namespace UnitTest.Domain +{ + [TestClass] + public class DomainTeste + { + #region Unit Test Methods + [TestMethod] + public void ObterTodos() + { + + Mock lMock = new Mock(); + + lMock.Setup(x => x.FindAll()).Returns(new List { new NotaFiscal { cnpjDestinatarioNf = 55555 } }); + + var lNotaFiscalService = new NotaFiscalService(lMock.Object); + + var lNotasFiscais = lNotaFiscalService.FindAll(); + + Assert.IsNotNull(lNotasFiscais); + Assert.AreEqual(1, lNotasFiscais.ToList().Count); + } + + [TestMethod] + public void ObterPorId() + { + Mock lMock = new Mock(); + + int id = 55555; + long numNF = 123; + + lMock.Setup(x => x.FindById(id)).Returns(new NotaFiscal { notaFiscalId = id, numeroNf = numNF, dataNf = DateTime.Now }); + + var lNotaFiscalApp = new NotaFiscalService(lMock.Object); + + var lNotaFiscal = lNotaFiscalApp.FindById(id); + + Assert.IsNotNull(lNotaFiscal); + + Assert.AreEqual(numNF, lNotaFiscal.numeroNf); + + } + + [TestMethod] + public void ObterPorNumeroNF() + { + Mock lMock = new Mock(); + + int id = 13432; + long numNF = 123; + decimal valor = 1050; + + lMock.Setup(x => x.FindByNumeroNF(numNF)).Returns(new NotaFiscal { dataNf = DateTime.Now, notaFiscalId = id, valorTotal = valor, numeroNf = numNF }); + + var lNotaFiscalApp = new NotaFiscalService(lMock.Object); + + var lNotaFiscal = lNotaFiscalApp.FindByNumeroNF(numNF); + + Assert.IsNotNull(lNotaFiscal); + + Assert.AreEqual(numNF, lNotaFiscal.numeroNf); + + } + + [TestMethod] + public void Salvar() + { + Mock lMock = new Mock(); + + // Entity + var lNotaFiscal = new NotaFiscal + { + dataNf = DateTime.Now, + numeroNf = 9999999, + cnpjEmissorNf = 1234, + cnpjDestinatarioNf = 5678, + valorTotal = 100.00M + }; + + lMock.Setup(x => x.Persist(lNotaFiscal)); + + var lNotaFiscalApp = new NotaFiscalService(lMock.Object); + + lNotaFiscalApp.Persist(lNotaFiscal); + + } + + [TestMethod] + public void Atualizar() + { + Mock lMock = new Mock(); + + // Entity + var lNotaFiscal = new NotaFiscal + { + dataNf = DateTime.Now, + numeroNf = 9999999, + cnpjEmissorNf = 1234, + cnpjDestinatarioNf = 5678, + valorTotal = 100.00M + }; + + lMock.Setup(x => x.Update(lNotaFiscal)); + + var lNotaFiscalApp = new NotaFiscalService(lMock.Object); + + lNotaFiscalApp.Update(lNotaFiscal); + + } + + [TestMethod] + public void Remover() + { + Mock lMock = new Mock(); + + int id = 5555; + + lMock.Setup(x => x.Remove(id)); + + var lNotaFiscalApp = new NotaFiscalService(lMock.Object); + + lNotaFiscalApp.Remove(id); + } + #endregion + + #region Utilities Methods + + + #endregion + } +} diff --git a/UnitTest/Properties/AssemblyInfo.cs b/UnitTest/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..21ff5d7 --- /dev/null +++ b/UnitTest/Properties/AssemblyInfo.cs @@ -0,0 +1,20 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +[assembly: AssemblyTitle("UnitTest")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("UnitTest")] +[assembly: AssemblyCopyright("Copyright © 2019")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +[assembly: ComVisible(false)] + +[assembly: Guid("ea519ce6-be7c-4c81-b212-c7a2afe074f2")] + +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/UnitTest/UnitTest.csproj b/UnitTest/UnitTest.csproj new file mode 100644 index 0000000..187e5bf --- /dev/null +++ b/UnitTest/UnitTest.csproj @@ -0,0 +1,120 @@ + + + + + Debug + AnyCPU + {EA519CE6-BE7C-4C81-B212-C7A2AFE074F2} + Library + Properties + UnitTest + UnitTest + v4.6.1 + 512 + {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + 15.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + $(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages + False + UnitTest + + + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\Castle.Core.4.3.1\lib\net45\Castle.Core.dll + + + ..\packages\EntityFramework.6.2.0\lib\net45\EntityFramework.dll + + + ..\packages\EntityFramework.6.2.0\lib\net45\EntityFramework.SqlServer.dll + + + ..\packages\MSTest.TestFramework.1.2.0\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll + + + ..\packages\MSTest.TestFramework.1.2.0\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll + + + ..\packages\Moq.4.10.1\lib\net45\Moq.dll + + + ..\packages\Newtonsoft.Json.6.0.4\lib\net45\Newtonsoft.Json.dll + True + + + + + + + + ..\packages\Microsoft.AspNet.WebApi.Client.5.2.7\lib\net45\System.Net.Http.Formatting.dll + + + ..\packages\System.Runtime.CompilerServices.Unsafe.4.5.0\lib\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll + + + ..\packages\System.Threading.Tasks.Extensions.4.5.1\lib\netstandard2.0\System.Threading.Tasks.Extensions.dll + + + ..\packages\Microsoft.AspNet.WebApi.Core.5.2.7\lib\net45\System.Web.Http.dll + + + + + + + + + + + + + + + + {2EB3B0BA-909B-4030-8B2D-6C331B237B34} + Application + + + {06E38496-787B-47D1-AA8B-0D5287F3BB9A} + Data + + + {CDFD9BFE-B279-478E-8783-E21E00734A9B} + Domain + + + {93FEB38B-B8F3-4F01-8022-AAA843C524B9} + WebAPI + + + + + + + Este projeto faz referência a pacotes do NuGet que não estão presentes neste computador. Use a Restauração de Pacotes do NuGet para baixá-los. Para obter mais informações, consulte http://go.microsoft.com/fwlink/?LinkID=322105. O arquivo ausente é {0}. + + + + + + \ No newline at end of file diff --git a/UnitTest/app.config b/UnitTest/app.config new file mode 100644 index 0000000..8a647f5 --- /dev/null +++ b/UnitTest/app.config @@ -0,0 +1,61 @@ + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/UnitTest/packages.config b/UnitTest/packages.config new file mode 100644 index 0000000..4686f7f --- /dev/null +++ b/UnitTest/packages.config @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/WebAPI/App_Start/SwaggerConfig.cs b/WebAPI/App_Start/SwaggerConfig.cs index c927e6b..0cf1daf 100644 --- a/WebAPI/App_Start/SwaggerConfig.cs +++ b/WebAPI/App_Start/SwaggerConfig.cs @@ -32,8 +32,8 @@ public static void Register() // hold additional metadata for an API. Version and title are required but you can also provide // additional fields by chaining methods off SingleApiVersion. // - c.SingleApiVersion("v1", "WebAPI"); - + c.SingleApiVersion("v1", "Documentação"); + c.IncludeXmlComments(string.Format(@"{0}\bin\DevPartner-Documentation.xml", System.AppDomain.CurrentDomain.BaseDirectory)); // If you want the output Swagger docs to be indented properly, enable the "PrettyPrint" option. // //c.PrettyPrint(); diff --git a/WebAPI/App_Start/WebApiConfig.cs b/WebAPI/App_Start/WebApiConfig.cs index 5cb6c51..ce8cbab 100644 --- a/WebAPI/App_Start/WebApiConfig.cs +++ b/WebAPI/App_Start/WebApiConfig.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Net.Http; -using System.Web.Http; -using Microsoft.Owin.Security.OAuth; -using Newtonsoft.Json.Serialization; +using System.Web.Http; namespace WebAPI { diff --git a/WebAPI/Areas/NotaFiscal/Controllers/NotaFiscalController.cs b/WebAPI/Areas/NotaFiscal/Controllers/NotaFiscalController.cs new file mode 100644 index 0000000..044f28c --- /dev/null +++ b/WebAPI/Areas/NotaFiscal/Controllers/NotaFiscalController.cs @@ -0,0 +1,154 @@ +using Application.Interfaces; +using Application.Model; +using System; +using System.Net; +using System.Net.Http; +using System.Web.Http; + +namespace WebAPI.Areas.NotaFiscal.Controllers +{ + [RoutePrefix("api/notaFiscal")] + public class NotaFiscalController : ApiController + { + private readonly INotaFiscalApp _notaFiscalApp; + + public NotaFiscalController(INotaFiscalApp notaFiscalApp) + { + this._notaFiscalApp = notaFiscalApp; + } + + /// + /// Obtem Todas as Notas fiscais cadastradas no sistema + /// + /// IHttpActionResult + [HttpGet] + [Route("obterTodos")] + public HttpResponseMessage ObterTodos() + { + try + { + var result = this._notaFiscalApp.FindAll(); + + return Request.CreateResponse(HttpStatusCode.OK, result); + } + catch (Exception ex) + { + return Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message); + } + } + + /// + /// Obtem uma nota fiscal pelo id + /// + /// Id da nota que se deseja Obter + [HttpGet] + [Route("obterPorId")] + public HttpResponseMessage ObterPorId(int id) + { + try + { + var result = this._notaFiscalApp.FindById(id); + + return Request.CreateResponse(HttpStatusCode.OK, result); + + } + catch (Exception ex) + { + return Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message); + } + } + + /// + /// Obtem uma nota fiscal pelo Numero da Nota Fiscal + /// + /// Numero da nota fiscal que se deseja Obter + [HttpGet] + [Route("obterPorNumeroNF")] + public HttpResponseMessage ObterPorNumeroNF(long numero) + { + try + { + var lNotaFiscal = this._notaFiscalApp.FindByNumeroNF(numero); + + return Request.CreateResponse(HttpStatusCode.OK, lNotaFiscal); + + } + catch (Exception ex) + { + return Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message); + } + } + + /// + /// Persiste uma nota fiscal no banco + /// + /// Objeto de nota fiscal com os dados para serem persistidos no banco + [HttpPost] + [Route("salvar")] + public HttpResponseMessage Salvar([FromBody]NotaFiscalVW notaFiscal) + { + if (notaFiscal == null) + return Request.CreateResponse(HttpStatusCode.BadRequest); + + try + { + + this._notaFiscalApp.Persist(notaFiscal); + + return Request.CreateResponse(HttpStatusCode.OK); + + } + catch (Exception ex) + { + return Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message); + } + } + + /// + /// Atualiza os dados de uma nota fiscal + /// + /// Objeto de nota fiscal com os dados para serem atualizados no banco + [HttpPut] + [Route("atualizar")] + public HttpResponseMessage Atualizar([FromBody]NotaFiscalVW notaFiscal) + { + if (notaFiscal == null) + return Request.CreateResponse(HttpStatusCode.BadRequest); + + try + { + this._notaFiscalApp.Update(notaFiscal); + + return Request.CreateResponse(HttpStatusCode.OK); + + } + catch (Exception ex) + { + return Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message); + } + } + + /// + /// Remove os dados de uma nota fiscal do banco + /// + /// Id da nota que se deseja remover do banco + [HttpDelete] + [Route("remover")] + public HttpResponseMessage Remover(int id) + { + try + { + this._notaFiscalApp.Remove(id); + + return Request.CreateResponse(HttpStatusCode.OK); + + } + catch (Exception ex) + { + return Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message); + } + } + + + } +} \ No newline at end of file diff --git a/WebAPI/Areas/NotaFiscal/NotaFiscalAreaRegistration.cs b/WebAPI/Areas/NotaFiscal/NotaFiscalAreaRegistration.cs new file mode 100644 index 0000000..7635436 --- /dev/null +++ b/WebAPI/Areas/NotaFiscal/NotaFiscalAreaRegistration.cs @@ -0,0 +1,24 @@ +using System.Web.Mvc; + +namespace WebAPI.Areas.NotaFiscal +{ + public class NotaFiscalAreaRegistration : AreaRegistration + { + public override string AreaName + { + get + { + return "NotaFiscal"; + } + } + + public override void RegisterArea(AreaRegistrationContext context) + { + context.MapRoute( + "NotaFiscal_default", + "NotaFiscal/{controller}/{action}/{id}", + new { action = "Index", id = UrlParameter.Optional } + ); + } + } +} \ No newline at end of file diff --git a/WebAPI/Areas/NotaFiscal/Views/web.config b/WebAPI/Areas/NotaFiscal/Views/web.config new file mode 100644 index 0000000..7344282 --- /dev/null +++ b/WebAPI/Areas/NotaFiscal/Views/web.config @@ -0,0 +1,36 @@ + + + + + +
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/WebAPI/Web.config b/WebAPI/Web.config index 45a29ce..4ce987c 100644 --- a/WebAPI/Web.config +++ b/WebAPI/Web.config @@ -9,6 +9,7 @@
+ diff --git a/WebAPI/WebAPI.csproj b/WebAPI/WebAPI.csproj index 8eed6ac..a2547fc 100644 --- a/WebAPI/WebAPI.csproj +++ b/WebAPI/WebAPI.csproj @@ -37,6 +37,7 @@ DEBUG;TRACE prompt 4 + bin\DevPartner-Documentation.xml true @@ -236,6 +237,8 @@ + + Global.asax @@ -297,6 +300,7 @@ + @@ -346,6 +350,7 @@ + diff --git a/WebAPI/packages.config b/WebAPI/packages.config index 6466833..8af2511 100644 --- a/WebAPI/packages.config +++ b/WebAPI/packages.config @@ -25,9 +25,13 @@ + + + + diff --git a/devpartner.sln b/devpartner.sln index 67a4d5a..73c65c4 100644 --- a/devpartner.sln +++ b/devpartner.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 -VisualStudioVersion = 15.0.27130.2027 +VisualStudioVersion = 15.0.27130.2036 MinimumVisualStudioVersion = 10.0.40219.1 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "01-Presentation", "01-Presentation", "{E4953CD8-6195-4E4F-B085-B2D09D950858}" EndProject @@ -33,6 +33,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mapeamento", "Mapeamento\Ma EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebAPI", "WebAPI\WebAPI.csproj", "{93FEB38B-B8F3-4F01-8022-AAA843C524B9}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "08 - Unit Testes", "08 - Unit Testes", "{1847CD6C-BBFD-4E14-8886-E6307AC837FB}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTest", "UnitTest\UnitTest.csproj", "{EA519CE6-BE7C-4C81-B212-C7A2AFE074F2}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -67,6 +71,10 @@ Global {93FEB38B-B8F3-4F01-8022-AAA843C524B9}.Debug|Any CPU.Build.0 = Debug|Any CPU {93FEB38B-B8F3-4F01-8022-AAA843C524B9}.Release|Any CPU.ActiveCfg = Release|Any CPU {93FEB38B-B8F3-4F01-8022-AAA843C524B9}.Release|Any CPU.Build.0 = Release|Any CPU + {EA519CE6-BE7C-4C81-B212-C7A2AFE074F2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {EA519CE6-BE7C-4C81-B212-C7A2AFE074F2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {EA519CE6-BE7C-4C81-B212-C7A2AFE074F2}.Release|Any CPU.ActiveCfg = Release|Any CPU + {EA519CE6-BE7C-4C81-B212-C7A2AFE074F2}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -81,6 +89,7 @@ Global {C870B696-5CBF-448D-8E9D-8978A127AAF3} = {BE53D742-31F3-4FC2-84BB-1C6CB9C44234} {E921D13C-3F89-450E-8DDE-B4F4A7DACDA2} = {BE53D742-31F3-4FC2-84BB-1C6CB9C44234} {93FEB38B-B8F3-4F01-8022-AAA843C524B9} = {E4953CD8-6195-4E4F-B085-B2D09D950858} + {EA519CE6-BE7C-4C81-B212-C7A2AFE074F2} = {1847CD6C-BBFD-4E14-8886-E6307AC837FB} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {BE09984D-0B2B-4347-8E40-E6C71C52EBCA}