public async Task EditAsync(EditAsphaltMixtureServiceModel editAsphaltMixtureServiceModel)
        {
            var asphaltMixture = await this.context
                                 .AsphaltMixtures
                                 .FindAsync(editAsphaltMixtureServiceModel.Id);

            if (asphaltMixture == null)
            {
                throw new ArgumentNullException(string.Format(InvalidAsphaltMixtureIdErrorMessage, editAsphaltMixtureServiceModel.Id));
            }

            if (string.IsNullOrWhiteSpace(editAsphaltMixtureServiceModel.Type))
            {
                throw new ArgumentNullException(EmptyAsphaltMixtureErrorMessage);
            }

            if (await this.context.AsphaltMixtures.AnyAsync(am => am.Type == editAsphaltMixtureServiceModel.Type))
            {
                throw new InvalidOperationException(AsphaltMixtureExistErrorMessage);
            }

            if (editAsphaltMixtureServiceModel.Type.Length > AttributesConstraints.AsphaltMixtureTypeMaxLength)
            {
                throw new InvalidOperationException(string.Format(AsphaltMixtureTypeMaxLengthErrorMessage, AttributesConstraints.AsphaltMixtureTypeMaxLength));
            }

            asphaltMixture.Type = editAsphaltMixtureServiceModel.Type;

            await this.context.SaveChangesAsync();
        }
Example #2
0
        public async Task EditAsync_ShouldSuccessfullyEdit()
        {
            MapperInitializer.InitializeMapper();
            var context = ApplicationDbContextInMemoryFactory.InitializeContext();

            await this.SeedDataAsync(context);

            var asphaltMixtureService          = new AsphaltMixtureService(context);
            var editAsphaltMixtureServiceModel = new EditAsphaltMixtureServiceModel();
            var asphaltMixtureId   = 1;
            var asphaltMixtureType = "AMT 3";

            editAsphaltMixtureServiceModel.Id   = asphaltMixtureId;
            editAsphaltMixtureServiceModel.Type = asphaltMixtureType;

            await asphaltMixtureService.EditAsync(editAsphaltMixtureServiceModel);

            var expectedResult = asphaltMixtureType;
            var actualResult   = asphaltMixtureService
                                 .All()
                                 .First()
                                 .Type;

            Assert.True(expectedResult == actualResult);
        }
Example #3
0
        public async Task EditAsync_WithNonExistingIdShouldThrowArgumentNullException()
        {
            MapperInitializer.InitializeMapper();
            var context = ApplicationDbContextInMemoryFactory.InitializeContext();

            await this.SeedDataAsync(context);

            var asphaltMixtureService          = new AsphaltMixtureService(context);
            var editAsphaltMixtureServiceModel = new EditAsphaltMixtureServiceModel();

            editAsphaltMixtureServiceModel.Id = 3;

            await Assert.ThrowsAsync <ArgumentNullException>(async() =>
            {
                await asphaltMixtureService.EditAsync(editAsphaltMixtureServiceModel);
            });
        }
Example #4
0
        public async Task EditAsync_WithOverMaxNameLengthShouldThrowInvalidOperationException()
        {
            MapperInitializer.InitializeMapper();
            var context = ApplicationDbContextInMemoryFactory.InitializeContext();

            await this.SeedDataAsync(context);

            var asphaltMixtureService          = new AsphaltMixtureService(context);
            var editAsphaltMixtureServiceModel = new EditAsphaltMixtureServiceModel();
            var asphaltMixtureType             = "qwertyuiop qwertyuiop qwertyuiop qwertyuiop qwertyuiop";

            editAsphaltMixtureServiceModel.Type = asphaltMixtureType;
            editAsphaltMixtureServiceModel.Id   = 1;
            var message = "Asphalt mixture's type cannot be more than 30 characters.";

            var exception = await Assert.ThrowsAsync <InvalidOperationException>(async() =>
            {
                await asphaltMixtureService.EditAsync(editAsphaltMixtureServiceModel);
            });

            Assert.Equal(message, exception.Message);
        }
Example #5
0
        public async Task EditAsync_WithExistingNameShouldThrowInvalidOperationException()
        {
            MapperInitializer.InitializeMapper();
            var context = ApplicationDbContextInMemoryFactory.InitializeContext();

            await this.SeedDataAsync(context);

            var asphaltMixtureService          = new AsphaltMixtureService(context);
            var editAsphaltMixtureServiceModel = new EditAsphaltMixtureServiceModel();
            var asphaltMixtureType             = "AMT 2";

            editAsphaltMixtureServiceModel.Type = asphaltMixtureType;
            editAsphaltMixtureServiceModel.Id   = 1;
            var message = "Asphalt mixture's type already exists.";

            var exception = await Assert.ThrowsAsync <InvalidOperationException>(async() =>
            {
                await asphaltMixtureService.EditAsync(editAsphaltMixtureServiceModel);
            });

            Assert.Equal(message, exception.Message);
        }