public async Task CreateSubject_WithCorrectData_ShouldSuccessfullyCreateSubject() { string errorMessagePrefix = "ClassService CreateSubjectAsync() does not work properly."; var context = NetBookDbContextInMemoryFactory.InitializeContext(); await this.SeedData(context); this.classService = new ClassService(context); var classId = "NoSubjectClasses"; var testSubject = new ClassSubjectServiceModel { Id = "TestCreateSubject", Subject = new SubjectServiceModel { Name = "TestSubject" }, Workload = 175, ClassId = classId, }; var actualResult = await this.classService.CreateSubjectAsync(testSubject); Assert.True(actualResult, errorMessagePrefix); var subject = context.SubjectClasses.Find("TestCreateSubject"); Assert.True(subject.Subject.Name == testSubject.Subject.Name, errorMessagePrefix + " " + "Subject Name is not set properly."); Assert.True(subject.Workload == testSubject.Workload, errorMessagePrefix + " " + "Workload is not set properly."); Assert.True(subject.ClassId == testSubject.ClassId, errorMessagePrefix + " " + "ClassId is not set properly"); }
public async Task CreateSubject_WithIncorrectData_ShouldThrowArgumentNullException() { string errorMessagePrefix = "ClassService CreateSubjectAsync() does not work properly."; var context = NetBookDbContextInMemoryFactory.InitializeContext(); await this.SeedData(context); this.classService = new ClassService(context); var testSubject = new ClassSubjectServiceModel(); await Assert.ThrowsAsync <ArgumentNullException>(async() => await this.classService.CreateSubjectAsync(testSubject)); }
public async Task <bool> CreateSubjectAsync(ClassSubjectServiceModel model) { var classEntity = await this.context.Classes.Include(c => c.Subjects).SingleOrDefaultAsync(x => x.Id == model.ClassId); if (classEntity == null) { throw new ArgumentNullException(nameof(classEntity)); } var subject = AutoMapper.Mapper.Map <SubjectClass>(model); classEntity.Subjects.Add(subject); this.context.Classes.Update(classEntity); await this.context.SubjectClasses.AddAsync(subject); int result = await this.context.SaveChangesAsync(); return(result > 0); }
public async Task <bool> EditSubjectAsync(ClassSubjectServiceModel model) { var subject = await this.context.SubjectClasses.Where(s => !s.IsDeleted) .SingleOrDefaultAsync(x => x.Id == model.Id); if (subject == null) { throw new ArgumentNullException(nameof(subject)); } if (subject.Workload != model.Workload) { subject.Workload = model.Workload; subject.ModifiedOn = DateTime.UtcNow; } this.context.SubjectClasses.Update(subject); int result = await this.context.SaveChangesAsync(); return(result > 0); }