public async Task SetUp() { _fixture = new Fixture(); _products = _fixture.CreateMany <Product>(10).ToList(); var options = new DbContextOptionsBuilder <CatalogContext>() .UseInMemoryDatabase(databaseName: $"Catalog_{Guid.NewGuid()}") .Options; _dbContext = new CatalogContext(options); await _dbContext.Products.AddRangeAsync(_products); await _dbContext.SaveChangesAsync(); _model = _fixture.Create <UpdateProductInputModel>(); _validator = new UpdateProductModelValidator(_dbContext); }
public async Task <IActionResult> Save([FromBody] ProductRequestModel request) { var sellerClaim = this.User.Claims.FirstOrDefault(x => x.Type == AccountConstants.Claims.OrganisationIdClaim); var serviceModel = new CreateUpdateProductModel { Id = request.Id, PrimaryProductId = request.PrimaryProductId, IsNew = request.IsNew, IsPublished = request.IsPublished, CategoryId = request.CategoryId, IsProtected = request.IsProtected, Videos = request.Videos, Files = request.Files, Images = request.Images, Sku = request.Sku, Name = request.Name, Description = request.Description, FormData = request.FormData, Username = this.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Email)?.Value, OrganisationId = GuidHelper.ParseNullable(sellerClaim?.Value), Language = CultureInfo.CurrentCulture.Name }; if (request.Id.HasValue) { var validator = new UpdateProductModelValidator(); var validationResult = await validator.ValidateAsync(serviceModel); if (validationResult.IsValid) { var productId = await this.productService.UpdateAsync(serviceModel); if (productId != null) { return(this.StatusCode((int)HttpStatusCode.OK, new { Id = productId })); } } throw new CustomException(string.Join(ErrorConstants.ErrorMessagesSeparator, validationResult.Errors.Select(x => x.ErrorMessage)), (int)HttpStatusCode.UnprocessableEntity); } else { var validator = new CreateProductModelValidator(); var validationResult = await validator.ValidateAsync(serviceModel); if (validationResult.IsValid) { var productId = await this.productService.CreateAsync(serviceModel); if (productId != null) { return(this.StatusCode((int)HttpStatusCode.Created, new { Id = productId })); } } throw new CustomException(string.Join(ErrorConstants.ErrorMessagesSeparator, validationResult.Errors.Select(x => x.ErrorMessage)), (int)HttpStatusCode.UnprocessableEntity); } }