-
Notifications
You must be signed in to change notification settings - Fork 15
/
OfficeService.cs
64 lines (51 loc) · 2.42 KB
/
OfficeService.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
namespace DentallApp.Features.Offices;
public class OfficeService : IOfficeService
{
private readonly IOfficeRepository _officeRepository;
public OfficeService(IOfficeRepository officeRepository)
{
_officeRepository = officeRepository;
}
public async Task<Response> CreateOfficeAsync(OfficeInsertDto officeInsertDto)
{
_officeRepository.Insert(officeInsertDto.MapToOfficeDto());
await _officeRepository.SaveAsync();
return new Response
{
Success = true,
Message = CreateResourceMessage
};
}
public async Task<Response> UpdateOfficeAsync(int officeId, int currentEmployeeId, OfficeUpdateDto officeUpdateDto)
{
var currentOffice = await _officeRepository.GetOfficeByIdAsync(officeId);
if (currentOffice is null)
return new Response(ResourceNotFoundMessage);
var strategy = _officeRepository.CreateExecutionStrategy();
await strategy.ExecuteAsync(
async () =>
{
using var transaction = await _officeRepository.BeginTransactionAsync();
if (officeUpdateDto.IsInactive() && officeUpdateDto.IsCheckboxTicked && currentOffice.IsEnabledEmployeeAccounts)
await _officeRepository.DisableEmployeeAccountsAsync(currentEmployeeId, currentOffice);
else if (officeUpdateDto.IsInactive() && officeUpdateDto.IsCheckboxUnticked && currentOffice.IsDisabledEmployeeAccounts)
await _officeRepository.EnableEmployeeAccountsAsync(currentOffice);
else if (officeUpdateDto.IsActive() && currentOffice.IsDisabledEmployeeAccounts)
await _officeRepository.EnableEmployeeAccountsAsync(currentOffice);
officeUpdateDto.MapToOfficeDto(currentOffice);
await _officeRepository.SaveAsync();
await transaction.CommitAsync();
});
return new Response
{
Success = true,
Message = UpdateResourceMessage
};
}
public async Task<IEnumerable<OfficeGetDto>> GetOfficesAsync()
=> await _officeRepository.GetOfficesAsync();
public async Task<IEnumerable<OfficeShowDto>> GetOfficesForEditAsync()
=> await _officeRepository.GetOfficesForEditAsync();
public async Task<IEnumerable<OfficeGetDto>> GetAllOfficesAsync()
=> await _officeRepository.GetAllOfficesAsync();
}