public async Task <IActionResult> GetById(Guid?id) { var sellerClaim = this.User.Claims.FirstOrDefault(x => x.Type == AccountConstants.Claims.OrganisationIdClaim); var serviceModel = new GetProductAttributeByIdServiceModel { Id = id.Value, Username = this.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Email)?.Value, OrganisationId = GuidHelper.ParseNullable(sellerClaim?.Value), Language = CultureInfo.CurrentCulture.Name }; var validator = new GetProductAttributeByIdModelValidator(); var validationResult = await validator.ValidateAsync(serviceModel); if (validationResult.IsValid) { var productAttribute = await this.productAttributesService.GetProductAttributeByIdAsync(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)); } else { return(this.StatusCode((int)HttpStatusCode.NotFound)); } } return(this.StatusCode((int)HttpStatusCode.UnprocessableEntity)); }
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); } }