public async Task <IActionResult> Get() { try { var deviceOptions = await _mediatorForOptionsRep.GetDeviceOptionsAsync(); var exchangeOptions = await _mediatorForOptionsRep.GetExchangeOptionsAsync(); var transportOption = await _mediatorForOptionsRep.GetTransportOptionsAsync(); var deviceOptionsDto = _mapper.Map <List <DeviceOptionDto> >(deviceOptions); var exchangeOptionsDto = _mapper.Map <List <ExchangeOptionDto> >(exchangeOptions); var transportOptionDto = _mapper.Map <TransportOptionsDto>(transportOption); var agregatorOptionDto = new OptionAgregatorDto { DeviceOptions = deviceOptionsDto, ExchangeOptions = exchangeOptionsDto, TransportOptions = transportOptionDto }; return(new JsonResult(agregatorOptionDto)); } catch (Exception ex) { _logger.Error(ex, "Ошибка в DevicesOptionController/Get"); throw; } }
public async Task <IActionResult> Post([FromBody] OptionAgregatorDto data) { if (data == null) { ModelState.AddModelError("AgregatorOptionDto", "POST body is null"); return(BadRequest(ModelState)); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } try { var deviceOptionDto = data.DeviceOptions?.FirstOrDefault(); var exchangeOptionDto = data.ExchangeOptions; var transportOptionDto = data.TransportOptions; var deviceOption = _mapper.Map <DeviceOption>(deviceOptionDto); var exchangeOption = _mapper.Map <IEnumerable <ExchangeOption> >(exchangeOptionDto); var transportOption = _mapper.Map <TransportOption>(transportOptionDto); await _mediatorForOptionsRep.AddDeviceOptionAsync(deviceOption, exchangeOption, transportOption); return(CreatedAtAction("Get", new { deviceName = deviceOptionDto.Name }, data)); //возвращает в ответе данные запроса. в Header пишет значение Location→ http://localhost:44138/api/DevicesOption/{deviceName} } catch (OptionHandlerException ex) { _logger.Error(ex, "Ошибка в DevicesOptionController/Post"); ModelState.AddModelError("PostException", ex.Message); return(BadRequest(ModelState)); } catch (Exception ex) { _logger.Error(ex, "Критическая Ошибка в DevicesOptionController/Post"); throw; } }