public IActionResult RechargeCard([FromBody] RechargeCard_ViewModel rechargeCardInfo) { try { if (ModelState.IsValid)//checking model state { var cardRechargeResult = CardsRepo.RechargeCard(rechargeCardInfo); if (cardRechargeResult.GetType() == typeof(CardChargeExpirationDate_ViewModel)) { return(Ok(cardRechargeResult)); //if the creation is successful return the new Expiration date } if (!(rechargeCardInfo.ChargeEuros == 30 || rechargeCardInfo.ChargeEuros == 60 || rechargeCardInfo.ChargeEuros == 200)) { return(BadRequest(_config["StatusCodesText:RechargeCardErr"])); //if not... return bad request... } if (cardRechargeResult.GetType() == typeof(bool)) { return(BadRequest(_config["StatusCodesText:GeneralInputError"])); } } return(BadRequest(ModelState)); //if model state is not valid } catch (Exception e) { return(StatusCode(500, _config["StatusCodesText:ServerErr"])); } }
public dynamic RechargeCard(RechargeCard_ViewModel rechargeCardInfo) { try { //check the card's existance. If it DOES exist then take the record, otherwise return false... var cardRecordToUpdate = db.Cards.Where(w => w.CardId == rechargeCardInfo.CardId) .FirstOrDefault(); if (cardRecordToUpdate == null) //if no card record is found under given cardId return false... { return(false); } //retrieve the number of days to extend based on the amount of money given... var daysToExtend_TicketExpirationDate = Convert_EurosToDays(rechargeCardInfo.ChargeEuros); if (daysToExtend_TicketExpirationDate == 0) { return(false); } //update necessary fields... cardRecordToUpdate.LastRechargedOn = DateTime.Now; //if the expiration date is not cardRecordToUpdate.ChargeExpiresOn = cardRecordToUpdate.ChargeExpiresOn > DateTime.Now ? cardRecordToUpdate.ChargeExpiresOn.AddDays(daysToExtend_TicketExpirationDate) : DateTime.Now.AddDays(daysToExtend_TicketExpirationDate); db.Cards.Update(cardRecordToUpdate); var savingResult = db.SaveChanges(); if (savingResult != 0)//check if an error has occured... { return new CardChargeExpirationDate_ViewModel() { ChargeExpirationDate = cardRecordToUpdate.ChargeExpiresOn } } ; return(false); } catch (Exception e) { throw e; } }