Skip to content
This repository has been archived by the owner on Oct 14, 2022. It is now read-only.

Commit

Permalink
Merge pull request #8 from futureleadersupc/fix/companies
Browse files Browse the repository at this point in the history
fix(companies): Endpoint changes
  • Loading branch information
dalbitresb12 authored Jun 11, 2022
2 parents f8752af + 16f84df commit c9cc11b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,18 @@ namespace WAW.API.Company.Controllers;
[Route("[controller]")]
[Produces(MediaTypeNames.Application.Json)]
[SwaggerTag("Create, read, update and delete Companies")]
public class CompanyController : ControllerBase {
public class CompaniesController : ControllerBase {
private readonly ICompanyService service;
private readonly IMapper mapper;

public CompanyController(ICompanyService service, 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);
Expand All @@ -36,7 +37,9 @@ public async Task<IEnumerable<CompanyResource>> GetAll() {
[ProducesResponseType(500)]
[SwaggerResponse(201, "The company was created successfully", typeof(CompanyResource))]
[SwaggerResponse(400, "The company data is invalid")]
public async Task<IActionResult> Post([FromBody] CompanyRequest companyRequest) {
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);
Expand All @@ -50,7 +53,11 @@ public async Task<IActionResult> Post([FromBody] CompanyRequest companyRequest)
[ProducesResponseType(500)]
[SwaggerResponse(200, "The company was updated successfully", typeof(CompanyResource))]
[SwaggerResponse(400, "The company data is invalid")]
public async Task<IActionResult> Put(int id, [FromBody] CompanyRequest companyRequest) {
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);
Expand All @@ -64,7 +71,9 @@ public async Task<IActionResult> Put(int id, [FromBody] CompanyRequest companyRe
[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(int id) {
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);
}
Expand Down
5 changes: 5 additions & 0 deletions WAW.API/Company/Resources/CompanyRequest.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
using System.ComponentModel.DataAnnotations;
using Swashbuckle.AspNetCore.Annotations;

namespace WAW.API.Company.Resources;

[SwaggerSchema(Required = new[] {"Name", "Email",})]
public class CompanyRequest {
[SwaggerSchema("Category name")]
[Required]
public string? Name { get; set; }

[SwaggerSchema("Category address")]
public string? Address { get; set; }

[Required]
[SwaggerSchema("Category email")]
public string? Email { get; set; }
}
9 changes: 9 additions & 0 deletions WAW.API/Company/Resources/CompanyResource.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
using Swashbuckle.AspNetCore.Annotations;

namespace WAW.API.Company.Resources;

public class CompanyResource {
[SwaggerSchema("Company identifier")]
public long Id { get; set; }

[SwaggerSchema("Company name")]
public string? Name { get; set; }

[SwaggerSchema("Company address")]
public string? Address { get; set; }

[SwaggerSchema("Company email")]
public string? Email { get; set; }
}

0 comments on commit c9cc11b

Please sign in to comment.