<commit_before>const {Users, Groups} = require('../models/users')
const mongoose = require('mongoose')

module.exports = function(router) {
  router.post('/collection/add', (req, res) => {
    //check that the title and author are not empty
    if(req.body.title.length < 1 || req.body.author.length < 1) return res.redirect('/dashboard')
    //add a new book to user then save
    req.user.books.push({
      owner: {_id: req.user._id, username: req.user.username},
      title: req.body.title,
      author: req.body.author,
      description: req.body.description,
      group: null
    })
    req.user.save()
    .catch(err => {
      console.log(err)
      res.render('error')
    })
    res.redirect('/dashboard')
  })

  router.post('/collection/remove/:id', (req, res) => {
    //find the book then delete from array
    //Check and remove if the book is inside any groups
    let book = req.user.books.id(req.params.id)
    if(book.group) {
      Groups.update({_id: book.group._id}, {$pull: {books: book._id}})
      .catch(err => {
        console.log(err)
        res.render('error')
      })
    }
    req.user.books.remove(req.params.id)
    req.user.save()
    .catch(err => {
      console.log(err)
      res.render('error')
    })
    res.redirect('/dashboard')
  })
}<commit_msg>user-collection-route final<commit_after>const {Users, Groups} = require('../models/users')
const mongoose = require('mongoose')

module.exports = function(router) {
  router.post('/collection/add', (req, res) => {
    //check that the title and author are not empty
    if(req.body.title.length < 1 || req.body.author.length < 1) return res.redirect('/dashboard')
    //add a new book to user then save
    req.user.books.push({
      owner: {_id: req.user._id, username: req.user.username},
      title: req.body.title,
      author: req.body.author,
      description: req.body.description,
      group: null
    })
    req.user.save()
    .catch(err => {
      console.log(err)
      res.render('error')
    })
    res.redirect('/dashboard')
  })

  router.post('/collection/remove/:id', (req, res) => {
    //find the book then delete from array
    //Check and remove if the book is inside any groups
    let book = req.user.books.id(req.params.id)
    //if the book is being borrowed do not remove from collection
    if(book.borrower) return res.render('/dashboard')
    if(book.group) {
      Groups.update({_id: book.group._id}, {$pull: {books: book._id}})
      .catch(err => {
        console.log(err)
        res.render('error')
      })
    }
    req.user.books.remove(req.params.id)
    req.user.save()
    .catch(err => {
      console.log(err)
      res.render('error')
    })
    res.redirect('/dashboard')
  })
}
{'fraction_non_alphanumeric': 0.16385630498533724, 'fraction_numerical': 0.001466275659824047, 'mean_word_length': 2.5860709592641262, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3808775', 'n_tokens_mistral': 886, 'n_tokens_neox': 886, 'n_words': 199}