<commit_before>
/**
 * Module dependencies.
 */

var mongoose = require('mongoose');
var Artwork = mongoose.model('Artwork');
var utils = require('../../lib/utils');
var extend = require('util')._extend;

/**
 * Load
 */

exports.load = function(req, res, next, id){
  // var User = mongoose.model('User')
  Artwork.load(id, function (err, artwork) {
    if (err) return next('err');
    if (!artwork) return next(new Error('not found'));
    req.artwork = artwork;
    next();
  })
}

/**
 * List
 */

exports.index = function(req, res){
  var page = (req.param('page') > 0 ? req.param('page') : 1) - 1;
  var perPage = 30;
  var options = {
    perPage: perPage,
    page: page
  }
  Artwork.list(options, function(err, artworks) {
    if (err) return res.render('500');
    Artwork.count().exec(function (err, count) {
      res.render('artworks/index', {
        title: 'Artwork',
        artworks: artworks,
        page: page + 1,
        pages: Math.ceil(count / perPage)
      });
    });
  });
}

/**
 * New artwork
 */

exports.new = function(req, res){
  var artwork = new Artwork({});
  res.render('artworks/new', {
    title: 'New Artwork',
    artwork: artwork,
    mediums: Artwork.getMediums,
    etsyOptions: Artwork.getEtsyOptions,
    soldTo: Artwork.soldToOptions,
    years: Artwork.getYears()
  });
}

/**
 * Create an artwork
 */

exports.create = function (req, res) {
  var artwork = new Artwork(req.body);

  artwork.uploadAndSave(req.files.image, function (err) {
    if (!err) {
      req.flash('success', 'Successfully created artwork!');
      return res.redirect('/artworks/'+artwork._id);
    }

    res.render('artworks/new', {
      title: 'New Artwork',
      artwork: artwork,
      errors: utils.errors(err.errors || err)
    });
  });
}

/**
 * Edit an artwork
 */

exports.edit = function (req, res) {
  res.render('artworks/edit', {
    title: 'Edit ' + req.artwork.title,
    artwork: req.artwork,
    mediums: Artwork.getMediums,
    etsyOptions: Artwork.getEtsyOptions,
    soldTo: Artwork.soldToOptions,
    years: Artwork.getYears()
  });
}

/**
 * Update artwork
 */

exports.update = function(req, res){
  var artwork = req.artwork;
  artwork = extend(artwork, req.body);
  artwork.sold = req.body.sold == undefined ? false : true;
  artwork.availableOnEtsy = req.body.availableOnEtsy == undefined ? false : true;
  artwork.uploadAndSave(req.files.image, function(err) {
    if (!err) {
      return res.redirect('/artworks/');
    }

    res.render('artworks/edit', {
      title: 'Edit Artwork',
      artwork: artwork,
      errors: err.errors
    });
  });
}

/**
 * Show
 */

exports.show = function(req, res){
  res.render('artworks/show', {
    title: req.artwork.title,
    artwork: req.artwork
  })<commit_msg>changed redirect for update to go to list of artworks<commit_after>
/**
 * Module dependencies.
 */

var mongoose = require('mongoose');
var Artwork = mongoose.model('Artwork');
var utils = require('../../lib/utils');
var extend = require('util')._extend;

/**
 * Load
 */

exports.load = function(req, res, next, id){
  // var User = mongoose.model('User')
  Artwork.load(id, function (err, artwork) {
    if (err) return next('err');
    if (!artwork) return next(new Error('not found'));
    req.artwork = artwork;
    next();
  })
}

/**
 * List
 */

exports.index = function(req, res){
  var page = (req.param('page') > 0 ? req.param('page') : 1) - 1;
  var perPage = 30;
  var options = {
    perPage: perPage,
    page: page
  }
  Artwork.list(options, function(err, artworks) {
    if (err) return res.render('500');
    Artwork.count().exec(function (err, count) {
      res.render('artworks/index', {
        title: 'Artwork',
        artworks: artworks,
        page: page + 1,
        pages: Math.ceil(count / perPage)
      });
    });
  });
}

/**
 * New artwork
 */

exports.new = function(req, res){
  var artwork = new Artwork({});
  res.render('artworks/new', {
    title: 'New Artwork',
    artwork: artwork,
    mediums: Artwork.getMediums,
    etsyOptions: Artwork.getEtsyOptions,
    soldTo: Artwork.soldToOptions,
    years: Artwork.getYears()
  });
}

/**
 * Create an artwork
 */

exports.create = function (req, res) {
  var artwork = new Artwork(req.body);

  artwork.uploadAndSave(req.files.image, function (err) {
    if (!err) {
      req.flash('success', 'Successfully created artwork!');
      return res.redirect('/artworks/'+artwork._id);
    }

    res.render('artworks/new', {
      title: 'New Artwork',
      artwork: artwork,
      errors: utils.errors(err.errors || err)
    });
  });
}

/**
 * Edit an artwork
 */

exports.edit = function (req, res) {
  res.render('artworks/edit', {
    title: 'Edit ' + req.artwork.title,
    artwork: req.artwork,
    mediums: Artwork.getMediums,
    etsyOptions: Artwork.getEtsyOptions,
    soldTo: Artwork.soldToOptions,
    years: Artwork.getYears()
  });
}

/**
 * Update artwork
 */

exports.update = function(req, res){
  var artwork = req.artwork;
  artwork = extend(artwork, req.body);
  artwork.sold = req.body.sold == undefined ? false : true;
  artwork.availableOnEtsy = req.body.availableOnEtsy == undefined ? false : true;
  artwork.uploadAndSave(req.files.image, function(err) {
    if (!err) {
      return res.redirect('/artworks/');
    }
    // changed to redirect to list because it was trying to pull in mediums and things
    res.redirect('/artworks/');
    // res.render('artworks/', {
    //   title: 'Edit Artwork',
    //   artwork: artwork,
    //   errors: err.errors
    // });
  });
}

/**
 * Show
 */

exports.show = function(req, res){
  res.render('artworks/show', {
    title: req.artwork.title,
    artwork: req.artwork
  })
{'fraction_non_alphanumeric': 0.1687423259077355, 'fraction_numerical': 0.003157340817400456, 'mean_word_length': 2.9054794520547946, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '212120', 'n_tokens_mistral': 1960, 'n_tokens_neox': 1958, 'n_words': 427}