Skip to content

Commit

Permalink
Merge c581dd1 into 1310fd1
Browse files Browse the repository at this point in the history
  • Loading branch information
habinezadalvan committed Aug 20, 2019
2 parents 1310fd1 + c581dd1 commit f0f5a33
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 57 deletions.
13 changes: 4 additions & 9 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,17 +26,11 @@ 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 tags = req.body.taglist ? req.body.taglist.split(' ') : [];
const article = {
slug: `${slug(title)}-${uniqid()}`,
title,
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/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
30 changes: 0 additions & 30 deletions test/mock/mock.cloud.js

This file was deleted.

4 changes: 2 additions & 2 deletions test/test.articles.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('Articles', () => {
.post('/api/v1/articles')
.set('Content-Type', 'application/json')
.set('Authorization', usertoken)
.field('tagList', 'tag1, tag2, tag3')
.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 +34,7 @@ describe('Articles', () => {
.field('title', 'Title')
.field('body', 'body field')
.field('description', 'description is here')
.field('tagList', 'tag1, tag2, tag3')
.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 f0f5a33

Please sign in to comment.