This repository was archived by the owner on Jun 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(back-end) Add Certificates controller and certificate view model
- Loading branch information
1 parent
e5507f2
commit 576ec37
Showing
3 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using AutoMapper; | ||
using Kaizen.Domain.Repositories; | ||
using Kaizen.Models.Certificate; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace Kaizen.Controllers | ||
{ | ||
[Route("api/[controller]")] | ||
[ApiController] | ||
[Authorize] | ||
public class CertificatesController : ControllerBase | ||
{ | ||
private readonly ICertificatesRepository _certificatesRepository; | ||
private readonly IMapper _mapper; | ||
|
||
public CertificatesController(ICertificatesRepository certificatesRepository, IMapper mapper) | ||
{ | ||
_certificatesRepository = certificatesRepository; | ||
_mapper = mapper; | ||
} | ||
|
||
[HttpGet("{id}")] | ||
public async Task<ActionResult<CertificateViewModel>> GetCertificate(int id) | ||
{ | ||
var certificate = await _certificatesRepository.FindByIdAsync(id); | ||
if (certificate is null) | ||
{ | ||
return NotFound($"No existe un certificado con el id {id}"); | ||
} | ||
|
||
return _mapper.Map<CertificateViewModel>(certificate); | ||
} | ||
|
||
[HttpGet("Client/{clientId}")] | ||
public async Task<ActionResult<IEnumerable<CertificateViewModel>>> GetClientCertificates(string clientId) | ||
{ | ||
var certificates = await _certificatesRepository.GetAll() | ||
.Include(c => c.WorkOrder) | ||
.ThenInclude(w => w.Activity) | ||
.ThenInclude(a => a.Client) | ||
.Where(c => c.WorkOrder.Activity.ClientId == clientId) | ||
.ToListAsync(); | ||
|
||
return Ok(_mapper.Map<IEnumerable<CertificateViewModel>>(certificates)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using AutoMapper; | ||
using Kaizen.Domain.Entities; | ||
using Kaizen.Models.Certificate; | ||
|
||
namespace Kaizen.Mappers | ||
{ | ||
public class CertificateMapperProfile : Profile | ||
{ | ||
public CertificateMapperProfile() | ||
{ | ||
CreateMap<Certificate, CertificateViewModel>(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System; | ||
using Kaizen.Models.WorkOrder; | ||
|
||
namespace Kaizen.Models.Certificate | ||
{ | ||
public class CertificateViewModel | ||
{ | ||
public int Id { get; set; } | ||
|
||
public DateTime Validity { get; set; } | ||
|
||
public WorkOrderViewModel WorkOrder { get; set; } | ||
} | ||
} |