user.controller.js 2.43 KB
Newer Older
Yoon, Daeki's avatar
Yoon, Daeki committed
1
2
import User from './user.model.js'
import formidable from 'formidable'
Yoon, Daeki's avatar
quiz    
Yoon, Daeki committed
3
import extend from 'lodash/extend.js'
Yoon, Daeki's avatar
Yoon, Daeki committed
4
import fs from 'fs'
Yoon, Daeki's avatar
Yoon, Daeki committed
5
6
7
8
9
10

const create = async (req, res) => {
  const user = new User(req.body)
  try {
    await user.save()
    return res.json({
Yoon, Daeki's avatar
Yoon, Daeki committed
11
      message: 'Successfully signed up!'
Yoon, Daeki's avatar
Yoon, Daeki committed
12
13
14
15
16
17
18
19
20
21
22
23
24
25
    })
  } catch (error) {
    return res.status(400).json({
      error: 'User creation error'
    })
  }
}

const list = async (req, res) => {
  try {
    let users = await User.find().select('name email updated created').exec()
    return res.json(users)
  } catch (error) {
    return res.status(400).json({
Yoon, Daeki's avatar
Yoon, Daeki committed
26
27
28
29
30
31
32
33
34
35
36
      error: 'Users not found'
    })
  }
}

const read = (req, res) => {
  req.profile.hashedPassword = undefined
  req.profile.salt = undefined
  return res.json(req.profile)
}

Yoon, Daeki's avatar
quiz    
Yoon, Daeki committed
37
const update = (req, res) => {
Yoon, Daeki's avatar
Yoon, Daeki committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
  let form = new formidable.IncomingForm()
  form.keepExtensions = true
  form.parse(req, async (err, fields, files) => {
    if (err) {
      return res.status(400).json({
        error: 'Photo could not be uploaded'
      })
    }
    let user = req.profile
    user = extend(user, fields)
    user.updated = new Date()
    if (files.photo) {
      user.photo.data = fs.readFileSync(files,photo.path)
      user.photo.contentType = files.photo.type
    }

    try {
      await user.save()
      user.hashedPassword = undefined
      user.salt = undefined
      res.json(user)
    } catch (error) {
      return res.status(400).json({
        error: 'User save error'
      })
    }
  })
}

const remove = async (req, res) => {
  try {
    let user = req.profile
    let deletedUser = await user.remove()
    deletedUser.hashedPassword = undefined
    deletedUser.salt = undefined
    res.json(deletedUser)
  } catch (error) {
    return res.status(400).json({
      error: 'User delete error'
    })
  }
}

Yoon, Daeki's avatar
quiz    
Yoon, Daeki committed
81
82
83
84
85
86
87
88
89
90
const isInstructor = (req, res, next) => {
  const instructor = req.profile && req.profile.instructor
  if (!instructor) {
    return res.status(403).json({
      error: 'User is not an instructor'
    })
  }
  next()
}

Yoon, Daeki's avatar
Yoon, Daeki committed
91
92
93
94
95
96
97
98
99
100
101
102
103
104
const userById = async (req, res, next, id) => {
  try {
    let user = await User.findById(id)
      .exec()
    if (!user) {
      return res.status(400).json({
        error: 'User not found'
      })
    }
    req.profile = user
    next()
  } catch (error) {
    return res.status(400).json({
      error: 'Could not retrieve user'
Yoon, Daeki's avatar
Yoon, Daeki committed
105
106
107
108
109
110
111
    })
  }
}

export default {
  create,
  list,
Yoon, Daeki's avatar
Yoon, Daeki committed
112
113
114
  read,
  update,
  remove,
Yoon, Daeki's avatar
quiz    
Yoon, Daeki committed
115
  isInstructor,
Yoon, Daeki's avatar
Yoon, Daeki committed
116
  userById,
Yoon, Daeki's avatar
Yoon, Daeki committed
117
}