< Summary

Class:SVETA.Api.Services.Implements.ImportingExporting.TemplateHelper
Assembly:SVETA.Api
File(s):/opt/dev/sveta_api_build/SVETA.Api/Services/Implements/ImportingExporting/TemplateHelper.cs
Covered lines:0
Uncovered lines:72
Coverable lines:72
Total lines:108
Line coverage:0% (0 of 72)
Covered branches:0
Total branches:30
Branch coverage:0% (0 of 30)

Metrics

MethodLine coverage Branch coverage
GetMappedProperties()0%0%
GetTemplateWithError(...)0%0%
GetTemplate(...)0%0%

File(s)

/opt/dev/sveta_api_build/SVETA.Api/Services/Implements/ImportingExporting/TemplateHelper.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.ComponentModel;
 4using System.Linq;
 5using System.Reflection;
 6using System.Threading.Tasks;
 7
 8namespace SVETA.Api.Services.Implements.ImportingExporting
 9{
 10    public static class TemplateHelper
 11    {
 12        public static List<PropertyInfo> GetMappedProperties<TRecord>() where TRecord : class
 013        {
 014            var props = typeof(TRecord).GetProperties(BindingFlags.Instance | BindingFlags.Public)
 015                .Where(x => Attribute.GetCustomAttribute(x, typeof(MapToAttribute)) != null)
 016                .ToList();
 017            return props.Select(x =>
 018            {
 019                if (x.PropertyType != typeof(string))
 020                {
 021                    throw new Exception($"Поле {typeof(TRecord).Name}.{x.Name} должно иметь тип string");
 022                }
 023                return x;
 024            }).ToList();
 025        }
 26
 27        public static List<string[]> GetTemplateWithError<TRecord>(Dictionary<TRecord, Exception> records) where TRecord
 028        {
 029            var props = TemplateHelper.GetMappedProperties<TRecord>();
 030            var result = new List<string[]>();
 31
 32            // описание шаблона
 033            var desc = typeof(TRecord).GetCustomAttributes(typeof(DescriptionAttribute), true).FirstOrDefault() as Descr
 034            result.Add(new string[] { desc != null ? desc.Description : $"Шаблон загрузки {typeof(TRecord).Name}" });
 35
 036            var row = new List<string>();
 37            // русские колонки
 038            foreach (var prop in props)
 039            {
 040                row.Add((Attribute.GetCustomAttribute(prop, typeof(MapToAttribute)) as MapToAttribute).RusColumn);
 041            }
 042            row.Add("Ошибка");
 043            result.Add(row.ToArray());
 44
 45            // английские колонки
 046            row.Clear();
 047            foreach (var prop in props)
 048            {
 049                row.Add(prop.Name);
 050            }
 051            row.Add("Error");
 052            result.Add(row.ToArray());
 53
 54            // данные и ошибки
 055            foreach (var invalidRec in records)
 056            {
 057                row.Clear();
 058                foreach (var prop in props)
 059                {
 060                    row.Add((prop.GetValue(invalidRec.Key) ?? string.Empty).ToString());
 061                }
 062                row.Add(invalidRec.Value.Message);
 063                result.Add(row.ToArray());
 064            }
 65
 066            return result;
 067        }
 68
 69        public static List<string[]> GetTemplate<TRecord>(List<TRecord> records) where TRecord : class
 070        {
 071            var props = TemplateHelper.GetMappedProperties<TRecord>();
 072            var result = new List<string[]>();
 73
 74            // описание шаблона
 075            var desc = typeof(TRecord).GetCustomAttributes(typeof(DescriptionAttribute), true).FirstOrDefault() as Descr
 076            result.Add(new string[] { desc != null ? desc.Description : $"Шаблон загрузки {typeof(TRecord).Name}" });
 77
 078            var row = new List<string>();
 79            // русские колонки
 080            foreach (var prop in props)
 081            {
 082                row.Add((Attribute.GetCustomAttribute(prop, typeof(MapToAttribute)) as MapToAttribute).RusColumn);
 083            }
 084            result.Add(row.ToArray());
 85
 86            // английские колонки
 087            row.Clear();
 088            foreach (var prop in props)
 089            {
 090                row.Add(prop.Name);
 091            }
 092            result.Add(row.ToArray());
 93
 94            // данные
 095            foreach (var invalidRec in records)
 096            {
 097                row.Clear();
 098                foreach (var prop in props)
 099                {
 0100                    row.Add((prop.GetValue(invalidRec) ?? string.Empty).ToString());
 0101                }
 0102                result.Add(row.ToArray());
 0103            }
 104
 0105            return result;
 0106        }
 107    }
 108}