Advertisement
Guest User

Untitled

a guest
Dec 14th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. module.exports = function(context) {
  2. const Client = context('pgClient');
  3. const configConstructor = context('config');
  4. const config = configConstructor(context);
  5.  
  6. const getClient = () => new Client({
  7. host: config.pgHost,
  8. user: config.pgUser,
  9. password: config.pgPassword,
  10. database: config.pgDatabase,
  11. });
  12.  
  13. const client = getClient();
  14. setTimeout(() =>
  15. client.connect((err) => {
  16. if (err) {
  17. console.log('failed to connect to postgres!');
  18. } else {
  19. console.log('successfully connected to postgres!');
  20. client.query('CREATE TABLE IF NOT EXISTS GameResult (ID SERIAL PRIMARY KEY, Won BOOL NOT NULL, Score INT NOT '
  21. + 'NULL, Total INT NOT NULL, InsertDate TIMESTAMP NOT NULL);', (err) => {
  22. if (err) {
  23. console.log('error creating game result table!');
  24. } else {
  25. console.log('successfully created game result table!');
  26. }
  27. client.end();
  28. });
  29. }
  30. }), 5000);
  31.  
  32. return {
  33. insertResult: (won, score, total, onSuccess, onError) => {
  34. const client = getClient();
  35. client.connect((err) => {
  36. if (err) {
  37. onError(err);
  38. client.end();
  39. } else {
  40. const query = {
  41. text: 'INSERT INTO GameResult(Won, Score, Total, InsertDate) VALUES($1, $2, $3, CURRENT_TIMESTAMP);',
  42. values: [won, score, total],
  43. };
  44. client.query(query, (err) => {
  45. if (err) {
  46. onError(err);
  47. } else {
  48. onSuccess();
  49. }
  50. client.end();
  51. });
  52. }
  53. });
  54. return;
  55. },
  56. // Should call onSuccess with integer.
  57. getTotalNumberOfGames: (onSuccess, onError) => {
  58. onSuccess(0);
  59. // TODO week 3
  60. },
  61. // Should call onSuccess with integer.
  62. getTotalNumberOfWins: (onSuccess, onError) => {
  63. onSuccess(0);
  64. // TODO week 3
  65. },
  66. // Should call onSuccess with integer.
  67. getTotalNumberOf21: (onSuccess, onError) => {
  68. onSuccess(0);
  69. // TODO week 3
  70. },
  71. };
  72. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement