-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
the previously introduced TagArticles grain is done, serving articles query by tag. And also some refactor of mediator handle. This is still not in the final form yet, as I think the final form should be map of enum and function.
- Loading branch information
riza_ramadan
committed
Jan 12, 2021
1 parent
d43d379
commit 6b5d51f
Showing
5 changed files
with
98 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
namespace Grains.Articles | ||
{ | ||
using Contracts; | ||
using Contracts.Articles; | ||
using Contracts.Users; | ||
using Orleans; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
public class BaseArticleGrain : Grain | ||
{ | ||
protected readonly IGrainFactory _factory; | ||
public BaseArticleGrain(IGrainFactory f) | ||
{ | ||
_factory = f; | ||
} | ||
|
||
protected async Task<List<ArticleUserPair>> | ||
GetArticlesData(List<(long ArticleId, string Author)> list) | ||
{ | ||
|
||
var articleTasks = new List<Task<(Article article, Error error)>>(list.Count); | ||
var authorTasks = new List<Task<(User User, Error Error)>>(list.Count); | ||
var authorSet = new HashSet<string>(); | ||
foreach (var each in list) | ||
{ | ||
var articleGrain = _factory.GetGrain<IArticleGrain>(each.ArticleId, each.Author); | ||
articleTasks.Add(articleGrain.GetArticle()); | ||
if (!authorSet.Contains(each.Author)) | ||
{ | ||
authorSet.Add(each.Author); | ||
var userGrain = _factory.GetGrain<IUserGrain>(each.Author); | ||
authorTasks.Add(userGrain.Get()); | ||
} | ||
} | ||
|
||
var articles = (await Task.WhenAll(articleTasks)).ToList(); | ||
var authors = (await Task.WhenAll(authorTasks)).ToDictionary(x => x.User.Username, y => y.User); | ||
|
||
return articles.Where(x => !x.error.Exist()) | ||
.Select(x => new ArticleUserPair(x.article, authors[x.article.Author])) | ||
.ToList(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters