<commit_before>const express = require('express')
const router = express.Router()
const {
  signup,
  getAllAlbums,
  getUser,
  login,
  getAlbumById,
  getReviewsByAlbumId,
  addReview,
  deleteReviewById,
  getReviewsByUser,
  getRecentReviews
} = require('../db/db')

module.exports = function(app) {

  router.get('/', (req, res) => {
    getRecentReviews()
    .then( reviews => {
      getAllAlbums()
      .then( albums => {
        if( req.cookies.user_id ){
          getUser( req.cookies.user_id )
          .then( response => {
            if( response.success ){
              res.render('index',{
                login: true,
                user: response.user,
                albums: albums,
                reviews: reviews
              })
            }
          })
        } else {
          res.render('index',{
            login: false,
            albums: albums,
            reviews: reviews
          })
        }
      })
    })
  })

  router.get('/login', (req, res) => {
    res.render('login',{
      message: 'Login with your email and password'
    })
  })

  router.post('/login', (req, res) => {
    login( req.body.email, req.body.password )
    .then( response => {
      if( response.success ){
        res.cookie('user_id', response.id )
        res.redirect('/profile/' + response.id)
      } else {
        res.render('login',{
          message: response.message
        })
      }
    })
  })

  router.get('/logout', (req, res) => {
    res.clearCookie('user_id')
    res.redirect('/')
  })

  router.get('/signup', (req, res) => {
    res.render('signup',{
      message: 'Create an account'
    })
  })

  router.post('/signup', (req, res) => {
    let tooShort = []
    if( req.body.name.length < 5 ){
      tooShort.push('username')
    }
    if( req.body.password.length < 5 ){
      tooShort.push('password')
    }
    if( tooShort.length ){
      message = tooShort.join(' and ') + ' too short'
      res.render('signup',{
        message: message
      })
    } else {
      signup(req.body.name, req.body.email, req.body.password)
      .then( response => {
        if( response.success ){
          res.cookie('user_id', response.user.id)
          res.redirect('/profile/' + response.id)
        } else {
          res.render('signup',{
            message: response.message
          })
        }
      })
    }
  })

  router.get('/albums/:albumId', (req, res) => {
    const albumId = req.params.albumId
    getAlbumById( albumId )
    .then( album => {
      getReviewsByAlbumId( albumId )
      .then( reviews => {
        getUser( req.cookies.user_id )
        .then( response => {
          if(response.success){<commit_msg>profile redirect at signup bugfix<commit_after>const express = require('express')
const router = express.Router()
const {
  signup,
  getAllAlbums,
  getUser,
  login,
  getAlbumById,
  getReviewsByAlbumId,
  addReview,
  deleteReviewById,
  getReviewsByUser,
  getRecentReviews
} = require('../db/db')

module.exports = function(app) {

  router.get('/', (req, res) => {
    getRecentReviews()
    .then( reviews => {
      getAllAlbums()
      .then( albums => {
        if( req.cookies.user_id ){
          getUser( req.cookies.user_id )
          .then( response => {
            if( response.success ){
              res.render('index',{
                login: true,
                user: response.user,
                albums: albums,
                reviews: reviews
              })
            }
          })
        } else {
          res.render('index',{
            login: false,
            albums: albums,
            reviews: reviews
          })
        }
      })
    })
  })

  router.get('/login', (req, res) => {
    res.render('login',{
      message: 'Login with your email and password'
    })
  })

  router.post('/login', (req, res) => {
    login( req.body.email, req.body.password )
    .then( response => {
      if( response.success ){
        res.cookie('user_id', response.id )
        res.redirect('/profile/' + response.id)
      } else {
        res.render('login',{
          message: response.message
        })
      }
    })
  })

  router.get('/logout', (req, res) => {
    res.clearCookie('user_id')
    res.redirect('/')
  })

  router.get('/signup', (req, res) => {
    res.render('signup',{
      message: 'Create an account'
    })
  })

  router.post('/signup', (req, res) => {
    let tooShort = []
    if( req.body.name.length < 5 ){
      tooShort.push('username')
    }
    if( req.body.password.length < 5 ){
      tooShort.push('password')
    }
    if( tooShort.length ){
      message = tooShort.join(' and ') + ' too short'
      res.render('signup',{
        message: message
      })
    } else {
      signup(req.body.name, req.body.email, req.body.password)
      .then( response => {
        if( response.success ){
          res.cookie('user_id', response.user.id)
          res.redirect('/profile/' + response.user.id)
        } else {
          res.render('signup',{
            message: response.message
          })
        }
      })
    }
  })

  router.get('/albums/:albumId', (req, res) => {
    const albumId = req.params.albumId
    getAlbumById( albumId )
    .then( album => {
      getReviewsByAlbumId( albumId )
      .then( reviews => {
        getUser( req.cookies.user_id )
        .then( response => {
          if(response.success){
{'fraction_non_alphanumeric': 0.15619788771539744, 'fraction_numerical': 0.0007411524921252547, 'mean_word_length': 1.7853457172342622, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 33, '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': '3342562', 'n_tokens_mistral': 1725, 'n_tokens_neox': 1692, 'n_words': 311}