<commit_before>log = require("#{__dirname}/../../src/log")

CONFIG = require("#{__dirname}/../../_config.yml")

User = require("#{__dirname}/../model/user")
Book = require("#{__dirname}/../model/book")

module.exports = {}

module.exports.addBook = (req, res) ->
  return res.send 401, err: "Not signed in" unless req.user

  isbn = req.param('isbn') || req.body?.book?.isbn
  return res.send 400, err: "Missing ISBN number" unless isbn?.length

  addedBook = {}

  Book.findOneAndUpdate({isbn: isbn}, {isbn: isbn}, {upsert: true})
  .exec()
  # .then null, (err) -> throw new Error "Error creating your book"
  .then (book) ->
    addedBook = book

    Book.fetchFromAmazon(addedBook)
  # .then null, (err) -> throw new Error "Error loading book from Amazon"
  .then (book) ->
    addedBook = book
    User.findOneAndUpdate {_id: req.user.id},
      $push:
        books:
          _id: addedBook._id
    .exec()
  # .then null, (err) -> throw new Error "Error adding your book"
  .then (user) ->
    res.send 201, book: addedBook#, user: req.user
  .then null, (err) ->
    res.send 500, err: err

module.exports.removeBook = (req, res) ->
  book_id = req.param('book_id') || req.body?.book?.id
  return res.send 400, err: "Missing Book ID" unless book_id?.length

  User.findOneAndUpdate {_id: req.user.id},
    $pull:
      books: book_id
  .exec()
  .then (user) ->
    res.send 201, user: user
  .then null, (err) ->
    res.send 500, err: err

module.exports.avgValueOverTime = (req, res) ->
  users_books = req.user.books
  creation_date = new Date(req.user._id.getTimestamp())

  # params
  min_value = parseInt req.param('min_value'), 10
  per_book = req.param 'per_book'
  limit_days = req.param 'days'

  match_users_books =
    $match:
      _id:
        $in: users_books

  unwind_prices =
    $unwind: "$prices"

  if min_value<commit_msg>Delete Should Respond With 204<commit_after>log = require("#{__dirname}/../../src/log")

CONFIG = require("#{__dirname}/../../_config.yml")

User = require("#{__dirname}/../model/user")
Book = require("#{__dirname}/../model/book")

module.exports = {}

module.exports.addBook = (req, res) ->
  return res.send 401, err: "Not signed in" unless req.user

  isbn = req.param('isbn') || req.body?.book?.isbn
  return res.send 400, err: "Missing ISBN number" unless isbn?.length

  addedBook = {}

  Book.findOneAndUpdate({isbn: isbn}, {isbn: isbn}, {upsert: true})
  .exec()
  # .then null, (err) -> throw new Error "Error creating your book"
  .then (book) ->
    addedBook = book

    Book.fetchFromAmazon(addedBook)
  # .then null, (err) -> throw new Error "Error loading book from Amazon"
  .then (book) ->
    addedBook = book
    User.findOneAndUpdate {_id: req.user.id},
      $push:
        books:
          _id: addedBook._id
    .exec()
  # .then null, (err) -> throw new Error "Error adding your book"
  .then (user) ->
    res.send 201, book: addedBook#, user: req.user
  .then null, (err) ->
    res.send 500, err: err

module.exports.removeBook = (req, res) ->
  book_id = req.param('book_id') || req.body?.book?.id
  return res.send 400, err: "Missing Book ID" unless book_id?.length

  User.findOneAndUpdate {_id: req.user.id},
    $pull:
      books: book_id
  .exec()
  .then (user) ->
    res.send 204 #no content
  .then null, (err) ->
    res.send 500, err: err

module.exports.avgValueOverTime = (req, res) ->
  users_books = req.user.books
  creation_date = new Date(req.user._id.getTimestamp())

  # params
  min_value = parseInt req.param('min_value'), 10
  per_book = req.param 'per_book'
  limit_days = req.param 'days'

  match_users_books =
    $match:
      _id:
        $in: users_books

  unwind_prices =
    $unwind: "$prices"

  if min_value
{'fraction_non_alphanumeric': 0.16187919463087247, 'fraction_numerical': 0.013154362416107382, 'mean_word_length': 3.0021482277121376, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, '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': '6119297', 'n_tokens_mistral': 1370, 'n_tokens_neox': 1299, 'n_words': 339}