Commit cf25c178 authored by Steve Smith's avatar Steve Smith

Adding working endpoint demo - run it from Swagger

parent f0ff77b4
using Microsoft.AspNetCore.Mvc;
namespace CleanArchitecture.Web.Endpoints
{
public abstract class BaseEndpoint<TRequest,TResponse> : ControllerBase
{
public abstract ActionResult<TResponse> Handle(TRequest request);
}
}
using CleanArchitecture.Core.Entities;
using CleanArchitecture.SharedKernel.Interfaces;
using Microsoft.AspNetCore.Mvc;
using System.ComponentModel.DataAnnotations;
namespace CleanArchitecture.Web.Endpoints.ToDoItems
{
public class CreateHandler : BaseEndpoint<CreateCommand, CreatedResult>
{
private readonly IRepository _repository;
public CreateHandler(IRepository repository)
{
_repository = repository;
}
[HttpPost("/endpoints/items")]
public override ActionResult<CreatedResult> Handle([FromBody]CreateCommand request)
{
var todoItem = new ToDoItem()
{
Title = request.Title,
Description = request.Description
};
_repository.Add(todoItem);
var result = new CreatedResult
{
Id = todoItem.Id,
Title = todoItem.Title,
Description = todoItem.Description
};
return Ok(result);
}
}
// Imagine these are separate classes attached to the Handler/Endpoint with + expansion in VS
public class CreateCommand
{
[Required]
public string Title { get; set; }
[Required]
public string Description { get; set; }
}
public class CreatedResult : CreateCommand
{
public int Id { get; set; }
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment