// Business function which processes the input provided by the user and saves the data after validation public bool SaveSellerCatalogue(List <CsvInput> input) { bool result = false; if (input != null && input.Any()) { int sellerId = input.FirstOrDefault().SellerId; //Gets seller id from input csv file int versionId = _catalogueRepo.GetSellersLastActiveVersion(sellerId); // Gets current active version for seller if (versionId == -1 && !_catalogueRepo.SaveNewSeller(sellerId)) // Saves seller if version is invalid and checks if seller was saved properly if not return false and ends flow { return(result); } input = RemoveDuplicates(input); // Private function called which takes decision on input and removes duplicates based on business function if (versionId > 0) // Gets remaiing products only when the seller has an active catalogue { string remainingProdIds = string.Join(',', input.Select(x => x.SellerProdId)); List <CsvInput> remainingProds = _catalogueRepo.GetRemainingProducts(remainingProdIds, sellerId, versionId); input.AddRange(remainingProds); } int newversionId = versionId == -1 ? 1 : versionId + 1; bool entriesSaved = _catalogueRepo.SaveNewCatalogueData(input, newversionId); // Saves the catalogue if (entriesSaved) // On successfully saving the catalogue, the active version for the seller is updated { result = _catalogueRepo.UpdateActiveVersion(sellerId, newversionId); } } return(result); }