POST /api/articles route working

This commit is contained in:
Josh Black 2017-09-21 21:46:37 -06:00
parent c2a449b2a9
commit d00c1a22c5
5 changed files with 51 additions and 6 deletions

View File

@ -7,7 +7,7 @@ export interface IArticle {
title: string;
description: string;
body: string;
tagList: [string];
tagList?: [string];
createdAt: Date;
updatedAt: Date;
favorited: boolean;

View File

@ -1,11 +1,12 @@
import { Router, NextFunction, Response } from 'express';
import { authentication } from '../utilities/authentication';
import { ProfileRequest } from '../interfaces/requests-interface';
import { JWTRequest, ProfileRequest } from '../interfaces/requests-interface';
import { Article, IArticleModel } from '../models/article-model';
import { IUserModel, User } from '../models/user-model';
import { IQuery } from '../interfaces/article-interface';
import { Schema } from 'mongoose';
import * as slugify from 'slugify';
const router: Router = Router();
const Promise = require('bluebird'); // FIXME: how to handle this in Typescript?
@ -14,6 +15,8 @@ const Promise = require('bluebird'); // FIXME: how to handle this in Typescript
/**
* GET /api/articles
*/
// FIXME: authorized user who has favorited own articles showing false.
// Should show true for all returned josh articles
router.get('/', authentication.optional, (req: ProfileRequest, res: Response, next: NextFunction) => {
// Try to determine the user making the request
@ -126,10 +129,46 @@ router.get('/', authentication.optional, (req: ProfileRequest, res: Response, ne
});
/**
* POST /api/articles
*/
router.post('/', authentication.required, (req: JWTRequest, res: Response, next: NextFunction) => {
// Examine the request body for completeness
const article: IArticleModel = new Article();
if (typeof req.body.article.title !== 'undefined' &&
typeof req.body.article.description !== 'undefined' &&
typeof req.body.article.body !== 'undefined') {
article.title = req.body.article.title;
article.description = req.body.article.description;
article.body = req.body.article.body;
article.slug = slugify(article.title, {lower: true});
} else {
res.json('Error in article input: missing title, desc, or body.');
}
if (typeof req.body.article.tagList !== 'undefined') {
article.tagList = req.body.article.tagList;
}
// Verify authentication successful, then save and return article
User
.findById(req.payload.id)
.then(user => {
article.author = user;
return article.save().then(() => {
return res.json({article: article.formatAsArticleJSON(user)});
});
})
.catch(next);
});
// TODO: Remaining routes
// GET /api/articles/feed
// GET /api/articles/:slug
// POST /api/articles
// PUT /api/articles/:slug
// DELETE /api/articles/:slug

View File

@ -1,6 +1,6 @@
import { Router, Response, NextFunction, Request } from 'express';
import { User } from '../models/user-model';
import { IUserModel, User } from '../models/user-model';
import * as passport from 'passport';
const router: Router = Router();
@ -11,7 +11,7 @@ const router: Router = Router();
*/
router.post('/', (req: Request, res: Response, next: NextFunction) => {
const user = new User();
const user: IUserModel = new User();
user.username = req.body.user.username;
user.email = req.body.user.email;
@ -32,7 +32,7 @@ router.post('/', (req: Request, res: Response, next: NextFunction) => {
/**
* POST /api/users/login
*/
router.post('/login', (req, res, next) => {
router.post('/login', (req: Request, res: Response, next: NextFunction) => {
if (!req.body.user.email) {
return res.status(422).json( {errors: {email: "Can't be blank"}} );

5
package-lock.json generated
View File

@ -2722,6 +2722,11 @@
"integrity": "sha1-VusCfWW00tzmyy4tMsTUr8nh1wc=",
"dev": true
},
"slugify": {
"version": "1.2.1",
"resolved": "https://registry.npmjs.org/slugify/-/slugify-1.2.1.tgz",
"integrity": "sha512-WgTaDQNSyEKXlNKHO8L0DJJtE3gLK83pTx9OM941PnDVhPpZ93FHIbF2D9b0W6d5DsMwBkd56cRFKgxXkPN2KA=="
},
"source-map": {
"version": "0.5.7",
"resolved": "https://registry.npmjs.org/source-map/-/source-map-0.5.7.tgz",

View File

@ -39,6 +39,7 @@
"mongoose": "^4.11.9",
"passport": "^0.4.0",
"passport-local": "^1.0.0",
"slugify": "^1.2.1",
"ts-node": "^3.3.0",
"typescript": "^2.5.2"
}