This repository has been archived by the owner on Oct 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
CompaniesController.cs
80 lines (70 loc) · 3.16 KB
/
CompaniesController.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using System.Net.Mime;
using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations;
using WAW.API.Company.Domain.Services;
using WAW.API.Company.Resources;
using WAW.API.Shared.Extensions;
namespace WAW.API.Company.Controllers;
using Domain.Models;
[ApiController]
[Route("[controller]")]
[Produces(MediaTypeNames.Application.Json)]
[SwaggerTag("Create, read, update and delete Companies")]
public class CompaniesController : ControllerBase {
private readonly ICompanyService service;
private readonly IMapper mapper;
public CompaniesController(ICompanyService service, IMapper mapper) {
this.service = service;
this.mapper = mapper;
}
[HttpGet]
[ProducesResponseType(typeof(IEnumerable<CompanyResource>), 200)]
[SwaggerResponse(200, "All the stored companies were retrieved successfully.", typeof(IEnumerable<CompanyResource>))]
public async Task<IEnumerable<CompanyResource>> GetAll() {
var companies = await service.ListAll();
return mapper.Map<IEnumerable<Company>, IEnumerable<CompanyResource>>(companies);
}
[HttpPost]
[ProducesResponseType(typeof(CompanyResource), 201)]
[ProducesResponseType(typeof(List<string>), 400)]
[ProducesResponseType(500)]
[SwaggerResponse(201, "The company was created successfully", typeof(CompanyResource))]
[SwaggerResponse(400, "The company data is invalid")]
public async Task<IActionResult> Post(
[FromBody] [SwaggerRequestBody("The company object about to create", Required = true)] CompanyRequest companyRequest
) {
if (!ModelState.IsValid) return BadRequest(ModelState.GetErrorMessages());
var company = mapper.Map<CompanyRequest, Company>(companyRequest);
var result = await service.Create(company);
return result.ToResponse<CompanyResource>(this, mapper);
}
[HttpPut("{id:int}")]
[ProducesResponseType(typeof(CompanyResource), 200)]
[ProducesResponseType(typeof(List<string>), 400)]
[ProducesResponseType(500)]
[SwaggerResponse(200, "The company was updated successfully", typeof(CompanyResource))]
[SwaggerResponse(400, "The company data is invalid")]
public async Task<IActionResult> Put(
[FromRoute] [SwaggerParameter("Company identifier", Required = true)] int id,
[FromBody] [SwaggerRequestBody("The company object about to update and its changes", Required = true)]
CompanyRequest companyRequest
) {
if (!ModelState.IsValid) return BadRequest(ModelState.GetErrorMessages());
var company = mapper.Map<CompanyRequest, Company>(companyRequest);
var result = await service.Update(id, company);
return result.ToResponse<CompanyResource>(this, mapper);
}
[HttpDelete("{id:int}")]
[ProducesResponseType(typeof(CompanyResource), 200)]
[ProducesResponseType(typeof(List<string>), 400)]
[ProducesResponseType(500)]
[SwaggerResponse(200, "The company was deleted successfully", typeof(CompanyResource))]
[SwaggerResponse(400, "The selected company to delete does not exist")]
public async Task<IActionResult> DeleteAsync(
[FromRoute] [SwaggerParameter("Company identifier", Required = true)] int id
) {
var result = await service.Delete(id);
return result.ToResponse<CompanyResource>(this, mapper);
}
}