public async Task <ActionResult> UploadPicture(IFormFile file, int blogPostId) { var result = await _pictureService.AddPictureAsync(file); if (result.Error is not null) { return(BadRequest(result.Error.Message)); } var newPicture = new PictureDTO() { BlogPostId = blogPostId, TypeDiscriminator = 1, PublicId = result.PublicId, Url = result.SecureUrl.AbsoluteUri, Description = file.FileName }; await _pictureRepository.AddPictureToBlogPostAsync(newPicture); if (await _pictureRepository.SaveAllChangesAsync()) { return(CreatedAtRoute( "GetBlogPostById", new { id = blogPostId }, newPicture )); } return(BadRequest(result.Error.Message)); }
public async Task <IActionResult> AddTask(ProblemViewModel model) { if (User.Identity.IsAuthenticated) { var user = await _userManager.GetUserAsync(User); ViewBag.Name = user.Name; } if (ModelState.IsValid) { var subject = _subjectService.GetSubject(model.Subject); Problem problem = new Problem { SubjectId = subject.Id, Number = model.Number, Answer = model.Answer }; await _problemService.AddProblemAsync(problem); foreach (var imageData in model.Condition) { using var binaryReader = new BinaryReader(imageData.OpenReadStream()); var content = binaryReader.ReadBytes((int)imageData.Length); var picture = new Picture { Content = content, Alt = $"{model.Subject} - {model.Number}", IsSolve = false, ProblemId = problem.Id }; await _pictureService.AddPictureAsync(picture); } foreach (var imageData in model.Solve) { using var binaryReader = new BinaryReader(imageData.OpenReadStream()); var content = binaryReader.ReadBytes((int)imageData.Length); var picture = new Picture { Content = content, Alt = $"{model.Subject} - {model.Number}", IsSolve = true, ProblemId = problem.Id }; await _pictureService.AddPictureAsync(picture); } return(RedirectToAction("Index", "Subject", new { subjectId = subject.Id })); } ModelState.AddModelError("Condition", ModelState.ErrorCount.ToString()); return(View(model)); }