public async Task <ResponseLogic> UpdateAsync(ProjectDTO project, int requestingUserId) { var currentProject = await _repository.FindAsync(project.Id); if (currentProject is null) { return(ResponseLogic.NOT_FOUND); } if (currentProject.Creator?.Id != requestingUserId) { return(ResponseLogic.UNAUTHORISED); } var currentSkills = currentProject.Skills; var skills = project.Skills; var skillsToAdd = skills.Where(s => !currentSkills.Contains(s)); var skillsToRemove = currentSkills.Where(s => !skills.Contains(s)); foreach (var skill in skillsToAdd) { await _skillLogic.CreateAsync(new SkillCreateDTO() { Name = skill.Name }); } foreach (var skill in skillsToRemove) { await _skillLogic.RemoveWithObjectAsync(skill); } var success = await _repository.UpdateAsync(project); if (success) { return(ResponseLogic.SUCCESS); } // roll back skill changes foreach (var skill in skillsToAdd) { await _skillLogic.RemoveWithObjectAsync(skill); } foreach (var skill in skillsToRemove) { await _skillLogic.CreateAsync(new SkillCreateDTO() { Name = skill.Name }); } return(ResponseLogic.ERROR_UPDATING); }
public async Task <ResponseLogic> UpdateAsync(int userId, UserDTO user) { var currentUser = await _repository.FindAsync(userId); if (currentUser is null) { return(ResponseLogic.NOT_FOUND); } var currentSkills = (currentUser.Skills == null) ? new SkillDTO[] { } : currentUser.Skills; var skills = (user.Skills == null) ? new SkillDTO[] { } : user.Skills; var skillsToAdd = skills.Where(s => !currentSkills.Contains(s)); var skillsToRemove = currentSkills.Where(s => !skills.Contains(s)); foreach (var skill in skillsToAdd) { await _skillLogic.CreateAsync(new SkillCreateDTO() { Name = skill.Name }); } foreach (var skill in skillsToRemove) { await _skillLogic.RemoveWithObjectAsync(skill); } var success = await _repository.UpdateAsync(userId, user); if (success) { return(ResponseLogic.SUCCESS); } // roll back skill changes foreach (var skill in skillsToAdd) { await _skillLogic.RemoveWithObjectAsync(skill); } foreach (var skill in skillsToRemove) { await _skillLogic.CreateAsync(new SkillCreateDTO() { Name = skill.Name }); } return(ResponseLogic.ERROR_UPDATING); }
public async Task <IActionResult> Post([FromBody] SkillCreateDTO skill) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var success = await _skillLogic.CreateAsync(skill); if (success == ResponseLogic.SUCCESS) { var createdSkill = await _skillLogic.FindExactAsync(skill.Name); return(CreatedAtAction(nameof(Get), new { createdSkill.Id }, createdSkill.Id)); } else { return(StatusCode(500)); } }