Skip to content

Commit

Permalink
Set up database and route for saving a subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
fgerschau committed Aug 15, 2020
1 parent 23b6ee7 commit 589c09c
Show file tree
Hide file tree
Showing 8 changed files with 303 additions and 4 deletions.
16 changes: 16 additions & 0 deletions app/config/database.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import mongoose from 'mongoose';

export default async () => {
// Connect to the database
try {
await mongoose.connect('mongodb://localhost/web-push-notifications', {
useNewUrlParser: true,
useUnifiedTopology: true,
});

mongoose.set('useCreateIndex', true);
} catch (e) {
console.error(`Couldn't connect to the database: ${e}`);
process.exit(1);
}
};
15 changes: 15 additions & 0 deletions app/controllers/subscriptionController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { NextFunction, Request, Response } from 'express';
import * as subscriptionRepository from '../repositories/subscriptionRepository';

export const post = async (req: Request, res: Response, next: NextFunction): Promise<void> => {
try {
const subscription = req.body;

const newSubscription = await subscriptionRepository.create(subscription);

// Send 201 - resource created
res.status(201).json(newSubscription);
} catch (e) {
next(e);
}
};
5 changes: 5 additions & 0 deletions app/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import express from 'express';
import path from 'path';
import bodyParser from 'body-parser';
import database from './config/database';
import initializeRoutes from './routes';

const app = express();
const port = 8080; // default port to listen

app.use(express.static(path.join(__dirname, '../client')));
app.use(bodyParser.json());

database();
initializeRoutes(app);

// start the Express server
app.listen(port, () => {
// eslint-disable-next-line no-console
Expand Down
21 changes: 21 additions & 0 deletions app/models/SubscriptionModel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import mongoose, { Schema, Document } from 'mongoose';

export interface ISubscription extends Document {
endpoint: string;
expirationTime?: number;
keys: {
auth: string;
p256dh: string;
}
}

const SubscriptionModel = new Schema({
endpoint: { type: String, unique: true, required: true },
expirationTime: { type: Number, required: false },
keys: {
auth: String,
p256dh: String,
},
});

export default mongoose.model<ISubscription>('Subscription', SubscriptionModel);
7 changes: 7 additions & 0 deletions app/repositories/subscriptionRepository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Subscription, { ISubscription } from '../models/SubscriptionModel';

export const create = async (subscription: ISubscription): Promise<ISubscription> => {
const newSubscription = new Subscription(subscription);
const savedSubscription = await newSubscription.save();
return savedSubscription.toObject();
};
8 changes: 8 additions & 0 deletions app/routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Express } from 'express';
import { post } from './controllers/subscriptionController';

const initializeRoutes = (app: Express): void => {
app.post('/subscription', post);
};

export default initializeRoutes;
Loading

0 comments on commit 589c09c

Please sign in to comment.