| | | 1 | | |
| | | 2 | | using System.Text.Json; |
| | | 3 | | using Microsoft.EntityFrameworkCore; |
| | | 4 | | using OzonDomains; |
| | | 5 | | using OzonOrdersWeb.Areas.OzonCards.ViewModels; |
| | | 6 | | using OzonRepositories.Context; |
| | | 7 | | |
| | | 8 | | public class ExcelMappingRepository : IExcelMappingRepository |
| | | 9 | | { |
| | | 10 | | private readonly ApplicationDbContext _context; |
| | | 11 | | |
| | 0 | 12 | | public ExcelMappingRepository(ApplicationDbContext context) |
| | | 13 | | { |
| | 0 | 14 | | _context = context; |
| | 0 | 15 | | } |
| | | 16 | | |
| | | 17 | | public async Task<ExcelMapping?> GetByIdAsync(int id) |
| | | 18 | | { |
| | 0 | 19 | | return await _context.ExcelMappings.FindAsync(id); |
| | 0 | 20 | | } |
| | | 21 | | |
| | | 22 | | public async Task<List<ExcelMapping>> GetAllAsync() |
| | | 23 | | { |
| | 0 | 24 | | return await _context.ExcelMappings |
| | 0 | 25 | | .OrderByDescending(x => x.CreatedDate) |
| | 0 | 26 | | .ToListAsync(); |
| | 0 | 27 | | } |
| | | 28 | | |
| | | 29 | | public async Task<int> CreateAsync(ExcelMapping mapping) |
| | | 30 | | { |
| | 0 | 31 | | _context.ExcelMappings.Add(mapping); |
| | 0 | 32 | | await _context.SaveChangesAsync(); |
| | 0 | 33 | | return mapping.Id; |
| | 0 | 34 | | } |
| | | 35 | | |
| | | 36 | | public async Task UpdateAsync(ExcelMapping mapping) |
| | | 37 | | { |
| | 0 | 38 | | mapping.ModifiedDate = DateTime.UtcNow; |
| | 0 | 39 | | _context.ExcelMappings.Update(mapping); |
| | 0 | 40 | | await _context.SaveChangesAsync(); |
| | 0 | 41 | | } |
| | | 42 | | |
| | | 43 | | public async Task DeleteAsync(int id) |
| | | 44 | | { |
| | 0 | 45 | | var mapping = await GetByIdAsync(id); |
| | 0 | 46 | | if (mapping != null) |
| | | 47 | | { |
| | 0 | 48 | | _context.ExcelMappings.Remove(mapping); |
| | 0 | 49 | | await _context.SaveChangesAsync(); |
| | | 50 | | } |
| | 0 | 51 | | } |
| | | 52 | | |
| | | 53 | | public async Task<ExcelMappingRequest?> GetRequestByIdAsync(int id) |
| | | 54 | | { |
| | 0 | 55 | | var mapping = await GetByIdAsync(id); |
| | 0 | 56 | | if (mapping == null) |
| | 0 | 57 | | return null; |
| | | 58 | | |
| | 0 | 59 | | return new ExcelMappingRequest |
| | 0 | 60 | | { |
| | 0 | 61 | | SourceFileName = mapping.SourceFileName, |
| | 0 | 62 | | DestinationFileName = mapping.DestinationFileName, |
| | 0 | 63 | | SourceSheet = mapping.SourceSheet, |
| | 0 | 64 | | DestinationSheet = mapping.DestinationSheet, |
| | 0 | 65 | | SourceHeaderRow = mapping.SourceHeaderRow, |
| | 0 | 66 | | DestinationHeaderRow = mapping.DestinationHeaderRow, |
| | 0 | 67 | | SourceDataStartRow = mapping.SourceDataStartRow, |
| | 0 | 68 | | DestinationDataStartRow = mapping.DestinationDataStartRow, |
| | 0 | 69 | | ColumnMappings = JsonSerializer.Deserialize<List<ColumnMappingExcel>>(mapping.ColumnMappingsJson) ?? new |
| | 0 | 70 | | }; |
| | 0 | 71 | | } |
| | | 72 | | } |