-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
113 lines (99 loc) · 2.85 KB
/
Program.cs
File metadata and controls
113 lines (99 loc) · 2.85 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
105
106
107
108
109
110
111
112
113
using BooksDataLoader.models;
using BooksDataLoader.repository;
using Cassandra;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
IConfiguration config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddEnvironmentVariables()
.Build();
var authorRepository = new AuthorRepository(config);
var bookRepository = new BookRepository(config);
void InitAuthors()
{
try
{
StreamReader reader = new StreamReader(config.GetValue<string>("authorsFile"));
string? line;
List<Task> dbWriteTasks = new List<Task>();
while ((line = reader.ReadLine()) != null)
{
try
{
// parse
line = line.Substring(line.IndexOf("{"));
var lineJson = JObject.Parse(line);
var key = lineJson.GetValue("key").ToString();
var authorId = key.Replace("/authors/", "");
var authorName = lineJson.GetValue("name").ToString();
// construct author object
var author = new Author
{
Id = authorId,
Name = authorName
};
// write to repository
Console.WriteLine("Saving author " + author.Name + "...");
dbWriteTasks.Add(authorRepository.InsertAsync(author));
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
Task.WaitAll(dbWriteTasks.ToArray());
} catch (IOException e)
{
Console.WriteLine(e.Message);
}
}
void InitWorks()
{
try
{
StreamReader reader = new StreamReader(config.GetValue<string>("worksFile"));
string? line;
List<Task> dbWriteTasks = new List<Task>();
while ((line = reader.ReadLine()) != null)
{
try
{
// parse
line = line.Substring(line.IndexOf("{"));
var lineJson = JObject.Parse(line);
var authorIds = lineJson.GetValue("authors").Select(
(val) => val.SelectToken("author.key").ToString().Replace("/authors/", "")
).ToList();
var authorNames = authorIds.Select(val => authorRepository.Get(val)?.Name ?? "Unknown author").ToList();
var date = lineJson.SelectToken("created.value").ToObject<DateTime>();
// construct book object
var book = new Book
{
Id = lineJson.GetValue("key").ToString().Replace("/works/", ""),
Name = lineJson.GetValue("title").ToString(),
Description = lineJson.SelectToken("description.value")?.ToString() ?? "",
PublishedDate = new LocalDate(date.Year, date.Month, date.Day),
AuthorIds = authorIds,
AuthorNames = authorNames,
CoverIds = lineJson.GetValue("covers")?.ToList().Select(val => val.ToString()).ToList() ?? new List<string>()
};
// write to repository
Console.WriteLine("Saving book " + book.Name + "...");
dbWriteTasks.Add(bookRepository.InsertAsync(book));
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
Task.WaitAll(dbWriteTasks.ToArray());
}
catch (IOException e)
{
Console.WriteLine(e.Message);
}
}
//InitAuthors();
InitWorks();