public HttpResponseMessage PostTest([FromBody] TestDataModel model) { foreach (var subject in model.SubjectList) { if (_questionDal.GetAllQuestions().Count(q => q.CategoryId == subject.Id) < model.NumberOfQuestions) { return(Request.CreateResponse(HttpStatusCode.BadRequest, new { Message = " Number of questions to high " })); } } var questionList = new List <Question>(); foreach (var subject in model.SubjectList) { var questions = _questionDal.GetAllQuestions() .Where(q => q.CategoryId == subject.Id) .OrderBy(r => Guid.NewGuid()) .Take(model.NumberOfQuestions).ToList(); questionList.AddRange(questions); } var test = _testDal.AddTestWithQuestions(model.TestName, model.TestLevelId, new TimeSpan(model.Hours, model.Minutes, model.Seconds), questionList); return(Request.CreateResponse(HttpStatusCode.Created, new { Message = "Test has been created" })); }