public async Task DeleteAsync(DeleteWarehouseServiceModel model) { var warehouse = await this.context.Warehouses.FirstOrDefaultAsync(x => x.Id == model.Id && x.SellerId == model.OrganisationId.Value && x.IsActive); if (warehouse == null) { throw new CustomException(this.warehouseLocalizer.GetString("WarehouseNotFound"), (int)HttpStatusCode.NotFound); } if (await this.context.Inventory.AnyAsync(x => x.WarehouseId == warehouse.Id && x.IsActive)) { throw new CustomException(this.warehouseLocalizer.GetString("WarehouseDeleteInventoryConflict"), (int)HttpStatusCode.Conflict); } warehouse.IsActive = false; await this.context.SaveChangesAsync(); }
public async Task <IActionResult> Delete(Guid?id) { var sellerClaim = this.User.Claims.FirstOrDefault(x => x.Type == AccountConstants.Claims.OrganisationIdClaim); var serviceModel = new DeleteWarehouseServiceModel { 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 DeleteWarehouseModelValidator(); var validationResult = await validator.ValidateAsync(serviceModel); if (validationResult.IsValid) { await this.warehouseService.DeleteAsync(serviceModel); return(this.StatusCode((int)HttpStatusCode.OK)); } throw new CustomException(string.Join(ErrorConstants.ErrorMessagesSeparator, validationResult.Errors.Select(x => x.ErrorMessage)), (int)HttpStatusCode.UnprocessableEntity); }