Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor of page descriptions api #48

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/backend/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import postings from './postings';
import sponsors from './sponsors'; // Import entire folder to bind to sponsor URL router section.
import upload from './file-upload/upload';
import geeseInfo from './geese-info';
import openingsDescription from './openings-description';
import newsletter from './newsletter_recipient';
import blogs from './blogs';
import geeseFeatures from './geese-features';
import products from './products';
import pageDescriptions from './page-descriptions'

const router = express.Router();

Expand All @@ -17,10 +17,11 @@ router.use('/postings', postings);
router.use('/sponsors', sponsors);
router.use('/upload', upload);
router.use('/geese-info', geeseInfo);
router.use('/openings-description', openingsDescription);
router.use('/newsletter', newsletter);
router.use('/blogs', blogs);
router.use('/geese-features', geeseFeatures);
router.use('/products', products)
router.use('/page-descriptions', pageDescriptions)


export default router;
40 changes: 0 additions & 40 deletions src/backend/api/openings-description/index.js

This file was deleted.

15 changes: 15 additions & 0 deletions src/backend/api/page-descriptions/add.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import db from '../../db';

export default (req, res) => {
const pageDescription = req.body;
db.pageDescriptions.addPageDescription(pageDescription)
.then((response) => {
if (response) {
res.send(response);
}
})
.catch((err) => {
console.log(`Could not add page description: ${err}`);
res.sendStatus(500);
});
}
20 changes: 20 additions & 0 deletions src/backend/api/page-descriptions/get.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import db from '../../db';
import * as R from 'ramda';

export default (req, res) => {
const { key } = req.params;

db.pageDescriptions
.getPageDescriptionByKey(key)
.then((pageDescription) => {
if (R.isEmpty(pageDescription)) {
res.sendStatus(404);
} else {
res.send(pageDescription);
}
})
.catch((err) => {
console.log(`Could not get page description with given key: ${err}`);
res.sendStatus(500);
});
};
27 changes: 27 additions & 0 deletions src/backend/api/page-descriptions/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import express from 'express';
import { param, body, check} from 'express-validator';
import validationCheck from '../../utils/validation-check';
import passArg from '../../utils/pass-arg';
import { validateRequest } from '../../google-auth';

import get from './get';
import add from './add';
import update from './update';

const router = express.Router();

router.post('/', [
body('key').exists(),
body('title').exists(),
], validationCheck, validateRequest, add);

router.get('/:key', [
param('key').isString()
], validationCheck, get);

router.patch('/:key', [
param('key').isString(),
body('title').exists(),
], validationCheck, validateRequest, update);

export default router;
16 changes: 16 additions & 0 deletions src/backend/api/page-descriptions/update.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import db from '../../db';

export default (req, res) => {
const pageDescription = req.body;
const key = req.params.key

db.pageDescriptions.updatePageDescriptionByKey(key, pageDescription)
.then((response) => {
console.log(response);
res.sendStatus(200);
})
.catch((err) => {
console.log(`Could not update page description: ${err}`);
res.sendStatus(500);
});
};
12 changes: 0 additions & 12 deletions src/backend/api/sponsors/description/get.js

This file was deleted.

22 changes: 0 additions & 22 deletions src/backend/api/sponsors/description/update.js

This file was deleted.

13 changes: 0 additions & 13 deletions src/backend/api/sponsors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,12 @@ import getSponsors from './get-sponsors';
import add from './add';
import update from './update';
import del from './del';
import getDesc from './description/get';
import updateDesc from './description/update';

const router = express.Router();

// Sponsor tiers.
router.get('/tiers', getTiers);

// Sponsor page description
router.get('/description', getDesc);

router.patch('/description', [
// Required
body('title').isString(),
body('description').isString(),
body('images').isArray()

], validationCheck, updateDesc);

// Sponsors
router.get('/', getSponsors);

Expand Down
13 changes: 0 additions & 13 deletions src/backend/api/team-descriptors/description/get.js

This file was deleted.

23 changes: 0 additions & 23 deletions src/backend/api/team-descriptors/description/update.js

This file was deleted.

18 changes: 0 additions & 18 deletions src/backend/api/team-descriptors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,13 @@ import { param, body } from 'express-validator';
import validationCheck from '../../utils/validation-check';
import { validateRequest } from '../../google-auth';

import getDesc from './description/get';
import updateDesc from './description/update';
import getAll from './get-all';
import add from './add';
import update from './update';
import del from './del';

const router = express.Router();

/* TEAMS PAGE DESCRIPTION */

router.get('/description', getDesc);

router.patch(
'/description',
[
body('title').isString(),
body('description').isString(),
body('images').isArray(),
],
validationCheck,
validateRequest,
updateDesc,
);

/* TEAMS */

router.get('/', getAll);
Expand Down
8 changes: 4 additions & 4 deletions src/backend/db/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import teamDescriptors from './team-descriptors';
import postings from './postings';
import sponsors from './sponsors';
import geeseInfo from './geese-info';
import openingsDescription from './openings-description';
import featurePermissions from './feature-permissions';
import newsletterRecipients from './newsletter-recipient';
import blogs from './blogs';
import geeseFeatures from './geese-features';
import products from './products'
import pageDescriptions from './page-descriptions';

export default {
users: {
Expand All @@ -28,9 +28,6 @@ export default {
geeseInfo: {
...geeseInfo(db),
},
openingsDescription: {
...openingsDescription(db),
},
newsletterRecipients: {
...newsletterRecipients(db),
},
Expand All @@ -45,5 +42,8 @@ export default {
},
products: {
...products(db),
},
pageDescriptions: {
...pageDescriptions(db)
}
};
18 changes: 0 additions & 18 deletions src/backend/db/openings-description.js

This file was deleted.

51 changes: 51 additions & 0 deletions src/backend/db/page-descriptions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import {
toPageDescription,
fromPageDescription,
} from '../models/page-descriptions';
import * as R from 'ramda';

const getPageDescriptionByKey = (db) => (key) =>
db('page_descriptions')
.where({ key })
.then((res) => {
return R.isEmpty(res) ? res : toPageDescription(res[0]);
})
.catch((err) => {
console.error(`Error in getPageDescriptionByKey: ${err}`);
throw err;
});

const addPageDescription = (db) => (pageDescription) =>
db('page_descriptions')
.insert(fromPageDescription(pageDescription))
.returning('id')
.then((response) => {
return response;
})
.catch((err) => {
console.error(`Error in addPageDescription: ${err}`);
throw err;
});

const updatePageDescriptionByKey = (db) => (key, pageDescription) =>
db('page_descriptions')
.where({
key,
})
.update({
...fromPageDescription(pageDescription),
})
.then((response) => {
return response;
})
.catch((err) => {
console.error(`Error in updatePageDescriptionByKey: ${err}`);
throw err;
});


export default (db) => ({
getPageDescriptionByKey: getPageDescriptionByKey(db),
addPageDescription: addPageDescription(db),
updatePageDescriptionByKey: updatePageDescriptionByKey(db),
});
Loading
Loading