Skip to content

Instantly share code, notes, and snippets.

@zulu15
Last active August 23, 2019 18:08
Show Gist options
  • Save zulu15/2fd422a6580741ff4f68a0178681d89b to your computer and use it in GitHub Desktop.
Save zulu15/2fd422a6580741ff4f68a0178681d89b to your computer and use it in GitHub Desktop.
public HttpResponseMessage GetClientes(string browserId)
{
log.Debug("Entró el browserId "+browserId);
Cliente cliente = null;
ServerResponse serverResponse = new ServerResponse();
try
{
//Buscamos un cliente que no haya sacado
using (SqlConnection conexion = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["localhost"].ConnectionString))
{
if (conexion.State != ConnectionState.Open) conexion.Open();
// Traemos si existe el cliente que está asociado al browserId
using (SqlCommand command = new SqlCommand("SELECT TOP 1 clientes.* FROM (select * from clientes) clientes LEFT JOIN (select cliente_id from clientes_browser) clientes_sync ON clientes.id = clientes_sync.cliente_id where clientes_sync.cliente_id is null", conexion))
{
using (SqlDataReader dr = command.ExecuteReader())
{
while (dr.Read())
{
cliente = new Cliente(Int32.Parse(dr.GetValue(0).ToString()),
dr.GetValue(1).ToString(),
dr.GetValue(2).ToString(),
dr.GetValue(3).ToString(),
dr.GetValue(4).ToString(),
dr.GetValue(5).ToString(),
dr.GetValue(6).ToString(),
dr.GetValue(7).ToString()
);
log.Debug("Devolvimos el cliente " + cliente.Id + " para browserId "+browserId);
}
}
}
}
serverResponse.Response = cliente;
}
catch (Exception e)
{
serverResponse.Error = "Error al obtener un cliente " + e.Message;
}
bool isBrowserSync = false;
if (serverResponse.Error == null)
{
//Asociamos el browserId con el cliente encontrado
int executionResult = -1;
try
{
//Obtenemos la conexión
using (SqlConnection conexion = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["localhost"].ConnectionString))
{
if (conexion.State != ConnectionState.Open) conexion.Open();
// Traemos los datos de los anuncios
using (SqlCommand command = new SqlCommand("INSERT INTO clientes_browser VALUES(@browserId,@clienteId,0)", conexion))
{
command.Parameters.AddWithValue("@browserId", browserId);
command.Parameters.AddWithValue("@clienteId", cliente.Id);
executionResult = command.ExecuteNonQuery();
log.Debug("Insertamos el cliente " + cliente.Id + " para browserId " + browserId);
}
}
}
catch (Exception e)
{
serverResponse.Error = e.Message;
}
}
var response = Request.CreateResponse(HttpStatusCode.OK);
response.Content = new StringContent(JsonConvert.SerializeObject(serverResponse), Encoding.UTF8, "application/json");
log.Debug("finaliza browserId " + browserId);
return response;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment