Пример #1
0
        public IActionResult Create([Bind("Id,ProjectId,Name,Description,EffortId,ResourceId,StatusId,Files")] TaskCreateModel viewModel)
        {
            try
            {
                if (!ModelIsValid(viewModel))
                {
                    this.AssignLookUps(_projectInteractor.GetProjectName(viewModel.ProjectId), viewModel);

                    return(View(viewModel));
                }

                var model = new TaskPT()
                {
                    //Id = 0,
                    ProjectId   = viewModel.ProjectId,
                    Name        = viewModel.Name,
                    Description = viewModel.Description,
                    EffortId    = (int)viewModel.EffortId,
                    StatusId    = viewModel.StatusId,
                    ResourceId  = (int)viewModel.ResourceId
                };

                //Save files to database
                var documents = new List <Documents>();

                if (viewModel.Files.Count() > 0)
                {
                    foreach (IFormFile item in viewModel.Files)
                    {
                        if (item.Length > 0)
                        {
                            string fileName = Path.GetFileName(item.FileName);
                            string fileType = Path.GetExtension(item.FileName);

                            documents.Add(new Documents()
                            {
                                FileName = fileName,
                                FileType = fileType,
                                Task     = model,
                                TaskId   = model.Id,
                            });
                        }
                    }

                    //link documents to model
                    model.Documents = documents;
                }

                var result = _interactor.CreateTask(model);
                if (result.Item1 == true)
                {
                    var files = _fileUpload.UploadDocuments(viewModel.Files, viewModel.ProjectId).Result;

                    TempData["success"] = $"'{viewModel.Name}' was added!";
                    return(RedirectToAction("Index", "Task", new { id = viewModel.ProjectId }));
                }

                TempData.Remove("fail");
                TempData["fail"] = $"Error: {result.Item2}";
                return(View());
            }
            catch (Exception ex)
            {
                TempData["fail"] = $"Error: {ex.Message}";
                return(View());
            }
        }