public async Task <IActionResult> CreateNewProject(ProjectCreateDto projectCreateDto) { if (string.IsNullOrWhiteSpace(projectCreateDto.Description) || string.IsNullOrWhiteSpace(projectCreateDto.ProjectName) || projectCreateDto.SelectedUserIds.Count == 0) { return(BadRequest("All fields are required!")); } var projectToCreate = _mapper.Map <Project>(projectCreateDto); var projectUsers = new List <ProjectsAndUsers>(); foreach (var userId in projectCreateDto.SelectedUserIds) { var projectUser = new ProjectsAndUsers { ApplicationUserId = userId, }; projectUsers.Add(projectUser); } projectToCreate.ProjectsAndUsers = projectUsers; var currentUserId = _userManager.GetUserId(User); projectToCreate.ApplicationUserId = currentUserId; _unitOfWork.Projects.Add(projectToCreate); if (await _unitOfWork.Complete() >= 1) { return(Ok("Created!")); } else { return(BadRequest("An error happened while creating new project!")); } }
public async Task <IActionResult> Edit(int id, [FromBody] ProjectEditViewModel projectEditViewModel) { if (id != projectEditViewModel.Id) { return(NotFound()); } if (string.IsNullOrWhiteSpace(projectEditViewModel.ProjectName) || string.IsNullOrWhiteSpace(projectEditViewModel.Description) || projectEditViewModel.SelectedUserIds.Count == 0) { return(BadRequest("All fields are required")); } else { var projectToEdit = await _unitOfWork.Projects.GetById(projectEditViewModel.Id); _mapper.Map(projectEditViewModel, projectToEdit); // update ProjectsAndUsers table var projectUsers = new List <ProjectsAndUsers>(); foreach (var userId in projectEditViewModel.SelectedUserIds) { var projectUser = new ProjectsAndUsers { ApplicationUserId = userId, ProjectId = id }; projectUsers.Add(projectUser); } projectToEdit.ProjectsAndUsers = projectUsers; await _unitOfWork.Complete(); return(RedirectToAction(nameof(Index))); } }