//Update Rater private void JsonUpdateDbProductRater(JsonEditViewModel jsonProduct, Product dbProduct) { double price; if (!string.IsNullOrEmpty(jsonProduct.IssueDescription)) { if (dbProduct.Description == null) { dbProduct.Description = new ProductDescription(); } dbProduct.Description.Issue = jsonProduct.IssueDescription; } if (!string.IsNullOrEmpty(jsonProduct.Price)) { if (dbProduct.Price == null) { dbProduct.Price = new Price(); } if (double.TryParse(jsonProduct.Price, NumberStyles.Float, CultureInfo.InvariantCulture.NumberFormat, out price)) { dbProduct.Price.Base = price; } } }
//Global Update Method private void JsonUpdateDbProduct(JsonEditViewModel jsonProduct, Product dbProduct) { var role = GetCurrentUserRoles(); if (role.Contains("Admin") || role.Contains("Moderator")) { JsonUpdateDbProductAdmin(jsonProduct, dbProduct); } else if (role.Contains("Rater")) { JsonUpdateDbProductRater(jsonProduct, dbProduct); } else if (role.Contains("Partner")) { JsonUpdateDbProductPartner(jsonProduct, dbProduct); } }
//Update Partner private void JsonUpdateDbProductPartner(JsonEditViewModel jsonProduct, Product dbProduct) { if (!string.IsNullOrEmpty(jsonProduct.Reserved)) { if (dbProduct.Description == null) { dbProduct.Description = new ProductDescription(); } if (jsonProduct.Reserved != "0") { dbProduct.Description.Reserved = User.Identity.Name; } else { dbProduct.Description.Reserved = null; } } }
//Update Admin private void JsonUpdateDbProductAdmin(JsonEditViewModel jsonProduct, Product dbProduct) { DateTime arrivalDate; DateTime saleDate; DateTime repairStart; DateTime repairEnd; double price; double salePrice; double purchasePrice; bool isOutOfUse = false; //Product if (DateTime.TryParse(jsonProduct.ArrivalDate, out arrivalDate)) { dbProduct.ArrivalDate = arrivalDate; } if (DateTime.TryParse(jsonProduct.SaleDate, out saleDate)) { dbProduct.SaleDate = saleDate; } if (bool.TryParse(jsonProduct.IsOutOfUse, out isOutOfUse)) { dbProduct.IsOutOfUse = isOutOfUse; } // Repair if (!string.IsNullOrEmpty(jsonProduct.RepairStartDate) || !string.IsNullOrEmpty(jsonProduct.RepairFinishDate) || jsonProduct.RepairStatus != null || jsonProduct.RepairPersonName != null) { if (dbProduct.Repair == null) { dbProduct.Repair = new Repair(); } if (DateTime.TryParse(jsonProduct.RepairStartDate, out repairStart)) { dbProduct.Repair.StartDate = repairStart; } if (DateTime.TryParse(jsonProduct.RepairFinishDate, out repairEnd)) { dbProduct.Repair.FinishDate = repairEnd; } dbProduct.Repair.Status = jsonProduct.RepairStatus; dbProduct.Repair.PersonName = jsonProduct.RepairPersonName; } // Price if (jsonProduct.Price != null || jsonProduct.SalePrice != null || jsonProduct.PurchasePrice != null) { if (dbProduct.Price == null) { dbProduct.Price = new Price(); } if (double.TryParse(jsonProduct.Price, NumberStyles.Float, CultureInfo.InvariantCulture.NumberFormat, out price)) { dbProduct.Price.Base = price; } if (double.TryParse(jsonProduct.SalePrice, NumberStyles.Float, CultureInfo.InvariantCulture.NumberFormat, out salePrice)) { dbProduct.Price.Sale = salePrice; } if (double.TryParse(jsonProduct.PurchasePrice, NumberStyles.Float, CultureInfo.InvariantCulture.NumberFormat, out purchasePrice)) { dbProduct.Price.Purchase = purchasePrice; } } // Description if (jsonProduct.Name != null || jsonProduct.Status != null || jsonProduct.IssueDescription != null || jsonProduct.Reserved != null) { if (dbProduct.Description == null) { dbProduct.Description = new ProductDescription(); } dbProduct.Description.Name = jsonProduct.Name; dbProduct.Description.StatusId = Convert.ToInt32(jsonProduct.Status); dbProduct.Description.Issue = jsonProduct.IssueDescription; if (jsonProduct.Reserved != "0") { dbProduct.Description.Reserved = User.Identity.Name; } else { dbProduct.Description.Reserved = null; } } // Category if (!string.IsNullOrEmpty(jsonProduct.SubCategoryId) || !string.IsNullOrEmpty(jsonProduct.CategoryId)) { int Id = 0; if (!string.IsNullOrEmpty(jsonProduct.CategoryId)) { Id = Convert.ToInt32(jsonProduct.CategoryId); } if (!string.IsNullOrEmpty(jsonProduct.SubCategoryId)) { Id = Convert.ToInt32(jsonProduct.SubCategoryId.Split('_')[0]); } dbProduct.Category = db.Categories.FirstOrDefault(c => c.Id == Id); } else { dbProduct.Category = null; } // Warehouse if (!string.IsNullOrEmpty(jsonProduct.WarehouseId)) { int warehouseId = Convert.ToInt32(jsonProduct.WarehouseId); dbProduct.Warehouse = db.Warehouses.FirstOrDefault(w => w.Id == warehouseId); } else { dbProduct.Warehouse = null; } }