This is a method which takes an input of a "page" object, which includes SortOrder. It then fetches the existing page items from a database and attempts to re-index.
I don't think this is at all efficient and reads horribly and wondered if anybody could make some recommendations?
public Boolean ReIndexOutputTemplatePages(OutputTemplatePage page)
{
try
{
int? requiredIndex = page.SortOrder;
var items = from t1 in db.OutputTemplatePages
where (t1.OutputTemplateId == page.OutputTemplateId)
orderby t1.SortOrder
select t1;
Boolean boolHasBeenAdded = false;
int ctr = 0;
if (items.Count() == 0)
{
Add(page);
boolHasBeenAdded = true;
}
else
{
foreach (OutputTemplatePage template in items)
{
if (requiredIndex == ctr)
{
Add(page);
ctr++;
boolHasBeenAdded = true;
}
OutputTemplateRepository repo = new OutputTemplateRepository();
OutputTemplatePage pg = repo.GetOutputTemplatePageById(template.Id);
pg.SortOrder = ctr;
repo.Save();
ctr++;
}
}
if (!boolHasBeenAdded)
{
Add(page);
}
return true;
}
catch (Exception)
{
return false;
}
}