public async Task PutAsync(int id, TestTaskDto model) { var testTask = await _context.TestTasks.FindAsync(id); testTask.Name = model.Name; testTask.Description = model.Description; testTask.Module = await _context.Modules.FindAsync(model.moduleId); testTask.Questions = model.Questions .Select(x => new TestQuestion { Text = x.Text, Points = x.Points, Answers = x.Answers .Select(y => new TestQuestionAnswer { Text = y.Text, IsRight = y.IsRight }) .ToList() }) .ToList(); testTask.Points = testTask.Questions.Select(x => x.Points).Aggregate((x, y) => x + y); _context.Entry(testTask).State = EntityState.Modified; await _context.SaveChangesAsync(); }
public async Task <TestTask> CreateAsync(TestTaskDto model) { var testTask = new TestTask { Name = model.Name, Description = model.Description, Module = await _context.Modules.FindAsync(model.moduleId), Questions = model.Questions .Select(x => new TestQuestion { Text = x.Text, Points = x.Points, Answers = x.Answers .Select(y => new TestQuestionAnswer { Text = y.Text, IsRight = y.IsRight }) .ToList() }) .ToList() }; testTask.Points = testTask.Questions.Select(x => x.Points).Aggregate((x, y) => x + y); _context.TestTasks.Add(testTask); await _context.SaveChangesAsync(); return(testTask); }
public async Task <IActionResult> PutTestTask(int id, TestTaskDto testTask) { try { await _testTasksService.PutAsync(id, testTask); } catch (DbUpdateConcurrencyException) { if (!_testTasksService.TestTaskExists(id)) { return(NotFound()); } throw; } return(NoContent()); }
public async Task <ActionResult <UlearnData.Models.Tasks.TestTasks.TestTask> > PostTestTask(TestTaskDto testTask) { var newTestTask = await _testTasksService.CreateAsync(testTask); return(CreatedAtAction("GetTestTask", new { id = newTestTask.Id }, newTestTask)); }