public async Task <ActionResult <DelayedChargeModel> > Post(int invoiceId, DelayedChargeModel model) { try { //Make sure DelayedChargeId is not already taken var existing = await _repository.GetDelayedChargeAsync(invoiceId, model.Id); if (existing != null) { return(BadRequest("DelayedCharge Id in Use")); } //map var DelayedCharge = _mapper.Map <DelayedCharge>(model); //save and return if (!await _repository.StoreNewDelayedChargeAsync(invoiceId, DelayedCharge)) { return(BadRequest("Bad request, could not create record!")); } else { var location = _linkGenerator.GetPathByAction("Get", "DelayedCharge", new { invoiceId = DelayedCharge.InvoiceId, DelayedCharge.Id }); return(Created(location, _mapper.Map <DelayedChargeModel>(DelayedCharge))); } } catch (Exception e) { _logger.LogError(e, e.Message); return(this.StatusCode(StatusCodes.Status500InternalServerError, "Database Failure")); } }
public async Task <ActionResult <DelayedChargeModel> > Put(int invoiceId, int Id, DelayedChargeModel updatedModel) { try { var currentDelayedCharge = await _repository.GetDelayedChargeAsync(invoiceId, Id); if (currentDelayedCharge == null) { return(NotFound($"Could not find DelayedCharge with Id of {Id}")); } _mapper.Map(updatedModel, currentDelayedCharge); if (await _repository.UpdateDelayedChargeAsync(invoiceId, currentDelayedCharge)) { return(_mapper.Map <DelayedChargeModel>(currentDelayedCharge)); } } catch (Exception e) { _logger.LogError(e, e.Message); return(this.StatusCode(StatusCodes.Status500InternalServerError, "Database Failure")); } return(BadRequest()); }