Skip to content
Open

CRUD #93

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
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;

namespace PromoCodeFactory.Core.Abstractions.Models.Employees
{
public class EmployeeCreateDto
{
public string FirstName { get; set; }

public string LastName { get; set; }

public string Email { get; set; }

public List<Guid> RoleIds { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using PromoCodeFactory.Core.Domain.Administration;
using System;
using System.Collections.Generic;

namespace PromoCodeFactory.Core.Abstractions.Models.Employees
{
public class EmployeeUpdateDto
{
public string FirstName { get; set; }

public string LastName { get; set; }

public string Email { get; set; }

public List<Guid> RoleIds { get; set; }

public int AppliedPromocodesCount { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using PromoCodeFactory.Core.Abstractions.Models.Employees;
using PromoCodeFactory.Core.Domain.Administration;

namespace PromoCodeFactory.Core.Abstractions.Repositories.Interfaces.Employees
{
public interface IEmployeeRepository : ICRUDRepository<Employee, EmployeeCreateDto, EmployeeUpdateDto>
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using PromoCodeFactory.Core.Domain;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace PromoCodeFactory.Core.Abstractions.Repositories.Interfaces
{
public interface ICRUDRepository<T, TCreate, TUpdate> : IRepository<T> where T : BaseEntity
{
Task<T> AddAsync(TCreate entity, CancellationToken cancellationToken = default);

Task<T> UpdateAsync(Guid entityId, TUpdate entity, CancellationToken cancellationToken = default);

Task<Guid> DeleteAsync(Guid entityId, CancellationToken cancellationToken = default);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace PromoCodeFactory.Core.Exceptions
{
public class NotFoundEntityException : Exception
{
public NotFoundEntityException(string entity) : base($"Entity \"{entity}\" not found.") { }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using PromoCodeFactory.Core.Abstractions.Models.Employees;
using PromoCodeFactory.Core.Abstractions.Repositories;
using PromoCodeFactory.Core.Abstractions.Repositories.Interfaces.Employees;
using PromoCodeFactory.Core.Domain.Administration;
using PromoCodeFactory.Core.Exceptions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace PromoCodeFactory.DataAccess.Repositories
{
public class EmployeeRepository : InMemoryRepository<Employee>, IEmployeeRepository
{
private IRepository<Role> roleRepository;

public EmployeeRepository(IEnumerable<Employee> data, IRepository<Role> roleRepository) : base(data)
{
this.roleRepository = roleRepository;
}

public async Task<Employee> AddAsync(EmployeeCreateDto entity, CancellationToken cancellationToken)
{
var roles = new List<Role>();

foreach (var rol in entity.RoleIds)
roles.Add(await this.roleRepository.GetByIdAsync(rol));

var newEmployee = new Employee()
{
Id = Guid.NewGuid(),
FirstName = entity.FirstName,
LastName = entity.LastName,
Email = entity.Email,
Roles = roles,
AppliedPromocodesCount = 0
};

((IList<Employee>)this.Data).Add(newEmployee);

return newEmployee;
}

public Task<Guid> DeleteAsync(Guid entityId, CancellationToken cancellationToken)
{
var employee = this.Data.Where(emp => emp.Id == entityId).FirstOrDefault();

if(employee != null)
{
((IList<Employee>)this.Data).Remove(employee);

return Task.FromResult(employee.Id);
}

throw new NotFoundEntityException(nameof(Employee));
}

public async Task<Employee> UpdateAsync(Guid entityId, EmployeeUpdateDto entity, CancellationToken cancellationToken)
{
var emp = await this.GetByIdAsync(entityId);

emp.FirstName = entity.FirstName;
emp.LastName = entity.LastName;
emp.Email = entity.Email;
emp.AppliedPromocodesCount = entity.AppliedPromocodesCount;

if (entity.RoleIds == null)
return emp;

var roles = new List<Role>();

foreach (var rol in entity.RoleIds)
roles.Add(await this.roleRepository.GetByIdAsync(rol));

emp.Roles = roles;

return emp;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc;
using PromoCodeFactory.Core.Abstractions.Models.Employees;
using PromoCodeFactory.Core.Abstractions.Repositories;
using PromoCodeFactory.Core.Abstractions.Repositories.Interfaces.Employees;
using PromoCodeFactory.Core.Domain.Administration;
using PromoCodeFactory.Core.Exceptions;
using PromoCodeFactory.WebHost.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace PromoCodeFactory.WebHost.Controllers
{
Expand All @@ -16,9 +20,9 @@ namespace PromoCodeFactory.WebHost.Controllers
[Route("api/v1/[controller]")]
public class EmployeesController : ControllerBase
{
private readonly IRepository<Employee> _employeeRepository;
private readonly IEmployeeRepository _employeeRepository;

public EmployeesController(IRepository<Employee> employeeRepository)
public EmployeesController(IEmployeeRepository employeeRepository)
{
_employeeRepository = employeeRepository;
}
Expand Down Expand Up @@ -70,5 +74,26 @@ public async Task<ActionResult<EmployeeResponse>> GetEmployeeByIdAsync(Guid id)

return employeeModel;
}

[HttpPost("Create")]
public async Task<Employee> CreateAsync([FromBody] EmployeeCreateDto employeeCreate, CancellationToken cancellationToken) =>
await this._employeeRepository.AddAsync(employeeCreate, cancellationToken);

[HttpPatch("Update/{id:guid}")]
public async Task<Employee> UpdateAsync(Guid id, [FromBody] EmployeeUpdateDto employeeUpdate, CancellationToken cancellationToken) =>
await this._employeeRepository.UpdateAsync(id, employeeUpdate, cancellationToken);

[HttpDelete("Delete")]
public async Task<ActionResult<Guid>> DeleteAsync(Guid id)
{
try
{
return await this._employeeRepository.DeleteAsync(id);
}
catch (NotFoundEntityException ex)
{
return NotFound();
}
}
}
}
5 changes: 3 additions & 2 deletions Homeworks/Base/src/PromoCodeFactory.WebHost/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using PromoCodeFactory.Core.Abstractions.Repositories;
using PromoCodeFactory.Core.Abstractions.Repositories.Interfaces.Employees;
using PromoCodeFactory.Core.Domain.Administration;
using PromoCodeFactory.DataAccess.Data;
using PromoCodeFactory.DataAccess.Repositories;
Expand All @@ -14,8 +15,8 @@ public class Startup
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddSingleton(typeof(IRepository<Employee>), (x) =>
new InMemoryRepository<Employee>(FakeDataFactory.Employees));
services.AddSingleton(typeof(IEmployeeRepository), (x) =>
new EmployeeRepository(FakeDataFactory.Employees, new InMemoryRepository<Role>(FakeDataFactory.Roles)));
services.AddSingleton(typeof(IRepository<Role>), (x) =>
new InMemoryRepository<Role>(FakeDataFactory.Roles));

Expand Down