Beispiel #1
0
        [HttpPost] // AJAX request
        public IActionResult CreateEditSuggestion(EditRequestViewModel vm)
        {
            var identityUserId = User.FindFirstValue(ClaimTypes.NameIdentifier);

            var materialToEdit = _service.GetMaterial(vm.EditMaterialViewModel.MaterialId);

            if (materialToEdit == null)
            {
                return(BadRequest(new[] { "Materiał o podanym Id nie istnieje." }));
            }

            if (identityUserId == materialToEdit.Creator.IdentityUserId)
            {
                return(RedirectToAction(
                           "ActionForbidden",
                           "Error",
                           new
                {
                    error = "nie masz uprawnień do tego działania. Jesteś autorem danego materiału - skorzystaj z opcji edycji"
                }));
            }

            var updater = _userService.FindByIdentityUserId(identityUserId);

            ICollection <BLL.Models.File> filesFromForm = new List <BLL.Models.File>();

            try
            {
                BLL.Models.EditRequest editRequest = _service.CreateEditRequest(
                    updater, materialToEdit, vm.Summary, vm.EditMaterialViewModel.Name, vm.EditMaterialViewModel.Description);

                var guid = Guid.NewGuid();

                // # physical save #
                _filesManagement.SaveFilesToFileSystem(vm.FormFiles, vm.EditMaterialViewModel.MaterialId, guid, editRequest.EditRequestId);

                // # database save #
                // existing files
                var newFiles = new List <BLL.Models.File>();

                if (vm.Files != null)
                {
                    newFiles.AddRange(
                        vm.Files
                        .Select(f => new BLL.Models.File(f.FileName, f.RelativePath, f.ContentType))
                        .ToList());
                }

                // new files
                filesFromForm = _filesManagement.ExtractFilesFromForm(
                    vm.FormFiles, vm.EditMaterialViewModel.MaterialId, guid, editRequest.EditRequestId);
                newFiles.AddRange(filesFromForm);

                editRequest.AddFiles(newFiles);
                _service.UpdateEditRequest(editRequest);
            }
            catch (ArgumentNullException)
            {
                return(BadRequest(new[] { "Materiał o podanym Id nie istnieje" }));
            }
            catch (Exception)
            {
                _filesManagement.RemoveFilesFromFileSystem(filesFromForm);
                return(BadRequest(new[] { "Coś poszło nie tak podczas zapisywania plików. Spróbuj ponownie." }));
            }

            return(Json(vm.EditMaterialViewModel.MaterialId));
        }