Advertisement
Guest User

Untitled

a guest
Jan 15th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. import express from 'express';
  2.  
  3. import * as ProductController from '../controllers/product';
  4. import checkToken from '../middlewares/checkToken';
  5. import getUser from '../middlewares/getUser';
  6.  
  7. const router = express.Router();
  8.  
  9. router.post('/products', checkToken, getUser, ProductController.create);
  10. router.put('/products', checkToken, getUser, ProductController.update);
  11. router.get('/products/:categoryUrl', ProductController.getProductsByCategoryUrl);
  12. router.get('/products/:categoryUrl/:productUrl', checkToken, getUser, ProductController.getProductByUrl);
  13. router.get('/products', ProductController.getAll);
  14.  
  15. export default router;
  16.  
  17. import Product from '../models/product';
  18. import Category from '../models/category';
  19. import * as PageStatusService from '../services/pageStatusService';
  20. import * as CategoryService from '../services/categoryService';
  21. import urlCheck from '../helpres/check-is-valid-url';
  22.  
  23. export async function create(req, res, next) {
  24. const productData = req.body;
  25. const userId = req.user._id;
  26. let product;
  27. let isStatusExist;
  28. let isCategoryExist;
  29.  
  30. productData.user = userId;
  31.  
  32. if (!urlCheck(productData.url)) {
  33. return next({
  34. status: 400,
  35. message: 'Invalid url value'
  36. });
  37. }
  38.  
  39. try {
  40. isStatusExist = await PageStatusService.isExistStatus(productData.status);
  41. } catch ({ message }) {
  42. return next({
  43. status: 400,
  44. message
  45. });
  46. }
  47.  
  48. if (!isStatusExist) {
  49. return next({
  50. status: 400,
  51. message: 'Invalid status value'
  52. });
  53. }
  54.  
  55. // TODO: add check for category, product belong to last level category
  56. // if categories with parent category productData.category is empty
  57.  
  58. try {
  59. isCategoryExist = await CategoryService.isExistCategoryById(productData.category);
  60. } catch ({ message }) {
  61. return next({
  62. status: 400,
  63. message
  64. });
  65. }
  66.  
  67. if (!isCategoryExist) {
  68. return next({
  69. status: 400,
  70. message: 'Invalid category value'
  71. });
  72. }
  73.  
  74. try {
  75. product = await Product.create(productData);
  76. } catch ({ message }) {
  77. return next({
  78. status: 400,
  79. message
  80. });
  81. }
  82.  
  83. res.json(product);
  84. }
  85.  
  86. ...
  87.  
  88. var should = require('should');
  89. var assert = require('assert');
  90. var request = require('supertest');
  91. var mongoose = require('mongoose');
  92. var winston = require('winston');
  93. var config = require('./config-debug');
  94.  
  95. describe('Account', function() {
  96. it('should return error trying to save duplicate username', function(done) {
  97. var profile = {
  98. username: 'vgheri',
  99. password: 'test',
  100. firstName: 'Valerio',
  101. lastName: 'Gheri'
  102. };
  103. // once we have specified the info we want to send to the server via POST verb,
  104. // we need to actually perform the action on the resource, in this case we want to
  105. // POST on /api/profiles and we want to send some info
  106. // We do this using the request object, requiring supertest!
  107. request(url)
  108. .post('/api/profiles')
  109. .send(profile)
  110. // end handles the response
  111. .end(function(err, res) {
  112. if (err) {
  113. throw err;
  114. }
  115. // this is should.js syntax, very clear
  116. res.should.have.status(400);
  117. done();
  118. });
  119. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement