public async Task <string> CreateAsync(ItemCreateServiceModel serviceModel) { if (!this.IsEntityStateValid(serviceModel)) { return(null); } var user = await this.Context.Users.SingleOrDefaultAsync(u => u.UserName == serviceModel.UserName); if (user == null || !await this.Context.SubCategories.AnyAsync(c => c.Id == serviceModel.SubCategoryId)) { return(null); } var item = Mapper.Map <Item>(serviceModel); item.UserId = user.Id; await this.Context.AddAsync(item); await this.pictureService.Upload(serviceModel.PictFormFiles, item.Id); await this.Context.SaveChangesAsync(); return(item.Id); }
public async Task CreateAsync_WithInvalidUsername_ShouldReturnNullId_AndNotInsertItemInDatabase() { // Arrange var item = new ItemCreateServiceModel { Id = SampleId, Title = SampleTitle, Description = SampleDescription, StartingPrice = SampleStartingPrice, MinIncrease = SampleMinIncrease, StartTime = SampleStartTime, EndTime = SampleEndTime, SubCategoryId = SampleSubCategoryId, UserName = SampleUsername, }; // Act var result = await this.itemsService.CreateAsync(item); // Assert result .Should() .BeNull(); this.dbContext .Items .Should() .BeEmpty(); }
public async Task CreateAsync_WithValidModel_ShouldReturnId_AndInsertItemInDatabase() { // Arrange await this.SeedUserAndSubCategory(); this.dbContext.SaveChanges(); var item = new ItemCreateServiceModel { Id = SampleId, Title = SampleTitle, Description = SampleDescription, StartingPrice = SampleStartingPrice, MinIncrease = SampleMinIncrease, StartTime = SampleStartTime, EndTime = SampleEndTime, SubCategoryId = SampleSubCategoryId, UserName = SampleUsername, PictFormFiles = new List <IFormFile>(), }; // Act var result = await this.itemsService.CreateAsync(item); // Assert result .Should() .NotBeNullOrEmpty() .And .Match(x => x.As <string>() == SampleId); this.dbContext .Items .Should() .HaveCount(1); }
public async Task CreateAsync_WithInvalidDescription_ShouldReturnNullId_AndNotInsertItemInDatabase() { // Arrange var random = new Random(); const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; var invalidDescription = new string(Enumerable.Repeat(chars, 501) .Select(s => s[random.Next(s.Length)]).ToArray()); var item = new ItemCreateServiceModel { Id = SampleId, Title = SampleTitle, Description = invalidDescription, StartingPrice = SampleStartingPrice, MinIncrease = SampleMinIncrease, StartTime = SampleStartTime, EndTime = SampleEndTime, SubCategoryId = SampleSubCategoryId, UserName = SampleUsername, }; // Act var result = await this.itemsService.CreateAsync(item); // Assert result .Should() .BeNull(); this.dbContext .Items .Should() .BeEmpty(); }