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 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); }
public async Task <ActionResult <IEnumerable <TipoSiglaDto> > > GetAll() { 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.GetAll }); var responseCommand = await myConn.Zz.GetApiWaitCommand(myId); if (responseCommand != null && responseCommand.Cmd.Equals(ServerCommands.LogResultOk)) { return(await SerializerAsync.DeserializeJsonList <TipoSiglaDto>(responseCommand.Json)); } } } return(NotFound()); } catch (Exception e) { ConsoleEx.WriteError(e); return(NotFound()); } }
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 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 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> GetAll(Command command) { Command cmd = new Command(command); try { var entities = await MyRepository.Get(null, null, "Endereco"); var list = entities.ToList(); if (list.Any()) { cmd.Cmd = ServerCommands.LogResultOk; var dtos = list.Select(t => t.ConvertDto()).ToList(); cmd.Json = await SerializerAsync.SerializeJsonList(dtos); } else { cmd.Cmd = ServerCommands.LogResultDeny; } } catch (Exception e) { ConsoleEx.WriteError(e); } return(cmd); }
public static async void ReturnLogoutRequest(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); UsersConnections.Remove(username); ConsoleEx.WriteLine("Usuario removido da lista de autorizados com sucesso"); } else if (dataJson.Cmd.Equals(ServerCommands.LogResultDeny)) { var username = await SerializerAsync.DeserializeJson <string>(dataJson.Json); UsersConnections.Remove(username); ConsoleEx.WriteLine("Deu merda ao remover o usuario da lista de autorizados"); } } } catch (Exception e) { Console.WriteLine(e); throw; } }
public static async Task <Command> GetAllStates(Command command) { Command cmd = new Command(command); try { using (var context = new ZZContext()) { var rep = new Repository <Estado>(context); var entities = await rep.Get(); var list = entities.ToList(); if (list.Any()) { cmd.Cmd = ServerCommands.LogResultOk; var dtos = list.Select(t => t.ConvertDto()).ToList(); cmd.Json = await SerializerAsync.SerializeJsonList(dtos); } else { cmd.Cmd = ServerCommands.LogResultDeny; } } } catch (Exception e) { ConsoleEx.WriteError(e); } return(cmd); }
public void ReturnServer(Object[] server, Object[] local) { var dataJson = SerializerAsync.DeserializeJson <Command>(server[0].ToString()).Result; try { if (dataJson != null) { if (!dataJson.Cmd.Equals(ServerCommands.Exit)) { if (dataJson.Cmd.Equals(ServerCommands.LogResultOk)) { ConsoleEx.WriteLine("Uhulll, consegui logar"); _expirationTimer = ZZApiMain.SetTimer( (LoginResultDto.ExpirationDate - LoginResultDto.CreatedDate).TotalMilliseconds, ExpirationTokenTimer); } else if (dataJson.Cmd.Equals(ServerCommands.LogResultDeny)) { ConsoleEx.WriteLine("Algo deu errado"); ZZApiMain.RemoveUserConnection(LoginResultDto.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 <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); }
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 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> GetAddress(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.Logradouro.Equals(dto.Logradouro) && e.Numero == dto.Numero); var endList = enderecos.ToList(); if (endList.Any()) { cmd.Cmd = ServerCommands.LogResultOk; var dtosEnd = endList.Select(e => e.ConvertDto()).ToList(); cmd.Json = await SerializerAsync.SerializeJsonList(dtosEnd); } else { var addressList = await SearchAddressAsync(dto.Estado, dto.Cidade, dto.Logradouro, CancellationToken.None); var viaCepResults = addressList.ToList(); if (viaCepResults.Any()) { cmd.Cmd = ServerCommands.LogResultOk; var dtos = viaCepResults.Select(d => new EnderecoDto { Cep = d.ZipCode, Bairro = d.Neighborhood, Cidade = d.City, Estado = d.StateInitials, Logradouro = d.Street, Complemento = d.Complement, Ibge = d.IBGECode, GIACode = d.GIACode, Numero = dto.Numero, Codigo = d.IBGECode.ToString() + d.GIACode.ToString() + d.ZipCode + dto.Numero.ToString() }); cmd.Json = await SerializerAsync.SerializeJsonList(dtos.ToList()); } 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 <Command> UpdateEstados(Command command) { Command cmd = new Command(command); try { using (var context = new ZZContext()) { var rep = new Repository <Estado>(context); var entities = await rep.Get(); var list = entities.ToList(); var ibgeList = await SearchEstadoAsync(); if (list.Any() && ibgeList.Any()) { foreach (var estado in ibgeList) { if (list.Exists(e => e.Ibge == estado.Ibge)) { var est = list.Find(e => e.Ibge == estado.Ibge); est.Ibge = estado.Ibge; est.Descricao = estado.Descricao; est.Sigla = estado.Sigla; } } await rep.InsertList(list); await rep.Save(); cmd.Cmd = ServerCommands.LogResultOk; var dtos = list.Select(t => t.ConvertDto()).ToList(); cmd.Json = await SerializerAsync.SerializeJsonList(dtos); } else if (ibgeList.Any()) { await rep.InsertList(ibgeList); await rep.Save(); cmd.Cmd = ServerCommands.LogResultOk; var dtos = ibgeList.Select(t => t.ConvertDto()).ToList(); cmd.Json = await SerializerAsync.SerializeJsonList(dtos); } 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 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); }
/// <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 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); }
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(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 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); }
/// <summary> /// Searches the asynchronous. /// </summary> /// <param name="zipCode">The zip code.</param> /// <param name="token">The token.</param> /// <returns></returns> public static async Task <ViaCEPResult> SearchAddressAsync(String zipCode, CancellationToken token) { using (var client = new HttpClient()) { client.BaseAddress = new Uri(BaseUrl); var response = await client.GetAsync($"/ws/{zipCode}/json", token).ConfigureAwait(false); response.EnsureSuccessStatusCode(); var jsonContent = await response.Content.ReadAsStringAsync(); var json = await SerializerAsync.DeserializeJson <ViaCEPResult>(jsonContent); json.ZipCode = new String(json.ZipCode.Where(Char.IsDigit).ToArray()); return(json); } }
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 void ProcessLogin(Object[] server, Object[] local) { var dataJson = SerializerAsync.DeserializeJson <Command>(server[0].ToString()).Result; try { if (dataJson != null) { if (!dataJson.Cmd.Equals(ServerCommands.Exit)) { if (dataJson.Cmd.Equals(ServerCommands.IsController)) { ConsoleEx.WriteLine("Oi Controller fofo *3* "); _client = new ControllerClient(); MyId = ServerCommands.IsController; _client.Login(this, connection); } else if (dataJson.Cmd.Equals(ServerCommands.IsUser)) { if (Server.VerifyUserAuthorization( SerializerAsync.DeserializeJson <string>(dataJson.Json).Result, this)) { ConsoleEx.WriteLine("Oi Cli fofo *3* "); MyId = SerializerAsync.DeserializeJson <string>(dataJson.Json).Result; _client = new UserClient(); _client.Login(this, connection); } else { ConsoleEx.WriteLine("Cliente não autorizado"); connection.WriteServer("", 542, ServerCommands.LogResultDeny, ""); } } else { _client.Command(dataJson); } } } } catch (Exception e) { ConsoleEx.WriteLine("Erro na criação do client : " + " | " + _client + " | " + dataJson + " | " + e.Message); } }
/// <summary> /// Converte um array de object recebido como entrada de um DelegateAction em Command /// </summary> /// <param name="s"></param> /// <returns><typeparam name="dataJson"></typeparam></returns> public static async Task <Command> ObjectToCommand(Object[] s) { try { var data = s[0].ToString(); Command dataJson = new Command(); dataJson = await SerializerAsync.DeserializeJson <Command>(data); return(dataJson); } catch (Exception e) { ConsoleEx.WriteError(e); return(null); } }
public override async Task Command(Command command) { try { if (Manager != null && Connection != null) { if (command.Cmd != null) { if (command.Cmd.Equals(ServerCommands.AddClientAuthorized)) { if (Manager.Server.AddAuthorizedUser( await SerializerAsync.DeserializeJson <string>(command.Json))) { command.Cmd = ServerCommands.LogResultOk; } else { command.Cmd = ServerCommands.LogResultDeny; } await Connection.WriteServer(command); } else if (command.Cmd.Equals(ServerCommands.RemoveClientAuthorized)) { if (Manager.Server.RemoveAuthorizedUser( await SerializerAsync.DeserializeJson <string>(command.Json))) { command.Cmd = ServerCommands.LogResultOk; } else { command.Cmd = ServerCommands.LogResultDeny; } await Connection.WriteServer(command); } } } } catch (Exception e) { ConsoleEx.WriteError("Erro ao receber command do controller ", e); throw; } }
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); }