Skip to content

Commit

Permalink
Add project files.
Browse files Browse the repository at this point in the history
  • Loading branch information
Israel Aece authored and Israel Aece committed Jan 27, 2017
1 parent cec503f commit e223069
Show file tree
Hide file tree
Showing 24 changed files with 546 additions and 0 deletions.
39 changes: 39 additions & 0 deletions WebCommands.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26014.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{8E77195D-0B83-4FB8-80C1-A6B74B705C80}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebCommands", "src\WebCommands\WebCommands.csproj", "{E905C345-7636-47B5-A121-042AACE155A2}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{E905C345-7636-47B5-A121-042AACE155A2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E905C345-7636-47B5-A121-042AACE155A2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E905C345-7636-47B5-A121-042AACE155A2}.Debug|x64.ActiveCfg = Debug|x64
{E905C345-7636-47B5-A121-042AACE155A2}.Debug|x64.Build.0 = Debug|x64
{E905C345-7636-47B5-A121-042AACE155A2}.Debug|x86.ActiveCfg = Debug|x86
{E905C345-7636-47B5-A121-042AACE155A2}.Debug|x86.Build.0 = Debug|x86
{E905C345-7636-47B5-A121-042AACE155A2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E905C345-7636-47B5-A121-042AACE155A2}.Release|Any CPU.Build.0 = Release|Any CPU
{E905C345-7636-47B5-A121-042AACE155A2}.Release|x64.ActiveCfg = Release|x64
{E905C345-7636-47B5-A121-042AACE155A2}.Release|x64.Build.0 = Release|x64
{E905C345-7636-47B5-A121-042AACE155A2}.Release|x86.ActiveCfg = Release|x86
{E905C345-7636-47B5-A121-042AACE155A2}.Release|x86.Build.0 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{E905C345-7636-47B5-A121-042AACE155A2} = {8E77195D-0B83-4FB8-80C1-A6B74B705C80}
EndGlobalSection
EndGlobal
9 changes: 9 additions & 0 deletions src/WebCommands/Dominio/Cliente.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace WebCommands.Dominio
{
public class Cliente
{
public string Nome { get; set; }

public string Documento { get; set; }
}
}
28 changes: 28 additions & 0 deletions src/WebCommands/Dominio/Comandos/CancelarNotaFiscal.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using WebCommands.Dominio.Repositorios;
using WebCommands.Infrastructure.Commands;

namespace WebCommands.Dominio.Comandos
{
public class CancelarNotaFiscal : Command
{
public Guid Codigo { get; set; }
}

public class CancelarNotaFiscalHandler : IHandler<CancelarNotaFiscal>
{
private readonly IRepositorioDeNotasFiscais repositorioDeNotasFiscais;

public CancelarNotaFiscalHandler(IRepositorioDeNotasFiscais repositorioDeNotasFiscais)
{
this.repositorioDeNotasFiscais = repositorioDeNotasFiscais;
}

public void Handle(CancelarNotaFiscal command)
{
var nf = this.repositorioDeNotasFiscais.BuscarPor(command.Codigo.ToString());

nf.Cancelar();
}
}
}
50 changes: 50 additions & 0 deletions src/WebCommands/Dominio/Comandos/EmitirNotaFiscal.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System.Collections.Generic;
using WebCommands.Dominio.Repositorios;
using WebCommands.Infrastructure.Commands;

namespace WebCommands.Dominio.Comandos
{
public class EmitirNotaFiscal : Command
{
public string DocumentoDoCliente { get; set; }

public IDictionary<string, int> Itens { get; set; }
}

public class EmitirNotaFiscalHandler : IHandler<EmitirNotaFiscal>
{
private readonly IRepositorioDeClientes repositorioDeClientes;
private readonly IRepositorioDeProdutos repoositorioDeProdutos;
private readonly IRepositorioDeNotasFiscais repositorioDeNotasFiscais;

public EmitirNotaFiscalHandler(
IRepositorioDeClientes repositorioDeClientes,
IRepositorioDeProdutos repoositorioDeProdutos,
IRepositorioDeNotasFiscais repositorioDeNotasFiscais)
{
this.repositorioDeClientes = repositorioDeClientes;
this.repoositorioDeProdutos = repoositorioDeProdutos;
this.repositorioDeNotasFiscais = repositorioDeNotasFiscais;
}

public void Handle(EmitirNotaFiscal command)
{
var cliente = this.repositorioDeClientes.BuscarPor(command.DocumentoDoCliente);
var nf = new NotaFiscal(cliente);

foreach (var item in command.Itens)
{
var produto = this.repoositorioDeProdutos.BuscarPor(item.Key);

nf.Adicionar(new NotaFiscal.Item()
{
Produto = produto.Descricao,
Valor = produto.Valor,
Quantidade = item.Value
});
}

this.repositorioDeNotasFiscais.Adicionar(nf);
}
}
}
64 changes: 64 additions & 0 deletions src/WebCommands/Dominio/NotaFiscal.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace WebCommands.Dominio
{
public class NotaFiscal
{
private readonly IList<Item> itens = new List<Item>();

public NotaFiscal(Cliente cliente)
{
this.Destinatario = new DadosDoDestinatario(cliente.Nome, cliente.Documento);
this.Situacao = Situacoes.Emitida;
this.Codigo = Guid.NewGuid().ToString();
}

public void Adicionar(Item item)
{
itens.Add(item);
this.Total += item.Quantidade * item.Valor;
}

public void Cancelar() => this.Situacao = Situacoes.Cancelada;

public string Codigo { get; private set; }

public Situacoes Situacao { get; private set; }

public DadosDoDestinatario Destinatario { get; private set; }

public decimal Total { get; private set; }

public IEnumerable<Item> Itens => itens.ToList();

public class DadosDoDestinatario
{
public DadosDoDestinatario(string nome, string documento)
{
this.Nome = nome;
this.Documento = documento;
}

public string Nome { get; private set; }

public string Documento { get; private set; }
}

public class Item
{
public string Produto { get; set; }

public int Quantidade { get; set; }

public decimal Valor { get; set; }
}

public enum Situacoes
{
Emitida,
Cancelada
}
}
}
9 changes: 9 additions & 0 deletions src/WebCommands/Dominio/Produto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace WebCommands.Dominio
{
public class Produto
{
public string Descricao { get; set; }

public decimal Valor { get; set; }
}
}
9 changes: 9 additions & 0 deletions src/WebCommands/Dominio/Repositorios/IRepositorio.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace WebCommands.Dominio.Repositorios
{
public interface IRepositorio<T> where T : class
{
void Adicionar(T entidade);

T BuscarPor(string codigo);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace WebCommands.Dominio.Repositorios
{
public interface IRepositorioDeClientes : IRepositorio<Cliente> { }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace WebCommands.Dominio.Repositorios
{
public interface IRepositorioDeNotasFiscais : IRepositorio<NotaFiscal> { }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace WebCommands.Dominio.Repositorios
{
public interface IRepositorioDeProdutos : IRepositorio<Produto> { }
}
16 changes: 16 additions & 0 deletions src/WebCommands/Infrastructure/Bus/IBus.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using WebCommands.Infrastructure.Commands;

namespace WebCommands.Infrastructure.Bus
{
public interface IBus
{
void RegisterHandler<T>() where T : Command;

Task Send<T>(T command) where T : Command;

IDictionary<string, Type> Handlers { get; }
}
}
45 changes: 45 additions & 0 deletions src/WebCommands/Infrastructure/Bus/InMemoryBus.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading.Tasks;
using WebCommands.Infrastructure.Commands;
using WebCommands.Infrastructure.Dependencies;

namespace WebCommands.Infrastructure.Bus
{
public class InMemoryBus : IBus
{
private readonly IDictionary<string, Type> handlers = new Dictionary<string, Type>();
private readonly IDependencyResolver dependencyResolver;

public InMemoryBus(IDependencyResolver dependencyResolver)
{
this.dependencyResolver = dependencyResolver;
}

public void RegisterHandler<T>() where T : Command
{
var commandType = typeof(T);

this.handlers.Add(commandType.Name, commandType);
}

public async Task Send<T>(T command) where T : Command
{
await Task.Run(() =>
{
var commandType = command.GetType();
if (this.handlers.ContainsKey(commandType.Name))
{
var handlerType = Type.GetType(commandType.FullName + "Handler");
var handler = this.dependencyResolver.Get(handlerType);
handlerType.GetMethod("Handle", new[] { commandType }).Invoke(handler, new object[] { command });
}
});
}

public IDictionary<string, Type> Handlers => handlers;
}
}
4 changes: 4 additions & 0 deletions src/WebCommands/Infrastructure/Commands/Command.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace WebCommands.Infrastructure.Commands
{
public abstract class Command { }
}
7 changes: 7 additions & 0 deletions src/WebCommands/Infrastructure/Commands/IHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace WebCommands.Infrastructure.Commands
{
public interface IHandler<T> where T : Command
{
void Handle(T command);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace WebCommands.Infrastructure.Dependencies
{
public class DefaultDependencyResolver : IDependencyResolver
{
private readonly Dictionary<Type, Type> itens = new Dictionary<Type, Type>();
private readonly Dictionary<Type, object> singletons = new Dictionary<Type, object>();

public void Add<TService, TImplementation>() where TImplementation : TService =>
itens.Add(typeof(TService), typeof(TImplementation));

public void Add<T>(T instance) where T : class =>
singletons.Add(typeof(T), instance);

public object Get(Type type)
{
if (singletons.ContainsKey(type))
return singletons[type];
else if (itens.ContainsKey(type))
return CreateInstance(itens[type]);

return CreateInstance(type);
}

private object CreateInstance(Type type)
{
var flags = BindingFlags.CreateInstance | BindingFlags.Public | BindingFlags.Instance;
var parameters = from p in type.GetConstructors(flags)[0].GetParameters()
select this.Get(p.ParameterType);

return Activator.CreateInstance(type, parameters.ToArray());
}
}
}
13 changes: 13 additions & 0 deletions src/WebCommands/Infrastructure/Dependencies/IDependencyResolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;

namespace WebCommands.Infrastructure.Dependencies
{
public interface IDependencyResolver
{
void Add<TService, TImplementation>() where TImplementation : TService;

void Add<T>(T instance) where T : class;

object Get(Type type);
}
}
20 changes: 20 additions & 0 deletions src/WebCommands/Infrastructure/Extensions/HttpExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;
using System;
using System.IO;
using WebCommands.Infrastructure.Commands;

namespace WebCommands.Infrastructure.Extensions
{
public static class HttpExtensions
{
private static readonly JsonSerializer serializer = new JsonSerializer();

public static Command ReadAsCommand(this HttpRequest request, Type commandType)
{
using (var sr = new StreamReader(request.Body))
using (var jr = new JsonTextReader(sr))
return serializer.Deserialize(jr, commandType) as Command;
}
}
}
Loading

0 comments on commit e223069

Please sign in to comment.