public async Task <IActionResult> Edit(EditAnnouncementViewData model) { if (ModelState.IsValid) { if (!HtmlHelpers.HtmlContainsText(model.Content)) { ModelState.AddModelError("Content", "Ogłoszenie musi zawierać treść."); } else { var ann = announcementService.GetAnnouncement(model.Id); if (ann == null) { return(NotFound()); } var authorizationResult = await authorizationService.AuthorizeAsync(User, ann, Operations.Edit); if (authorizationResult.Succeeded) { if (model.Files != null) { foreach (var file in model.Files) { var ext = Path.GetExtension(file.FileName).ToLowerInvariant(); var fileName = Path.GetFileNameWithoutExtension(file.FileName); if (UploadedFile.PermittedExtensions.Contains(ext)) { using (var memoryStream = new MemoryStream()) { await file.CopyToAsync(memoryStream); if (memoryStream.Length < UploadedFile.MaxFileSize) { var fileToUpload = new UploadedFile { FileName = fileName, FileExtension = ext, Content = memoryStream.ToArray(), AnnouncementID = model.Id }; announcementService.SaveUploadedFile(fileToUpload); } else { ModelState.AddModelError("File", "Plik jest zbyt duży."); } } } else { ModelState.AddModelError("Extension", "Niedozwolony typ pliku"); } } } if (ModelState.IsValid) { var announcement = new Announcement { Content = model.Content }; if (model.SelectedCourseID.HasValue) { announcement.CourseID = model.SelectedCourseID.Value; } announcementService.EditAnnouncement(model.Id, announcement); } } } } if (ModelState.IsValid) { TempData["Toast"] = Toasts.AnnouncementEdited; return(Redirect(model.ReturnUrl)); } else { var uploadedFile = new List <UploadedFile>(); foreach (var file in model.UploadedFiles) { uploadedFile.Add(new UploadedFile { FileName = file.FileName, FileExtension = file.FileExtension, UploadedFileID = file.UploadedFileID }); } var result = new EditAnnouncementViewData { Id = model.Id, Content = model.Content, UploadedFiles = model.UploadedFiles, UserCourses = coursesService.GetUserCourses(userManager.GetUserId(HttpContext.User)), ReturnUrl = model.ReturnUrl }; if (model.SelectedCourseID.HasValue) { result.SelectedCourseID = model.SelectedCourseID.Value; } else { result.SelectedCourseID = null; } return(View(result)); } }