Exemple #1
0
        private async Task PostearRecurso <T>(IRepository <T> repository, string objeto, string resourcePath)
        {
            IEnumerable <T> data = await repository.GetForPortalWeb(objeto);

            Type type = typeof(T);

            System.Reflection.PropertyInfo propiedadRowId         = type.GetProperty("RowID");
            System.Reflection.PropertyInfo propiedadTipoOperacion = type.GetProperty("Sfl_TableOperation");
            System.Reflection.PropertyInfo propiedadId            = type.GetProperty("Id");

            foreach (T item in data)
            {
                int    rowId         = (int)propiedadRowId.GetValue(item);
                string tipoOperacion = (string)propiedadTipoOperacion.GetValue(item);
                string Id            = (string)propiedadId.GetValue(item).ToString();

                string stringRequest = JsonSerializer.Serialize(item, new JsonSerializerOptions {
                    WriteIndented = true
                });
                _logger.Information($"Se envia recurso para {tipoOperacion}:{ stringRequest }");
                HttpResponseMessage stringTask = new HttpResponseMessage();
                object stream = new object();
                switch (tipoOperacion)
                {
                case "INSERT":
                    stringTask = await client.PostAsync($"{_configuration["HostPortalWeb:BasePath"]}/{resourcePath}", new StringContent(stringRequest, Encoding.UTF8, "application/json"));

                    stream = await stringTask.Content.ReadAsStreamAsync();

                    break;

                case "UPDATE":
                    switch (resourcePath)
                    {
                    case "ClienteDireccionesEntrega":
                        System.Reflection.PropertyInfo propiedadIdCliente = type.GetProperty("IdCliente");
                        string idCliente = (string)propiedadIdCliente.GetValue(item);
                        stringTask = await client.PutAsync($"{_configuration["HostPortalWeb:BasePath"]}/{resourcePath}/{idCliente}/{Id}", new StringContent(stringRequest, Encoding.UTF8, "application/json"));

                        stream = await stringTask.Content.ReadAsStreamAsync();

                        break;

                    case "ListasDePrecio":
                        System.Reflection.PropertyInfo propiedadIdProducto = type.GetProperty("Idproducto");
                        System.Reflection.PropertyInfo propiedadFecha      = type.GetProperty("Fecha");
                        string   idProducto = (string)propiedadIdProducto.GetValue(item);
                        DateTime fecha      = (DateTime)propiedadFecha.GetValue(item);
                        string   fechaParam = fecha.ToString("yyyy-MM-dd");
                        stringTask = await client.PutAsync($"{_configuration["HostPortalWeb:BasePath"]}/{resourcePath}/{Id}/{idProducto}/{fechaParam}", new StringContent(stringRequest, Encoding.UTF8, "application/json"));

                        stream = await stringTask.Content.ReadAsStreamAsync();

                        break;

                    default:
                        stringTask = await client.PutAsync($"{_configuration["HostPortalWeb:BasePath"]}/{resourcePath}/{Id}", new StringContent(stringRequest, Encoding.UTF8, "application/json"));

                        stream = await stringTask.Content.ReadAsStreamAsync();

                        break;
                    }

                    break;

                case "DELETE":
                    System.Reflection.PropertyInfo propiedadActivo = type.GetProperty("Activo");
                    propiedadActivo.SetValue(item, 0);
                    stringRequest = JsonSerializer.Serialize(item, new JsonSerializerOptions {
                        WriteIndented = true
                    });
                    stringTask = await client.PutAsync($"{_configuration["HostPortalWeb:BasePath"]}/{resourcePath}", new StringContent(stringRequest, Encoding.UTF8, "application/json"));

                    stream = await stringTask.Content.ReadAsStreamAsync();

                    break;

                default:
                    break;
                }



                if (stringTask.IsSuccessStatusCode)
                {
                    var content = await JsonSerializer.DeserializeAsync <PortalWebResponse>((Stream)stream);

                    try
                    {
                        await repository.ActualizaRecursoTransferido(rowId, "S", objeto);

                        _logger.Information("Recurso generado con exito");
                    }
                    catch (Exception ex)
                    {
                        _logger.Fatal($"Error al actualizar recurso como transferido: {ex.Message}");
                    }
                }
                else
                {
                    try
                    {
                        await repository.ActualizaRecursoTransferido(rowId, "E", objeto);
                    }
                    catch (Exception ex)
                    {
                        _logger.Fatal($"Error al actualizar recurso como no transferido: {ex.Message}");
                    }

                    try
                    {
                        var content = await JsonSerializer.DeserializeAsync <PortalWebResponse>((Stream)stream);

                        _logger.Fatal($"Error al postear recurso en {resourcePath}: {content}");
                    }
                    catch
                    {
                        _logger.Fatal($"Error al postear recurso en {resourcePath}: no se pudieron obtener detalles del error");
                    }
                }
            }
        }