<commit_before>const apiRouter = require('express').Router();

// controller for /matches
const getMatches = require('../db/controllers/getMatchesGivenSelfAndOffset.js');

// controllers for /relationships
const addToRequests = require('../db/controllers/moveMatchIntoRequestsGivenSelfIdAndMatchId.js');
const addToRejects = require('../db/controllers/moveMatchIntoRejectsGivenSelfIdAndMatchId.js');
const changeToRejectFromConnection = require('../db/controllers/moveConnectionIntoRejectsGivenSelfIdAndConnectionId.js');
const changeToConnectionFromRequest = require('../db/controllers/moveRequestIntoConnectionsGivenSelfIdAndRequestId.js');
const changeToRejectFromRequest = require('../db/controllers/moveRequestIntoRejectsGivenSelfIdAndRequestId.js');

// controllers for /profile/:id
const updateDescription = require('../db/controllers/updateDescriptionGivenBodyAndUserId.js');
const updateLanguages = require('../db/controllers/updateLanguagesGivenTypeAndUserId');

// controller for /rooms
const findRoom = require('../db/controllers/findOrCreateRoomGivenUserIds');

// controllers /messages
const getMessages = require('../db/controllers/getMessagesGivenRoomId');
const postMessage = require('../db/controllers/postMessageGivenUserIdAndRoomId');

apiRouter.route('/matches')
  .get((req, res) => { // get matches
    getMatches(req.body.self, req.body.offset)
    .then(matches => {
      res.json(matches);
    })
    .catch(() => {
      res.sendStatus(404);
    });
  });

apiRouter.route('/relationships')
  .post((req, res) => { // post to rejects and requests
    if (req.body.newType === 'request') {
      return addToRequests(req.body.selfId, req.body.matchId)
        .then(() => {
          res.sendStatus(201);
        })
        .catch(() => {
          res.sendStatus(404);
        });
    }
    if (req.body.newType === 'reject') {
      return addToRejects(req.body.selfId, req.body.matchId)
        .then(() => {
          res.sendStatus(201);
        })
        .catch(() => {
          res.sendStatus(404);
        });
    }
    return res.sendStatus(404);
  })
  .put((req, res) => { // change relationship type
    if (req.body.oldType === 'connection') {
      return changeToRejectFromConnection(req.body.selfId, req.body.connectionId)
        .then(() => {
          res.sendStatus(200);
        })
        .catch(() => {
          res.sendStatus(404);
        });
    }
    if (req.body.oldType === 'request') {
      if (req.body.newType === 'connection') {
        return changeToConnectionFromRequest(req.body.selfId, req.body.requestId)
          .then(() => {
            res.sendStatus(200);
          })
          .catch(() => {
            res.sendStatus(404);
          });
      }
      if (req.body.newType === 'reject') {
        return changeToRejectFromRequest(req.body.selfId, req.body.requestId)
          .then(() => {
            res.sendStatus(200);
          })
          .catch(() => {
            res.sendStatus(404);
          });
      }
      return res.sendStatus(404);
    }
    return res.sendStatus(404);
  });

apiRouter.route('/profile/:id')
  .put((req, res) => { // update profile
    updateDescription(req.params.id, req.body.description)
    .then(() => (
      updateLanguages('learn', req.params.id, req.body.languages.willLearn)
    ))
    .then(() => (
      updateLanguages('offer', req.params.id, req.body.languages.canTeach)
    ))
    .then(() => {
      res.sendStatus(200);
    })
    .catch(() => {
      res.sendStatus(404);
    });
  });

apiRouter.route('/rooms')
  .get((req, res) => {
    findRoom(req.body.selfId, req.body.otherId)
      .then(rooms => {
        res.json(rooms[0]);
      })
      .catch(() => {
        res.sendStatus(404);
      });
  });

apiRouter.route('/messages')
  .get((req, res) => {

  })
  .post((req, res) => {

  });<commit_msg>Handle get and post to /api/messages<commit_after>const apiRouter = require('express').Router();

// controller for /matches
const getMatches = require('../db/controllers/getMatchesGivenSelfAndOffset.js');

// controllers for /relationships
const addToRequests = require('../db/controllers/moveMatchIntoRequestsGivenSelfIdAndMatchId.js');
const addToRejects = require('../db/controllers/moveMatchIntoRejectsGivenSelfIdAndMatchId.js');
const changeToRejectFromConnection = require('../db/controllers/moveConnectionIntoRejectsGivenSelfIdAndConnectionId.js');
const changeToConnectionFromRequest = require('../db/controllers/moveRequestIntoConnectionsGivenSelfIdAndRequestId.js');
const changeToRejectFromRequest = require('../db/controllers/moveRequestIntoRejectsGivenSelfIdAndRequestId.js');

// controllers for /profile/:id
const updateDescription = require('../db/controllers/updateDescriptionGivenBodyAndUserId.js');
const updateLanguages = require('../db/controllers/updateLanguagesGivenTypeAndUserId');

// controller for /rooms
const findRoom = require('../db/controllers/findOrCreateRoomGivenUserIds');

// controllers /messages
const getMessages = require('../db/controllers/getMessagesGivenRoomId');
const postMessage = require('../db/controllers/postMessageGivenUserIdAndRoomId');

apiRouter.route('/matches')
  .get((req, res) => { // get matches
    getMatches(req.body.self, req.body.offset)
    .then(matches => {
      res.json(matches);
    })
    .catch(() => {
      res.sendStatus(404);
    });
  });

apiRouter.route('/relationships')
  .post((req, res) => { // post to rejects and requests
    if (req.body.newType === 'request') {
      return addToRequests(req.body.selfId, req.body.matchId)
        .then(() => {
          res.sendStatus(201);
        })
        .catch(() => {
          res.sendStatus(404);
        });
    }
    if (req.body.newType === 'reject') {
      return addToRejects(req.body.selfId, req.body.matchId)
        .then(() => {
          res.sendStatus(201);
        })
        .catch(() => {
          res.sendStatus(404);
        });
    }
    return res.sendStatus(404);
  })
  .put((req, res) => { // change relationship type
    if (req.body.oldType === 'connection') {
      return changeToRejectFromConnection(req.body.selfId, req.body.connectionId)
        .then(() => {
          res.sendStatus(200);
        })
        .catch(() => {
          res.sendStatus(404);
        });
    }
    if (req.body.oldType === 'request') {
      if (req.body.newType === 'connection') {
        return changeToConnectionFromRequest(req.body.selfId, req.body.requestId)
          .then(() => {
            res.sendStatus(200);
          })
          .catch(() => {
            res.sendStatus(404);
          });
      }
      if (req.body.newType === 'reject') {
        return changeToRejectFromRequest(req.body.selfId, req.body.requestId)
          .then(() => {
            res.sendStatus(200);
          })
          .catch(() => {
            res.sendStatus(404);
          });
      }
      return res.sendStatus(404);
    }
    return res.sendStatus(404);
  });

apiRouter.route('/profile/:id')
  .put((req, res) => { // update profile
    updateDescription(req.params.id, req.body.description)
    .then(() => (
      updateLanguages('learn', req.params.id, req.body.languages.willLearn)
    ))
    .then(() => (
      updateLanguages('offer', req.params.id, req.body.languages.canTeach)
    ))
    .then(() => {
      res.sendStatus(200);
    })
    .catch(() => {
      res.sendStatus(404);
    });
  });

apiRouter.route('/rooms')
  .get((req, res) => {
    findRoom(req.body.selfId, req.body.otherId)
      .then(rooms => {
        res.json(rooms[0]);
      })
      .catch(() => {
        res.sendStatus(404);
      });
  });

apiRouter.route('/messages')
  .get((req, res) => {
    getMessages(req.body.roomId)
      .then(messages => {
        res.json(messages);
      })
      .catch(() => {
        res.sendStatus(404);
      });
  })
  .post((req, res) => {
    postMessage(req.body.userId, req.body.roomId, req.body.message)
      .then(() => {
        res.sendStatus(201);
      })
      .catch(() => {
        res.sendStatus(404);
      });
  });
{'fraction_non_alphanumeric': 0.17235812740593567, 'fraction_numerical': 0.014032037749906866, 'mean_word_length': 2.6410488245931285, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 57, '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': '1974516', 'n_tokens_mistral': 2597, 'n_tokens_neox': 2497, 'n_words': 349}