| | | 1 | | using OzonDomains.Models; |
| | | 2 | | using OzonRepositories.Data; |
| | | 3 | | |
| | | 4 | | namespace Services.DataServices |
| | | 5 | | { |
| | | 6 | | public class OzonClientServices : IDataServices |
| | | 7 | | { |
| | | 8 | | private readonly OzonClientRepository _repository; |
| | | 9 | | private readonly CryptographyServices _cryptography; |
| | | 10 | | private readonly OrdersDataServices _ordersDataServices; |
| | | 11 | | |
| | 21 | 12 | | public OzonClientServices(OzonClientRepository repository, |
| | 21 | 13 | | CryptographyServices cryptography, |
| | 21 | 14 | | OrdersDataServices ordersDataServices) |
| | | 15 | | { |
| | 21 | 16 | | _repository = repository; |
| | 21 | 17 | | _cryptography = cryptography; |
| | 21 | 18 | | _ordersDataServices = ordersDataServices; |
| | 21 | 19 | | } |
| | | 20 | | public async Task<int> AddOzonClient(OzonClient value) |
| | | 21 | | { |
| | 0 | 22 | | if (value.ClientId != null && value.ApiKey != null) |
| | | 23 | | { |
| | 0 | 24 | | value.ClientId = _cryptography.Encrypt(value.ClientId); |
| | 0 | 25 | | value.ApiKey = _cryptography.Encrypt(value.ApiKey); |
| | | 26 | | } |
| | 0 | 27 | | return await _repository.Add(value); |
| | 0 | 28 | | } |
| | | 29 | | |
| | | 30 | | public async Task<int> DeleteOzonClient(OzonClient value) |
| | | 31 | | { |
| | 0 | 32 | | var clientOrders = await _ordersDataServices.GetOrders(); |
| | 0 | 33 | | var ordersToUpdate = clientOrders.Where(order => order.OzonClient == value).ToList(); |
| | | 34 | | |
| | 0 | 35 | | foreach (var order in ordersToUpdate) |
| | | 36 | | { |
| | 0 | 37 | | order.OzonClient = null; |
| | 0 | 38 | | await _ordersDataServices.UpdateOrder(order); |
| | | 39 | | } |
| | | 40 | | |
| | 0 | 41 | | return await _repository.Delete(value); |
| | 0 | 42 | | } |
| | | 43 | | |
| | | 44 | | public virtual async Task<List<OzonClient>> GetOzonClients() |
| | | 45 | | { |
| | 0 | 46 | | List<OzonClient> ozonClients = await _repository.Get(); |
| | 0 | 47 | | foreach (var ozonClient in ozonClients) |
| | | 48 | | { |
| | 0 | 49 | | if (ozonClient != null && ozonClient.ClientId != null && ozonClient.ApiKey != null) |
| | | 50 | | { |
| | 0 | 51 | | ozonClient.DecryptClientId = _cryptography.Decrypt(ozonClient.ClientId); |
| | 0 | 52 | | ozonClient.DecryptApiKey = _cryptography.Decrypt(ozonClient.ApiKey); |
| | | 53 | | } |
| | | 54 | | } |
| | | 55 | | |
| | 0 | 56 | | return ozonClients; |
| | 0 | 57 | | } |
| | | 58 | | |
| | | 59 | | public async Task<OzonClient?> GetOzonClientAsync(int id) |
| | | 60 | | { |
| | 0 | 61 | | var ozonClient = await _repository.GetAsync(id); |
| | 0 | 62 | | if (ozonClient != null && ozonClient.ClientId != null && ozonClient.ApiKey != null) |
| | | 63 | | { |
| | 0 | 64 | | ozonClient.DecryptClientId = _cryptography.Decrypt(ozonClient.ClientId); |
| | 0 | 65 | | ozonClient.DecryptApiKey = _cryptography.Decrypt(ozonClient.ApiKey); |
| | | 66 | | } |
| | 0 | 67 | | return ozonClient; |
| | 0 | 68 | | } |
| | | 69 | | |
| | | 70 | | public async Task<OzonClient?> GetOzonClientAsync(OzonClient value) |
| | | 71 | | { |
| | 0 | 72 | | var ozonClient = await _repository.GetAsync(value); |
| | 0 | 73 | | if (ozonClient != null && ozonClient.ClientId != null && ozonClient.ApiKey != null) |
| | | 74 | | { |
| | 0 | 75 | | ozonClient.DecryptClientId = _cryptography.Decrypt(ozonClient.ClientId); |
| | 0 | 76 | | ozonClient.DecryptApiKey = _cryptography.Decrypt(ozonClient.ApiKey); |
| | | 77 | | } |
| | 0 | 78 | | return ozonClient; |
| | 0 | 79 | | } |
| | | 80 | | |
| | | 81 | | public async Task<int> Update(OzonClient value) |
| | | 82 | | { |
| | 0 | 83 | | if (value.ClientId != null && value.ApiKey != null) |
| | | 84 | | { |
| | 0 | 85 | | value.ClientId = _cryptography.Encrypt(value.ClientId); |
| | 0 | 86 | | value.ApiKey = _cryptography.Encrypt(value.ApiKey); |
| | | 87 | | } |
| | | 88 | | |
| | 0 | 89 | | return await _repository.Update(value); |
| | 0 | 90 | | } |
| | | 91 | | public Task<int> SaveChanges() |
| | | 92 | | { |
| | 0 | 93 | | return _repository.SaveChanges(); |
| | | 94 | | } |
| | | 95 | | } |
| | | 96 | | } |