-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDemoDataFetcher.cs
More file actions
104 lines (85 loc) · 4.44 KB
/
DemoDataFetcher.cs
File metadata and controls
104 lines (85 loc) · 4.44 KB
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
using DevExtremeVSTemplateMVC.Models;
using DevExtremeVSTemplateMVC.Utils;
using Microsoft.EntityFrameworkCore;
namespace DevExtremeVSTemplateMVC.DAL
{
public static class DemoDataFetcher
{
static Dictionary<string, string> apiMapping = new Dictionary<string, string>() {
{ nameof(DemoDbContext.Tasks), "/Employees/AllTasks" },
{ "GetContact", "/Users/Contacts/{0}" },
};
public static Task DownloadTask = null;
public static async Task Download(HttpClient httpClient, IConfiguration config) {
string baseUrlAPI = config.GetValue<string>(ConfigKeys.BaseUrlAPIKey);
IList<EmployeeTask> allTasks = await FetchListFromApiAsync<EmployeeTask>(httpClient, baseUrlAPI + apiMapping[nameof(DemoDbContext.Tasks)]);
IList<TaskList> kanbanTaskLists = PrepareKanbanData(allTasks);
Contact contact = await FetchEntityFromApiAsync<Contact>(httpClient, baseUrlAPI + string.Format(apiMapping["GetContact"], DemoConsts.DemoUserProfileId));
PopulateContact(contact);
Directory.CreateDirectory(config.GetValue<string>("DatabasePathDirectory"));
string databasePath = string.Format("{0}/{1}",
config.GetValue<string>(ConfigKeys.DatabasePathDirectoryKey),
config.GetValue<string>(ConfigKeys.DatabaseFileNameKey));
var options = new DbContextOptionsBuilder<DemoDbContext>()
.UseSqlite($"Data Source={databasePath}")
.Options;
using var db = new DemoDbContext(options);
db.Database.EnsureDeleted();
db.Database.EnsureCreated();
db.Tasks.AddRange(allTasks);
db.Contacts.Add(contact);
db.TaskLists.AddRange(kanbanTaskLists);
await db.SaveChangesAsync();
}
static void PopulateContact(Contact contact) {
contact.Position = "Designer";
contact.Department = "UI/UX";
contact.BirthDate = new DateTime(1980, 5, 3);
contact.HiredDate = new DateTime(2023, 3, 3);
contact.Gender = "female";
contact.DomainUsername = "corp\\amelia.harper";
contact.Country = "USA";
contact.Supervisor = "Sam Adamson";
}
static IList<TaskList> PrepareKanbanData(IList<EmployeeTask> allTasks) {
var orderedTasks = allTasks.Where(t => t.Owner == DemoConsts.DemoFilteredOwnerName).ToList();
int openIndex = 0;
int inProgressIndex = 0;
int deferredIndex = 0;
int completedIndex = 0;
for (int i = 0; i < orderedTasks.Count; i++) {
if (orderedTasks[i].Status == "Open") {
orderedTasks[i].OrderIndex = openIndex++;
} else if (orderedTasks[i].Status == "In Progress") {
orderedTasks[i].OrderIndex = inProgressIndex++;
} else if (orderedTasks[i].Status == "Deferred") {
orderedTasks[i].OrderIndex = deferredIndex++;
} else if (orderedTasks[i].Status == "Completed") {
orderedTasks[i].OrderIndex = completedIndex++;
}
}
return new List<TaskList> {
new TaskList { Id = 1, ListName = "Open", OrderIndex = 1},
new TaskList { Id = 2, ListName = "In Progress", OrderIndex = 2},
new TaskList { Id = 3, ListName = "Deferred", OrderIndex = 3},
new TaskList { Id = 4, ListName = "Completed", OrderIndex = 4}
};
}
public static async Task<IList<T>> FetchListFromApiAsync<T>(HttpClient httpClient, string actionUrl) {
var response = await httpClient.GetAsync(actionUrl);
if (response.IsSuccessStatusCode) {
var allData = await response.Content.ReadFromJsonAsync<IList<T>>();
return allData;
}
throw new HttpRequestException("Failed to fetch data from a remote endpoint");
}
public static async Task<T> FetchEntityFromApiAsync<T>(HttpClient httpClient, string actionUrl) {
var response = await httpClient.GetAsync(actionUrl);
if (response.IsSuccessStatusCode) {
var entity = await response.Content.ReadFromJsonAsync<T>();
return entity;
}
throw new HttpRequestException("Failed to fetch data from a remote endpoint");
}
}
}