public async Task <IActionResult> AddVisitsTicketTariffAsync(string visitTariffId, [FromBody] TicketTariffDto ticketTariff) { _logger.LogInformation($"Starting method '{nameof(AddVisitsTicketTariffAsync)}'."); if (string.IsNullOrEmpty(visitTariffId)) { return(OnInvalidParameterError($"Parameter '{nameof(visitTariffId)}' cannot be null or empty.")); } TicketTariff ticketTariffToBeAdded = null; VisitTariff visitTariff = null; try { visitTariff = await _visitTariffDbService.GetAsync(visitTariffId); // Ignore Id if the client set it. Id of entity is set internally by the server. ticketTariff.Id = null; ticketTariffToBeAdded = MapToDomainModel(ticketTariff); var addedTicketTariff = await _ticketTariffDbService.RestrictedAddAsync(ticketTariffToBeAdded); // Add new ticket tariff to parent visit tariff. visitTariff.TicketTariffs.Add(addedTicketTariff); await _visitTariffDbService.RestrictedUpdateAsync(visitTariff); // Reverse map only for response to the client. var addedTariffDto = MapToDto(addedTicketTariff); var response = new ResponseWrapper(addedTariffDto); string addedTicketTariffUrl = $"{ControllerPrefix}/{addedTicketTariff.Id}"; _logger.LogInformation($"Finished method '{nameof(AddVisitsTicketTariffAsync)}'."); return(Created(addedTicketTariffUrl, response)); } catch (InvalidOperationException ex) when(visitTariff is null) { return(OnNotFoundError($"Cannot found '{typeof(VisitTariff).Name}' with specified id: '{visitTariffId}'.", ex)); } catch (InvalidOperationException ex) { return(OnInvalidParameterError($"Element '{typeof(TicketTariff).Name}' already exists.", ex)); } catch (InternalDbServiceException ex) { LogInternalDbServiceException(ex, _ticketTariffDbService.GetType()); throw; } catch (Exception ex) { LogUnexpectedException(ex); throw; } }
public async Task <IActionResult> UpdateTariffAsync(string id, [FromBody] VisitTariffDto tariff) { _logger.LogInformation($"Starting method '{nameof(UpdateTariffAsync)}'."); if (string.IsNullOrEmpty(id)) { return(OnInvalidParameterError($"An argument '{nameof(id)}' cannot be null or empty.")); } if (!id.Equals(tariff.Id)) { return(OnMismatchParameterError($"An '{nameof(id)}' in URL end field '{nameof(tariff.Id).ToLower()}' in request body mismatches. Value in URL: '{id}'. Value in body: '{tariff.Id}'.")); } try { var tariffToBeUpdated = MapToDomainModel(tariff); var updatedTariff = await _tariffDbService.RestrictedUpdateAsync(tariffToBeUpdated); // Revers map for client response. tariff = MapToDto(updatedTariff); _logger.LogInformation($"Finished method '{nameof(UpdateTariffAsync)}'."); var response = new ResponseWrapper(tariff); return(Ok(response)); } catch (InvalidOperationException ex) { return(OnNotFoundError($"Cannot found element '{typeof(VisitTariff).Name}' with specified id: '{id}'.", ex)); } catch (InternalDbServiceException ex) { LogInternalDbServiceException(ex, _tariffDbService.GetType()); throw; } catch (Exception ex) { LogUnexpectedException(ex); throw; } }