This repository was archived by the owner on May 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAuthorService.cs
42 lines (38 loc) · 1.65 KB
/
AuthorService.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
// ---------------------------------------------------------------
// Copyright (c) 2023 Planet Dotnet. All rights reserved.
// Licensed under the MIT License.
// See License.txt in the project root for license information.
// ---------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Newtonsoft.Json;
using PlanetDotnet.Authors.Models.Authors;
namespace PlanetDotnet.Authors.Services
{
public static class AuthorService
{
public static async Task<IEnumerable<Author>> GetAllAuthors()
{
var assembly = Assembly.GetExecutingAssembly();
var resourceNames = assembly.GetManifestResourceNames();
var authorsResourceNames = resourceNames.Where(res =>
res.StartsWith("PlanetDotnet.Authors", StringComparison.OrdinalIgnoreCase) &&
res.EndsWith(".json", StringComparison.OrdinalIgnoreCase));
var authorsTasks = authorsResourceNames.Select(name => ReadAuthor(assembly, name));
var authors = await Task.WhenAll(authorsTasks).ConfigureAwait(false);
return authors;
}
private static async Task<Author> ReadAuthor(Assembly assembly, string authorResourceName)
{
using var stream = assembly.GetManifestResourceStream(authorResourceName);
using var reader = new StreamReader(stream);
var json = await reader.ReadToEndAsync().ConfigureAwait(false);
var author = JsonConvert.DeserializeObject<Author>(json);
return author;
}
}
}