I have a code block that imports CSV to list and map to a database. And there are few validations and I've to return error messages based on those validations. Right now all those logic is handled by multiple if else conditions. The code is working but I don't think it's the right approach. Is there any way I can replace these conditions with something clean?
if (fileCSV == null && fileCSV.ContentLength == 0)
{
importModel.Error = "Error1";
return importModel;
}
else
{
List<ImportModel> mappings = _importService.ImportCSVToList<ImportModel>(fileCSV);
if (mappings.Count > 0)
{
IEnumerable<ImportModel> duplicates = mappings.GroupBy(x => x.ProductSku).SelectMany(g => g.Skip(1)).ToList();
if (duplicates.Any())
{
importModel.Error = "Error2";
return importModel;
}
else
{
var products = _productService.GetProducts(productSkuList).ToList();
if (!importModel.InvalidSkuList.Any())
{
bool isImported = _productService.Import(mappings);
if (!isImported)
{
importModel.Error = "Error3";
}
}
else
{
return importModel;
}
}
}
else
{
importModel.Error = "Error4";
return importModel;
}
}