public async Task <IHttpActionResult> GetAll(Guid districtId) { try { var userProblems = await UserProblemCore.GetUserProblemsAsync(Identity.Id, districtId); if (userProblems == null) { return(BadRequest("Invalid input")); } var resultModel = userProblems.Select(userProblem => new UserProblemModel { Id = userProblem.Id, UserId = userProblem.UserId, ProblemId = userProblem.ProblemId, DistrictId = userProblem.DistrictId, Name = userProblem.Problem.Name, Description = userProblem.Problem.Description, Order = userProblem.Order }).OrderBy(p => p.Order).ToList(); return(Ok(resultModel)); } catch (Exception ex) { return(InternalServerError(ex)); } }
public async Task <IHttpActionResult> Assign([FromBody] AssignUserProblemsModel model) { try { var districtId = model.DistrictId; var userProblems = await UserProblemCore.GetUserProblemsAsync(Identity.Id, districtId).ConfigureAwait(false); if (userProblems != null && userProblems.Count > 0) { var result = await UserProblemCore.DeleteAsync(userProblems).ConfigureAwait(false); if (!result) { return(InternalServerError()); } } if (model.UserProblems.Count == 0) { return(Ok()); } var newUserProblems = model.UserProblems.Select(userProblemModel => new UserProblem { UserId = Identity.Id, ProblemId = userProblemModel.ProblemId, DistrictId = districtId, Date = DateTime.Now, Order = userProblemModel.Order }).ToArray(); var createdProblems = await UserProblemCore.CreateAsync(newUserProblems).ConfigureAwait(false); if (createdProblems == null) { return(InternalServerError()); } return(Ok()); } catch (Exception ex) { return(InternalServerError()); } }