update new controller (#64)

pull/65/head
Rick Anderson 2022-09-21 11:50:56 -10:00 committed by GitHub
parent 05aa9ab952
commit 8dd4ff3147
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 17 deletions

View File

@ -46,8 +46,7 @@ public class ValuesController : ControllerBase
{ {
var errorType = new MathErrorFeature var errorType = new MathErrorFeature
{ {
MathError = MathError = MathErrorType.DivisionByZeroError
MathErrorType.DivisionByZeroError
}; };
HttpContext.Features.Set(errorType); HttpContext.Features.Set(errorType);
return BadRequest(); return BadRequest();
@ -65,8 +64,7 @@ public class ValuesController : ControllerBase
{ {
var errorType = new MathErrorFeature var errorType = new MathErrorFeature
{ {
MathError = MathError = MathErrorType.NegativeRadicandError
MathErrorType.NegativeRadicandError
}; };
HttpContext.Features.Set(errorType); HttpContext.Features.Set(errorType);
return BadRequest(); return BadRequest();
@ -79,12 +77,12 @@ public class ValuesController : ControllerBase
} }
// </snippet> // </snippet>
// <snippet> // <snippet3>
[Route("api/[controller]/[action]")] [Route("api/[controller]/[action]")]
[ApiController] [ApiController]
public class Values3Controller : ControllerBase public class Values3Controller : ControllerBase
{ {
// /api/values/divide/1/2 // /api/values3/divide/1/2
[HttpGet("{Numerator}/{Denominator}")] [HttpGet("{Numerator}/{Denominator}")]
public IActionResult Divide(double Numerator, double Denominator) public IActionResult Divide(double Numerator, double Denominator)
{ {
@ -92,12 +90,11 @@ public class Values3Controller : ControllerBase
{ {
var errorType = new MathErrorFeature var errorType = new MathErrorFeature
{ {
MathError = MathError = MathErrorType.DivisionByZeroError
MathErrorType.DivisionByZeroError
}; };
HttpContext.Features.Set(errorType); HttpContext.Features.Set(errorType);
return Problem( return Problem(
title: "Wrong Input", title: "Bad Input",
detail: "The number you inputed is zero", detail: "The number you inputed is zero",
type: "https://en.wikipedia.org/wiki/Division_by_zero", type: "https://en.wikipedia.org/wiki/Division_by_zero",
statusCode: StatusCodes.Status400BadRequest statusCode: StatusCodes.Status400BadRequest
@ -108,7 +105,7 @@ public class Values3Controller : ControllerBase
return Ok(calculation); return Ok(calculation);
} }
// /api/values/squareroot/4 // /api/values3/squareroot/4
[HttpGet("{radicand}")] [HttpGet("{radicand}")]
public IActionResult Squareroot(double radicand) public IActionResult Squareroot(double radicand)
{ {
@ -116,12 +113,11 @@ public class Values3Controller : ControllerBase
{ {
var errorType = new MathErrorFeature var errorType = new MathErrorFeature
{ {
MathError = MathError = MathErrorType.NegativeRadicandError
MathErrorType.NegativeRadicandError
}; };
HttpContext.Features.Set(errorType); HttpContext.Features.Set(errorType);
return Problem( return Problem(
title: "Wrong Input", title: "Bad Input",
detail: "Negative or complex numbers are not handled", detail: "Negative or complex numbers are not handled",
type: "https://en.wikipedia.org/wiki/Square_root", type: "https://en.wikipedia.org/wiki/Square_root",
statusCode: StatusCodes.Status400BadRequest statusCode: StatusCodes.Status400BadRequest
@ -133,4 +129,4 @@ public class Values3Controller : ControllerBase
} }
} }
// </snippet> // </snippet3>

View File

@ -1,4 +1,4 @@
#define API_CONTROLLER // MIDDLEWARE API_CONTROLLER API_CONT_SHORT #define MIDDLEWARE // MIDDLEWARE API_CONTROLLER API_CONT_SHORT
#if NEVER #if NEVER
#elif MIDDLEWARE #elif MIDDLEWARE
// <snippet_middleware> // <snippet_middleware>
@ -42,7 +42,7 @@ app.Use(async (context, next) =>
HttpContext = context, HttpContext = context,
ProblemDetails = ProblemDetails =
{ {
Title = "Wrong Input", Title = "Bad Input",
Detail = details.Detail, Detail = details.Detail,
Type = details.Type Type = details.Type
} }
@ -109,7 +109,7 @@ builder.Services.AddProblemDetails(options =>
}; };
context.ProblemDetails.Type = details.Type; context.ProblemDetails.Type = details.Type;
context.ProblemDetails.Title = "Wrong Input"; context.ProblemDetails.Title = "Bad Input";
context.ProblemDetails.Detail = details.Detail; context.ProblemDetails.Detail = details.Detail;
} }
} }