public async Task <Course> AddCourseAsync(int teacherId, DateTime start, DateTime end, string courseName) { var teacher = await this.context.Users.FirstOrDefaultAsync(us => us.Id == teacherId); if (teacher.RoleId != 2) { throw new IncorrectPermissionsException("You don't have access."); } if (this.context.Courses.Any(co => co.Name == courseName)) { throw new EntityAlreadyExistsException("Course already exists"); } var course = new Course { Name = courseName, TeacherId = teacher.Id, Start = start, End = end }; context.Courses.Add(course); await context.SaveChangesAsync(); return(course); }
public async Task UpdateRoleAsync(int userId, int newRoleId) { Validations.RangeNumbers(0, int.MaxValue, userId, Validations.POSITIVE_ERROR); if (newRoleId == 1) { throw new IncorrectPermissionsException("You are not allowed to set someone's role to Adminitrator"); } var user = await GetUserByIdAsync(userId); if (user == null) { throw new ArgumentNullException("User doesn't exist"); } user.RoleId = newRoleId; var role = context.UserRoles.FirstOrDefault(r => r.UserId == userId); context.UserRoles.Remove(role); context.UserRoles.Add(new IdentityUserRole <int>() { RoleId = newRoleId, UserId = user.Id }); await context.SaveChangesAsync(); }
public async Task GetCourseByIdShouldReturnCorrectCourse() { var contextOptions = new DbContextOptionsBuilder <AcademySiteContext>() .UseInMemoryDatabase(databaseName: "GetCourseByIdShouldReturnCorrectCourse") .Options; using (var context = new AcademySiteContext(contextOptions)) { context.Courses.Add(new Course() { CourseId = 1, Name = "DummyCourse", Start = DateTime.Now, Teacher = new User { Id = 1, UserName = "******" } }); await context.SaveChangesAsync(); } using (var context = new AcademySiteContext(contextOptions)) { var courseService = new CourseService(context); var result = await courseService.GetCourseByIdAsync(1); Assert.IsTrue(result != null && result.Name == "DummyCourse"); } }