public async Task <IActionResult> DiscountCardRegistration([FromBody] DiscountCardRegistrationViewModel dcr) { TransportCard transportCard = await _cardService.GetTransportCardById(dcr.TransportCardId); if ((DateTime.Now.Date - transportCard.CreateDate.Date).Days > 60) { return(BadRequest(new { response_message = "Cannot Proceed. Card is only eligible for discount registration within 60 days after the card was purchased.", isError = true })); } DiscountCardDetails toSave = new DiscountCardDetails { TransportCardId = dcr.TransportCardId, GovernmentIdNumber = dcr.IdNumber, GovernmentIdType = dcr.IdType }; CardType cardType = await _cardTypeService.GetCardTypeById(dcr.CardTypeId); bool discountDetailsResult = await _discountCardDetailsService.AddDiscountCardDetails(toSave); if (!discountDetailsResult) { HttpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest; return(BadRequest(new { response_message = "Internal Server Error. Please contact your local IT.", isError = true })); } bool transportCardUpdateResult = await _cardService.DiscountCardRegistration(dcr.TransportCardId, cardType.id); //rolling back / deleting inserted if (!transportCardUpdateResult) { DiscountCardDetails discountCardDetails = await _discountCardDetailsService.GetDiscountCardDetailsById(cardType.id); bool responseRollback = await _discountCardDetailsService.DeleteDiscountCardDetails(discountCardDetails); //while (!responseRollback) //{ // await Task.Delay(3000); // responseRollback = _discountCardDetailsService.DeleteDiscountCardDetails(discountCardDetails).GetAwaiter().GetResult(); //} //transportCardUpdateResult = true; } if (transportCardUpdateResult) { return(Ok(new { response_message = "Successfully registered discount card.", isError = false })); } return(BadRequest(new { response_message = "Internal Server Error. Please contact your local IT.", isError = true })); }
public async Task <bool> DeleteDiscountCardDetails(DiscountCardDetails discountCardDetails) { try { _db.DiscountCardDetails.Remove(discountCardDetails); await _db.SaveChangesAsync(); return(true); } catch (Exception) { return(false); } }
public async Task <bool> AddDiscountCardDetails(DiscountCardDetails discountCardDetails) { try { await _db.DiscountCardDetails.AddAsync(discountCardDetails); await _db.SaveChangesAsync(); return(true); } catch (Exception ex) { return(false); } }
public async Task <DiscountCardDetails> GetDiscountCardDetailsById(int id) { DiscountCardDetails toReturn = await Task.Run(() => _db.DiscountCardDetails.Where(x => x.TransportCardId == id).SingleOrDefault()); return(toReturn); }