public async Task <ICommandResult <int> > HandleAsync(CourseCreateCommand command) { if (_context.Course.Any(x => String.Equals(x.Name.ToLower(), command.Name.ToLower()))) { throw new Exception("This name is already used!"); } var newCourse = new Course() { Name = command.Name, MadeById = command.CreatedById, Password = command.Password, SpecializationId = command.SpecializationId }; _context.Course.Add(newCourse); await _context.SaveChangesAsync(); var privileges = new List <UserCoursePrivilege>() { new UserCoursePrivilege() { CourseId = newCourse.Id, PrivilegeId = (int)PrivilegeEnum.CanManageCourse, UserId = command.CreatedById }, new UserCoursePrivilege() { CourseId = newCourse.Id, PrivilegeId = (int)PrivilegeEnum.IsInvolvedWithCourse, UserId = command.CreatedById } }; _context.Subscription.Add(new Subscription() { CourseId = newCourse.Id, JoinedAt = DateTime.Now, UserId = command.CreatedById, Blacklisted = false }); _context.UserCoursePrivilege.AddRange(privileges); await _context.SaveChangesAsync(); return(CommandResult <int> .Success(newCourse.Id)); }
public async Task <IActionResult> CreateNewCourse(CourseCreateCommand command) { var newCourseId = await _commandBus.ExecuteAsync <int>(command); return(Ok(newCourseId)); }