示例#1
0
        [HttpPost] // AJAX request
        public IActionResult Add(AddMaterialViewModel vm)
        {
            var identityUserId = User.FindFirstValue(ClaimTypes.NameIdentifier);

            var lesson = _traversalService.GetLesson(vm.LessonId);

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

            var creator = _userService.FindByIdentityUserId(identityUserId);
            var state   = _service.GetState(MaterialStateEnum.PENDING);

            var materialToAdd = new BLL.Models.Material(vm.Name, vm.Description, lesson, creator, state);

            _service.AddMaterial(materialToAdd);

            var guid = Guid.NewGuid();

            try
            {
                _filesManagement.SaveFilesToFileSystem(vm.FormFiles, materialToAdd.MaterialId, guid);
            }
            catch (Exception)
            {
                return(BadRequest(new[] { "Somethig went wrong while saving files to the file system. Try again." }));
            }

            // TODO splitted into two roundtrips to name folders with materialId (can be changed in the future)
            var filesToAdd = _filesManagement.ExtractFilesFromForm(vm.FormFiles, materialToAdd.MaterialId, guid);

            materialToAdd.AddFiles(filesToAdd);
            _service.UpdateMaterial(materialToAdd);

            return(Json(materialToAdd.MaterialId));
        }