Esempio n. 1
0
        public async Task <string> AddAccessoriesAsync(AddAccessorieInputModel model, string userId)
        {
            if (model.Name == null ||
                model.ImagePath == null)
            {
                throw new ArgumentNullException(ExceptionsServices.Null_PropertiesErrorMessage);
            }

            await this.productService.AddProductAsync(model.Name, model.Group, userId);

            var productId = this.productService.GetIdByNameAndGroup(model.Name, model.Group);
            var groupId   = this.groupService.FindByGroupName(model.Group).Id;

            var photoUrl = await this.cloudinaryService.UploadPhotoAsync(
                model.ImagePath,
                $"{model.Group}_{model.Name}_{model.Id}",
                GlobalConstants.CloudFolderForAccessoriesPhotos);

            var accessories = new Accessorie
            {
                Id          = Guid.NewGuid().ToString(),
                Description = model.Description,
                ImagePath   = photoUrl,
                ProductId   = productId,
                GroupId     = groupId,
                CreatedOn   = DateTime.UtcNow,
                ModifiedOn  = DateTime.UtcNow,
            };

            await this.accessoriesRepository.AddAsync(accessories);

            await this.accessoriesRepository.SaveChangesAsync();

            return(accessories.Id);
        }
        public async Task AddFinishedModel_WithCorrectData_ShouldSuccessfullyAdd()
        {
            // Arrange
            var context = ApplicationDbContextInMemoryFactory.InitializeContext();

            var groupRepository      = new EfDeletableEntityRepository <Product_Group>(context);
            var productRepository    = new EfDeletableEntityRepository <Product>(context);
            var accessorieRepository = new EfDeletableEntityRepository <Accessorie>(context);

            var groupService      = new GroupService(groupRepository);
            var prodcutService    = new ProductService(productRepository, groupService);
            var cloudinaryService = new FakeCloudinary();
            var accessorieService = new AccessoriesService(accessorieRepository, prodcutService, groupService, cloudinaryService);

            var seeder = new DbContextTestsSeeder();
            await seeder.SeedUsersAsync(context);

            await seeder.SeedGroupAsync(context);

            var user = new ApplicationUser
            {
                Id             = "abc",
                FirstName      = "Nikolay",
                LastName       = "Doychev",
                Email          = "*****@*****.**",
                EmailConfirmed = true,
            };

            var       fileName = "Img";
            IFormFile file     = new FormFile(
                new MemoryStream(Encoding.UTF8.GetBytes("This is a dummy file")),
                0,
                0,
                fileName,
                "dummy.png");

            var accessorie = new AddAccessorieInputModel
            {
                Id          = "abc1",
                Description = "Some description test 1",
                ImagePath   = file,
                Name        = "Аксесоар 1",
            };

            // Act
            AutoMapperConfig.RegisterMappings(typeof(AddAccessorieInputModel).Assembly);
            var result = await accessorieService.AddAccessoriesAsync(accessorie, user.Id.ToString());

            var actual = context.Accessories.FirstOrDefault(x => x.Product.Name == "Аксесоар 1");

            var expectedName        = "Аксесоар 1";
            var expectedDescription = "Some description test 1";

            // Assert
            AssertExtension.EqualsWithMessage(expectedName, actual.Product.Name, string.Format(ErrorMessage, "AddAccessorie returns correct Name"));
            AssertExtension.EqualsWithMessage(expectedDescription, actual.Description, string.Format(ErrorMessage, "AddAccessorie returns correct Description"));
        }
        public async Task <IActionResult> Add(AddAccessorieInputModel inputModel)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(inputModel));
            }

            var user = await this.userManager.GetUserAsync(this.User);

            await this.accessoriesService.AddAccessoriesAsync(inputModel, user.Id.ToString());

            this.TempData["SuccessfullyCreateAccessorie"] = $"Успешно създаден аксесоар {inputModel.Name}";

            return(this.RedirectToAction("All", "Accessorie", new { area = string.Empty }));
        }
        public async Task AddProject_WithNullValue_ShouldThrowException()
        {
            // Arrange
            var context = ApplicationDbContextInMemoryFactory.InitializeContext();

            var groupRepository      = new EfDeletableEntityRepository <Product_Group>(context);
            var productRepository    = new EfDeletableEntityRepository <Product>(context);
            var accessorieRepository = new EfDeletableEntityRepository <Accessorie>(context);

            var groupService      = new GroupService(groupRepository);
            var prodcutService    = new ProductService(productRepository, groupService);
            var cloudinaryService = new FakeCloudinary();
            var accessorieService = new AccessoriesService(accessorieRepository, prodcutService, groupService, cloudinaryService);

            var user = new ApplicationUser
            {
                Id             = "abc",
                FirstName      = "Nikolay",
                LastName       = "Doychev",
                Email          = "*****@*****.**",
                EmailConfirmed = true,
            };

            var       fileName = "Img";
            IFormFile file     = new FormFile(
                new MemoryStream(Encoding.UTF8.GetBytes("This is a dummy file")),
                0,
                0,
                fileName,
                "dummy.png");

            var accessorie = new AddAccessorieInputModel
            {
                Id          = "abc1",
                Description = "Some description test 1",
                Name        = "Аксесоар 1",
            };

            var seeder = new DbContextTestsSeeder();
            await seeder.SeedUsersAsync(context);

            await seeder.SeedGroupAsync(context);

            // Act
            AutoMapperConfig.RegisterMappings(typeof(AddAccessorieInputModel).Assembly);
            await Assert.ThrowsAsync <ArgumentNullException>(() => accessorieService.AddAccessoriesAsync(accessorie, user.Id.ToString()));
        }
        public IActionResult Add()
        {
            var createAccessorieInputModel = new AddAccessorieInputModel();

            return(this.View(createAccessorieInputModel));
        }