public async Task <ProductAttributeServiceModel> CreateProductAttributeAsync(CreateUpdateProductAttributeServiceModel model) { var productAttribute = new ProductAttribute { Order = model.Order, SellerId = model.OrganisationId.Value }; this.context.ProductAttributes.Add(productAttribute.FillCommonProperties()); var productAttributeTranslation = new ProductAttributeTranslation { ProductAttributeId = productAttribute.Id, Name = model.Name, Language = model.Language }; this.context.ProductAttributeTranslations.Add(productAttributeTranslation.FillCommonProperties()); await this.context.SaveChangesAsync(); await this.RebuildCategorySchemasAsync( model.OrganisationId.Value, model.Language, model.Username); return(await this.GetProductAttributeByIdAsync(new GetProductAttributeByIdServiceModel { Id = productAttribute.Id, Language = model.Language, OrganisationId = model.OrganisationId, Username = model.Username })); }
public async Task <IActionResult> Save([FromBody] ProductAttributeRequestModel request) { var sellerClaim = this.User.Claims.FirstOrDefault(x => x.Type == AccountConstants.Claims.OrganisationIdClaim); var serviceModel = new CreateUpdateProductAttributeServiceModel { Id = request.Id, Name = request.Name, 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 UpdateProductAttributeModelValidator(); var validationResult = await validator.ValidateAsync(serviceModel); if (validationResult.IsValid) { var productAttribute = await this.productAttributesService.UpdateProductAttributeAsync(serviceModel); if (productAttribute != null) { var response = new ProductAttributeResponseModel { Id = productAttribute.Id, Name = productAttribute.Name, Order = productAttribute.Order, Items = productAttribute.ProductAttributeItems.OrEmptyIfNull().Select(x => new ProductAttributeItemResponseModel { Id = x.Id, Name = x.Name, Order = x.Order, LastModifiedDate = x.LastModifiedDate, CreatedDate = x.CreatedDate }), LastModifiedDate = productAttribute.LastModifiedDate, CreatedDate = productAttribute.CreatedDate }; return(this.StatusCode((int)HttpStatusCode.OK, response)); } } throw new CustomException(string.Join(ErrorConstants.ErrorMessagesSeparator, validationResult.Errors.Select(x => x.ErrorMessage)), (int)HttpStatusCode.UnprocessableEntity); } else { var validator = new CreateProductAttributeModelValidator(); var validationResult = await validator.ValidateAsync(serviceModel); if (validationResult.IsValid) { var productAttribute = await this.productAttributesService.CreateProductAttributeAsync(serviceModel); if (productAttribute != null) { var response = new ProductAttributeResponseModel { Id = productAttribute.Id, Name = productAttribute.Name, Order = productAttribute.Order, Items = productAttribute.ProductAttributeItems.OrEmptyIfNull().Select(x => new ProductAttributeItemResponseModel { Id = x.Id, Name = x.Name, Order = x.Order, LastModifiedDate = x.LastModifiedDate, CreatedDate = x.CreatedDate }), LastModifiedDate = productAttribute.LastModifiedDate, CreatedDate = productAttribute.CreatedDate }; return(this.StatusCode((int)HttpStatusCode.Created, response)); } } throw new CustomException(string.Join(ErrorConstants.ErrorMessagesSeparator, validationResult.Errors.Select(x => x.ErrorMessage)), (int)HttpStatusCode.UnprocessableEntity); } }