-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Matheus Alcantara <[email protected]>
- Loading branch information
1 parent
39f4591
commit 36c5c9b
Showing
13 changed files
with
51,118 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net.Mime; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
using WebApiSample.Models; | ||
|
||
namespace WebApiSample.Controllers | ||
{ | ||
#region snippet_Inherit | ||
[Produces(MediaTypeNames.Application.Json)] | ||
[Route("[controller]")] | ||
public class PetsController : MyControllerBase | ||
#endregion | ||
{ | ||
private static readonly List<Pet> _petsInMemoryStore = new List<Pet>(); | ||
|
||
public PetsController() | ||
{ | ||
if (_petsInMemoryStore.Count == 0) | ||
{ | ||
_petsInMemoryStore.Add( | ||
new Pet | ||
{ | ||
Breed = "Collie", | ||
Id = 1, | ||
Name = "Fido", | ||
PetType = PetType.Dog | ||
}); | ||
} | ||
} | ||
|
||
[HttpGet] | ||
public ActionResult<List<Pet>> GetAll() => _petsInMemoryStore; | ||
|
||
[HttpGet("{id}")] | ||
[ProducesResponseType(StatusCodes.Status404NotFound)] | ||
public ActionResult<Pet> GetById(int id) | ||
{ | ||
var pet = _petsInMemoryStore.FirstOrDefault(p => p.Id == id); | ||
|
||
#region snippet_ProblemDetailsStatusCode | ||
if (pet == null) | ||
{ | ||
return NotFound(); | ||
} | ||
#endregion | ||
|
||
return pet; | ||
} | ||
|
||
#region snippet_400And201 | ||
[HttpPost] | ||
[ProducesResponseType(StatusCodes.Status201Created)] | ||
[ProducesResponseType(StatusCodes.Status400BadRequest)] | ||
public ActionResult<Pet> Create(Pet pet) | ||
{ | ||
pet.Id = _petsInMemoryStore.Any() ? | ||
_petsInMemoryStore.Max(p => p.Id) + 1 : 1; | ||
_petsInMemoryStore.Add(pet); | ||
|
||
return CreatedAtAction(nameof(GetById), new { id = pet.Id }, pet); | ||
} | ||
#endregion | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net.Mime; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
using WebApiSample.Models; | ||
|
||
namespace WebApiSample.Controllers | ||
{ | ||
#region snippet_Inherit | ||
[Produces(MediaTypeNames.Application.Json)] | ||
[Route("[controller]")] | ||
public class PetsController : MyControllerBase | ||
#endregion | ||
{ | ||
private static readonly List<Pet> _petsInMemoryStore = new List<Pet>(); | ||
|
||
public PetsController() | ||
{ | ||
if (_petsInMemoryStore.Count == 0) | ||
{ | ||
_petsInMemoryStore.Add( | ||
new Pet | ||
{ | ||
Breed = "Collie", | ||
Id = 1, | ||
Name = "Fido", | ||
PetType = PetType.Dog | ||
}); | ||
} | ||
} | ||
|
||
[HttpGet] | ||
public ActionResult<List<Pet>> GetAll() => _petsInMemoryStore; | ||
|
||
[HttpGet("{id}")] | ||
[ProducesResponseType(StatusCodes.Status404NotFound)] | ||
public ActionResult<Pet> GetById(int id) | ||
{ | ||
var pet = _petsInMemoryStore.FirstOrDefault(p => p.Id == id); | ||
|
||
#region snippet_ProblemDetailsStatusCode | ||
if (pet == null) | ||
{ | ||
return NotFound(); | ||
} | ||
#endregion | ||
|
||
return pet; | ||
} | ||
|
||
#region snippet_400And201 | ||
[HttpPost] | ||
[ProducesResponseType(StatusCodes.Status201Created)] | ||
[ProducesResponseType(StatusCodes.Status400BadRequest)] | ||
public ActionResult<Pet> Create(Pet pet) | ||
{ | ||
pet.Id = _petsInMemoryStore.Any() ? | ||
_petsInMemoryStore.Max(p => p.Id) + 1 : 1; | ||
_petsInMemoryStore.Add(pet); | ||
|
||
return CreatedAtAction(nameof(GetById), new { id = pet.Id }, pet); | ||
} | ||
#endregion | ||
} | ||
} |
Oops, something went wrong.