public async Task <IActionResult> UploadMangaInfo([FromBody] MangaUploadCommand command,
                                                          CancellationToken token)
        {
            //TODO: Add validation
            string mangaId = await _mediator.Send(command, token);

            return(Ok(mangaId));
        }
Exemple #2
0
        public static async Task SeedMangas(IHost host)
        {
            using (var scope = host.Services.CreateScope())
            {
                var provider = scope.ServiceProvider;

                IMediator mediator = provider.GetRequiredService <IMediator>();

                string mangaInfoPath = Path.Combine(GetSeeededDataDirectory(), "Manga.json");

                MangaUploadCommand manga =
                    JsonConvert.DeserializeObject <MangaUploadCommand>(GetFileContent(mangaInfoPath));

                string mangaId = await mediator.Send(manga);

                string chaptersInfoPath = Path.Combine(GetSeeededDataDirectory(), "Chapters.json");

                List <ChapterUploadCommand> chapters =
                    JsonConvert.DeserializeObject <List <ChapterUploadCommand> >(GetFileContent(chaptersInfoPath));
                List <string> ChapterIds = new List <string>();
                foreach (var chapter in chapters)
                {
                    chapter.MangaId = mangaId;
                    string id = await mediator.Send(chapter);

                    ChapterIds.Add(id);
                }

                string picturesInfoPath = Path.Combine(GetSeeededDataDirectory(), "Pictures.json");

                List <PictureIntermediary> pictures =
                    JsonConvert.DeserializeObject <List <PictureIntermediary> >(GetFileContent(picturesInfoPath));

                Dictionary <string, string> ChapterNumberToChapterId = new Dictionary <string, string>();

                var picturesWithUniqueChapter = pictures
                                                .GroupBy(x => x.Chapter)
                                                .Select(x => x.First())
                                                .ToList();

                for (int i = 0; i < picturesWithUniqueChapter.Count; i++)
                {
                    ChapterNumberToChapterId.TryAdd(picturesWithUniqueChapter[i].Chapter, ChapterIds[i]);
                }

                string pictureLocationBase = Path.Combine(GetSeeededDataDirectory(), "Pictures");

                Dictionary <PictureIntermediary, IFormFile> pictureToFile =
                    new Dictionary <PictureIntermediary, IFormFile>();

                foreach (var picture in pictures)
                {
                    string path = Path.Combine(pictureLocationBase, picture.Chapter, picture.Order);

                    path = Path.ChangeExtension(path, "jpg");

                    FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read);

                    IFormFile formFile = new FormFile(file, 0, file.Length,
                                                      "Alexander", "Oleg.jpg");
                    pictureToFile.Add(picture, formFile);
                }

                List <ImageUploadCommand> images = new List <ImageUploadCommand>();

                foreach (var picture in pictures)
                {
                    var form = pictureToFile.GetValueOrDefault(picture);

                    int order = int.Parse(picture.Order);

                    string ChapterId = ChapterNumberToChapterId.GetValueOrDefault(picture.Chapter);

                    images.Add(new ImageUploadCommand(form, ChapterId, order));
                }

                foreach (var image in images)
                {
                    await mediator.Send(image);
                }
            }
        }