| | | 1 | | using System.Text.Json; |
| | | 2 | | using OzonOrdersWeb.Areas.OzonCards.ViewModels; |
| | | 3 | | |
| | | 4 | | namespace Services.DataServices.ExcelMapping; |
| | | 5 | | |
| | | 6 | | public class ExcelMappingService : IExcelMappingService |
| | | 7 | | { |
| | | 8 | | private readonly IExcelMappingRepository _repository; |
| | | 9 | | |
| | 0 | 10 | | public ExcelMappingService(IExcelMappingRepository repository) |
| | | 11 | | { |
| | 0 | 12 | | _repository = repository; |
| | 0 | 13 | | } |
| | | 14 | | |
| | | 15 | | public async Task<int> SaveMappingAsync(ExcelMappingRequest request, string name, string? createdBy = null) |
| | | 16 | | { |
| | 0 | 17 | | var mapping = new OzonDomains.ExcelMapping |
| | 0 | 18 | | { |
| | 0 | 19 | | Name = name, |
| | 0 | 20 | | SourceFileName = request.SourceFileName, |
| | 0 | 21 | | DestinationFileName = request.DestinationFileName, |
| | 0 | 22 | | SourceSheet = request.SourceSheet, |
| | 0 | 23 | | DestinationSheet = request.DestinationSheet, |
| | 0 | 24 | | SourceHeaderRow = request.SourceHeaderRow, |
| | 0 | 25 | | DestinationHeaderRow = request.DestinationHeaderRow, |
| | 0 | 26 | | SourceDataStartRow = request.SourceDataStartRow, |
| | 0 | 27 | | DestinationDataStartRow = request.DestinationDataStartRow, |
| | 0 | 28 | | ColumnMappingsJson = JsonSerializer.Serialize(request.ColumnMappings), |
| | 0 | 29 | | CreatedBy = createdBy ?? "" |
| | 0 | 30 | | }; |
| | | 31 | | |
| | 0 | 32 | | return await _repository.CreateAsync(mapping); |
| | 0 | 33 | | } |
| | | 34 | | |
| | | 35 | | public async Task<ExcelMappingRequest?> GetMappingAsync(int id) |
| | | 36 | | { |
| | 0 | 37 | | return await _repository.GetRequestByIdAsync(id); |
| | 0 | 38 | | } |
| | | 39 | | |
| | | 40 | | public async Task<List<OzonDomains.ExcelMapping>> GetAllMappingsAsync() |
| | | 41 | | { |
| | 0 | 42 | | return await _repository.GetAllAsync(); |
| | 0 | 43 | | } |
| | | 44 | | |
| | | 45 | | public async Task UpdateMappingAsync(int id, ExcelMappingRequest request, string? name = null) |
| | | 46 | | { |
| | 0 | 47 | | var existingMapping = await _repository.GetByIdAsync(id); |
| | 0 | 48 | | if (existingMapping == null) |
| | 0 | 49 | | throw new ArgumentException("Mapping not found"); |
| | | 50 | | |
| | 0 | 51 | | if (!string.IsNullOrEmpty(name)) |
| | 0 | 52 | | existingMapping.Name = name; |
| | | 53 | | |
| | 0 | 54 | | existingMapping.SourceFileName = request.SourceFileName; |
| | 0 | 55 | | existingMapping.DestinationFileName = request.DestinationFileName; |
| | 0 | 56 | | existingMapping.SourceSheet = request.SourceSheet; |
| | 0 | 57 | | existingMapping.DestinationSheet = request.DestinationSheet; |
| | 0 | 58 | | existingMapping.SourceHeaderRow = request.SourceHeaderRow; |
| | 0 | 59 | | existingMapping.DestinationHeaderRow = request.DestinationHeaderRow; |
| | 0 | 60 | | existingMapping.SourceDataStartRow = request.SourceDataStartRow; |
| | 0 | 61 | | existingMapping.DestinationDataStartRow = request.DestinationDataStartRow; |
| | 0 | 62 | | existingMapping.ColumnMappingsJson = JsonSerializer.Serialize(request.ColumnMappings); |
| | 0 | 63 | | existingMapping.ModifiedDate = DateTime.UtcNow; |
| | | 64 | | |
| | 0 | 65 | | await _repository.UpdateAsync(existingMapping); |
| | 0 | 66 | | } |
| | | 67 | | |
| | | 68 | | public async Task DeleteMappingAsync(int id) |
| | | 69 | | { |
| | 0 | 70 | | await _repository.DeleteAsync(id); |
| | 0 | 71 | | } |
| | | 72 | | } |