-
-
Notifications
You must be signed in to change notification settings - Fork 99
/
EmployeeRepository.cs
134 lines (114 loc) · 4.78 KB
/
EmployeeRepository.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using System.Threading;
using System.Threading.Tasks;
using HappyCode.NetCoreBoilerplate.Core.Models;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using HappyCode.NetCoreBoilerplate.Core.Dtos;
using HappyCode.NetCoreBoilerplate.Core.Extensions;
namespace HappyCode.NetCoreBoilerplate.Core.Repositories
{
public interface IEmployeeRepository
{
Task<List<EmployeeDto>> GetAllAsync(CancellationToken cancellationToken);
Task<EmployeeDto> GetByIdAsync(int id, CancellationToken cancellationToken);
Task<EmployeeDetailsDto> GetByIdWithDetailsAsync(object id, CancellationToken cancellationToken);
Task<EmployeeDto> GetOldestAsync(CancellationToken cancellationToken);
Task<bool> DeleteByIdAsync(int id, CancellationToken cancellationToken);
Task<EmployeeDto> InsertAsync(EmployeePostDto employeePostDto, CancellationToken cancellationToken);
Task<EmployeeDto> UpdateAsync(int id, EmployeePutDto employeePutDto, CancellationToken cancellationToken);
}
internal class EmployeeRepository : RepositoryBase<Employee>, IEmployeeRepository
{
public EmployeeRepository(EmployeesContext dbContext) : base(dbContext)
{
}
public async Task<List<EmployeeDto>> GetAllAsync(CancellationToken cancellationToken)
{
var employees = await DbContext.Employees
.AsNoTracking()
.ToListAsync(cancellationToken);
return employees.Select(EmployeeExtensions.MapToDto).ToList();
}
public async Task<EmployeeDto> GetByIdAsync(int id, CancellationToken cancellationToken)
{
var emp = await DbContext.Employees
.AsNoTracking()
.SingleOrDefaultAsync(x => x.EmpNo == id, cancellationToken);
if (emp == null)
{
return null;
}
return emp.MapToDto();
}
public async Task<EmployeeDetailsDto> GetByIdWithDetailsAsync(object id, CancellationToken cancellationToken)
{
var emp = await DbContext.Employees
.Include(x => x.Department)
.SingleOrDefaultAsync(x => x.EmpNo == (int)id, cancellationToken);
if (emp == null)
{
return null;
}
return new EmployeeDetailsDto
{
Id = emp.EmpNo,
FirstName = emp.FirstName,
LastName = emp.LastName,
BirthDate = emp.BirthDate,
Gender = emp.Gender,
Department = new DepartmentDto
{
Id = emp.Department.DeptNo,
Name = emp.Department.DeptName,
}
};
}
public async Task<EmployeeDto> GetOldestAsync(CancellationToken cancellationToken)
{
var emp = await DbContext.Employees
.OrderBy(x => x.BirthDate)
.FirstOrDefaultAsync(cancellationToken);
if (emp == null)
{
return null;
}
return emp.MapToDto();
}
public async Task<EmployeeDto> InsertAsync(EmployeePostDto employeePostDto, CancellationToken cancellationToken)
{
var employee = new Employee
{
FirstName = employeePostDto.FirstName,
LastName = employeePostDto.LastName,
BirthDate = employeePostDto.BirthDate.Value,
Gender = employeePostDto.Gender,
};
await DbContext.Employees.AddAsync(employee, cancellationToken);
await DbContext.SaveChangesAsync(cancellationToken);
return employee.MapToDto();
}
public async Task<EmployeeDto> UpdateAsync(int id, EmployeePutDto employeePutDto, CancellationToken cancellationToken)
{
var emp = await DbContext.Employees
.SingleOrDefaultAsync(x => x.EmpNo == id, cancellationToken);
if (emp is null)
{
return null;
}
emp.LastName = employeePutDto.LastName;
await DbContext.SaveChangesAsync(cancellationToken);
return emp.MapToDto();
}
public async Task<bool> DeleteByIdAsync(int id, CancellationToken cancellationToken)
{
var emp = await DbContext.Employees
.SingleOrDefaultAsync(x => x.EmpNo == id, cancellationToken);
if (emp == null)
{
return false;
}
DbContext.Employees.Remove(emp);
return await DbContext.SaveChangesAsync(cancellationToken) > 0;
}
}
}