public async Task <IDeletableEntityRepository <Material> > UploadAsync(MaterialUploadViewModel input, ApplicationUser user)
        {
            var material = AutoMapperConfig.MapperInstance.Map <Material>(input);

            await this.MaterialsRepository.AddAsync(material);

            material.AuthorId = user.Id;

            var uploadResult = await this.Cloudinary.UploadMultipleAsync(input.Files);

            foreach (var fileUrl in uploadResult)
            {
                var file = new MaterialFiles
                {
                    MaterialId = material.Id,
                    Material   = material,
                    UserId     = user.Id,
                    FileUrl    = fileUrl,
                };

                material.FilesUrls.Add(file);
                await this.MaterialFilesRepository.AddAsync(file);
            }

            await this.MaterialsRepository.SaveChangesAsync();

            await this.MaterialFilesRepository.SaveChangesAsync();

            user.Materials.Add(material);

            return(this.MaterialsRepository);
        }
Beispiel #2
0
        public async Task <IActionResult> Upload(MaterialUploadViewModel input)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(input));
            }

            var user = await this.UserManager.GetUserAsync(this.User);

            var result = await MaterialsService.UploadAsync(input, user);

            return(this.RedirectToAction("All"));
        }