| | | 1 | | using Microsoft.EntityFrameworkCore; |
| | | 2 | | using OzonDomains; |
| | | 3 | | using OzonDomains.Models; |
| | | 4 | | using OzonRepositories.Context; |
| | | 5 | | |
| | | 6 | | namespace OzonRepositories.Data |
| | | 7 | | { |
| | | 8 | | public class ClientStatusRepository : MainRepository, IRepository<ClientStatus> |
| | | 9 | | { |
| | 0 | 10 | | public ClientStatusRepository(ApplicationDbContext orderContext) : base(orderContext) |
| | | 11 | | { |
| | 0 | 12 | | } |
| | | 13 | | |
| | | 14 | | public Task<int> Add(ClientStatus value) |
| | | 15 | | { |
| | 0 | 16 | | _context.ClientStatuses.AddAsync(value); |
| | 0 | 17 | | return _context.SaveChangesAsync(); |
| | | 18 | | } |
| | | 19 | | |
| | | 20 | | public Task<int> Delete(ClientStatus value) |
| | | 21 | | { |
| | 0 | 22 | | _context.Remove(value); |
| | 0 | 23 | | return _context.SaveChangesAsync(); |
| | | 24 | | } |
| | | 25 | | |
| | | 26 | | public async Task<List<ClientStatus>> Get() |
| | | 27 | | { |
| | 0 | 28 | | return await _context.ClientStatuses |
| | 0 | 29 | | .Include(s => s.StatusColor) |
| | 0 | 30 | | .ToListAsync(); |
| | 0 | 31 | | } |
| | | 32 | | |
| | | 33 | | public async Task<ClientStatus?> GetAsync(int id) |
| | | 34 | | { |
| | 0 | 35 | | return await _context.ClientStatuses |
| | 0 | 36 | | .Include(s => s.StatusColor) |
| | 0 | 37 | | .FirstOrDefaultAsync(a => a.Id == id); |
| | 0 | 38 | | } |
| | | 39 | | |
| | | 40 | | public async Task<ClientStatus?> GetAsync(ClientStatus value) |
| | | 41 | | { |
| | 0 | 42 | | return await _context.ClientStatuses |
| | 0 | 43 | | .Include(s => s.StatusColor) |
| | 0 | 44 | | .SingleOrDefaultAsync(a => a.Name == value.Name); |
| | 0 | 45 | | } |
| | | 46 | | |
| | | 47 | | public async Task<ClientStatus?> GetByNameAsync(string name) |
| | | 48 | | { |
| | 0 | 49 | | if (string.IsNullOrEmpty(name)) |
| | 0 | 50 | | return null; |
| | | 51 | | |
| | 0 | 52 | | return await _context.ClientStatuses.FirstOrDefaultAsync(a => a.Name == name); |
| | 0 | 53 | | } |
| | | 54 | | |
| | | 55 | | public async Task<int> Update(ClientStatus value) |
| | | 56 | | { |
| | 0 | 57 | | await Task.CompletedTask; |
| | 0 | 58 | | _context.ClientStatuses.Update(value); |
| | 0 | 59 | | return _context.SaveChanges(); |
| | 0 | 60 | | } |
| | | 61 | | |
| | | 62 | | public async Task UpdateStatusColor(int statusId, string colorCode) |
| | | 63 | | { |
| | 0 | 64 | | var status = await _context.ClientStatuses.FindAsync(statusId); |
| | 0 | 65 | | if (status != null) |
| | | 66 | | { |
| | 0 | 67 | | var color = await _context.StatusColors.FirstOrDefaultAsync(c => c.ColorCode == colorCode); |
| | 0 | 68 | | if (color == null) |
| | | 69 | | { |
| | 0 | 70 | | color = new StatusColor { ColorCode = colorCode }; |
| | 0 | 71 | | await _context.StatusColors.AddAsync(color); |
| | 0 | 72 | | await _context.SaveChangesAsync(); |
| | | 73 | | } |
| | | 74 | | |
| | 0 | 75 | | status.StatusColorId = color.Id; |
| | 0 | 76 | | await _context.SaveChangesAsync(); |
| | 0 | 77 | | } |
| | 0 | 78 | | } |
| | | 79 | | |
| | | 80 | | public async Task ResetStatusColor(int statusId) |
| | | 81 | | { |
| | 0 | 82 | | var status = await _context.ClientStatuses.FindAsync(statusId); |
| | 0 | 83 | | if (status != null) |
| | | 84 | | { |
| | 0 | 85 | | status.StatusColorId = null; |
| | 0 | 86 | | await _context.SaveChangesAsync(); |
| | | 87 | | } |
| | 0 | 88 | | } |
| | | 89 | | } |
| | | 90 | | } |