public async Task <HttpResponseMessage> UpdateFilmAsync(UpdateFilmCommand command) { try { return(await client.PutAsync($"{baseUrl}/api/films/", RequestHelper.GetStringContentFromObject(command))); } catch (Exception e) { var message = e.InnerException.Message; throw; } }
public async Task <IActionResult> Update(UpdateFilmCommand command) { try { return(Ok(await Mediator.Send(command))); } catch (ValidationException e) { return(BadRequest(e.Message)); } catch (ArgumentNullException) { return(BadRequest("No entities with this primary key were found in the database.")); } }
public IActionResult Put(int id, [FromBody] Film film) { if (!this.ModelState.IsValid) { return(this.BadRequest(this.ModelState)); } try { var command = new UpdateFilmCommand(id, film); this.handler.Handle(command); } catch (InvalidOperationException) { return(this.StatusCode(403)); } return(this.StatusCode(204)); }
public async Task <IActionResult> Put([FromBody] UpdateFilmCommand command) { var entity = await _context.Films.FirstOrDefaultAsync(e => e.Id == command.Id); if (entity == null) { throw new NotFoundException(nameof(Film), command.Id); } entity.Id = command.Id; entity.Budget = command.Budget; entity.Length = command.Length; entity.ReleaseYear = command.ReleaseYear; entity.Name = command.Name; entity.Genre = command.Genre; entity.Director = command.Director; await _context.SaveChangesAsync(); return(NoContent()); }
public object Any(UpdateFilmCommand command) { return(new UpdateFilmCommandResponse { Result = ResultType.OK }); }
public FilmsModule(Handler handler) : base("/api/films") { this.handler = handler; this.Get("/", async(request, response, routeData) => { var command = new ListFilmsCommand(); var films = this.handler.Execute <ListFilmsCommand, IEnumerable <Film> >(command); await response.AsJson(films); }); this.Get("/{id:int}", async(request, response, routeData) => { var command = new ListFilmsByIdCommand(routeData.As <int>("id")); var film = this.handler.Execute <ListFilmsByIdCommand, Film>(command); if (film == null) { response.StatusCode = 404; return; } await response.AsJson(film); }); this.Post("/", async(req, res, routeData) => { var result = req.BindAndValidate <Film>(); if (!result.ValidationResult.IsValid) { res.StatusCode = 422; await res.Negotiate(result.ValidationResult.GetFormattedErrors()); return; } try { var command = new CreateFilmCommand(result.Data); this.handler.Handle(command); res.StatusCode = 201; } catch (Exception) { res.StatusCode = 403; } }); this.Put("/{id:int}", async(req, res, routeData) => { var result = req.BindAndValidate <Film>(); if (!result.ValidationResult.IsValid) { res.StatusCode = 422; await res.Negotiate(result.ValidationResult.GetFormattedErrors()); return; } try { var command = new UpdateFilmCommand(routeData.As <int>("id"), result.Data); this.handler.Handle(command); res.StatusCode = 204; } catch (Exception) { res.StatusCode = 403; } }); this.Delete("/{id:int}", (req, res, routeData) => { try { var command = new DeleteFilmCommand(routeData.As <int>("id")); this.handler.Handle(command); res.StatusCode = 204; } catch (Exception) { res.StatusCode = 403; } return(Task.CompletedTask); }); }
public async Task <IActionResult> UpdateAsync([FromBody] UpdateFilmCommand command, CancellationToken cancellationToken) { await _updateFilm.HandleAsync(command, cancellationToken); return(NoContent()); }