public async Task <IActionResult> DeleteQuestion(long projectId, long schemeQuestionId) { // check if the requestor is an admin or coordinator. int.TryParse(HttpContext.User.Claims.FirstOrDefault(c => c.Type.Equals("Id"))?.Value, out var requestorId); var user = await _unitOfWork.Users.SingleOrDefaultAsync(u => u.Id == requestorId); if (!_unitOfWork.Users.CanUserEdit(user)) { return(Forbid()); } var project = await _unitOfWork.Projects.GetProjectWithScheme(p => p.Id == projectId); if (project == null) { return(NotFound()); } if (project.Status != (int)ProjectStatus.Active) { // project locked no update allowed return(StatusCode(403)); } var schemeQuestionTree = _unitOfWork.Schemes.GetSchemeQuestionsAsTree(project.Scheme); if (schemeQuestionTree == null || schemeQuestionTree.Count == 0) { return(StatusCode(304)); } var questionToDelete = _unitOfWork.SchemeQuestions.FindSchemeQuestionWithChildQuestionsDto(schemeQuestionId, schemeQuestionTree); if (questionToDelete == null) { return(NotFound()); } var helperMethods = new HelperMethods(_unitOfWork); var deletedQuestionIds = await helperMethods.DeleteSchemeQuestionAndAllNestedQuestions(questionToDelete); var outline = JsonConvert.DeserializeObject <Dictionary <long, OrderInfo> >(project.Scheme.QuestionNumbering); foreach (var id in deletedQuestionIds) { outline.Remove(id); } project.Scheme.QuestionNumbering = JsonConvert.SerializeObject(outline); await _unitOfWork.Complete(); var returnObject = new SchemeReturnDto { SchemeQuestions = Mapper.Map <ICollection <SchemeQuestionReturnDto> >(project.Scheme.Questions), Outline = JsonConvert.DeserializeObject(project.Scheme.QuestionNumbering) }; return(Ok(returnObject)); }
public async Task <IActionResult> GetAll(long projectId) { var project = await _unitOfWork.Projects.GetProjectScheme(p => p.Id == projectId); if (project == null) { return(NotFound()); } var outlineToReturn = project.Scheme.QuestionNumbering != null? JsonConvert.DeserializeObject(project.Scheme.QuestionNumbering) : new {}; var returnObject = new SchemeReturnDto { SchemeQuestions = Mapper.Map <ICollection <SchemeQuestionReturnDto> >(project.Scheme.Questions), Outline = outlineToReturn }; return(Ok(returnObject)); }