const router = require('express').Router()
const Owner = require('../models/owner')
const upload = require('../middlewares/upload-photo')

router.post('/owners', upload.single('photo'), async (req, res)=> {
  try {
    const owner = new Owner()
    owner.name = req.body.name
    owner.about = req.body.about
    owner.photo = req.file.location
    await owner.save()

    res.json({
      success: true,
      message: 'Success create owner'
    })

  } catch(err) {
    res.status(500).json({
      success: false,
      message: err.message
    })
  }
})

router.get('/owners', async (req, res) => {
  try {
    let owners = await Owner.find()
    res.json({
      owners
    })
  } catch (err) {
    res.status(500).json({
      success: false,
      message: err.message
    })
  }
})

module.exports = router