Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using O2NextGen.CertificateManagement.Business.Models;

namespace O2NextGen.CertificateManagement.Business.Services
{
public interface ICertificatesService
{
IReadOnlyCollection<Certificate> GetAll();
Task<IReadOnlyCollection<Certificate>> GetAllAsync(CancellationToken cancellationToken);

Certificate GetById(long id);
Task<Certificate> GetByIdAsync(long id, CancellationToken cancellationToken);

Certificate Update(Certificate certificate);
Task<Certificate> UpdateAsync(Certificate certificate, CancellationToken cancellationToken);

Certificate Add(Certificate certificate);
Task<Certificate> AddAsync(Certificate certificate, CancellationToken cancellationToken);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using O2NextGen.CertificateManagement.Business.Models;
using O2NextGen.CertificateManagement.Business.Services;

Expand Down Expand Up @@ -29,32 +31,36 @@ public InMemoryCertificatesService()
#endregion

#region Methods
public IReadOnlyCollection<Certificate> GetAll()
public async Task<IReadOnlyCollection<Certificate>> GetAllAsync(CancellationToken cancellationToken)
{
return Certificates.AsReadOnly();
await Task.Delay(3000, cancellationToken);
return await Task.FromResult<IReadOnlyCollection<Certificate>>(Certificates.AsReadOnly());
}

public Certificate GetById(long id)
public async Task<Certificate> GetByIdAsync(long id, CancellationToken cancellationToken)
{
return Certificates.SingleOrDefault(g => g.Id == id);
await Task.Delay(3000, cancellationToken);
return await Task.FromResult(Certificates.SingleOrDefault(g => g.Id == id));
}

public Certificate Update(Certificate certificate)
public async Task<Certificate> UpdateAsync(Certificate certificate, CancellationToken cancellationToken)
{
await Task.Delay(5000, cancellationToken);
var toUpdate = Certificates.SingleOrDefault(g => g.Id == certificate.Id);
if (toUpdate == null)
return null;

toUpdate.Name = certificate.Name;

return toUpdate;
return await Task.FromResult(toUpdate);
}

public Certificate Add(Certificate certificate)
public async Task<Certificate> AddAsync(Certificate certificate, CancellationToken cancellationToken)
{
await Task.Delay(3000, cancellationToken);
certificate.Id = ++_currentId;
Certificates.Add(certificate);
return certificate;
return await Task.FromResult(certificate);
}
#endregion
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using O2NextGen.CertificateManagement.Business.Services;
using O2NextGen.CertificateManagement.Web.Mappings;
using O2NextGen.CertificateManagement.Web.Mappings;
using O2NextGen.CertificateManagement.Web.Models;

namespace O2NextGen.CertificateManagement.Web.Controllers
Expand All @@ -12,8 +14,6 @@ public class CertificatesController : Controller

private readonly ICertificatesService _certificatesService;



#endregion


Expand All @@ -31,19 +31,19 @@ public CertificatesController(ICertificatesService certificatesService)

[HttpGet]
[Route("")]
public IActionResult Index()
public async Task<IActionResult> Index()
{
var models = _certificatesService.GetAll();
var models = await _certificatesService.GetAllAsync(CancellationToken.None);
if (models == null)
return NotFound();
return View(models.ToViewModel());
}

[HttpGet]
[Route("{id}")]
public IActionResult Detail(long id)
public async Task<IActionResult> Detail(long id, CancellationToken ct)
{
var certificate = _certificatesService.GetById(id);
var certificate = await _certificatesService.GetByIdAsync(id, ct);
if (certificate == null)
return NotFound();
return View(certificate.ToViewModel());
Expand All @@ -52,9 +52,9 @@ public IActionResult Detail(long id)
[HttpPost]
[Route("id")]
[ValidateAntiForgeryToken]
public IActionResult Edit(long id, CertificateViewModel model)
public async Task<IActionResult> Edit(long id, CertificateViewModel model, CancellationToken ct)
{
var certificate = _certificatesService.Update(model.ToModel());
var certificate = await _certificatesService.UpdateAsync(model.ToModel(), ct);
if (certificate == null)
return NotFound();
certificate.Name = model.Name;
Expand All @@ -71,9 +71,9 @@ public IActionResult Create()

[HttpPost]
[Route("")]
public IActionResult CreateReally(CertificateViewModel model)
public async Task<IActionResult> CreateReally(CertificateViewModel model, CancellationToken ct)
{
_certificatesService.Add(model.ToModel());
await _certificatesService.AddAsync(model.ToModel(), ct);
return RedirectToAction("Index");
}

Expand Down