public static void ValidateData(List <StationerySupplier> list) { StationeryRepository srepo = new StationeryRepository(); SupplierRepository repo = new SupplierRepository(); // IEnumerable<StationerySupplier> originalList = _stationeryRepo.GetStationerySupplier(); //check that all supplier id is valid if (list.Select(x => x.SupplierId).Distinct().Except(repo.GetAll().Select(x => x.SupplierId)).Any()) { throw new Exception("Supplier Code is not valid"); } //check that stationery exists as in database, and there is no missing stationery List <string> itemlist = srepo.GetAll().Select(x => x.ItemNum).ToList(); if (list.Select(x => x.ItemNum).Distinct().Except(itemlist).Any() || itemlist.Except(list.Select(x => x.ItemNum).Distinct()).Any()) { throw new Exception("Stationery in the file does not match database"); } //List<string> itemlist2 = list.Where(x => x.Rank == 1).Select(x => x.ItemNum).Distinct().ToList(); if (list.Where(x => x.Rank == 1).Select(x => x.ItemNum).Distinct().Count() != (srepo.GetAll().Select(x => x.ItemNum).Count())) { throw new Exception("Each stationery should have at least one primary supplier"); } //check that composite PK is ok (stationery-rank is distinct) if (list.Select(x => new { x.ItemNum, x.Rank }).Distinct().Count() > list.Count) { throw new Exception("Stationery with duplicated supplier/ranks detected"); } }
protected override ValidationResult IsValid(object value, ValidationContext validationContext) { if (value != null) { var valueAsString = value.ToString(); StationeryRepository sr = new StationeryRepository(); if (!sr.GetAllItemNum().ToList().Contains(valueAsString)) { var errorMessage = FormatErrorMessage(validationContext.DisplayName); return(new ValidationResult(errorMessage)); } } return(ValidationResult.Success); }
public IEnumerable <ValidationResult> Validate(ValidationContext validationContext) { StationeryRepository sr = new StationeryRepository(); var ItemCurrentQuantity = sr.GetById(ItemNum).CurrentQty; var isPositive = true; if (Sign != null) { isPositive = Convert.ToBoolean(Sign); } if (!isPositive && Quantity > ItemCurrentQuantity) { yield return(new ValidationResult("Number exceed current quantity", new List <string> { "Quantity" })); } }
public StationeryController() { //instantiating the Stationery repository this.StationeryRepository = new StationeryRepository(); }