Code styling in ProjectionPartDriver, QueryPartDriver and ProjectionManager

This commit is contained in:
Benedek Farkas
2024-12-07 12:38:12 +01:00
parent a74b858f20
commit 2234501139
3 changed files with 82 additions and 90 deletions

View File

@@ -81,9 +81,7 @@ namespace Orchard.Projections.Drivers {
var pageSizeKey = "pageSize" + part.Record.PagerSuffix;
if (queryString.AllKeys.Contains(pageSizeKey)) {
int qsPageSize;
if (Int32.TryParse(queryString[pageSizeKey], out qsPageSize)) {
if (Int32.TryParse(queryString[pageSizeKey], out int qsPageSize)) {
if (part.Record.MaxItems == 0 || qsPageSize <= part.Record.MaxItems) {
pageSize = qsPageSize;
}
@@ -223,81 +221,80 @@ namespace Orchard.Projections.Drivers {
}
protected override DriverResult Editor(ProjectionPart part, dynamic shapeHelper) {
return ContentShape("Parts_ProjectionPart_Edit",
() => {
var model = new ProjectionPartEditViewModel();
return ContentShape("Parts_ProjectionPart_Edit", () => {
var model = new ProjectionPartEditViewModel();
// for create read the setting values
var settings = part.TypePartDefinition.Settings.GetModel<ProjectionPartSettings>();
if (part.Id == 0) {
model = new ProjectionPartEditViewModel {
DisplayPager = settings.DisplayPager,
Items = settings.Items,
Skip = settings.Skip,
PagerSuffix = settings.PagerSuffix,
MaxItems = settings.MaxItems,
QueryLayoutRecordId = settings.QueryLayoutRecordId
};
}
else {
model = new ProjectionPartEditViewModel {
DisplayPager = part.Record.DisplayPager,
Items = part.Record.Items,
ItemsPerPage = part.Record.ItemsPerPage,
Skip = part.Record.Skip,
PagerSuffix = part.Record.PagerSuffix,
MaxItems = part.Record.MaxItems,
QueryLayoutRecordId = "-1"
};
// concatenated Query and Layout ids for the view
if (part.Record.QueryPartRecord != null) {
model.QueryLayoutRecordId = part.Record.QueryPartRecord.Id + ";";
}
// for create read the setting values
var settings = part.TypePartDefinition.Settings.GetModel<ProjectionPartSettings>();
if (part.Id == 0) {
model = new ProjectionPartEditViewModel {
DisplayPager = settings.DisplayPager,
Items = settings.Items,
Skip = settings.Skip,
PagerSuffix = settings.PagerSuffix,
MaxItems = settings.MaxItems,
QueryLayoutRecordId = settings.QueryLayoutRecordId
};
}
else {
model = new ProjectionPartEditViewModel {
DisplayPager = part.Record.DisplayPager,
Items = part.Record.Items,
ItemsPerPage = part.Record.ItemsPerPage,
Skip = part.Record.Skip,
PagerSuffix = part.Record.PagerSuffix,
MaxItems = part.Record.MaxItems,
QueryLayoutRecordId = "-1"
};
// concatenated Query and Layout ids for the view
if (part.Record.QueryPartRecord != null) {
model.QueryLayoutRecordId = part.Record.QueryPartRecord.Id + ";";
}
if (part.Record.LayoutRecord != null) {
model.QueryLayoutRecordId += part.Record.LayoutRecord.Id.ToString();
}
else {
model.QueryLayoutRecordId += "-1";
}
}
if (part.Record.LayoutRecord != null) {
model.QueryLayoutRecordId += part.Record.LayoutRecord.Id.ToString();
}
else {
model.QueryLayoutRecordId += "-1";
}
}
model.PartId = part.Id;
model.PartId = part.Id;
// lock fields
model.LockEditingItems = settings.LockEditingItems;
model.LockEditingSkip = settings.LockEditingSkip;
model.LockEditingMaxItems = settings.LockEditingMaxItems;
model.LockEditingPagerSuffix = settings.LockEditingPagerSuffix;
model.LockEditingDisplayPager = settings.LockEditingDisplayPager;
// lock fields
model.LockEditingItems = settings.LockEditingItems;
model.LockEditingSkip = settings.LockEditingSkip;
model.LockEditingMaxItems = settings.LockEditingMaxItems;
model.LockEditingPagerSuffix = settings.LockEditingPagerSuffix;
model.LockEditingDisplayPager = settings.LockEditingDisplayPager;
// populating the list of queries and layouts
var layouts = _projectionManager.DescribeLayouts().SelectMany(x => x.Descriptors).ToList();
model.QueryRecordEntries = Services.ContentManager.Query<QueryPart, QueryPartRecord>().Join<TitlePartRecord>().OrderBy(x => x.Title).List()
.Select(x => new QueryRecordEntry {
Id = x.Id,
Name = x.Name,
LayoutRecordEntries = x.Layouts.Select(l => new LayoutRecordEntry {
Id = l.Id,
Description = GetLayoutDescription(layouts, l)
})
});
// populating the list of queries and layouts
var layouts = _projectionManager.DescribeLayouts().SelectMany(x => x.Descriptors).ToList();
model.QueryRecordEntries = Services.ContentManager.Query<QueryPart, QueryPartRecord>().Join<TitlePartRecord>().OrderBy(x => x.Title).List()
.Select(x => new QueryRecordEntry {
Id = x.Id,
Name = x.Name,
LayoutRecordEntries = x.Layouts.Select(l => new LayoutRecordEntry {
Id = l.Id,
Description = GetLayoutDescription(layouts, l)
})
});
// if any values, use default list of the settings
if (!string.IsNullOrWhiteSpace(settings.FilterQueryRecordId)) {
var filterQueryRecordId = settings.FilterQueryRecordId.Split('&');
model.QueryRecordIdFilterEntries = filterQueryRecordId
.Select(x => new QueryRecordFilterEntry {
Id = x.Split(';')[0],
LayoutId = x.Split(';')[1]
});
}
else {
model.QueryRecordIdFilterEntries = new List<QueryRecordFilterEntry>();
}
// if any values, use default list of the settings
if (!string.IsNullOrWhiteSpace(settings.FilterQueryRecordId)) {
var filterQueryRecordId = settings.FilterQueryRecordId.Split('&');
model.QueryRecordIdFilterEntries = filterQueryRecordId
.Select(x => new QueryRecordFilterEntry {
Id = x.Split(';')[0],
LayoutId = x.Split(';')[1]
});
}
else {
model.QueryRecordIdFilterEntries = new List<QueryRecordFilterEntry>();
}
return shapeHelper.EditorTemplate(TemplateName: TemplateName, Model: model, Prefix: Prefix);
});
return shapeHelper.EditorTemplate(TemplateName: TemplateName, Model: model, Prefix: Prefix);
});
}
private static string GetLayoutDescription(IEnumerable<LayoutDescriptor> layouts, LayoutRecord l) {
@@ -313,7 +310,7 @@ namespace Orchard.Projections.Drivers {
updater.TryUpdateModel(model, Prefix, null, null);
model.PartId = part.Id;
// check the setting, if it is unlocked, assign the setting value
if (settings.LockEditingDisplayPager) {
part.Record.DisplayPager = settings.DisplayPager;
@@ -376,12 +373,11 @@ namespace Orchard.Projections.Drivers {
protected override void ImportCompleted(ProjectionPart part, ImportContentContext context) {
// Assign the query only when everything is imported.
var query = context.Attribute(part.PartDefinition.Name, "Query");
if (query != null && context.GetItemFromSession(query).As<QueryPart>()!=null) {
if (query != null && context.GetItemFromSession(query).As<QueryPart>() != null) {
part.Record.QueryPartRecord = context.GetItemFromSession(query).As<QueryPart>().Record;
var layoutIndex = context.Attribute(part.PartDefinition.Name, "LayoutIndex");
int layoutIndexValue;
if (layoutIndex != null
&& Int32.TryParse(layoutIndex, out layoutIndexValue)
&& Int32.TryParse(layoutIndex, out int layoutIndexValue)
&& layoutIndexValue >= 0
&& part.Record.QueryPartRecord.Layouts.Count > layoutIndexValue) {
part.Record.LayoutRecord = part.Record.QueryPartRecord.Layouts[Int32.Parse(layoutIndex)];

View File

@@ -34,10 +34,10 @@ namespace Orchard.Projections.Drivers {
part.VersionScope = model.VersionScope;
}
}
return ContentShape("Parts_QueryPart_Edit",
() => {
return shapeHelper.EditorTemplate(TemplateName: "Parts/QueryPart_Edit", Model: model, Prefix: Prefix);
});
return ContentShape("Parts_QueryPart_Edit", () => {
return shapeHelper.EditorTemplate(TemplateName: "Parts/QueryPart_Edit", Model: model, Prefix: Prefix);
});
}
protected override void Exporting(QueryPart part, ExportContentContext context) {
@@ -60,12 +60,12 @@ namespace Orchard.Projections.Drivers {
}
return new XElement("Filter",
new XAttribute("Category", filter.Category ?? ""),
new XAttribute("Description", filter.Description ?? ""),
new XAttribute("Position", filter.Position),
new XAttribute("State", state ?? ""),
new XAttribute("Type", filter.Type ?? "")
);
new XAttribute("Category", filter.Category ?? ""),
new XAttribute("Description", filter.Description ?? ""),
new XAttribute("Position", filter.Position),
new XAttribute("State", state ?? ""),
new XAttribute("Type", filter.Type ?? "")
);
})
)
)

View File

@@ -108,11 +108,7 @@ namespace Orchard.Projections.Services {
}
public int GetCount(int queryId, ContentPart part) {
var queryRecord = _queryRepository.Get(queryId);
if (queryRecord == null) {
throw new ArgumentException("queryId");
}
var queryRecord = _queryRepository.Get(queryId) ?? throw new ArgumentException("queryId");
// Prepare tokens.
Dictionary<string, object> tokens = new Dictionary<string, object>();