Skip to content
This repository was archived by the owner on Jun 25, 2022. It is now read-only.

Commit

Permalink
feat(back-end) Add Certificates controller and certificate view model
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlosPavajeau committed Mar 19, 2021
1 parent e5507f2 commit 576ec37
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
52 changes: 52 additions & 0 deletions Kaizen/Controllers/CertificatesController.cs
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));
}
}
}
14 changes: 14 additions & 0 deletions Kaizen/Mappers/CertificateMapperProfile.cs
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>();
}
}
}
14 changes: 14 additions & 0 deletions Kaizen/Models/Certificate/CertificateViewModel.cs
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; }
}
}

0 comments on commit 576ec37

Please sign in to comment.