diff --git a/api/interfaces/article-interface.ts b/api/interfaces/article-interface.ts index a96c517..6c3cd24 100644 --- a/api/interfaces/article-interface.ts +++ b/api/interfaces/article-interface.ts @@ -7,7 +7,7 @@ export interface IArticle { title: string; description: string; body: string; - tagList: [string]; + tagList?: [string]; createdAt: Date; updatedAt: Date; favorited: boolean; diff --git a/api/routes/articles-routes.ts b/api/routes/articles-routes.ts index 3ddd04d..0c241df 100644 --- a/api/routes/articles-routes.ts +++ b/api/routes/articles-routes.ts @@ -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 diff --git a/api/routes/users-routes.ts b/api/routes/users-routes.ts index 83a76c8..c15a011 100644 --- a/api/routes/users-routes.ts +++ b/api/routes/users-routes.ts @@ -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"}} ); diff --git a/package-lock.json b/package-lock.json index d6f8728..5354b13 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index 8501015..ddbf150 100644 --- a/package.json +++ b/package.json @@ -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" }