forked from ardalis/Result
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreatePersonEndpoint.cs
More file actions
40 lines (34 loc) · 1.28 KB
/
CreatePersonEndpoint.cs
File metadata and controls
40 lines (34 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using Ardalis.ApiEndpoints;
using Ardalis.Result.AspNetCore;
using Ardalis.Result.Sample.Core.Services;
using Microsoft.AspNetCore.Mvc;
using System;
using Ardalis.Result.Sample.Core.DTOs;
using Ardalis.Result.Sample.Core.Model;
namespace Ardalis.Result.SampleWeb.PersonFeature;
public class CreatePersonEndpoint : EndpointBaseSync
.WithRequest<CreatePersonRequestDto>
.WithActionResult<Person>
{
private readonly PersonService _personService;
public CreatePersonEndpoint(PersonService personService)
{
_personService = personService;
}
/// <summary>
/// This uses an extension method to convert to an ActionResult
/// </summary>
/// <returns></returns>
[HttpPost("/Person/Create/")]
public override ActionResult<Person> Handle(CreatePersonRequestDto request)
{
if (DateTime.Now.Second % 2 == 0) // just so we can show both versions
{
// Extension method on ControllerBase
return this.ToActionResult(_personService.Create(request.FirstName, request.LastName));
}
Result<Person> result = _personService.Create(request.FirstName, request.LastName);
// Extension method on a Result instance (passing in ControllerBase instance)
return result.ToActionResult(this);
}
}