public (bool, string) CreateTask(TaskPT task) { if (!TaskExists(task.Name)) { var now = DateTime.Now; task.StatusId = (int)StatusId.New; task.CreatedDate = now; task.ModifiedDate = now; _db.Task.Add(task); _db.SaveChanges(); return(true, null); } return(false, $"A task '{task.Name}' already exists."); }
public (bool, string) UpdateTask(int id, TaskPT task) { var result = _db.Task.Where(x => x.Id == id).FirstOrDefault(); if (result != null) { //result.Project = task.ProjectId //shouldnt be able to switch the task to another project result.Name = task.Name; result.Description = task.Description; result.StatusId = task.StatusId; result.ModifiedDate = DateTime.Now; result.EffortId = task.EffortId; _db.Task.Update(result); if (task.Documents.Count > 0) { _db.Documents.UpdateRange(task.Documents); } _db.SaveChanges(); //the resource has changed, notify them if (result.ResourceId != task.ResourceId) { var email = new EmailModel() { ToEmails = new string[] { "*****@*****.**" }, Message = "You have been assigned a new task" }; _emailServer.SendEmail(email); } return(true, null); } return(false, $"Task id '{task.Id}' could not be found."); }
public IActionResult Edit(int id, [Bind("Id,ProjectId,Name,Description,EffortId,ResourceId,StatusId,Files")] TaskCreateModel viewModel) { if (id != viewModel.Id) { return(NotFound()); } try { if (!ModelIsValid(viewModel)) { this.AssignLookUps(_projectInteractor.GetProjectName(viewModel.ProjectId), viewModel); return(View(viewModel)); } var model = new TaskPT() { Id = viewModel.Id, ProjectId = viewModel.ProjectId, Name = viewModel.Name, Description = viewModel.Description?.TruncateAndTrim(255), 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 = viewModel.Id, }); } } //link documents to model model.Documents = documents; } var result = _interactor.UpdateTask(id, model); if (result.Item1 == true) { var files = _fileUpload.UploadDocuments(viewModel.Files, viewModel.ProjectId).Result; TempData["success"] = $"'{viewModel.Name}' was updated!"; 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()); } }