public ActionResult <ProdutoDTO> CadastrarLance([FromBody] LanceDTO DataLance) { try { ClaimsIdentity identity = (ClaimsIdentity)HttpContext.User.Identity; Leilao le = new Leilao(db); le.CadastrarLance(DataLance, identity); return(StatusCode(200, new { message = "Lance Efetuado com Sucesso" })); } catch (HttpException ex) { return(StatusCode((int)ex.CodeStatus, new { ID = ex.traceID, Error = ex.Message, Describe = ex.describe })); } }
public void CadastrarLance(LanceDTO lance, ClaimsIdentity identity) { try { decimal LanceAtual = db.TB_LE_LANCEs.Where(x => x.ID_PRODUTO == lance.ID_PRODUTO).OrderByDescending(x => x.VL_LANCE).Select(x => x.VL_LANCE).FirstOrDefault(); decimal vlProduto = db.TB_LE_PRODUTOs.Where(x => x.ID_PRODUTO == lance.ID_PRODUTO).Max(x => x.VL_PRODUTO); if (LanceAtual >= lance.VL_LANCE) { throw new HttpException("Erro ao Efetuar Lance", "Não foi possivel fazer um lance neste produto pois o valor e Igual ou inferior ao maior lance atual ja feito", HttpStatusCode.BadRequest); } if (vlProduto >= lance.VL_LANCE) { throw new HttpException("Erro ao Efetuar Lance", "Não foi possivel fazer um lance neste produto pois o valor e Igual ou inferior ao valor do Produto", HttpStatusCode.BadRequest); } List <Claim> claim = identity.Claims.ToList(); TB_LE_LANCE LanceObject = new TB_LE_LANCE(); LanceObject.ID_PRODUTO = lance.ID_PRODUTO; LanceObject.ID_USUARIO = int.Parse(claim[0].Value); LanceObject.VL_LANCE = lance.VL_LANCE; LanceObject.DT_LANCE = DateTime.Now; db.TB_LE_LANCEs.Add(LanceObject); db.SaveChanges(); } catch (HttpException) { throw; } catch (Exception ex) { throw new HttpException("Erro Interno no Sistema", string.Format("Ocorrou um erro durante o cadastro. Contate o administrador. {0}", ex.Message), HttpStatusCode.InternalServerError); } }