Skip to content

Commit

Permalink
removes sections from cms and mortar route
Browse files Browse the repository at this point in the history
  • Loading branch information
jhmullen committed Jan 15, 2019
1 parent e4cfd0b commit e030c7b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 109 deletions.
85 changes: 14 additions & 71 deletions packages/cms/src/api/cmsRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,9 @@ const isEnabled = (req, res, next) => {

const profileReqTreeOnly = {
attributes: ["id", "title", "slug", "dimension", "ordering"],
include: [
{
association: "sections", attributes: ["id", "title", "slug", "ordering", "profile_id"],
include: [
{association: "topics", attributes: ["id", "title", "slug", "ordering", "section_id", "type"]}
]
}
]
include: [{
association: "topics", attributes: ["id", "title", "slug", "ordering", "profile_id", "type"]
}]
};

const storyReqTreeOnly = {
Expand All @@ -45,9 +40,7 @@ const formatterReqTreeOnly = {
const profileReqProfileOnly = {
include: [
{association: "generators", attributes: ["id", "name"]},
{association: "materializers", attributes: ["id", "name", "ordering"]},
{association: "stats", attributes: ["id", "ordering"]},
{association: "footnotes", attributes: ["id", "ordering"]}
{association: "materializers", attributes: ["id", "name", "ordering"]}
]
};

Expand All @@ -59,13 +52,6 @@ const storyReqStoryOnly = {
]
};

const sectionReqSectionOnly = {
include: [
{association: "subtitles", attributes: ["id", "ordering"]},
{association: "descriptions", attributes: ["id", "ordering"]}
]
};

const topicReqTopicOnly = {
include: [
{association: "subtitles", attributes: ["id", "ordering"]},
Expand All @@ -92,8 +78,7 @@ const storyTopicReqStoryTopicOnly = {
*/
const cmsTables = [
"author", "formatter", "generator", "materializer", "profile",
"profile_footnote", "profile_stat", "section", "section_description",
"section_subtitle", "selector", "story", "story_description", "story_footnote", "storytopic",
"selector", "story", "story_description", "story_footnote", "storytopic",
"storytopic_description", "storytopic_stat", "storytopic_subtitle", "storytopic_visualization",
"topic", "topic_description", "topic_stat", "topic_subtitle", "topic_visualization"
];
Expand Down Expand Up @@ -124,10 +109,7 @@ const sortProfileTree = (db, profiles) => {
profiles = profiles.map(p => p.toJSON());
profiles = flatSort(db.profile, profiles);
profiles.forEach(p => {
p.sections = flatSort(db.section, p.sections);
p.sections.forEach(s => {
s.topics = flatSort(db.topic, s.topics);
});
p.topics = flatSort(db.topic, p.topics);
});
return profiles;
};
Expand All @@ -144,8 +126,6 @@ const sortStoryTree = (db, stories) => {
const sortProfile = (db, profile) => {
profile = profile.toJSON();
profile.materializers = flatSort(db.materializer, profile.materializers);
profile.stats = flatSort(db.profile_stat, profile.stats);
profile.footnotes = flatSort(db.profile_footnote, profile.footnotes);
return profile;
};

Expand All @@ -157,13 +137,6 @@ const sortStory = (db, story) => {
return story;
};

const sortSection = (db, section) => {
section = section.toJSON();
section.subtitles = flatSort(db.section_subtitle, section.subtitles);
section.descriptions = flatSort(db.section_description, section.descriptions);
return section;
};

const sortTopic = (db, topic) => {
topic = topic.toJSON();
topic.subtitles = flatSort(db.topic_subtitle, topic.subtitles);
Expand Down Expand Up @@ -312,13 +285,6 @@ module.exports = function(app) {
res.json(sortStory(db, story)).end();
});

app.get("/api/cms/section/get/:id", async(req, res) => {
const {id} = req.params;
const reqObj = Object.assign({}, sectionReqSectionOnly, {where: {id}});
const section = await db.section.findOne(reqObj);
res.json(sortSection(db, section)).end();
});

app.get("/api/cms/topic/get/:id", async(req, res) => {
const {id} = req.params;
const reqObj = Object.assign({}, topicReqTopicOnly, {where: {id}});
Expand Down Expand Up @@ -353,7 +319,7 @@ module.exports = function(app) {

// Top-level tables have their own special gets, so exclude them from the "simple" gets
const getList = cmsTables.filter(tableName =>
!["profile", "section", "topic", "story", "storytopic"].includes(tableName)
!["profile", "topic", "story", "storytopic"].includes(tableName)
);

getList.forEach(ref => {
Expand All @@ -374,13 +340,11 @@ module.exports = function(app) {
app.post("/api/cms/profile/newScaffold", isEnabled, (req, res) => {
const profileData = req.body;
db.profile.create({slug: profileData.slug, ordering: profileData.ordering, dimension: profileData.dimName}).then(profile => {
db.section.create({ordering: 0, profile_id: profile.id}).then(section => {
db.topic.create({ordering: 0, section_id: section.id}).then(() => {
db.profile.findAll(profileReqTreeOnly).then(profiles => {
profiles = sortProfileTree(db, profiles);
populateSearch(profileData, db);
res.json(profiles).end();
});
db.topic.create({ordering: 0, profile_id: profile.id}).then(() => {
db.profile.findAll(profileReqTreeOnly).then(profiles => {
profiles = sortProfileTree(db, profiles);
populateSearch(profileData, db);
res.json(profiles).end();
});
});
});
Expand All @@ -401,8 +365,6 @@ module.exports = function(app) {
* and "parent" refers to the foreign key that need be referenced in the associated where clause.
*/
const deleteList = [
{elements: ["profile_footnote", "profile_stat"], parent: "profile_id"},
{elements: ["section_description", "section_subtitle"], parent: "section_id"},
{elements: ["author", "story_description", "story_footnote"], parent: "story_id"},
{elements: ["topic_subtitle", "topic_description", "topic_stat", "topic_visualization"], parent: "topic_id"}
];
Expand Down Expand Up @@ -487,30 +449,11 @@ module.exports = function(app) {
});
});

app.delete("/api/cms/section/delete", isEnabled, (req, res) => {
db.section.findOne({where: {id: req.query.id}}).then(row => {
db.section.update({ordering: sequelize.literal("ordering -1")}, {where: {profile_id: row.profile_id, ordering: {[Op.gt]: row.ordering}}}).then(() => {
db.section.destroy({where: {id: req.query.id}}).then(() => {
db.section.findAll({
where: {profile_id: row.profile_id},
attributes: ["id", "title", "slug", "ordering", "profile_id"],
order: [["ordering", "ASC"]],
include: [
{association: "topics", attributes: ["id", "title", "slug", "ordering", "section_id"]}
]
}).then(rows => {
res.json(rows).end();
});
});
});
});
});

app.delete("/api/cms/topic/delete", isEnabled, (req, res) => {
db.topic.findOne({where: {id: req.query.id}}).then(row => {
db.topic.update({ordering: sequelize.literal("ordering -1")}, {where: {section_id: row.section_id, ordering: {[Op.gt]: row.ordering}}}).then(() => {
db.topic.update({ordering: sequelize.literal("ordering -1")}, {where: {profile_id: row.profile_id, ordering: {[Op.gt]: row.ordering}}}).then(() => {
db.topic.destroy({where: {id: req.query.id}}).then(() => {
db.topic.findAll({where: {section_id: row.section_id}, attributes: ["id", "title", "slug", "ordering", "section_id", "type"], order: [["ordering", "ASC"]]}).then(rows => {
db.topic.findAll({where: {profile_id: row.profile_id}, attributes: ["id", "title", "slug", "ordering", "profile_id", "type"], order: [["ordering", "ASC"]]}).then(rows => {
res.json(rows).end();
});
});
Expand Down
55 changes: 18 additions & 37 deletions packages/cms/src/api/mortarRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,16 @@ const searchMap = {
};

const profileReq = {
include: [
{association: "stats", separate: true},
{association: "footnotes", separate: true},
{
association: "sections", separate: true,
include: [
{association: "subtitles", separate: true},
{association: "descriptions", separate: true},
{
association: "topics", separate: true,
include: [
{association: "subtitles", separate: true},
{association: "descriptions", separate: true},
{association: "visualizations", separate: true},
{association: "stats", separate: true},
{association: "selectors", separate: true}
]
}
]
}
]
include: [{
association: "topics", separate: true,
include: [
{association: "subtitles", separate: true},
{association: "descriptions", separate: true},
{association: "visualizations", separate: true},
{association: "stats", separate: true},
{association: "selectors", separate: true}
]
}]
};

const topicReq = [
Expand Down Expand Up @@ -84,22 +73,14 @@ const sorter = (a, b) => a.ordering - b.ordering;
// Using nested ORDER BY in the massive includes is incredibly difficult so do it manually here. Eventually move it up to the query.
const sortProfile = profile => {
profile = profile.toJSON();
if (profile.stats) profile.stats.sort(sorter);
if (profile.sections) {
profile.sections.sort(sorter);
profile.sections.map(section => {
if (section.subtitles) section.subtitles.sort(sorter);
if (section.descriptions) section.descriptions.sort(sorter);
if (section.topics) {
section.topics.sort(sorter);
section.topics.map(topic => {
if (topic.subtitles) topic.subtitles.sort(sorter);
if (topic.selectors) topic.selectors.sort(sorter);
if (topic.stats) topic.stats.sort(sorter);
if (topic.descriptions) topic.descriptions.sort(sorter);
if (topic.visualizations) topic.visualizations.sort(sorter);
});
}
if (profile.topics) {
profile.topics.sort(sorter);
profile.topics.forEach(topic => {
if (topic.subtitles) topic.subtitles.sort(sorter);
if (topic.selectors) topic.selectors.sort(sorter);
if (topic.stats) topic.stats.sort(sorter);
if (topic.descriptions) topic.descriptions.sort(sorter);
if (topic.visualizations) topic.visualizations.sort(sorter);
});
}
return profile;
Expand Down
1 change: 0 additions & 1 deletion packages/cms/src/profile/ProfileBuilder.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {connect} from "react-redux";
import {NonIdealState, Tree, Dialog, Intent, Alert} from "@blueprintjs/core";
import NewProfile from "./NewProfile";
import ProfileEditor from "./ProfileEditor";
import SectionEditor from "./SectionEditor";
import TopicEditor from "./TopicEditor";
import PropTypes from "prop-types";
import Search from "../components/Search/Search";
Expand Down

0 comments on commit e030c7b

Please sign in to comment.