-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathCustomerController.cs
More file actions
69 lines (54 loc) · 2.05 KB
/
Copy pathCustomerController.cs
File metadata and controls
69 lines (54 loc) · 2.05 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using DeveloperTest.Business.Interfaces;
using DeveloperTest.Models;
using Microsoft.AspNetCore.Mvc;
using System.Threading;
using System.Threading.Tasks;
namespace DeveloperTest.Controllers
{
[ApiController, Route("[controller]")]
public class CustomerController : ControllerBase
{
private readonly ICustomerService _customerService;
public CustomerController(ICustomerService customerService)
{
_customerService = customerService;
}
[HttpGet]
public async Task<IActionResult> Get(CancellationToken token)
{
var customers = await _customerService.GetCustomersAsync(token);
if (customers is null)
return NotFound();
return Ok(customers);
}
[HttpGet("{id}")]
public async Task<IActionResult> Get([FromRoute]int id, CancellationToken token)
{
var customers = await _customerService.GetCustomerAsync(id, token);
if (customers is null)
return NotFound();
return Ok(customers);
}
[HttpPost]
public async Task<IActionResult> Create([FromBody] BaseCustomerModel model, CancellationToken token)
{
var created = await _customerService.CreateCustomerAsync(model, token);
if (created is null)
return BadRequest(new { error = "Unable to create customer" });
return Created($"{GetBaseUrl()}/customer/{created.Id}", created);
}
//Added for manual testing purposes
[HttpDelete("{id}")]
public async Task<IActionResult> Delete([FromRoute] int id, CancellationToken token)
{
var deleted = await _customerService.DeleteCustomer(id, token);
if (deleted)
return NoContent();
return NotFound();
}
private string GetBaseUrl()
{
return $"{HttpContext.Request.Scheme}://{HttpContext.Request.Host.ToUriComponent()}";
}
}
}