/// <summary> /// Return a ProblemJson. Status code wil be set according to the ProbkemJson. /// </summary> /// <param name="controller"></param> /// <param name="problemJson">The Problem description.</param> /// <returns></returns> public static ActionResult Problem(this Controller controller, ProblemJson problemJson) { return(new ObjectResult(problemJson) { StatusCode = problemJson.StatusCode }); }
public async Task <ActionResult> CustomerMove(int key, [SingleParameterBinder(typeof(NewAddress))] NewAddress newAddress) { if (newAddress == null) { return(this.Problem(ProblemJsonBuilder.CreateBadParameters())); } try { var customer = await customerRepository.GetEnitityByKeyAsync(key); var hypermediaCustomer = new HypermediaCustomer(customer); hypermediaCustomer.MoveAction.Execute(newAddress); return(Ok()); } catch (EntityNotFoundException) { return(this.Problem(ProblemJsonBuilder.CreateEntityNotFound())); } catch (CanNotExecuteActionException) { return(this.CanNotExecute()); } catch (ActionParameterValidationException e) { var problem = new ProblemJson() { Title = $"Can not use provided object of type '{typeof(NewAddress)}'", Detail = e.Message, ProblemType = "WebApiHypermediaExtensionsCore.Hypermedia.BadActionParameter", StatusCode = 422 // Unprocessable Entity }; return(this.UnprocessableEntity(problem)); } }
public IActionResult Error(int code) { var problem = ProblemJson.Create(code); // Handle error here return(StatusCode(code, problem)); }
public async Task <ActionResult> BuyCar(int key, BuyCarParameters parameter) { if (parameter == null) { var problem = new ProblemJson { Title = $"Can not use provided object of type '{typeof(BuyCarParameters)}'", Detail = "Json or contained links might be invalid", ProblemType = "WebApi.HypermediaExtensions.Hypermedia.BadActionParameter", StatusCode = 422 // Unprocessable Entity }; return(this.UnprocessableEntity(problem)); } try { //shortcut for get car from repository var car = new HypermediaCarHto(parameter.Brand, parameter.CarId); var customer = await customerRepository.GetEnitityByKeyAsync(key).ConfigureAwait(false); //do what has to be done return(Ok()); } catch (EntityNotFoundException) { return(this.Problem(ProblemJsonBuilder.CreateEntityNotFound())); } catch (CanNotExecuteActionException) { return(this.CanNotExecute()); } }
public async Task <ActionResult> RemoveCustomer(int key) { try { return(Ok()); } catch (EntityNotFoundException) { return(this.Problem(ProblemJsonBuilder.CreateEntityNotFound())); } catch (CanNotExecuteActionException) { return(this.CanNotExecute()); } catch (ActionParameterValidationException e) { var problem = new ProblemJson() { Title = $"Can not use provided object of type '{typeof(NewAddress)}'", Detail = e.Message, ProblemType = "WebApi.HypermediaExtensions.Hypermedia.BadActionParameter", StatusCode = 422 // Unprocessable Entity }; return(this.UnprocessableEntity(problem)); } }
public Arena(ProblemJson[] problems) { this.Problems = problems.Select(p => new ArenaProblem { Problem = p, Id = p.id, MapResults = p.sourceSeeds.Select(seed => new ArenaMapResult { Seed = seed }).ToArray() }).ToArray(); }
/// <summary> /// Return a ProblemJson. Status code wil be set according to the ProbkemJson. /// </summary> /// <param name="controller"></param> /// <param name="problemJson">The Problem description.</param> /// <returns></returns> public static ActionResult Problem(this ControllerBase controller, ProblemJson problemJson) { var objectResult = new ObjectResult(problemJson) { StatusCode = problemJson.StatusCode }; objectResult.ContentTypes.Add(DefaultMediaTypes.ProblemJson); return(objectResult); }
public static ProblemJson CreateEntityNotFound() { var problem = new ProblemJson { Title = "Entity not found", Detail = string.Empty, ProblemType = "CarShack.EntityNotFound", StatusCode = 404 }; return(problem); }
public static ProblemJson CreateBadParameters() { var problem = new ProblemJson { Title = "Bad Parameters", Detail = "Review parameter schema.", ProblemType = "CarShack.BadParameters", StatusCode = 400 }; return(problem); }
private static Task HandleExceptionAsync(HttpContext context, Exception exception) { var code = HttpStatusCode.InternalServerError; var result = JsonConvert.SerializeObject( ProblemJson.Create((int)code, exception.Message) ); context.Response.ContentType = ProblemJson.MediaType; context.Response.StatusCode = (int)code; return(context.Response.WriteAsync(result)); }
public static ProblemJson CreateEntityNotFound() { var problem = new ProblemJson { Title = "Entity not found", Detail = "", ProblemType = "CustomerDemo.EntityNotFound", StatusCode = 404 }; return(problem); }
/// <summary> /// Indicates that the provided ActionParameters were tecnicaly correct but the internal validation (in the bussiness logic) did not accept the parameters. /// </summary> /// <param name="controller"></param> /// <param name="problemJson">Optional Problem Json.</param> /// <returns></returns> public static ActionResult UnprocessableEntity(this ControllerBase controller, ProblemJson problemJson = null) { if (problemJson == null) { problemJson = new ProblemJson { Title = "Can not use provided object", Detail = "", ProblemType = "WebApi.HypermediaExtensions.Hypermedia.BadActionParameter", StatusCode = 422 // Unprocessable Entity }; } return(new ObjectResult(problemJson) { StatusCode = problemJson.StatusCode }); }
/// <summary> /// The action which was requested can not be executed. Migth have changed state since received the Hypermedia. /// </summary> /// <param name="controller"></param> /// <param name="problemJson"></param> /// <returns></returns> public static ActionResult CanNotExecute(this ControllerBase controller, ProblemJson problemJson = null) { if (problemJson == null) { problemJson = new ProblemJson { Title = "Can not execute Action", Detail = "", ProblemType = "WebApi.HypermediaExtensions.Hypermedia.ActionNotAvailable", StatusCode = 400 }; } return(new ObjectResult(problemJson) { StatusCode = problemJson.StatusCode }); }
public async Task <ActionResult> MarkAsFavoriteAction([HypermediaActionParameterFromBody] MarkAsFavoriteParameters favoriteCustomer) { if (favoriteCustomer == null) { var problem = new ProblemJson { Title = $"Can not use provided object of type '{typeof(MarkAsFavoriteParameters)}'", Detail = "Json or contained links might be invalid", ProblemType = "WebApi.HypermediaExtensions.Hypermedia.BadActionParameter", StatusCode = 422 // Unprocessable Entity }; return(this.UnprocessableEntity(problem)); } try { var customer = await customerRepository.GetEnitityByKeyAsync(favoriteCustomer.CustomerId).ConfigureAwait(false); var hypermediaCustomer = customer.ToHto(); hypermediaCustomer.MarkAsFavorite.Execute(favoriteCustomer); return(Ok()); } catch (EntityNotFoundException) { return(this.Problem(ProblemJsonBuilder.CreateEntityNotFound())); } catch (InvalidLinkException e) { var problem = new ProblemJson() { Title = $"Can not use provided object of type '{typeof(MarkAsFavoriteParameters)}'", Detail = e.Message, ProblemType = "WebApi.HypermediaExtensions.Hypermedia.BadActionParameter", StatusCode = 422 // Unprocessable Entity }; return(this.UnprocessableEntity(problem)); } catch (CanNotExecuteActionException) { return(this.CanNotExecute()); } }