public async Task <IActionResult> PutEvent([FromRoute] int id, [FromBody] ViewModel.Event @event) { try { _logger.LogInformation("EventController: PutEvent() by ID is being invoked"); if (!ModelState.IsValid) { _logger.LogWarning("EventController: Invalid request is passed"); return(BadRequest(ModelState)); } if (id != @event.Id) { _logger.LogWarning("EventController: EventID is not correct"); return(BadRequest()); } var config = new MapperConfiguration(cfg => { cfg.CreateMap <ViewModel.Event, Model.Event>(); }); var mapper = config.CreateMapper(); var modelEvent = mapper.Map <ViewModel.Event, Event>(@event); var result = await _eventRepository.UpdateEvent(id, modelEvent); _logger.LogInformation("EventController: PutEvent() by ID is successfully invoked"); return(result == true?Ok(@event) : Ok(result)); } catch (Exception ex) { _logger.LogError("EventController: Erro processing PutEvent() method. Exception: {0}", ex.Message); return(null); } }
public async Task <IActionResult> PostEvent([FromBody] ViewModel.Event @event) { try { _logger.LogInformation("EventController.PostEvent(): Method is being invoked"); if (!ModelState.IsValid) { _logger.LogWarning("EventController.PostEvent(): Invalid request is passed."); return(BadRequest(ModelState)); } var config = new MapperConfiguration(cfg => { cfg.CreateMap <ViewModel.Event, Model.Event>(); }); var mapper = config.CreateMapper(); var modelEvent = mapper.Map <ViewModel.Event, Event>(@event); var response = await _eventRepository.CreateEvent(modelEvent); _logger.LogInformation("EventController.PostEvent(): Method is successfully invoked"); return(CreatedAtAction("GetEvent", new { id = @event.Id }, @event)); } catch (Exception ex) { _logger.LogError("EventController.PostEvent(): Error processing the method. Exception: {0}", ex.Message); return(StatusCode(StatusCodes.Status500InternalServerError)); } }