public async Task <IActionResult> Update(EventPutRequest request) { try { if (request == null || !ModelState.IsValid) { return(BadRequest(request)); } ResultHandler <EventModel> getByIdResult = await this._eventsService.GetById(request.Id); if (!getByIdResult.Success) { return(StatusCode(500, getByIdResult.ErrorCode)); } EventModel item = getByIdResult.Value as EventModel; string ownerId = this._userService.OwnerId.ToString(); if (!item.OwnerId.Equals(ownerId)) { return(Unauthorized(request)); } item.End = request.End; item.IsAllDay = request.IsAllDay; item.Title = request.Name; item.Start = request.Start; item.URL = request.URL; item.Base64Id = request.Base64Id; item.Description = request.Description; item.Details = request.Details; item.ImagePath = request.ImagePath; item.UpdateDate = DateTime.UtcNow; ResultHandler result = await this._eventsService.Update(item); if (result.Success) { EventModelDTO eventModelDTO = this._mapper.Map <EventModelDTO>(item); return(Ok(eventModelDTO)); } else { return(StatusCode(500, result.ErrorCode)); } } catch (Exception ex) { this._log.LogError(ex, "EventsController.Put"); return(StatusCode(500, ErrorCode.Exception)); } }
public ActionResult Put([FromBody] EventPutRequest request) { try { if (!ModelState.IsValid) { return(BadRequest()); } ResultService <EventModel> getByIdResult = this._eventsService.GetById(request.Id); if (!getByIdResult.Success) { return(StatusCode(500, getByIdResult.ErrorCode)); } EventModel item = getByIdResult.Value as EventModel; item.End = request.End; item.IsAllDay = request.IsAllDay; item.Name = request.Name; item.Start = request.Start; item.URL = request.URL; item.UpdateDate = DateTime.UtcNow; ResultService result = this._eventsService.Update(item); if (result.Success) { return(Ok()); } else { return(StatusCode(500, result.ErrorCode)); } } catch (Exception ex) { return(StatusCode(500, ErrorCode.Unknown)); //TODO: Log the Exception. } }
public async Task Put_WhenDifferentOwnerId_ShouldReturnUnauthorized() { //Arrange ResultHandler <EventModel> expected = ResultHandler.Ok(this._eventModels.First()); this._genericServiceMock .Setup(x => x.GetById(It.IsAny <Guid>())) .ReturnsAsync(expected); this._genericServiceMock .Setup(x => x.Update(It.IsAny <EventModel>())) .ReturnsAsync(ResultHandler.Ok()); this._userServiceMock .Setup(x => x.OwnerId) .Returns(Guid.NewGuid()); //Act EventPutRequest eventPutRequest = new EventPutRequest(); IActionResult actual = await this._controller.Update(eventPutRequest); //Assert AssertHttpCode <UnauthorizedObjectResult>(actual, eventPutRequest.GetHashCode()); }