Last active
October 12, 2017 17:19
-
-
Save redwards510/d07ce29f85fd6f5cf802ac7521d77e9c to your computer and use it in GitHub Desktop.
Asp.Net Core 2.0 Async WebApi Template with EF Core 2.0 Entities for Scaffolding EF Core web api controllers with a lot more Swagger-friendly ResponseType attributes. Includes instructions to do custom scaffold.
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
// This began life as the default Microsoft template you get when you scaffold an Entity Framework .Net core 2.0 Api Controller. | |
// What I've done is add more Swagger-friendly API documentation, calls to a repository, mapping using Mapster. | |
// It produces nearly complete Api methods. | |
// Problems: The PUT method has no documentation | |
// ProTip: If your formatting gets completely hosed when you paste code into the template file, try hitting ctrl+z. It will reformat back the way you had it. | |
// File goes in Templates\ControllerGenerator\ApiControllerWithContext.cshtml in the root of your project. Then | |
@inherits Microsoft.VisualStudio.Web.CodeGeneration.Templating.RazorTemplateBase | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Threading.Tasks; | |
using Mapster; | |
using Microsoft.AspNetCore.Http; | |
using Microsoft.AspNetCore.Mvc; | |
using Microsoft.EntityFrameworkCore; | |
@{ | |
foreach (var namespaceName in Model.RequiredNamespaces) | |
{ | |
@:using @namespaceName; | |
} | |
} | |
namespace @Model.ControllerNamespace | |
{ | |
@{ | |
string routePrefix = Model.ControllerRootName; | |
var entitySetName = Model.ModelMetadata.EntitySetName; | |
var primaryKeyName = Model.ModelMetadata.PrimaryKeys[0].PropertyName; | |
var primaryKeyShortTypeName = Model.ModelMetadata.PrimaryKeys[0].ShortTypeName; | |
var primaryKeyType = Model.ModelMetadata.PrimaryKeys[0].TypeName; | |
var primaryKeyIsAutoGenerated = Model.ModelMetadata.PrimaryKeys[0].IsAutoGenerated; | |
} | |
[Produces("application/json")] | |
[Route("@routePrefix")] | |
public class @(Model.ControllerName) : Controller | |
{ | |
private readonly @(Model.ContextTypeName) _context; | |
// Repositories | |
private readonly IAffiliateRepository _affiliateRepository; | |
private readonly I@(entitySetName)Repository _@(Model.ModelVariable)Repository; | |
public @(Model.ControllerName)(@(Model.ContextTypeName) context, | |
IAffiliateRepository affiliateRepository, | |
I@(entitySetName)Repository @(Model.ModelVariable)Repository) | |
{ | |
_context = context; | |
this._affiliateRepository = affiliateRepository; | |
this._@(Model.ModelVariable)Repository = @(Model.ModelVariable)Repository; | |
} | |
/// <summary> | |
/// Gets all @(routePrefix) | |
/// </summary> | |
/// <returns>collection of @(entitySetName)DTOs</returns> | |
[HttpGet(Name = "GetAll@(routePrefix)")] | |
public Task<List<@(Model.ModelTypeName)DTO>> Get@(routePrefix)() | |
{ | |
var result = await _@(Model.ModelVariable)Repository.GetAll(); | |
return result.Adapt<List<@(Model.ModelTypeName)>, List<@(Model.ModelTypeName)DTO>>(); | |
} | |
/// <summary> | |
/// Returns a single @(entitySetName) record. | |
/// </summary> | |
/// <remarks> | |
/// </remarks> | |
/// <param name="id">primary key of the @(entitySetName) record</param> | |
/// <returns>@(entitySetName)DTO</returns> | |
[HttpGet("{id}", Name = "Get@(Model.ModelTypeName)")] | |
public async Task<IActionResult> Get@(Model.ModelTypeName)([FromRoute] @(primaryKeyShortTypeName) id) | |
{ | |
if (id < 1) | |
{ | |
return BadRequest(); | |
} | |
var @(Model.ModelVariable) = await _@(Model.ModelVariable)Repository.GetSingle(id); | |
var dto = @(Model.ModelVariable).Adapt<@(entitySetName)DTO>(); | |
return Ok(dto); | |
} | |
[HttpPut("{id}", Name = "Put@(Model.ModelTypeName)")] | |
public async Task<IActionResult> Put@(Model.ModelTypeName)([FromRoute] @(primaryKeyShortTypeName) id, [FromBody] @(Model.ModelTypeName)DTO @(Model.ModelVariable)) | |
{ | |
var entity = @(Model.ModelVariable).Adapt<@(Model.ModelTypeName)>(); | |
_@(Model.ModelVariable)Repository.Update(entity); | |
await _@(Model.ModelVariable)Repository.Commit(); | |
return NoContent(); | |
} | |
/// <summary> | |
/// Deletes a @(Model.ModelTypeName) from the database. | |
/// </summary> | |
/// <remarks> | |
/// </remarks> | |
/// <param name="id">primary key of record to be deleted</param> | |
/// <returns></returns> | |
[HttpDelete("{id}")] | |
[ProducesResponseType(typeof(@(Model.ModelTypeName)DTO), 204)] | |
public async Task<IActionResult> Delete(int id) | |
{ | |
var entity = _@(Model.ModelVariable)Repository.GetSingle(id); | |
if (entity == null) | |
{ | |
return NotFound(); | |
} | |
_@(Model.ModelVariable)Repository.DeleteWhere(a => a.Id == id); | |
await _@(Model.ModelVariable)Repository.Commit(); | |
return new NoContentResult(); | |
} | |
/// <summary> | |
/// Creates a @(Model.ModelTypeName) | |
/// </summary> | |
/// <remarks> | |
/// </remarks> | |
/// <returns>A newly-created @(Model.ModelTypeName) object</returns> | |
/// <response code="201">Returns the newly-created item</response> | |
/// <response code="400">If the item is null or invalid</response> | |
[HttpPost] | |
[ProducesResponseType(typeof(@(Model.ModelTypeName)DTO), 201)] | |
[ProducesResponseType(typeof(@(Model.ModelTypeName)DTO), 400)] | |
public async Task<IActionResult> Post@(Model.ModelTypeName)([FromBody] @(Model.ModelTypeName)DTO @(Model.ModelVariable)) | |
{ | |
if (@(Model.ModelVariable) == null) | |
{ | |
return BadRequest(); | |
} | |
var entity = @(Model.ModelVariable).Adapt<@(Model.ModelTypeName)>(); | |
_@(Model.ModelVariable)Repository.Add(entity); | |
await _@(Model.ModelVariable)Repository.Commit(); | |
var dto = entity.Adapt<@(Model.ModelTypeName)DTO>(); | |
return CreatedAtRoute("Get@(Model.ModelTypeName)", new { id = entity.Id }, dto); | |
} | |
} // end controller | |
} // end namespace |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment