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 finishedModelsRepository = new EfDeletableEntityRepository <Finished_Model>(context); var groupService = new GroupService(groupRepository); var prodcutService = new ProductService(productRepository, groupService); var cloudinaryService = new FakeCloudinary(); var finishedModelsService = new FinishedModelService(finishedModelsRepository, 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 finishedModel = new AddFinishedModelInputModel { Id = "abc1", Description = "Some description test 1", ImagePath = file, TypeProject = TypeProject.Classic.ToString(), Name = "Model 1", }; // Act AutoMapperConfig.RegisterMappings(typeof(AddFinishedModelInputModel).Assembly); var result = await finishedModelsService.AddFinishedModelAsync(finishedModel, user.Id.ToString()); var actual = context.Finished_Models.FirstOrDefault(x => x.Product.Name == "Model 1"); var expectedName = "Model 1"; var expectedDescription = "Some description test 1"; var expectedTypeOfProject = TypeProject.Classic.ToString(); // Assert AssertExtension.EqualsWithMessage(expectedName, actual.Product.Name, string.Format(ErrorMessage, "AddFinishedModel returns correct Name")); AssertExtension.EqualsWithMessage(expectedDescription, actual.Description, string.Format(ErrorMessage, "AddFinishedModel returns correct Power")); AssertExtension.EqualsWithMessage(expectedTypeOfProject, actual.TypeProject.ToString(), string.Format(ErrorMessage, "AddFinishedModel returns correct TypeOfProject")); }
public async Task AddFinishedModel_WithNullValue_ShouldThrowException() { // Arrange var context = ApplicationDbContextInMemoryFactory.InitializeContext(); var groupRepository = new EfDeletableEntityRepository <Product_Group>(context); var productRepository = new EfDeletableEntityRepository <Product>(context); var finishedModelsRepository = new EfDeletableEntityRepository <Finished_Model>(context); var groupService = new GroupService(groupRepository); var prodcutService = new ProductService(productRepository, groupService); var cloudinaryService = new FakeCloudinary(); var finishedModelsService = new FinishedModelService(finishedModelsRepository, 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 finishedModel = new AddFinishedModelInputModel { Id = "abc1", Description = "Some description test 1", TypeProject = TypeProject.Classic.ToString(), Name = "Model 1", }; var seeder = new DbContextTestsSeeder(); await seeder.SeedUsersAsync(context); await seeder.SeedGroupAsync(context); await seeder.SeedProdcutAsync(context); // Act AutoMapperConfig.RegisterMappings(typeof(AddFinishedModelInputModel).Assembly); await Assert.ThrowsAsync <ArgumentNullException>(() => finishedModelsService.AddFinishedModelAsync(finishedModel, user.Id.ToString())); }
public async Task <IActionResult> Add(AddFinishedModelInputModel inputModel) { if (!this.ModelState.IsValid) { return(this.View(inputModel)); } var user = await this.userManager.GetUserAsync(this.User); await this.finishedModelService.AddFinishedModelAsync(inputModel, user.Id.ToString()); var typeOfProject = Enum.Parse <TypeProject>(inputModel.TypeProject); this.TempData["SuccessfullyCreateFinishedModel"] = $"Успешно създаден модел с име: {inputModel.Name}"; return(this.RedirectToAction("All", "FinishedModels", new { type = typeOfProject, area = string.Empty })); }
public async Task <string> AddFinishedModelAsync(AddFinishedModelInputModel model, string userId) { var typeOfProject = Enum.Parse <TypeProject>(model.TypeProject); if (model.Name == null || model.ImagePath == null) { throw new ArgumentNullException(NullFinishedModelPropertiesErrorMessage); } 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.CloudFolderForFinishedModelPhotos); var finishedModel = new KaminiCenter.Data.Models.Finished_Model { Id = Guid.NewGuid().ToString(), CreatedOn = DateTime.UtcNow, Description = model.Description, IsDeleted = false, ImagePath = photoUrl, GroupId = groupId, ProductId = productId, TypeProject = typeOfProject, }; await this.finishedModelRepository.AddAsync(finishedModel); await this.finishedModelRepository.SaveChangesAsync(); return(finishedModel.Id); }
public IActionResult Add() { var createFinishedModelInputModel = new AddFinishedModelInputModel(); return(this.View(createFinishedModelInputModel)); }