public override async Task <Command> Add(Command command) { Command cmd = new Command(command); try { var dto = await SerializerAsync.DeserializeJson <UserPlantaDto>(command.Json); if (!string.IsNullOrEmpty(dto.UserId) && dto.PlantaId > 0) { var userPlantas = await MyRepository.Get(t => t.PlantaId == dto.PlantaId && t.UserId.Equals(dto.UserId)); var userDbSet = Context.Set <UserAccount>(); var user = await userDbSet.FindAsync(dto.UserId); var plantaRep = new Repository <Planta>(Context); var planta = await plantaRep.GetById(dto.PlantaId); if (userPlantas != null && !userPlantas.Any() && user != null && planta != null) { var entity = new UserPlanta(); entity.UpdateEntity(dto); entity.Codigo = dto.UserId + dto.PlantaId + DateTime.Now.ToString(); entity.Planta = planta; var insertEntity = await MyRepository.Insert(entity); if (insertEntity != null) { cmd.Cmd = ServerCommands.LogResultOk; cmd.Json = await SerializerAsync.SerializeJson(true); await MyRepository.Save(); cmd.EntityId = entity.Id; } else { cmd.Cmd = ServerCommands.RepeatedHumanCode; ConsoleEx.WriteLine(ServerCommands.RepeatedHumanCode); } } else { cmd.Cmd = ServerCommands.LogResultDeny; } } else { cmd.Cmd = ServerCommands.LogResultDeny; } } catch (Exception e) { ConsoleEx.WriteError(e); } return(cmd); }
public async Task <Command> Delete(Command command) { Command cmd = new Command(command); try { var entity = await MyRepository.GetById(cmd.EntityId); if (entity != null) { entity.IsActive = false; cmd.Cmd = ServerCommands.LogResultOk; cmd.Json = await SerializerAsync.SerializeJson(true); await MyRepository.Save(); } else { cmd.Cmd = ServerCommands.LogResultDeny; cmd.Json = await SerializerAsync.SerializeJson(false); } } catch (Exception e) { ConsoleEx.WriteError(e); } return(cmd); }
private void ExpirationTokenTimer(Object source, ElapsedEventArgs e) { Zz.WriteServer(new Command { Cmd = ServerCommands.Logout, Json = SerializerAsync.SerializeJson("Token expired").Result }); ZZApiMain.RemoveUserConnection(LoginResultDto.Username); }
public async Task <Command> GetByHumanCode(Command command) { Command cmd = new Command(command); try { var dto = await SerializerAsync.DeserializeJson <EntityDto>(cmd.Json); var entity = await MyRepository.GetByHumanCode(dto.Codigo); if (entity != null) { cmd.Cmd = ServerCommands.LogResultOk; cmd.Json = await SerializerAsync.SerializeJson(entity.ConvertDto()); } else { cmd.Cmd = ServerCommands.LogResultDeny; } } catch (Exception e) { ConsoleEx.WriteError(e); } return(cmd); }
public override async Task <Command> Edit(Command command) { Command cmd = new Command(command); try { var dto = await SerializerAsync.DeserializeJson <TipoEntradaDto>(command.Json); var tipoEntrada = await MyRepository.GetById(cmd.EntityId); if (tipoEntrada != null) { tipoEntrada.Descricao = dto.Description; cmd.Cmd = ServerCommands.LogResultOk; cmd.Json = await SerializerAsync.SerializeJson(true); await MyRepository.Save(); } else { cmd.Cmd = ServerCommands.LogResultDeny; cmd.Json = await SerializerAsync.SerializeJson(false); } } catch (Exception e) { ConsoleEx.WriteError(e); } return(cmd); }
public override async Task <Command> Add(Command command) { Command cmd = new Command(command); try { var dto = await SerializerAsync.DeserializeJson <ServicoDto>(command.Json); var servicos = await MyRepository.Get(t => t.Codigo.Equals(dto.Codigo)); if (servicos != null && !servicos.Any()) { var unidadeMedidaRep = new Repository <UnidadeMedida>(Context); var unidadeMedida = await unidadeMedidaRep.GetById(dto.UnidadeMedidaId); var tipoServicoRep = new Repository <TipoServico>(Context); var tipoServico = await tipoServicoRep.GetById(dto.TipoServicoId); var centroCustoRep = new Repository <CentroCustoSintetico>(Context); var centroCusto = await centroCustoRep.GetById(dto.CentroCustoId); if (unidadeMedida != null && tipoServico != null && centroCusto != null) { var entity = new Servico(); entity.UpdateEntity(dto); entity.UnidadeMedida = unidadeMedida; entity.TipoServico = tipoServico; entity.CentroCusto = centroCusto; var insertEntity = await MyRepository.Insert(entity); if (insertEntity != null) { cmd.Cmd = ServerCommands.LogResultOk; cmd.Json = await SerializerAsync.SerializeJson(true); await MyRepository.Save(); cmd.EntityId = entity.Id; } else { cmd.Cmd = ServerCommands.RepeatedHumanCode; ConsoleEx.WriteLine(ServerCommands.RepeatedHumanCode); } } } else { cmd.Cmd = ServerCommands.LogResultDeny; } } catch (Exception e) { ConsoleEx.WriteError(e); } return(cmd); }
public static async Task <Command> GetAddressByZipCode(Command command) { Command cmd = new Command(command); try { using (var context = new ZZContext()) { var dto = await SerializerAsync.DeserializeJson <EnderecoDto>(cmd.Json); var rep = new Repository <Endereco>(context); var enderecos = await rep.Get(e => e.Cep.Equals(dto.Cep) && e.Numero == dto.Numero); if (enderecos != null && enderecos.Any()) { var endList = enderecos.FirstOrDefault(); cmd.Cmd = ServerCommands.LogResultOk; var dtosEnd = endList.ConvertDto(); cmd.Json = await SerializerAsync.SerializeJson(dtosEnd); } else { var address = await SearchAddressAsync(dto.Cep, CancellationToken.None); if (address != null) { cmd.Cmd = ServerCommands.LogResultOk; var entity = new EnderecoDto { Cep = address.ZipCode, Bairro = address.Neighborhood, Cidade = address.City, Estado = address.StateInitials, Logradouro = address.Street, Complemento = address.Complement, Ibge = address.IBGECode, GIACode = address.GIACode, Numero = dto.Numero, Codigo = address.IBGECode.ToString() + address.GIACode.ToString() + address.ZipCode + dto.Numero.ToString() }; cmd.Json = await SerializerAsync.SerializeJson(entity); } else { cmd.Cmd = ServerCommands.LogResultDeny; } } } } catch (Exception e) { ConsoleEx.WriteError(e); } return(cmd); }
public static async Task RemoveUserConnection(string username) { var del = new DelegateAction { act = ReturnLogoutRequest }; var cmd = new Command { Cmd = ServerCommands.RemoveClientAuthorized, Json = await SerializerAsync.SerializeJson(username) }; await Zz.WriteServer(del, cmd); }
public override async Task <Command> Edit(Command command) { Command cmd = new Command(command); try { var dto = await SerializerAsync.DeserializeJson <ServicoDto>(command.Json); var servico = await MyRepository.GetById(cmd.EntityId); if (servico != null) { servico.UpdateEntity(dto); var unidadeMedidaRep = new Repository <UnidadeMedida>(Context); var unidadeMedida = await unidadeMedidaRep.GetById(dto.UnidadeMedidaId); if (unidadeMedida != null) { servico.UnidadeMedida = unidadeMedida; } var tipoServicoRep = new Repository <TipoServico>(Context); var tipoServico = await tipoServicoRep.GetById(dto.TipoServicoId); if (tipoServico != null) { servico.TipoServico = tipoServico; } var centroCustoRep = new Repository <CentroCustoSintetico>(Context); var centroCusto = await centroCustoRep.GetById(dto.CentroCustoId); if (centroCusto != null) { servico.CentroCusto = centroCusto; } cmd.Cmd = ServerCommands.LogResultOk; cmd.Json = await SerializerAsync.SerializeJson(true); await MyRepository.Save(); } else { cmd.Cmd = ServerCommands.LogResultDeny; cmd.Json = await SerializerAsync.SerializeJson(false); } } catch (Exception e) { ConsoleEx.WriteError(e); } return(cmd); }
/// <summary> /// Envia para o client um Command com id, comando e json /// </summary> /// <param name="cmd">Command que contem id, comando e json</param> /// <returns></returns> public async Task WriteServer(Command cmd) { if (string.IsNullOrEmpty(cmd.Id)) { cmd.Id = ServerCommands.BroadCastId; } var cmdSend = await SerializerAsync.SerializeJson(cmd); _bufferWrite.Enqueue(cmdSend); }
public async Task <string> ApiWriteServer(string username, Command cmd) { cmd.Id = username + await GetCommandId(); cmd.IsWait = true; var cmdSend = await SerializerAsync.SerializeJson(cmd); _bufferWrite.Enqueue(cmdSend); return(cmd.Id); }
public override async Task <Command> Add(Command command) { Command cmd = new Command(command); try { var dto = await SerializerAsync.DeserializeJson <TabelaDto>(command.Json); var tabelas = await MyRepository.Get(t => t.ServicoId == dto.ServicoId && t.Descricao.Equals(dto.Description)); if (tabelas != null && !tabelas.Any()) { var servicoRep = new Repository <Servico>(Context); var servico = await servicoRep.GetById(dto.ServicoId); if (servico != null) { var entity = new TabelaCusto(); entity.UpdateEntity(dto); entity.DataTabela = DateTime.Now; entity.Servico = servico; var insertEntity = await MyRepository.Insert(entity); if (insertEntity != null) { cmd.Cmd = ServerCommands.LogResultOk; cmd.Json = await SerializerAsync.SerializeJson(true); await MyRepository.Save(); cmd.EntityId = entity.Id; } else { cmd.Cmd = ServerCommands.RepeatedHumanCode; ConsoleEx.WriteLine(ServerCommands.RepeatedHumanCode); } } } else { cmd.Cmd = ServerCommands.LogResultDeny; } } catch (Exception e) { ConsoleEx.WriteError(e); } return(cmd); }
public static async Task AddUserConnection(LoginResultDto loginResult) { var del = new DelegateAction { act = ReturnLoginRequest }; var cmd = new Command { Cmd = ServerCommands.AddClientAuthorized, Json = await SerializerAsync.SerializeJson(loginResult.Username) }; await Zz.WriteServer(del, cmd); var conn = new ConnectionManager(loginResult); UsersConnections.Add(loginResult.Username, conn); }
public async Task WriteServer(DelegateAction del, Command cmd) { if (string.IsNullOrEmpty(cmd.Id)) { cmd.Id = await GetCommandId(); } var cmdSend = await SerializerAsync.SerializeJson(cmd); if (!_srvsCmdsBuffer.ContainsKey(cmd.Id)) { _srvsCmdsBuffer.Add(cmd.Id, del); } _bufferWrite.Enqueue(cmdSend); }
public async Task WriteServer(string id, long playerId, string command, string json) { if (string.IsNullOrEmpty(id)) { id = ServerCommands.BroadCastId; } var cmd = new Command { Id = id, EntityId = playerId, Cmd = command, Json = json }; var cmdSend = await SerializerAsync.SerializeJson(cmd); _bufferWrite.Enqueue(cmdSend); }
public override async Task <Command> Edit(Command command) { Command cmd = new Command(command); try { cmd.Cmd = ServerCommands.LogResultDeny; cmd.Json = await SerializerAsync.SerializeJson(false); } catch (Exception e) { ConsoleEx.WriteError(e); } return(cmd); }
public override async Task <Command> Add(Command command) { Command cmd = new Command(command); try { var dto = await SerializerAsync.DeserializeJson <TipoEntradaDto>(command.Json); var tipos = await MyRepository.Get(t => t.Descricao.Equals(dto.Description)); if (tipos != null && !tipos.Any()) { var entity = new TipoEntrada(); entity.UpdateEntity(dto); var insertEntity = await MyRepository.Insert(entity); if (insertEntity != null) { cmd.Cmd = ServerCommands.LogResultOk; cmd.Json = await SerializerAsync.SerializeJson(true); await MyRepository.Save(); cmd.EntityId = entity.Id; } else { cmd.Cmd = ServerCommands.RepeatedHumanCode; ConsoleEx.WriteLine(ServerCommands.RepeatedHumanCode); } } else { cmd.Cmd = ServerCommands.LogResultDeny; } } catch (Exception e) { ConsoleEx.WriteError(e); } return(cmd); }
public override async Task <Command> Edit(Command command) { Command cmd = new Command(command); try { var dto = await SerializerAsync.DeserializeJson <CompraManualDto>(command.Json); var compras = await MyRepository.Get(c => c.Id == cmd.EntityId, null, "TipoEntrada,Fornecedor,Itens,Itens.Servico,Itens.Unidade"); var compraManual = compras.FirstOrDefault(); if (compraManual != null) { compraManual.UpdateEntity(dto); if (compraManual.Itens != null && compraManual.Itens.Count > 0) { compraManual.ValorTotal = 0; foreach (var item in compraManual.Itens) { compraManual.ValorTotal += item.ValorTotal; } } cmd.Cmd = ServerCommands.LogResultOk; cmd.Json = await SerializerAsync.SerializeJson(true); await MyRepository.Save(); } else { cmd.Cmd = ServerCommands.LogResultDeny; cmd.Json = await SerializerAsync.SerializeJson(false); } } catch (Exception e) { ConsoleEx.WriteError(e); } return(cmd); }
public async Task WriteServer(DelegateAction del, string id, string command, string json) { if (string.IsNullOrEmpty(id)) { id = await GetCommandId(); } var cmd = new Command { Id = id, Cmd = command, Json = json }; var cmdSend = await SerializerAsync.SerializeJson(cmd); if (!_srvsCmdsBuffer.ContainsKey(id)) { _srvsCmdsBuffer.Add(id, del); } _bufferWrite.Enqueue(cmdSend); }
public override async Task <Command> Edit(Command command) { Command cmd = new Command(command); try { var dto = await SerializerAsync.DeserializeJson <UserDadosDto>(command.Json); var cliente = await MyRepository.GetById(cmd.EntityId); if (cliente != null) { cliente.UpdateEntity(dto); var enderecoRep = new Repository <Endereco>(Context); var endereco = await enderecoRep.GetById(dto.Endereco.Id); if (endereco != null) { endereco.UpdateEntity(dto.Endereco); cliente.Endereco = endereco; } cmd.Cmd = ServerCommands.LogResultOk; cmd.Json = await SerializerAsync.SerializeJson(true); await MyRepository.Save(); } else { cmd.Cmd = ServerCommands.LogResultDeny; cmd.Json = await SerializerAsync.SerializeJson(false); } } catch (Exception e) { ConsoleEx.WriteError(e); } return(cmd); }
public async Task <ActionResult <long> > SaveAddress(EnderecoDto dto) { try { var myUsername = User.Identity.Name; if (ZZApiMain.VerifyUserAuthorize(myUsername)) { if (ZZApiMain.UsersConnections.TryGetValue(myUsername, out var myConn)) { var myId = await myConn.Zz.ApiWriteServer(myUsername, new Command { Cmd = ServerCommands.SaveAddress, Json = await SerializerAsync.SerializeJson(dto) }); var responseCommand = await myConn.Zz.GetApiWaitCommand(myId); if (responseCommand != null && responseCommand.Cmd.Equals(ServerCommands.LogResultOk)) { return(responseCommand.EntityId); } } } return(-1); } catch (Exception e) { ConsoleEx.WriteError(e); return(NotFound()); } }
public override async Task <Command> Add(Command command) { Command cmd = new Command(command); try { var dto = await SerializerAsync.DeserializeJson <UserDadosDto>(command.Json); var clientes = await MyRepository.Get(t => t.Codigo.Equals(dto.Codigo)); if (clientes != null && !clientes.Any()) { var entity = new Cliente(); entity.UpdateEntity(dto); var myCmd = new Command(); if (entity.EnderecoId <= 0) { var enderecoCmd = new Command { Json = await SerializerAsync.SerializeJson(dto.Endereco) }; if (string.IsNullOrEmpty(entity.Endereco.Cep)) { myCmd = await LocalizationManager.GetAddress(enderecoCmd); } else { myCmd = await LocalizationManager.GetAddressByZipCode(enderecoCmd); } entity.Endereco.UpdateEntity(await SerializerAsync.DeserializeJson <EnderecoDto>(myCmd.Json)); entity.EnderecoId = entity.Endereco.Id; } if (entity.EnderecoId > 0) { entity.Endereco = null; } var insertEntity = await MyRepository.Insert(entity); if (insertEntity != null) { cmd.Cmd = ServerCommands.LogResultOk; cmd.Json = await SerializerAsync.SerializeJson(true); await MyRepository.Save(); cmd.EntityId = entity.Id; } else { cmd.Cmd = ServerCommands.RepeatedHumanCode; ConsoleEx.WriteLine(ServerCommands.RepeatedHumanCode); } } else { cmd.Cmd = ServerCommands.LogResultDeny; } } catch (Exception e) { ConsoleEx.WriteError(e); } return(cmd); }
public async Task <ActionResult <UserDadosDto> > GetByHumanCode(UserDadosDto dto) { try { var myUsername = User.Identity.Name; if (ZZApiMain.VerifyUserAuthorize(myUsername)) { if (ZZApiMain.UsersConnections.TryGetValue(myUsername, out var myConn)) { var myId = await myConn.Zz.ApiWriteServer(myUsername, new Command { Cmd = ServerCommands.GetById, EntityId = dto.Id, Tela = Tela, Json = await SerializerAsync.SerializeJson(dto) }); var responseCommand = await myConn.Zz.GetApiWaitCommand(myId); if (responseCommand != null && responseCommand.Cmd.Equals(ServerCommands.LogResultOk)) { return(await SerializerAsync.DeserializeJson <UserDadosDto>(responseCommand.Json)); } } } return(NotFound()); } catch (Exception e) { ConsoleEx.WriteError(e); return(NotFound()); } }
public async Task <ActionResult <bool> > Delete(TipoSiglaDto dto) { try { var myUsername = User.Identity.Name; if (ZZApiMain.VerifyUserAuthorize(myUsername)) { if (ZZApiMain.UsersConnections.TryGetValue(myUsername, out var myConn)) { var myId = await myConn.Zz.ApiWriteServer(myUsername, new Command { Tela = Tela, Cmd = ServerCommands.Disable, EntityId = dto.Id, Json = await SerializerAsync.SerializeJson(dto) }); var responseCommand = await myConn.Zz.GetApiWaitCommand(myId); if (responseCommand != null && responseCommand.Cmd.Equals(ServerCommands.LogResultOk)) { return(true); } } } return(false); } catch (Exception e) { ConsoleEx.WriteError(e); return(NotFound()); } }
public static async Task <Command> SaveAddress(Command command) { Command cmd = new Command(command); try { using (var context = new ZZContext()) { var dto = await SerializerAsync.DeserializeJson <EnderecoDto>(cmd.Json); var repAddress = new Repository <Endereco>(context); if (dto.Ibge <= 0) { var viacep = await SearchAddressAsync(dto.Cep, CancellationToken.None); dto.Ibge = viacep.IBGECode; dto.GIACode = viacep.GIACode; } if (String.IsNullOrWhiteSpace(dto.Codigo)) { dto.Codigo = dto.Ibge.ToString() + dto.GIACode.ToString() + dto.Cep + dto.Numero.ToString(); } var address = await repAddress.Get(e => e.Logradouro.Equals(dto.Logradouro) && e.Numero == dto.Numero); if (!address.Any()) { var entity = new Endereco(); entity.UpdateEntity(dto); var insertEntity = await repAddress.Insert(entity); if (insertEntity != null) { cmd.Cmd = ServerCommands.LogResultOk; await repAddress.Save(); cmd.Json = await SerializerAsync.SerializeJson(entity); cmd.EntityId = entity.Id; } else { cmd.Cmd = ServerCommands.RepeatedHumanCode; ConsoleEx.WriteLine(ServerCommands.RepeatedHumanCode); } } else { cmd.Cmd = ServerCommands.LogResultDeny; } } } catch (Exception e) { ConsoleEx.WriteError(e); } return(cmd); }
public static async void ReturnLoginRequest(Object[] server, Object[] local) { var dataJson = SerializerAsync.DeserializeJson <Command>(server[0].ToString()).Result; try { if (dataJson != null) { if (dataJson.Cmd.Equals(ServerCommands.LogResultOk)) { var username = await SerializerAsync.DeserializeJson <string>(dataJson.Json); if (UsersConnections.TryGetValue(username, out var conn)) { await conn.Zz.WriteServer(new Command { Cmd = ServerCommands.IsUser, Json = await SerializerAsync.SerializeJson(username) }); } } else if (dataJson.Cmd.Equals(ServerCommands.LogResultDeny)) { var username = await SerializerAsync.DeserializeJson <string>(dataJson.Json); await RemoveUserConnection(username); } } } catch (Exception e) { Console.WriteLine(e); throw; } }
public override async Task <Command> Add(Command command) { Command cmd = new Command(command); try { var dto = await SerializerAsync.DeserializeJson <MovimentoEstoqueDto>(command.Json); if (await VerififyMovimentoEstoque(dto)) { var servicoRep = new Repository <Servico>(Context); var servico = await servicoRep.GetById(dto.ServicoId); var tipoEntradaRep = new Repository <TipoEntrada>(Context); var tipoEntrada = await tipoEntradaRep.GetById(dto.TipoEntradaId); var estoqueRep = new Repository <Estoque>(Context); var estoque = await estoqueRep.GetById(dto.EstoqueId); var entity = new MovimentoEstoque(); entity.UpdateEntity(dto); entity.Estoque = estoque; entity.Servico = servico; entity.TipoEntrada = tipoEntrada; if (String.IsNullOrEmpty(entity.Codigo)) { if (!string.IsNullOrEmpty(entity.Servico.Codigo)) { entity.Codigo = entity.Servico.Codigo + DateTime.Now.ToString(); } } if (entity.DataMovimento == DateTime.MinValue) { entity.DataMovimento = entity.Data; } var insertEntity = await MyRepository.Insert(entity); if (insertEntity != null) { cmd.Cmd = ServerCommands.LogResultOk; cmd.Json = await SerializerAsync.SerializeJson(true); await MyRepository.Save(); cmd.EntityId = entity.Id; } else { cmd.Cmd = ServerCommands.RepeatedHumanCode; ConsoleEx.WriteLine(ServerCommands.RepeatedHumanCode); } } else { cmd.Cmd = ServerCommands.LogResultDeny; } } catch (Exception e) { ConsoleEx.WriteError(e); } return(cmd); }
public override async Task <Command> Add(Command command) { Command cmd = new Command(command); try { var dto = await SerializerAsync.DeserializeJson <CompraManualDto>(command.Json); var compras = await MyRepository.Get(t => t.Codigo.Equals(dto.Codigo)); if (compras != null && !compras.Any()) { var unidadeMedidaRep = new Repository <UnidadeMedida>(Context); var servicoRep = new Repository <Servico>(Context); var tipoEntradaRep = new Repository <TipoEntrada>(Context); var tipoEntrada = await tipoEntradaRep.GetById(dto.TipoEntradaId); var fornecedorRep = new Repository <Fornecedor>(Context); var fornecedor = await fornecedorRep.GetById(dto.FornecedorId); var entity = new CompraManual(); entity.UpdateEntity(dto); if (tipoEntrada != null) { entity.TipoEntrada = tipoEntrada; } if (fornecedor != null) { entity.Fornecedor = fornecedor; } if (entity.DataEmissao == DateTime.MinValue) { entity.DataEmissao = DateTime.Now; } if (String.IsNullOrEmpty(entity.Codigo)) { if (!string.IsNullOrEmpty(entity.Fornecedor.NomeFantasia)) { entity.Codigo = entity.Fornecedor.NomeFantasia + DateTime.Now.ToString(); } } if (entity.Itens != null && entity.Itens.Count > 0) { entity.ValorTotal = 0; foreach (var item in entity.Itens) { var unidadeMedida = await unidadeMedidaRep.GetById(item.UnidadeId); var servico = await servicoRep.GetById(item.ServicoId); if (unidadeMedida != null) { item.Unidade = unidadeMedida; } if (servico != null) { item.Servico = servico; item.Codigo = servico.Codigo + DateTime.Now.ToString(); } entity.ValorTotal += item.ValorTotal; } } var insertEntity = await MyRepository.Insert(entity); if (insertEntity != null) { cmd.Cmd = ServerCommands.LogResultOk; cmd.Json = await SerializerAsync.SerializeJson(true); await MyRepository.Save(); cmd.EntityId = entity.Id; } else { cmd.Cmd = ServerCommands.RepeatedHumanCode; ConsoleEx.WriteLine(ServerCommands.RepeatedHumanCode); } } else { cmd.Cmd = ServerCommands.LogResultDeny; } } catch (Exception e) { ConsoleEx.WriteError(e); } return(cmd); }
public override async Task <Command> Add(Command command) { Command cmd = new Command(command); try { var dto = await SerializerAsync.DeserializeJson <DtoLigacao>(command.Json); if (dto.FirstDtoId > 0 && dto.SecondDtoId > 0) { var funcionarioEstoques = await MyRepository.Get(t => t.FuncionarioId == dto.FirstDtoId && t.EstoqueId == dto.SecondDtoId); var funcRep = new Repository <Funcionario>(Context); var funcionario = await funcRep.GetById(dto.FirstDtoId); var estoqueRep = new Repository <Estoque>(Context); var estoque = await estoqueRep.GetById(dto.SecondDtoId); if (funcionarioEstoques != null && !funcionarioEstoques.Any() && funcionario != null && estoque != null) { var entity = new FuncionarioEstoque(); entity.UpdateEntity(dto); entity.Codigo = DateTime.Now.ToString(); entity.Funcionario = funcionario; entity.Estoque = estoque; var insertEntity = await MyRepository.Insert(entity); if (insertEntity != null) { cmd.Cmd = ServerCommands.LogResultOk; cmd.Json = await SerializerAsync.SerializeJson(true); await MyRepository.Save(); cmd.EntityId = entity.Id; } else { cmd.Cmd = ServerCommands.RepeatedHumanCode; ConsoleEx.WriteLine(ServerCommands.RepeatedHumanCode); } } else { cmd.Cmd = ServerCommands.LogResultDeny; } } else { cmd.Cmd = ServerCommands.LogResultDeny; } } catch (Exception e) { ConsoleEx.WriteError(e); } return(cmd); }