Skip to content

Commit

Permalink
Merge 40f6275 into 1310fd1
Browse files Browse the repository at this point in the history
  • Loading branch information
habinezadalvan committed Aug 20, 2019
2 parents 1310fd1 + 40f6275 commit ab9220e
Show file tree
Hide file tree
Showing 11 changed files with 14 additions and 64 deletions.
14 changes: 3 additions & 11 deletions src/controllers/articles.controller.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import 'dotenv/config';
import slug from 'slug';
import uniqid from 'uniqid';
import cloudinary from 'cloudinary';
import models from '../models';
import Userservice from '../services/user.service';
import articleService from '../services/article.service';
import cloudinaryHelper from '../helpers/cloudinaryHelper';


const db = models.Article;
/**
Expand All @@ -25,23 +26,15 @@ class Articles {
static async createArticles(req, res) {
const userId = req.auth.id;
const findUser = await Userservice.getOneUser(userId);
let images = req.files;
images = await Promise.all(
images.map(async (file) => {
const { secure_url } = await cloudinary.v2.uploader.upload(file.path);
return secure_url;
})
);
const images = await cloudinaryHelper(req.files);

if (findUser) {
const { title } = req.body;
const tags = req.body.taglist.split(' ');
const article = {
slug: `${slug(title)}-${uniqid()}`,
title,
description: req.body.description,
body: req.body.body,
taglist: tags,
authorId: req.auth.id,
images
};
Expand All @@ -53,7 +46,6 @@ class Articles {
title: createdArticle.title,
description: createdArticle.description,
body: createdArticle.body,
taglist: createdArticle.taglist,
flagged: createdArticle.flagged,
favorited: createdArticle.favorited,
favoritedcount: createdArticle.favoritedcount,
Expand Down
11 changes: 11 additions & 0 deletions src/helpers/cloudinaryHelper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import 'dotenv/config';
import cloudinary from 'cloudinary';

export default async (images = []) => Promise.all(
images.map(async (file) => {
const { secure_url } = process.env.NODE_ENV === 'test'
? 'image.jpg'
: await cloudinary.v2.uploader.upload(file.path);
return secure_url;
})
);
9 changes: 0 additions & 9 deletions src/middlewares/fakecloud.js

This file was deleted.

2 changes: 0 additions & 2 deletions src/middlewares/validators/socialLogin-mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ dotenv.config();
export const mock = (req, res, next) => {
if (req.url.includes('login/')) {
res.redirect(`/auth/fake?provider=${req.url.split('/')[2]}`);
} else if (req.url.includes('articles') && req.method === 'POST') {
res.redirect(307, '/fake');
} else {
next();
}
Expand Down
3 changes: 0 additions & 3 deletions src/migrations/20190813125249-create-article.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ module.exports = {
type: Sequelize.TEXT,
allowNull: false,
},
taglist: {
type: Sequelize.ARRAY(Sequelize.STRING)
},
favorited: {
type: Sequelize.BOOLEAN,
allowNull: true,
Expand Down
1 change: 0 additions & 1 deletion src/models/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ module.exports = (sequelize, DataTypes) => {
title: { type: DataTypes.STRING, allowNull: false },
description: { type: DataTypes.TEXT, allowNull: false },
body: { type: DataTypes.TEXT, allowNull: false },
taglist: { type: DataTypes.ARRAY(DataTypes.STRING), allowNull: true },
favorited: { type: DataTypes.BOOLEAN, allowNull: true, defaultValue: false },
favoritedcount: { type: DataTypes.INTEGER, defaultValue: 0 },
flagged: { type: DataTypes.BOOLEAN, defaultValue: false },
Expand Down
3 changes: 0 additions & 3 deletions src/routes/api/article/article.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@ import articleController from '../../../controllers/articles.controller';
import imageUpload from '../../../middlewares/multer';
import validate from '../../../middlewares/validators/general.validation';
import { schema } from '../../../middlewares/validators/schemas';
import fakeCloud from '../../../middlewares/fakecloud';
import confirmEmailAuth from '../../../middlewares/emailVarification.middleware';

const router = express.Router();

router.post('/fake', auth, fakeCloud, validate(schema.articleSchema), articleController.createArticles);


router.post('/articles', [auth, confirmEmailAuth], imageUpload.array('images', 10), validate(schema.articleSchema), articleController.createArticles);
router.get('/articles', [auth, confirmEmailAuth], articleController.getAllArticles);
Expand Down
2 changes: 0 additions & 2 deletions src/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import oauth from './api/oauth/oauth.routes';
import error from '../middlewares/error.middleware';
import notfound from '../middlewares/404.middleware';
import { mock } from '../middlewares/validators/socialLogin-mock';
import article from './api/article/article.routes';

dotenv.config();

Expand Down Expand Up @@ -38,7 +37,6 @@ const apiVersion = process.env.API_VERSION;
const baseUrl = `/api/${apiVersion}`;

router.get('/', (req, res) => res.status(200).json({ status: 200, data: 'Welcome to Authors Haven.' }));
router.use(article);
router.use(baseUrl, api);
router.use(oauth);

Expand Down
1 change: 0 additions & 1 deletion src/seeders/20190814094408-demo-article.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export default {
title: 'faketitle',
description: 'fakedescription',
body: 'fakebody',
taglist: ['asdf', 'sdfsf'],
favorited: true,
favoritedcount: 12,
flagged: true,
Expand Down
30 changes: 0 additions & 30 deletions test/mock/mock.cloud.js

This file was deleted.

2 changes: 0 additions & 2 deletions test/test.articles.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ describe('Articles', () => {
.post('/api/v1/articles')
.set('Content-Type', 'application/json')
.set('Authorization', usertoken)
.field('tagList', 'tag1, tag2, tag3')
.attach('images', fs.readFileSync(`${__dirname}/mock/pic.jpeg`), 'pic.jpeg')
.end((err, res) => {
expect(res.status).to.be.deep.equal(400);
Expand All @@ -34,7 +33,6 @@ describe('Articles', () => {
.field('title', 'Title')
.field('body', 'body field')
.field('description', 'description is here')
.field('tagList', 'tag1, tag2, tag3')
.attach('images', fs.readFileSync(`${__dirname}/mock/pic.jpeg`), 'pic.jpeg')
.end((err, res) => {
expect(res.status).to.be.deep.equal(201);
Expand Down

0 comments on commit ab9220e

Please sign in to comment.