-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBaseApiController.cs
51 lines (41 loc) · 1.09 KB
/
BaseApiController.cs
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
41
42
43
44
45
46
47
48
49
50
51
using MediatR;
using Microsoft.AspNetCore.Mvc;
using SharedKernel.Result;
using Web.Infrastructure;
namespace Web.Controllers;
[ApiController]
[Route("/api/[controller]")]
public class BaseApiController : ControllerBase
{
private IMediator _mediator;
protected IMediator Mediator => this._mediator ??= this.HttpContext.RequestServices.GetService<IMediator>()!;
protected string UserId => this.User.GetUserId().ToString();
protected ActionResult HandleResult<T>(Result<T> result)
{
if (result is null)
{
return NotFound();
}
if (result.IsSuccess && result.Value != null)
{
return Ok(result.Value);
}
if (result.IsSuccess && result.Value == null)
{
return NotFound();
}
return BadRequest(result.Error);
}
protected ActionResult HandleResult(Result result)
{
if (result is null)
{
return NotFound();
}
if (result.IsSuccess)
{
return Ok();
}
return BadRequest(result.Error);
}
}