Commit inicial: ITCA_API_SAP_WMS (.NET Core 3.1)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
2b82744eb8
168 files changed
+11066
No files matched your search
@@ -0,0 +1,372 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Data.SqlClient;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Net.Connection.Attributes;
|
||||
using Net.CrossCotting;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Net.Connection
|
||||
{
|
||||
public class ConnectionSQL : IConnectionSQL
|
||||
{
|
||||
public string _cnx { get; set; }
|
||||
|
||||
public ConnectionSQL(IConfiguration configuration, IServiceProvider serviceProvider)
|
||||
{
|
||||
}
|
||||
|
||||
public ConnectionSQL()
|
||||
{
|
||||
}
|
||||
public string DevuelveConnectionSQL()
|
||||
{
|
||||
return _cnx;
|
||||
}
|
||||
|
||||
public IEnumerable<T> ExecuteSqlViewFindByCondition<T>(string procedureName, object parameters)
|
||||
{
|
||||
return ExecuteSqlQuery<T>(procedureName, GetParametersSqlQueryAnotation(parameters, ActionType.View).ToArray());
|
||||
}
|
||||
|
||||
public T ExecuteSqlViewId<T>(string procedureName, object parameters)
|
||||
{
|
||||
return ExecuteSqlQuery<T>(procedureName, GetParametersSqlQueryAnotation(parameters, ActionType.Everything).ToArray()).FirstOrDefault();
|
||||
}
|
||||
|
||||
public IEnumerable<T> ExecuteSqlViewAll<T>(string procedureName, T parameters)
|
||||
{
|
||||
return ExecuteSqlQuery<T>(procedureName, GetParametersSqlQueryAnotation(parameters, ActionType.Everything).ToArray());
|
||||
}
|
||||
|
||||
public object ExecuteSqlInsert<T>(string procedureName, object parameters)
|
||||
{
|
||||
return ExecuteSqlNonQuery(procedureName, GetParametersSqlQueryAnotation(parameters, ActionType.Insert).ToArray())[0].valor;
|
||||
}
|
||||
public object ExecuteSqlUpdate<T>(string procedureName, object parameters)
|
||||
{
|
||||
return ExecuteSqlNonQuery(procedureName, GetParametersSqlQueryAnotation(parameters, ActionType.Update).ToArray());
|
||||
}
|
||||
|
||||
public object ExecuteSqlDelete<T>(string procedureName, T parameters)
|
||||
{
|
||||
return ExecuteSqlNonQuery(procedureName, GetParametersSqlQueryAnotation(parameters, ActionType.Delete).ToArray());
|
||||
}
|
||||
|
||||
public DbParametro[] ExecuteSqlNonQuery(string procedureName, DbParametro[] parameters)
|
||||
{
|
||||
SqlConnection cnx = new SqlConnection(_cnx);
|
||||
SqlCommand cmd = cnx.CreateCommand();
|
||||
cmd.CommandType = CommandType.StoredProcedure;
|
||||
cmd.CommandText = procedureName;
|
||||
cmd.CommandTimeout = 0;
|
||||
try
|
||||
{
|
||||
foreach (DbParametro parametro in parameters)
|
||||
{
|
||||
SqlParameter prm = new SqlParameter();
|
||||
prm.ParameterName = parametro.nombre;
|
||||
if (!parametro.dbType.Equals (System.Data.SqlDbType.VarBinary))
|
||||
{
|
||||
prm.SqlDbType = parametro.dbType;
|
||||
prm.Size = (parametro.size == 0) ? prm.Size : parametro.size;
|
||||
}
|
||||
|
||||
prm.Direction = parametro.direccion;
|
||||
prm.Value = parametro.valor;
|
||||
cmd.Parameters.Add(prm);
|
||||
}
|
||||
|
||||
if (cmd.Connection.State.HasFlag(ConnectionState.Closed))
|
||||
cmd.Connection.Open();
|
||||
|
||||
cmd.Transaction = cmd.Connection.BeginTransaction();
|
||||
cmd.ExecuteNonQuery();
|
||||
if (parameters != null)
|
||||
{
|
||||
SqlParameterCollection sqlParameters = cmd.Parameters;
|
||||
|
||||
for (int i = 0; i < parameters.Length; i++)
|
||||
{
|
||||
parameters[i].valor = sqlParameters[parameters[i].nombre.ToUpper().Trim()].Value;
|
||||
}
|
||||
}
|
||||
cmd.Transaction.Commit();
|
||||
}
|
||||
catch (SqlException ex)
|
||||
{
|
||||
if (cmd.Transaction != null)
|
||||
{
|
||||
cmd.Transaction.Rollback();
|
||||
}
|
||||
throw ex;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (cmd.Transaction != null)
|
||||
{
|
||||
cmd.Transaction.Rollback();
|
||||
}
|
||||
throw ex;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (cmd.Connection != null && cmd.Connection.State == ConnectionState.Open)
|
||||
{
|
||||
cmd.Connection.Close();
|
||||
cmd.Connection.Dispose();
|
||||
cmd.Dispose();
|
||||
}
|
||||
}
|
||||
return parameters;
|
||||
}
|
||||
|
||||
public IEnumerable<T> ExecuteSqlQuery<T>(string comandSql)
|
||||
{
|
||||
return ExecuteSqlQuery<T>(comandSql, null);
|
||||
}
|
||||
|
||||
public IEnumerable<T> ExecuteSqlQuery<T>(string procedureName, DbParametro[] parameters)
|
||||
{
|
||||
IEnumerable<T> queryResult;
|
||||
SqlCommand cmd = new SqlCommand(procedureName);
|
||||
cmd.CommandText = procedureName;
|
||||
cmd.CommandType = CommandType.StoredProcedure;
|
||||
cmd.CommandTimeout = 0;
|
||||
try
|
||||
{
|
||||
// Asignamos los parámetros
|
||||
if (parameters != null)
|
||||
{
|
||||
foreach (DbParametro parametro in parameters)
|
||||
{
|
||||
SqlParameter prm = new SqlParameter();
|
||||
prm.ParameterName = parametro.nombre;
|
||||
prm.SqlDbType = parametro.dbType;
|
||||
prm.Size = parametro.size;
|
||||
prm.Direction = parametro.direccion;
|
||||
prm.Value = parametro.valor;
|
||||
cmd.Parameters.Add(prm);
|
||||
}
|
||||
}
|
||||
// Creamos la Conexion
|
||||
cmd.Connection = new SqlConnection(_cnx);
|
||||
if (cmd.Connection.State.HasFlag(ConnectionState.Closed))
|
||||
cmd.Connection.Open();
|
||||
|
||||
DataTable dataTable = new DataTable();
|
||||
cmd.Transaction = cmd.Connection.BeginTransaction();
|
||||
using (IDataReader reader = cmd.ExecuteReader())
|
||||
{
|
||||
dataTable.Load(reader);
|
||||
queryResult = ConvertTo<T>(dataTable);
|
||||
}
|
||||
cmd.Transaction.Commit();
|
||||
|
||||
}
|
||||
catch (SqlException ex)
|
||||
{
|
||||
if (cmd.Transaction != null)
|
||||
{
|
||||
cmd.Transaction.Rollback();
|
||||
}
|
||||
throw ex;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (cmd.Connection != null && cmd.Connection.State == ConnectionState.Open)
|
||||
{
|
||||
cmd.Connection.Close();
|
||||
cmd.Connection.Dispose();
|
||||
cmd.Dispose();
|
||||
}
|
||||
}
|
||||
return queryResult;
|
||||
}
|
||||
|
||||
//private static SqlParameterCollection CreateParametros(string procedureName, string conexion)
|
||||
//{
|
||||
// SqlCommand cmd = new SqlCommand(procedureName.Trim());
|
||||
// try
|
||||
// {
|
||||
// cmd.CommandType = CommandType.StoredProcedure;
|
||||
// cmd.Connection = new SqlConnection(conexion);
|
||||
// if (cmd.Connection.State == ConnectionState.Closed)
|
||||
// {
|
||||
// cmd.Connection.Open();
|
||||
// }
|
||||
// SqlCommandBuilder.DeriveParameters(cmd);
|
||||
// for (int i = 0; i < cmd.Parameters.Count; i++)
|
||||
// {
|
||||
// string Nombre = cmd.Parameters[i].ParameterName;
|
||||
// if (Nombre.Substring(0, 1) == "@")
|
||||
// {
|
||||
// Nombre = Nombre.Substring(1);
|
||||
// }
|
||||
// cmd.Parameters[i].ParameterName = Nombre.ToUpper();
|
||||
// }
|
||||
// return cmd.Parameters;
|
||||
// }
|
||||
// catch (Exception ex)
|
||||
// {
|
||||
// throw ex;
|
||||
// }
|
||||
// finally
|
||||
// {
|
||||
// cmd.Connection.Dispose();
|
||||
// cmd.Dispose();
|
||||
// }
|
||||
//}
|
||||
|
||||
//private static IList<DbParametro> GetParametersSqlQuery(Object parameters)
|
||||
//{
|
||||
// IList<DbParametro> listaParametro = new List<DbParametro>();
|
||||
|
||||
// foreach (PropertyInfo prop in parameters.GetType().GetProperties())
|
||||
// {
|
||||
// dynamic valor = prop.GetValue(parameters, null);
|
||||
// Type type = Nullable.GetUnderlyingType(prop.PropertyType);
|
||||
|
||||
// if (valor != null)
|
||||
// {
|
||||
// type = (type == null) ? prop.PropertyType : type;
|
||||
|
||||
// if (type != typeof(System.Collections.Generic.ICollection<>))
|
||||
// {
|
||||
// listaParametro.Add(new DbParametro() { nombre = prop.Name, valor = valor });
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// return listaParametro;
|
||||
//}
|
||||
|
||||
private static IList<DbParametro> GetParametersSqlQueryAnotation<T>(T parameters, ActionType actionType)
|
||||
{
|
||||
IList<DbParametro> listaParametro = new List<DbParametro>();
|
||||
|
||||
foreach (PropertyInfo prop in parameters.GetType().GetProperties())
|
||||
{
|
||||
dynamic valor = prop.GetValue(parameters, null);
|
||||
Type type = Nullable.GetUnderlyingType(prop.PropertyType);
|
||||
|
||||
if (valor != null)
|
||||
{
|
||||
type = (type == null) ? prop.PropertyType : type;
|
||||
var attributes = prop.GetCustomAttributes(false);
|
||||
var columnMapping = attributes.FirstOrDefault(a => a.GetType() == typeof(DBParameter));
|
||||
if (columnMapping != null)
|
||||
{
|
||||
DbParametro parametro = new DbParametro() { valor = valor };
|
||||
var mapsto = columnMapping as DBParameter;
|
||||
|
||||
switch (actionType)
|
||||
{
|
||||
case ActionType.Insert:
|
||||
if (mapsto.actionType == ActionType.Everything)
|
||||
{
|
||||
mapsto.actionType = ActionType.Insert;
|
||||
}
|
||||
break;
|
||||
case ActionType.Update:
|
||||
if (mapsto.actionType == ActionType.Everything)
|
||||
{
|
||||
mapsto.actionType = ActionType.Update;
|
||||
}
|
||||
break;
|
||||
case ActionType.Delete:
|
||||
if (mapsto.actionType == ActionType.Everything)
|
||||
{
|
||||
mapsto.actionType = ActionType.Delete;
|
||||
}
|
||||
break;
|
||||
case ActionType.View:
|
||||
if (mapsto.actionType == ActionType.View ||
|
||||
mapsto.actionType == ActionType.Everything)
|
||||
{
|
||||
mapsto.actionType = ActionType.View;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (mapsto.actionType == actionType)
|
||||
{
|
||||
parametro.nombre = (mapsto.name == "") ? prop.Name : mapsto.name;
|
||||
parametro.direccion = (mapsto.Key == true && mapsto.actionType == ActionType.Insert) ? ParameterDirection.Output : mapsto.direction;
|
||||
parametro.dbType = mapsto.dbType;
|
||||
parametro.size = mapsto.size;
|
||||
listaParametro.Add(parametro);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return listaParametro;
|
||||
}
|
||||
|
||||
public static IList<T> ConvertTo<T>(DataTable table)
|
||||
{
|
||||
if (table == null)
|
||||
return null;
|
||||
|
||||
List<DataRow> rows = new List<DataRow>();
|
||||
foreach (DataRow row in table.Rows)
|
||||
rows.Add(row);
|
||||
|
||||
return ConvertTo<T>(rows);
|
||||
}
|
||||
|
||||
public static IList<T> ConvertTo<T>(IList<DataRow> rows)
|
||||
{
|
||||
IList<T> list = null;
|
||||
if (rows != null)
|
||||
{
|
||||
list = new List<T>();
|
||||
foreach (DataRow row in rows)
|
||||
{
|
||||
T item = CreateItem<T>(row);
|
||||
list.Add(item);
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
public static T CreateItem<T>(DataRow row)
|
||||
{
|
||||
string columnName;
|
||||
T obj = default(T);
|
||||
if (row != null)
|
||||
{
|
||||
obj = Activator.CreateInstance<T>();
|
||||
foreach (DataColumn column in row.Table.Columns)
|
||||
{
|
||||
columnName = column.ColumnName;
|
||||
//Get property with same columnName
|
||||
PropertyInfo prop = obj.GetType().GetProperty(columnName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
|
||||
try
|
||||
{
|
||||
if (prop != null)
|
||||
{
|
||||
//Get value for the column
|
||||
object value = (row[columnName].GetType() == typeof(DBNull)) ? null : row[columnName];
|
||||
//Set property value
|
||||
prop.SetValue(obj, value, null);
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
throw;
|
||||
//Catch whatever here
|
||||
}
|
||||
}
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
using System.Data;
|
||||
|
||||
namespace Net.Connection
|
||||
{
|
||||
public class DbParametro
|
||||
{
|
||||
public string nombre { get; set; }
|
||||
public object valor { get; set; }
|
||||
|
||||
public int size { get; set; }
|
||||
public ParameterDirection direccion { get; set; }
|
||||
public SqlDbType dbType { get; set; }
|
||||
|
||||
public DbParametro()
|
||||
{
|
||||
nombre = "";
|
||||
valor = null;
|
||||
direccion = ParameterDirection.Input;
|
||||
}
|
||||
|
||||
public DbParametro(string Nombre, object Valor, ParameterDirection Direccion)
|
||||
{
|
||||
nombre = Nombre;
|
||||
valor = Valor;
|
||||
direccion = Direccion;
|
||||
}
|
||||
|
||||
public DbParametro(string Nombre, object Valor)
|
||||
{
|
||||
nombre = Nombre;
|
||||
valor = Valor;
|
||||
direccion = ParameterDirection.Input;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Net.Connection
|
||||
{
|
||||
public interface IConnectionSQL
|
||||
{
|
||||
public string _cnx { get; set; }
|
||||
public string DevuelveConnectionSQL();
|
||||
public object ExecuteSqlInsert<T>(string procedureName, object parameters);
|
||||
public object ExecuteSqlUpdate<T>(string procedureName, object parameters);
|
||||
public object ExecuteSqlDelete<T>(string procedureName, T parameters);
|
||||
public T ExecuteSqlViewId<T>(string procedureName, object parameters);
|
||||
public IEnumerable<T> ExecuteSqlViewFindByCondition<T>(string procedureName, object parameters);
|
||||
public IEnumerable<T> ExecuteSqlViewAll<T>(string procedureName, T parameters);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
|
||||
<PackageReference Include="Microsoft.Data.SqlClient" Version="2.0.1" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Net.Connection.Attributes\Net.Connection.Attributes.csproj" />
|
||||
<ProjectReference Include="..\Net.CrossCotting\Net.CrossCotting.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,14 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Net.Connection
|
||||
{
|
||||
public interface IRepositoryBase<T>
|
||||
{
|
||||
IEnumerable<T> FindAll(T entity, string storeProcedure);
|
||||
IEnumerable<T> FindByCondition(object entity, string storeProcedure);
|
||||
T FindById(object entity, string storeProcedure);
|
||||
int Create(object entity, string storeProcedure);
|
||||
void Update(object entity, string storeProcedure);
|
||||
void Delete(T entity, string storeProcedure);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Net.Connection
|
||||
{
|
||||
public abstract class RepositoryBase<T> : IRepositoryBase<T> where T : class
|
||||
{
|
||||
protected IConnectionSQL context { get; set; }
|
||||
|
||||
public RepositoryBase(IConnectionSQL context)
|
||||
{
|
||||
this.context = context;
|
||||
}
|
||||
public IEnumerable<T> FindAll(T entity, string storeProcedure)
|
||||
{
|
||||
return context.ExecuteSqlViewAll<T>(storeProcedure, entity);
|
||||
}
|
||||
|
||||
public IEnumerable<T> FindByCondition(object entity, string storeProcedure)
|
||||
{
|
||||
return context.ExecuteSqlViewFindByCondition<T>(storeProcedure, entity);
|
||||
}
|
||||
|
||||
public T FindById(object entity, string storeProcedure)
|
||||
{
|
||||
return context.ExecuteSqlViewId<T>(storeProcedure, entity);
|
||||
}
|
||||
|
||||
public int Create(object entity, string storeProcedure)
|
||||
{
|
||||
return (int)context.ExecuteSqlInsert<T>(storeProcedure, entity);
|
||||
}
|
||||
|
||||
public void Delete(T entity, string storeProcedure)
|
||||
{
|
||||
context.ExecuteSqlDelete<T>(storeProcedure, entity);
|
||||
}
|
||||
|
||||
public void Update(object entity, string storeProcedure)
|
||||
{
|
||||
this.context.ExecuteSqlUpdate<T>(storeProcedure, entity);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user