Skip to content

Commit

Permalink
Use StatusCode instead of hardcoded integers (dotnet#10886)
Browse files Browse the repository at this point in the history
* Added missing ProducesResponseType, aligned response types

Fixes dotnet#10613

* Apply suggestions from code review

Co-Authored-By: bartlannoeye <[email protected]>

* Use StatusCodes instead of int for define-controller samples

* Use StatusCodes instead of int for action-return-samples

* Apply PR remarks
  • Loading branch information
bartlannoeye authored and scottaddie committed Feb 19, 2019
1 parent 0f8195f commit b10558c
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using WebApiSample.DataAccess.Models;
using WebApiSample.DataAccess.Repositories;

Expand All @@ -27,8 +28,7 @@ public IEnumerable<Product> Get()

#region snippet_GetById
[HttpGet("{id}")]
[ProducesResponseType(200)]
[ProducesResponseType(404)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public ActionResult<Product> GetById(int id)
{
if (!_repository.TryGetProduct(id, out var product))
Expand All @@ -42,8 +42,8 @@ public ActionResult<Product> GetById(int id)

#region snippet_CreateAsync
[HttpPost]
[ProducesResponseType(201)]
[ProducesResponseType(400)]
[ProducesResponseType(StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<ActionResult<Product>> CreateAsync(Product product)
{
if (product.Description.Contains("XYZ Widget"))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using WebApiSample.DataAccess.Models;
using WebApiSample.DataAccess.Repositories;

Expand All @@ -26,8 +27,8 @@ public IEnumerable<Product> Get()

#region snippet_GetById
[HttpGet("{id}")]
[ProducesResponseType(200, Type = typeof(Product))]
[ProducesResponseType(404)]
[ProducesResponseType(typeof(Product), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public IActionResult GetById(int id)
{
if (!_repository.TryGetProduct(id, out var product))
Expand All @@ -41,8 +42,8 @@ public IActionResult GetById(int id)

#region snippet_CreateAsync
[HttpPost]
[ProducesResponseType(201, Type = typeof(Product))]
[ProducesResponseType(400)]
[ProducesResponseType(typeof(Product), StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<IActionResult> CreateAsync([FromBody] Product product)
{
if (product.Description.Contains("XYZ Widget"))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using WebApiSample.DataAccess.Models;
using WebApiSample.DataAccess.Repositories;
Expand All @@ -25,7 +26,7 @@ public async Task<ActionResult<List<Pet>>> GetAllAsync()
}

[HttpGet("{id}")]
[ProducesResponseType(404)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<ActionResult<Pet>> GetByIdAsync(int id)
{
var pet = await _repository.GetPetAsync(id);
Expand All @@ -39,8 +40,8 @@ public async Task<ActionResult<Pet>> GetByIdAsync(int id)
}

[HttpPost]
[ProducesResponseType(201)]
[ProducesResponseType(400)]
[ProducesResponseType(StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<ActionResult<Pet>> CreateAsync(Pet pet)
{
if (!ModelState.IsValid)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using WebApiSample.DataAccess.Models;
using WebApiSample.DataAccess.Repositories;

Expand All @@ -21,7 +22,7 @@ public ProductsController(ProductsRepository repository)

#region snippet_GetById
[HttpGet("{id}")]
[ProducesResponseType(404)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<ActionResult<Product>> GetByIdAsync(int id)
{
var product = await _repository.GetProductAsync(id);
Expand Down Expand Up @@ -56,8 +57,8 @@ public async Task<ActionResult<List<Product>>> GetAsync(
#endregion

[HttpPost]
[ProducesResponseType(201)]
[ProducesResponseType(400)]
[ProducesResponseType(StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<ActionResult<Product>> CreateAsync(Product product)
{
await _repository.AddProductAsync(product);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using WebApiSample.DataAccess.Models;
using WebApiSample.DataAccess.Repositories;
Expand All @@ -17,7 +18,7 @@ public ProductsController(ProductsRepository repository)
}

[HttpGet("{id}")]
[ProducesResponseType(404)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<ActionResult<Product>> GetByIdAsync(int id)
{
var product = await _repository.GetProductAsync(id);
Expand Down Expand Up @@ -51,8 +52,8 @@ public async Task<ActionResult<List<Product>>> GetAsync(
}

[HttpPost]
[ProducesResponseType(201)]
[ProducesResponseType(400)]
[ProducesResponseType(StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<ActionResult<Product>> CreateAsync(Product product)
{
await _repository.AddProductAsync(product);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using WebApiSample.DataAccess.Models;
using WebApiSample.DataAccess.Repositories;

Expand All @@ -19,7 +20,7 @@ public PetsController(PetsRepository repository)
}

[HttpGet]
[ProducesResponseType(typeof(IEnumerable<Pet>), 200)]
[ProducesResponseType(typeof(IEnumerable<Pet>), StatusCodes.Status200OK)]
public async Task<IActionResult> GetAllAsync()
{
var pets = await _repository.GetPetsAsync();
Expand All @@ -28,8 +29,8 @@ public async Task<IActionResult> GetAllAsync()
}

[HttpGet("{id}")]
[ProducesResponseType(typeof(Pet), 200)]
[ProducesResponseType(404)]
[ProducesResponseType(typeof(Pet), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> GetByIdAsync(int id)
{
var pet = await _repository.GetPetAsync(id);
Expand All @@ -43,8 +44,8 @@ public async Task<IActionResult> GetByIdAsync(int id)
}

[HttpPost]
[ProducesResponseType(typeof(Pet), 201)]
[ProducesResponseType(400)]
[ProducesResponseType(typeof(Pet), StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<IActionResult> CreateAsync([FromBody] Pet pet)
{
#region snippet_ModelStateIsValidCheck
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using WebApiSample.DataAccess.Models;
using WebApiSample.DataAccess.Repositories;

Expand All @@ -20,8 +21,8 @@ public ProductsController(ProductsRepository repository)

#region snippet_GetById
[HttpGet("{id}")]
[ProducesResponseType(typeof(Product), 200)]
[ProducesResponseType(404)]
[ProducesResponseType(typeof(Product), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> GetByIdAsync(int id)
{
var product = await _repository.GetProductAsync(id);
Expand All @@ -37,7 +38,7 @@ public async Task<IActionResult> GetByIdAsync(int id)

#region snippet_BindingSourceAttributes
[HttpGet]
[ProducesResponseType(typeof(List<Product>), 200)]
[ProducesResponseType(typeof(List<Product>), StatusCodes.Status200OK)]
public async Task<IActionResult> GetAsync(
[FromQuery] bool discontinuedOnly = false)
{
Expand All @@ -57,8 +58,8 @@ public async Task<IActionResult> GetAsync(
#endregion

[HttpPost]
[ProducesResponseType(typeof(Product), 201)]
[ProducesResponseType(400)]
[ProducesResponseType(typeof(Product), StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<IActionResult> CreateAsync([FromBody] Product product)
{
if (!ModelState.IsValid)
Expand Down

0 comments on commit b10558c

Please sign in to comment.