public async Task <bool> Handle(UpdateClassroomCommand request, CancellationToken cancellationToken) { if (request.Id <= 0) { throw new Exception("Invalid Id to update."); } Classroom classroom = await _context.Classrooms.FindAsync(request.Id); if (request.StudentIds.Any()) { classroom.StudentClassrooms = new List <StudentClassroom>(); foreach (int studentId in request.StudentIds) { classroom.StudentClassrooms.Add(new StudentClassroom { StudentId = studentId }); } _context.Classrooms.Update(classroom); return(await _context.Commit() > 0); } return(false); }
public async Task <ClassroomDto> Handle(CreateClassroomCommand request, CancellationToken cancellationToken) { // do some kind of checks that make sense... Classroom classroom = _mapper.Map <CreateClassroomCommand, Classroom>(request); if (request.StudentIds.Any()) { classroom.StudentClassrooms = new List <StudentClassroom>(); foreach (int studentId in request.StudentIds) { classroom.StudentClassrooms.Add(new StudentClassroom { StudentId = studentId }); } } await _context.Classrooms.AddAsync(classroom); int success = await _context.Commit(); if (success <= 0) { throw new Exception("Cannot save database changes."); } return(_mapper.Map <Classroom, ClassroomDto>(classroom)); }
public async Task <bool> Handle(DeleteTeacherCommand request, CancellationToken cancellationToken) { if (request.Id <= 0) { throw new Exception("Invalid Id."); } var entity = await _context.Teachers.FindAsync(request.Id); if (entity == null) { return(false); } _context.Teachers.Remove(entity); int success = await _context.Commit(); return(success > 0); }
public async Task <TeacherDto> Handle(CreateTeacherCommand request, CancellationToken cancellationToken) { // do some kind of checks that make sense... Teacher teacher = _mapper.Map <CreateTeacherCommand, Teacher>(request); await _context.Teachers.AddAsync(teacher); int success = await _context.Commit(); if (success <= 0) { throw new Exception("Cannot save database changes."); } return(_mapper.Map <Teacher, TeacherDto>(teacher)); }