public async Task <Response> PostTalonera( string urlBase, string servicePrefix, string controller, CreateTaloneraRequest request) { try { string requestString = JsonConvert.SerializeObject(request); StringContent content = new StringContent(requestString, Encoding.UTF8, "application/json"); HttpClient client = new HttpClient { BaseAddress = new Uri(urlBase) }; string url = $"{servicePrefix}{controller}"; HttpResponseMessage response = await client.PostAsync(url, content); string result = await response.Content.ReadAsStringAsync(); if (!response.IsSuccessStatusCode) { return(new Response { IsSuccess = false, Message = result, }); } return(new Response { IsSuccess = true, Result = "exitoso" }); } catch (Exception ex) { return(new Response { IsSuccess = false, Message = ex.Message }); } }
public async Task <IActionResult> PostTalonera([FromBody] CreateTaloneraRequest request) { if (!ModelState.IsValid) { return(BadRequest()); } EmpresaEntity empresa = _context.Empresa.Where(e => e.id == request.EmpresaId).FirstOrDefault(); if (empresa == null) { return(BadRequest(new Response { IsSuccess = false, Message = "La empresa especificada no existe." })); } DistribuidorEntity dist = _context.Distribuidor.Where(d => d.id == request.DistId).FirstOrDefault(); if (dist == null) { return(BadRequest(new Response { IsSuccess = false, Message = "El distribuidor especificado no existe." })); } _context.Talonera.Add(new TaloneraEntity { RangoInicio = request.RangoInicio, RangoFin = request.RangoFin, Empresa = empresa, Distribuidor = dist, StatusTalonera = "Activo" });; await _context.SaveChangesAsync(); return(NoContent()); }