public async Task <IActionResult> Delete(Guid?id) { var sellerClaim = this.User.Claims.FirstOrDefault(x => x.Type == AccountConstants.Claims.OrganisationIdClaim); var serviceModel = new DeleteProductServiceModel { Id = id, Language = CultureInfo.CurrentCulture.Name, Username = this.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Email)?.Value, OrganisationId = GuidHelper.ParseNullable(sellerClaim?.Value) }; var validator = new DeleteProductModelValidator(); var validationResult = await validator.ValidateAsync(serviceModel); if (validationResult.IsValid) { await this.productService.DeleteAsync(serviceModel); return(this.StatusCode((int)HttpStatusCode.OK)); } throw new CustomException(string.Join(ErrorConstants.ErrorMessagesSeparator, validationResult.Errors.Select(x => x.ErrorMessage)), (int)HttpStatusCode.UnprocessableEntity); }
public async Task DeleteAsync(DeleteProductServiceModel model) { var product = await this.catalogContext.Products.FirstOrDefaultAsync(x => x.Id == model.Id && x.Brand.SellerId == model.OrganisationId && x.IsActive); if (product == null) { throw new CustomException(this.productLocalizer.GetString("ProductNotFound"), (int)HttpStatusCode.NotFound); } if (await this.catalogContext.Products.AnyAsync(x => x.PrimaryProductId == model.Id && x.Brand.SellerId == model.OrganisationId && x.IsActive)) { throw new CustomException(this.productLocalizer.GetString("ProductVariantsDeleteProductConflict"), (int)HttpStatusCode.Conflict); } product.IsActive = false; await this.catalogContext.SaveChangesAsync(); await this.productIndexingRepository.IndexAsync(product.Id); }