Example #1
0
        public async Task EditSubject_WithExistentIdAndCorrectData_ShouldSuccessfullyEditSubject()
        {
            string errorMessagePrefix = "SubjectService EditSubjectAsync() does not work properly.";

            var context = NetBookDbContextInMemoryFactory.InitializeContext();

            await this.SeedData(context);

            this.subjectService = new SubjectService(context);

            var testId = context.Subjects.First().Id;

            var testSubject = new SubjectServiceModel
            {
                Id   = testId,
                Name = "EditSubject",
            };

            var actualResult = await this.subjectService.EditSubjectAsync(testSubject);

            var editedSubject = context.Subjects.First();

            Assert.True(actualResult, errorMessagePrefix);

            Assert.True(editedSubject.Name == testSubject.Name, errorMessagePrefix + " " + "Subject Name is not set properly.");
        }
        public async Task <bool> EditSubjectAsync(SubjectServiceModel model)
        {
            Subject subjectFromDb = await this.context.Subjects.SingleOrDefaultAsync(s => s.Id == model.Id);

            if (subjectFromDb == null)
            {
                throw new ArgumentNullException(nameof(subjectFromDb));
            }

            if (model.Name != subjectFromDb.Name)
            {
                var sameSubjectName = await this.context.Subjects.SingleOrDefaultAsync(x => x.Name == model.Name);

                if (sameSubjectName != null)
                {
                    return(false);
                }

                subjectFromDb.Name = model.Name;

                subjectFromDb.ModifiedOn = DateTime.UtcNow;
            }

            this.context.Subjects.Update(subjectFromDb);

            int result = await this.context.SaveChangesAsync();

            return(result > 0);
        }
        public async ASYNC.Task <SubjectServiceModel> Delete(int id)
        {
            var subject = await this.GetById(id);

            var subjectToDelete = new SubjectServiceModel()
            {
                Id        = subject.Id,
                Name      = subject.Name,
                TeacherId = subject.TeacherId,
            };

            return(subjectToDelete);
        }
Example #4
0
        public async Task CreateSubject_WithCorrectData_ShouldSuccessfullyCreateSubject()
        {
            string errorMessagePrefix = "SubjectService CreateSubjectAsync() does not work properly.";

            var context = NetBookDbContextInMemoryFactory.InitializeContext();

            await this.SeedData(context);

            this.subjectService = new SubjectService(context);

            var testSubject = new SubjectServiceModel();

            var actualResult = await this.subjectService.CreateSubjectAsync(testSubject);

            Assert.True(actualResult, errorMessagePrefix);
        }
Example #5
0
        public async Task <IActionResult> Create(SubjectCreateInputModel model)
        {
            if (this.ModelState.IsValid)
            {
                SubjectServiceModel serviceModel = model.To <SubjectServiceModel>();

                bool result = await this.subjectService.CreateSubjectAsync(serviceModel);

                if (result)
                {
                    return(this.RedirectToAction("All"));
                }
            }

            return(this.View(model));
        }
        public async Task <bool> CreateSubjectAsync(SubjectServiceModel model)
        {
            Subject subjectToAdd = AutoMapper.Mapper.Map <Subject>(model);

            var subjectWithSameName = await this.context.Subjects.SingleOrDefaultAsync(s => s.Name == subjectToAdd.Name);

            if (subjectWithSameName != null)
            {
                return(false);
            }

            await this.context.Subjects.AddAsync(subjectToAdd);

            int result = await this.context.SaveChangesAsync();

            return(result > 0);
        }
Example #7
0
        public async Task EditSubject_WithNonExistentId_ShouldThrowArgumentNullException()
        {
            string errorMessagePrefix = "SubjectService EditSubjectAsync() does not work properly.";

            var context = NetBookDbContextInMemoryFactory.InitializeContext();

            await this.SeedData(context);

            this.subjectService = new SubjectService(context);

            var testId = "Non_Existent";

            var testSubject = new SubjectServiceModel {
                Id = testId
            };

            await Assert.ThrowsAsync <ArgumentNullException>(async() => await this.subjectService.EditSubjectAsync(testSubject));
        }
Example #8
0
        public async Task CreateSubject_WithSameSubjectName_ShouldReturnFalse()
        {
            string errorMessagePrefix = "SubjectService CreateSubjectAsync() does not work properly.";

            var context = NetBookDbContextInMemoryFactory.InitializeContext();

            await this.SeedData(context);

            this.subjectService = new SubjectService(context);

            var testSubject = new SubjectServiceModel {
                Name = "TestSubject"
            };

            var actualResult = await this.subjectService.CreateSubjectAsync(testSubject);

            Assert.True(!actualResult, errorMessagePrefix + " " + "Subject with same name can not be created.");
        }
Example #9
0
        public async Task EditSubject_WithExistentIdAndSameSubjectName_ShouldReturnFalse()
        {
            string errorMessagePrefix = "SubjectService EditSubjectAsync() does not work properly.";

            var context = NetBookDbContextInMemoryFactory.InitializeContext();

            await this.SeedData(context);

            this.subjectService = new SubjectService(context);

            var testId = context.Subjects.First().Id;

            var testSubject = new SubjectServiceModel
            {
                Id   = testId,
                Name = "TestSubject2",
            };

            var actualResult = await this.subjectService.EditSubjectAsync(testSubject);

            Assert.True(!actualResult, errorMessagePrefix);
        }