-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImportService.cs
269 lines (230 loc) · 8.18 KB
/
ImportService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ExcelParsingUtil.Models;
using Microsoft.AspNetCore.Mvc.ModelBinding;
namespace ExcelParsingUtil
{
public static class ImportService
{
public static async Task<ImportResult> ImportFile(ImportFileData importData, ModelStateDictionary modelState)
{
if ((importData.FileData.Length / 1024.0F / 1024.0F) > 1.1f)
{
AddModelError(
modelState,
"File exceeds maximum size of 1MB",
0
);
return null;
}
#region Variable Initialization
IList<ImportRow> rows;
IList<ComicBookInventory> newComicBookInventory = new List<ComicBookInventory>();
var expectedColumns = new[]
{
"issuenumber",
"title",
"description",
"flag",
"datepublished",
};
#endregion
using (var document = ExcelParsingUtil.OpenDocumentFromByteArray(importData.FileData, true))
{
var worksheetPart = ExcelParsingUtil.GetWorksheetPartFromDocument(document, "");
if (!ExcelParsingUtil.HasAnyRows(worksheetPart))
{
AddModelError(
modelState,
"File is empty",
0
);
return null;
}
#region Data Converter
ImportRow DataConverter(IList<string> rowData, IList<string> columnNames,
int rowNumber)
{
if (expectedColumns.Except(columnNames).Any())
{
var exception = new Exception("Missing expected columns in data");
exception.Data.Add("rowNumber", rowNumber);
throw exception;
}
var importRow = new ImportRow
{
IssueNumber = rowData[columnNames.IndexOf("issuenumber")].Trim(),
Title = rowData[columnNames.IndexOf("title")].Trim(),
Description = rowData[columnNames.IndexOf("description")].Trim(),
Flag = rowData[columnNames.IndexOf("flag")].Trim(),
DatePublished = DateTime.FromOADate(double.Parse(rowData[columnNames.IndexOf("datepublished")].Trim())),
ItemGuid = Guid.NewGuid(),
RowNumber = rowNumber,
};
ValidateImportRow(importRow, modelState);
return importRow;
}
#endregion
#region Parse File and Validate
try
{
rows = ExcelParsingUtil.GetDataToList(
document,
DataConverter
);
}
catch (Exception e)
{
var rowNumber = 0;
if (e.Data.Keys.Count > 0)
{
int.TryParse(e.Data["rowNumber"].ToString(), out rowNumber);
}
AddModelError(
modelState,
e.Message,
rowNumber
);
return null;
}
if (!rows.Any())
{
AddModelError(
modelState,
"No rows",
0
);
return null;
}
if (TooManyErrors(modelState))
{
return null;
}
#endregion
#region Loop Rows for each Sample Result for Review
foreach (var row in rows)
{
if (row == null)
{
continue;
}
try
{
ComicBookInventory currentComicBookInventory = null;
currentComicBookInventory = ProcessOneComicBook(row);
if (modelState.ErrorCount == 0)
{
newComicBookInventory.Add(currentComicBookInventory);
}
}
catch (Exception e)
{
AddModelError(
modelState,
e.Message,
row.RowNumber
);
}
if (!TooManyErrors(modelState))
{
continue;
}
break;
}
if (modelState.ErrorCount > 0)
{
return null;
}
#endregion
#region Persist Sample Results for Review objects
// Using a repository save these in a database
// await repository.AddMultiple(newSampleResultReview);
// await DbContext.SaveChangesAsync();
#endregion
}
#region Create Import Stats for Front-end
var importStats = new ImportResult
{
NumberOfRows = rows.Count,
FileName = importData.FileName
};
#endregion
return importStats;
}
private static void AddModelError(ModelStateDictionary modelState, string message, int rowNumber)
{
var prefixMessage = "Error while processing row " + rowNumber + ": ";
if (rowNumber == 0)
{
prefixMessage = "";
}
modelState.AddModelError("form", prefixMessage + message);
}
private static ComicBookInventory ProcessOneComicBook(
ImportRow row
)
{
return new ComicBookInventory()
{
// Id = row.ItemGuid,
IssueNumber = row.IssueNumber,
Title = row.Title,
Description = row.Description,
Flag = row.Flag,
DatePublished = row.DatePublished,
};
}
private static bool TooManyErrors(ModelStateDictionary modelState, bool addTooManyErrorsMessage = true)
{
var tooManyErrors = modelState?["form"]?.Errors?.Count > 10;
if (tooManyErrors && addTooManyErrorsMessage)
{
AddModelError(
modelState,
"Stopped processing after 10 or more errors",
0
);
}
return tooManyErrors;
}
#region Validation
private static void ValidateImportRow(ImportRow row, ModelStateDictionary modelState)
{
if (string.IsNullOrEmpty(row.IssueNumber))
{
AddModelError(
modelState,
"Issue Number cannot be left blank",
row.RowNumber
);
}
if (string.IsNullOrEmpty(row.Title))
{
AddModelError(
modelState,
"Title cannot be left blank",
row.RowNumber
);
}
if (string.IsNullOrEmpty(row.Description))
{
AddModelError(
modelState,
"Description cannot be left blank",
row.RowNumber
);
}
if (row.DatePublished > DateTimeOffset.Now)
{
AddModelError(
modelState,
"Date Published must be a past date",
row.RowNumber
);
}
}
#endregion
}
}