public IActionResult UpdateEvent(int id, [FromBody] EventUpdateDto updateEvent) { var eventEntity = _unitOfWork.Event.GetById(id); if (eventEntity == null) { return(NotFound()); } if (updateEvent == null) { return(NotFound()); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } _mapper.Map(updateEvent, eventEntity); if (!_unitOfWork.Save()) { return(StatusCode(500, "A problem happened while handling your request.")); } return(GetEventById(id)); }
public IActionResult EventUpdate(int id, [FromBody] EventUpdateDto updateEvent) { var eventEntity = _transActionRepo.GetEvent(id); if (eventEntity == null) { return(NotFound()); } if (updateEvent == null) { return(NotFound()); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } Mapper.Map(updateEvent, eventEntity); if (!_transActionRepo.Save()) { return(StatusCode(500, "A problem happened while handling your request.")); } return(NoContent()); }
public ActionResult <EventDto> UpdateEvent(int id, [FromBody] EventUpdateDto eventUpdateDto) { if (eventUpdateDto == null) { return(BadRequest()); } var existingEventItem = _eventRepository.GetSingle(id); if (existingEventItem == null) { return(NotFound()); } _mapper.Map(eventUpdateDto, existingEventItem); _eventRepository.Update(id, existingEventItem); if (!_eventRepository.Save()) { throw new Exception("Updating an event failed on save."); } return(Ok(_mapper.Map <EventDto>(existingEventItem))); }
public ActionResult <EventDto> PartiallyUpdateEvent(int id, [FromBody] JsonPatchDocument <EventUpdateDto> patchDoc) { if (patchDoc == null) { return(BadRequest()); } EventEntity existingEntity = _eventRepository.GetSingle(id); if (existingEntity == null) { return(NotFound()); } EventUpdateDto eventUpdateDto = _mapper.Map <EventUpdateDto>(existingEntity); patchDoc.ApplyTo(eventUpdateDto); TryValidateModel(eventUpdateDto); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } _mapper.Map(eventUpdateDto, existingEntity); EventEntity updated = _eventRepository.Update(id, existingEntity); if (!_eventRepository.Save()) { throw new Exception("Updating an event failed on save."); } return(Ok(_mapper.Map <EventDto>(updated))); }
public async Task <IActionResult> UpdateEvent([FromRoute] int id, [FromBody] EventUpdateDto evtUpdDto) { var eventItem = await _repository.GetEventById(id); if (eventItem == null) { return(NotFound()); } _mapper.Map(evtUpdDto, eventItem); //Real Update _repository.UpdateEvent(eventItem); // Not Necessary return(NoContent()); }
public async Task <ActionResult> UpdateAsync([FromForm] EventUpdateDto eventUpdateDto) { _logger.LogMethodCallingWithObject(eventUpdateDto); var role = User.Claims.FirstOrDefault(x => x.Type.Equals(ClaimsIdentity.DefaultRoleClaimType))?.Value; var userId = User.Claims.FirstOrDefault(x => x.Type.Equals(ClaimsIdentity.DefaultNameClaimType))?.Value; var organizerId = await _eventManager.GetEventOrganizerIdAsync(eventUpdateDto.Id); if (role != "Admin" && !Equals(userId, organizerId.ToString())) { return(Forbid("Access denied")); } var hostRoot = _hostServices.GetHostPath(); await _eventManager.UpdateEventAsync(eventUpdateDto, hostRoot); return(Ok()); }
public IActionResult UpdateEvent(int id, [FromBody] EventUpdateDto updateEvent) { if (!ModelState.IsValid) { return(BadRequest(new TransActionResponse(ModelState))); } var eventEntity = _unitOfWork.Event.GetById(id); if (eventEntity == null) { return(StatusCode(404, new TransActionResponse("Event Not found"))); } _mapper.Map(updateEvent, eventEntity); _unitOfWork.Event.Update(eventEntity); if (!_unitOfWork.Save()) { return(StatusCode(500, new TransActionResponse("A problem happened while handling your request."))); } return(GetEventById(id)); }
public async Task <IActionResult> UpdateEvent([FromRoute] Guid id, [FromBody] EventUpdateDto eventToUpdate) { //model state validation is not required due to the [ApiController] attribute automatically returning UnprocessableEntity (see startup.cs) //when model binding fails var eventFromRepo = await _eventRepository.GetEventAsync(id); if (eventFromRepo == null) { return(NotFound()); } //fetch the user id from the JWT via HttpContext. Then get the user from the repository. This is to ensure that an authorized user //is calling the API with a valid user id var user = await _userRepository.GetUserAsync(User.GetUserId()); //check if the user is an administrator or owner of the object var isAdmin = User?.IsAdministrator() ?? false; var userIsAdminOrOwner = isAdmin ? isAdmin : (user?.Id != eventFromRepo.UserId ? false : true); if (!userIsAdminOrOwner) { //returning status code instead of Forbid() as forbid only works with authentication handlers return(StatusCode(403, new { Error = "You cannot modify other users submissions unless you are an administrator." })); } _mapper.Map(eventToUpdate, eventFromRepo); //map fields from EventUpdateDto to Event that was fetched from repository _eventRepository.UpdateEvent(eventFromRepo); //Call to empty method. This is just for information and is not required if (!await _eventRepository.SaveChangesAsync()) { throw new Exception($"Error updating Event {eventFromRepo.Id} to the database"); } return(NoContent()); }