| | | 1 | | using Microsoft.EntityFrameworkCore; |
| | | 2 | | using MySqlConnector; |
| | | 3 | | using OzonDomains.Models; |
| | | 4 | | using OzonRepositories.Context; |
| | | 5 | | |
| | | 6 | | namespace OzonRepositories.Data; |
| | | 7 | | |
| | | 8 | | public class EmailRoutingRuleRepository : MainRepository, IRepository<EmailRoutingRule> |
| | | 9 | | { |
| | 5 | 10 | | public EmailRoutingRuleRepository(ApplicationDbContext orderContext) : base(orderContext) |
| | | 11 | | { |
| | 5 | 12 | | } |
| | | 13 | | |
| | | 14 | | public Task<List<EmailRoutingRule>> Get() |
| | | 15 | | { |
| | 0 | 16 | | return _context.EmailRoutingRules |
| | 0 | 17 | | .OrderBy(x => x.Priority) |
| | 0 | 18 | | .ThenBy(x => x.Name) |
| | 0 | 19 | | .ToListAsync(); |
| | | 20 | | } |
| | | 21 | | |
| | | 22 | | public async Task<EmailRoutingRule?> GetAsync(int id) |
| | | 23 | | { |
| | 0 | 24 | | return await _context.EmailRoutingRules.FirstOrDefaultAsync(x => x.Id == id); |
| | 0 | 25 | | } |
| | | 26 | | |
| | | 27 | | public async Task<EmailRoutingRule?> GetAsync(EmailRoutingRule value) |
| | | 28 | | { |
| | 0 | 29 | | return await _context.EmailRoutingRules.FirstOrDefaultAsync(x => x.Name == value.Name); |
| | 0 | 30 | | } |
| | | 31 | | |
| | | 32 | | public async Task<int> Add(EmailRoutingRule value) |
| | | 33 | | { |
| | 0 | 34 | | _context.EmailRoutingRules.Add(value); |
| | 0 | 35 | | return await _context.SaveChangesAsync(); |
| | 0 | 36 | | } |
| | | 37 | | |
| | | 38 | | public async Task<int> Update(EmailRoutingRule value) |
| | | 39 | | { |
| | 0 | 40 | | value.UpdatedAt = DateTime.UtcNow; |
| | 0 | 41 | | _context.EmailRoutingRules.Update(value); |
| | 0 | 42 | | return await _context.SaveChangesAsync(); |
| | 0 | 43 | | } |
| | | 44 | | |
| | | 45 | | public async Task<int> Delete(EmailRoutingRule value) |
| | | 46 | | { |
| | 0 | 47 | | _context.EmailRoutingRules.Remove(value); |
| | 0 | 48 | | return await _context.SaveChangesAsync(); |
| | 0 | 49 | | } |
| | | 50 | | |
| | | 51 | | public async Task<List<EmailRoutingRule>> GetActiveRules() |
| | | 52 | | { |
| | | 53 | | const int maxAttempts = 3; |
| | 12 | 54 | | for (var attempt = 1; attempt <= maxAttempts; attempt++) |
| | | 55 | | { |
| | | 56 | | try |
| | | 57 | | { |
| | 6 | 58 | | return await _context.EmailRoutingRules |
| | 6 | 59 | | .AsNoTracking() |
| | 6 | 60 | | .Where(x => x.IsActive) |
| | 6 | 61 | | .OrderBy(x => x.Priority) |
| | 6 | 62 | | .ToListAsync(); |
| | | 63 | | } |
| | 0 | 64 | | catch (MySqlEndOfStreamException) when (attempt < maxAttempts) |
| | | 65 | | { |
| | 0 | 66 | | await Task.Delay(TimeSpan.FromMilliseconds(400 * attempt)); |
| | | 67 | | } |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | // На эту строку не попадем, но компилятор требует return. |
| | 0 | 71 | | return []; |
| | 6 | 72 | | } |
| | | 73 | | } |