From 9cc7bf7ae75355235e134d944753b721d480c249 Mon Sep 17 00:00:00 2001 From: Doris-Siu Date: Fri, 17 Feb 2023 14:32:38 +0000 Subject: [PATCH 1/6] Create api folder for working and add .gitignore --- .gitignore | 1 + 2-api/cyf-ecommerce-api/package-lock.json | 529 ++++++++++++++++++++++ 2-api/cyf-ecommerce-api/package.json | 15 + 2-api/cyf-ecommerce-api/server.js | 31 ++ 4 files changed, 576 insertions(+) create mode 100644 .gitignore create mode 100644 2-api/cyf-ecommerce-api/package-lock.json create mode 100644 2-api/cyf-ecommerce-api/package.json create mode 100644 2-api/cyf-ecommerce-api/server.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..3c3629e6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/2-api/cyf-ecommerce-api/package-lock.json b/2-api/cyf-ecommerce-api/package-lock.json new file mode 100644 index 00000000..1a3b302f --- /dev/null +++ b/2-api/cyf-ecommerce-api/package-lock.json @@ -0,0 +1,529 @@ +{ + "name": "cyf-ecommerce-api", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "accepts": { + "version": "1.3.8", + "resolved": "https://registry.npmjs.org/accepts/-/accepts-1.3.8.tgz", + "integrity": "sha512-PYAthTa2m2VKxuvSD3DPC/Gy+U+sOA1LAuT8mkmRuvw+NACSaeXEQ+NHcVF7rONl6qcaxV3Uuemwawk+7+SJLw==", + "requires": { + "mime-types": "~2.1.34", + "negotiator": "0.6.3" + } + }, + "array-flatten": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz", + "integrity": "sha512-PCVAQswWemu6UdxsDFFX/+gVeYqKAod3D3UVm91jHwynguOwAvYPhx8nNlM++NqRcK6CxxpUafjmhIdKiHibqg==" + }, + "body-parser": { + "version": "1.20.1", + "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.1.tgz", + "integrity": "sha512-jWi7abTbYwajOytWCQc37VulmWiRae5RyTpaCyDcS5/lMdtwSz5lOpDE67srw/HYe35f1z3fDQw+3txg7gNtWw==", + "requires": { + "bytes": "3.1.2", + "content-type": "~1.0.4", + "debug": "2.6.9", + "depd": "2.0.0", + "destroy": "1.2.0", + "http-errors": "2.0.0", + "iconv-lite": "0.4.24", + "on-finished": "2.4.1", + "qs": "6.11.0", + "raw-body": "2.5.1", + "type-is": "~1.6.18", + "unpipe": "1.0.0" + } + }, + "buffer-writer": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/buffer-writer/-/buffer-writer-2.0.0.tgz", + "integrity": "sha512-a7ZpuTZU1TRtnwyCNW3I5dc0wWNC3VR9S++Ewyk2HHZdrO3CQJqSpd+95Us590V6AL7JqUAH2IwZ/398PmNFgw==" + }, + "bytes": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.2.tgz", + "integrity": "sha512-/Nf7TyzTx6S3yRJObOAV7956r8cr2+Oj8AC5dt8wSP3BQAoeX58NoHyCU8P8zGkNXStjTSi6fzO6F0pBdcYbEg==" + }, + "call-bind": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/call-bind/-/call-bind-1.0.2.tgz", + "integrity": "sha512-7O+FbCihrB5WGbFYesctwmTKae6rOiIzmz1icreWJ+0aA7LJfuqhEso2T9ncpcFtzMQtzXf2QGGueWJGTYsqrA==", + "requires": { + "function-bind": "^1.1.1", + "get-intrinsic": "^1.0.2" + } + }, + "content-disposition": { + "version": "0.5.4", + "resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-0.5.4.tgz", + "integrity": "sha512-FveZTNuGw04cxlAiWbzi6zTAL/lhehaWbTtgluJh4/E95DqMwTmha3KZN1aAWA8cFIhHzMZUvLevkw5Rqk+tSQ==", + "requires": { + "safe-buffer": "5.2.1" + } + }, + "content-type": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/content-type/-/content-type-1.0.5.tgz", + "integrity": "sha512-nTjqfcBFEipKdXCv4YDQWCfmcLZKm81ldF0pAopTvyrFGVbcR6P/VAAd5G7N+0tTr8QqiU0tFadD6FK4NtJwOA==" + }, + "cookie": { + "version": "0.5.0", + "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.5.0.tgz", + "integrity": "sha512-YZ3GUyn/o8gfKJlnlX7g7xq4gyO6OSuhGPKaaGssGB2qgDUS0gPgtTvoyZLTt9Ab6dC4hfc9dV5arkvc/OCmrw==" + }, + "cookie-signature": { + "version": "1.0.6", + "resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz", + "integrity": "sha512-QADzlaHc8icV8I7vbaJXJwod9HWYp8uCqf1xa4OfNu1T7JVxQIrUgOWtHdNDtPiywmFbiS12VjotIXLrKM3orQ==" + }, + "debug": { + "version": "2.6.9", + "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", + "integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==", + "requires": { + "ms": "2.0.0" + } + }, + "depd": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/depd/-/depd-2.0.0.tgz", + "integrity": "sha512-g7nH6P6dyDioJogAAGprGpCtVImJhpPk/roCzdb3fIh61/s/nPsfR6onyMwkCAR/OlC3yBC0lESvUoQEAssIrw==" + }, + "destroy": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/destroy/-/destroy-1.2.0.tgz", + "integrity": "sha512-2sJGJTaXIIaR1w4iJSNoN0hnMY7Gpc/n8D4qSCJw8QqFWXf7cuAgnEHxBpweaVcPevC2l3KpjYCx3NypQQgaJg==" + }, + "ee-first": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", + "integrity": "sha512-WMwm9LhRUo+WUaRN+vRuETqG89IgZphVSNkdFgeb6sS/E4OrDIN7t48CAewSHXc6C8lefD8KKfr5vY61brQlow==" + }, + "encodeurl": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/encodeurl/-/encodeurl-1.0.2.tgz", + "integrity": "sha512-TPJXq8JqFaVYm2CWmPvnP2Iyo4ZSM7/QKcSmuMLDObfpH5fi7RUGmd/rTDf+rut/saiDiQEeVTNgAmJEdAOx0w==" + }, + "escape-html": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/escape-html/-/escape-html-1.0.3.tgz", + "integrity": "sha512-NiSupZ4OeuGwr68lGIeym/ksIZMJodUGOSCZ/FSnTxcrekbvqrgdUxlJOMpijaKZVjAJrWrGs/6Jy8OMuyj9ow==" + }, + "etag": { + "version": "1.8.1", + "resolved": "https://registry.npmjs.org/etag/-/etag-1.8.1.tgz", + "integrity": "sha512-aIL5Fx7mawVa300al2BnEE4iNvo1qETxLrPI/o05L7z6go7fCw1J6EQmbK4FmJ2AS7kgVF/KEZWufBfdClMcPg==" + }, + "express": { + "version": "4.18.2", + "resolved": "https://registry.npmjs.org/express/-/express-4.18.2.tgz", + "integrity": "sha512-5/PsL6iGPdfQ/lKM1UuielYgv3BUoJfz1aUwU9vHZ+J7gyvwdQXFEBIEIaxeGf0GIcreATNyBExtalisDbuMqQ==", + "requires": { + "accepts": "~1.3.8", + "array-flatten": "1.1.1", + "body-parser": "1.20.1", + "content-disposition": "0.5.4", + "content-type": "~1.0.4", + "cookie": "0.5.0", + "cookie-signature": "1.0.6", + "debug": "2.6.9", + "depd": "2.0.0", + "encodeurl": "~1.0.2", + "escape-html": "~1.0.3", + "etag": "~1.8.1", + "finalhandler": "1.2.0", + "fresh": "0.5.2", + "http-errors": "2.0.0", + "merge-descriptors": "1.0.1", + "methods": "~1.1.2", + "on-finished": "2.4.1", + "parseurl": "~1.3.3", + "path-to-regexp": "0.1.7", + "proxy-addr": "~2.0.7", + "qs": "6.11.0", + "range-parser": "~1.2.1", + "safe-buffer": "5.2.1", + "send": "0.18.0", + "serve-static": "1.15.0", + "setprototypeof": "1.2.0", + "statuses": "2.0.1", + "type-is": "~1.6.18", + "utils-merge": "1.0.1", + "vary": "~1.1.2" + } + }, + "finalhandler": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/finalhandler/-/finalhandler-1.2.0.tgz", + "integrity": "sha512-5uXcUVftlQMFnWC9qu/svkWv3GTd2PfUhK/3PLkYNAe7FbqJMt3515HaxE6eRL74GdsriiwujiawdaB1BpEISg==", + "requires": { + "debug": "2.6.9", + "encodeurl": "~1.0.2", + "escape-html": "~1.0.3", + "on-finished": "2.4.1", + "parseurl": "~1.3.3", + "statuses": "2.0.1", + "unpipe": "~1.0.0" + } + }, + "forwarded": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.2.0.tgz", + "integrity": "sha512-buRG0fpBtRHSTCOASe6hD258tEubFoRLb4ZNA6NxMVHNw2gOcwHo9wyablzMzOA5z9xA9L1KNjk/Nt6MT9aYow==" + }, + "fresh": { + "version": "0.5.2", + "resolved": "https://registry.npmjs.org/fresh/-/fresh-0.5.2.tgz", + "integrity": "sha512-zJ2mQYM18rEFOudeV4GShTGIQ7RbzA7ozbU9I/XBpm7kqgMywgmylMwXHxZJmkVoYkna9d2pVXVXPdYTP9ej8Q==" + }, + "function-bind": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.1.tgz", + "integrity": "sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A==" + }, + "get-intrinsic": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/get-intrinsic/-/get-intrinsic-1.2.0.tgz", + "integrity": "sha512-L049y6nFOuom5wGyRc3/gdTLO94dySVKRACj1RmJZBQXlbTMhtNIgkWkUHq+jYmZvKf14EW1EoJnnjbmoHij0Q==", + "requires": { + "function-bind": "^1.1.1", + "has": "^1.0.3", + "has-symbols": "^1.0.3" + } + }, + "has": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/has/-/has-1.0.3.tgz", + "integrity": "sha512-f2dvO0VU6Oej7RkWJGrehjbzMAjFp5/VKPp5tTpWIV4JHHZK1/BxbFRtf/siA2SWTe09caDmVtYYzWEIbBS4zw==", + "requires": { + "function-bind": "^1.1.1" + } + }, + "has-symbols": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/has-symbols/-/has-symbols-1.0.3.tgz", + "integrity": "sha512-l3LCuF6MgDNwTDKkdYGEihYjt5pRPbEg46rtlmnSPlUbgmB8LOIrKJbYYFBSbnPaJexMKtiPO8hmeRjRz2Td+A==" + }, + "http-errors": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/http-errors/-/http-errors-2.0.0.tgz", + "integrity": "sha512-FtwrG/euBzaEjYeRqOgly7G0qviiXoJWnvEH2Z1plBdXgbyjv34pHTSb9zoeHMyDy33+DWy5Wt9Wo+TURtOYSQ==", + "requires": { + "depd": "2.0.0", + "inherits": "2.0.4", + "setprototypeof": "1.2.0", + "statuses": "2.0.1", + "toidentifier": "1.0.1" + } + }, + "iconv-lite": { + "version": "0.4.24", + "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz", + "integrity": "sha512-v3MXnZAcvnywkTUEZomIActle7RXXeedOR31wwl7VlyoXO4Qi9arvSenNQWne1TcRwhCL1HwLI21bEqdpj8/rA==", + "requires": { + "safer-buffer": ">= 2.1.2 < 3" + } + }, + "inherits": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", + "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==" + }, + "ipaddr.js": { + "version": "1.9.1", + "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.1.tgz", + "integrity": "sha512-0KI/607xoxSToH7GjN1FfSbLoU0+btTicjsQSWQlh/hZykN8KpmMf7uYwPW3R+akZ6R/w18ZlXSHBYXiYUPO3g==" + }, + "media-typer": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", + "integrity": "sha512-dq+qelQ9akHpcOl/gUVRTxVIOkAJ1wR3QAvb4RsVjS8oVoFjDGTc679wJYmUmknUF5HwMLOgb5O+a3KxfWapPQ==" + }, + "merge-descriptors": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-1.0.1.tgz", + "integrity": "sha512-cCi6g3/Zr1iqQi6ySbseM1Xvooa98N0w31jzUYrXPX2xqObmFGHJ0tQ5u74H3mVh7wLouTseZyYIq39g8cNp1w==" + }, + "methods": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/methods/-/methods-1.1.2.tgz", + "integrity": "sha512-iclAHeNqNm68zFtnZ0e+1L2yUIdvzNoauKU4WBA3VvH/vPFieF7qfRlwUZU+DA9P9bPXIS90ulxoUoCH23sV2w==" + }, + "mime": { + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/mime/-/mime-1.6.0.tgz", + "integrity": "sha512-x0Vn8spI+wuJ1O6S7gnbaQg8Pxh4NNHb7KSINmEWKiPE4RKOplvijn+NkmYmmRgP68mc70j2EbeTFRsrswaQeg==" + }, + "mime-db": { + "version": "1.52.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", + "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==" + }, + "mime-types": { + "version": "2.1.35", + "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz", + "integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==", + "requires": { + "mime-db": "1.52.0" + } + }, + "ms": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", + "integrity": "sha512-Tpp60P6IUJDTuOq/5Z8cdskzJujfwqfOTkrwIwj7IRISpnkJnT6SyJ4PCPnGMoFjC9ddhal5KVIYtAt97ix05A==" + }, + "negotiator": { + "version": "0.6.3", + "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.3.tgz", + "integrity": "sha512-+EUsqGPLsM+j/zdChZjsnX51g4XrHFOIXwfnCVPGlQk/k5giakcKsuxCObBRu6DSm9opw/O6slWbJdghQM4bBg==" + }, + "object-inspect": { + "version": "1.12.3", + "resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.12.3.tgz", + "integrity": "sha512-geUvdk7c+eizMNUDkRpW1wJwgfOiOeHbxBR/hLXK1aT6zmVSO0jsQcs7fj6MGw89jC/cjGfLcNOrtMYtGqm81g==" + }, + "on-finished": { + "version": "2.4.1", + "resolved": "https://registry.npmjs.org/on-finished/-/on-finished-2.4.1.tgz", + "integrity": "sha512-oVlzkg3ENAhCk2zdv7IJwd/QUD4z2RxRwpkcGY8psCVcCYZNq4wYnVWALHM+brtuJjePWiYF/ClmuDr8Ch5+kg==", + "requires": { + "ee-first": "1.1.1" + } + }, + "packet-reader": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/packet-reader/-/packet-reader-1.0.0.tgz", + "integrity": "sha512-HAKu/fG3HpHFO0AA8WE8q2g+gBJaZ9MG7fcKk+IJPLTGAD6Psw4443l+9DGRbOIh3/aXr7Phy0TjilYivJo5XQ==" + }, + "parseurl": { + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.3.tgz", + "integrity": "sha512-CiyeOxFT/JZyN5m0z9PfXw4SCBJ6Sygz1Dpl0wqjlhDEGGBP1GnsUVEL0p63hoG1fcj3fHynXi9NYO4nWOL+qQ==" + }, + "path-to-regexp": { + "version": "0.1.7", + "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.7.tgz", + "integrity": "sha512-5DFkuoqlv1uYQKxy8omFBeJPQcdoE07Kv2sferDCrAq1ohOU+MSDswDIbnx3YAM60qIOnYa53wBhXW0EbMonrQ==" + }, + "pg": { + "version": "8.9.0", + "resolved": "https://registry.npmjs.org/pg/-/pg-8.9.0.tgz", + "integrity": "sha512-ZJM+qkEbtOHRuXjmvBtOgNOXOtLSbxiMiUVMgE4rV6Zwocy03RicCVvDXgx8l4Biwo8/qORUnEqn2fdQzV7KCg==", + "requires": { + "buffer-writer": "2.0.0", + "packet-reader": "1.0.0", + "pg-connection-string": "^2.5.0", + "pg-pool": "^3.5.2", + "pg-protocol": "^1.6.0", + "pg-types": "^2.1.0", + "pgpass": "1.x" + } + }, + "pg-connection-string": { + "version": "2.5.0", + "resolved": "https://registry.npmjs.org/pg-connection-string/-/pg-connection-string-2.5.0.tgz", + "integrity": "sha512-r5o/V/ORTA6TmUnyWZR9nCj1klXCO2CEKNRlVuJptZe85QuhFayC7WeMic7ndayT5IRIR0S0xFxFi2ousartlQ==" + }, + "pg-int8": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/pg-int8/-/pg-int8-1.0.1.tgz", + "integrity": "sha512-WCtabS6t3c8SkpDBUlb1kjOs7l66xsGdKpIPZsg4wR+B3+u9UAum2odSsF9tnvxg80h4ZxLWMy4pRjOsFIqQpw==" + }, + "pg-pool": { + "version": "3.5.2", + "resolved": "https://registry.npmjs.org/pg-pool/-/pg-pool-3.5.2.tgz", + "integrity": "sha512-His3Fh17Z4eg7oANLob6ZvH8xIVen3phEZh2QuyrIl4dQSDVEabNducv6ysROKpDNPSD+12tONZVWfSgMvDD9w==" + }, + "pg-protocol": { + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/pg-protocol/-/pg-protocol-1.6.0.tgz", + "integrity": "sha512-M+PDm637OY5WM307051+bsDia5Xej6d9IR4GwJse1qA1DIhiKlksvrneZOYQq42OM+spubpcNYEo2FcKQrDk+Q==" + }, + "pg-types": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/pg-types/-/pg-types-2.2.0.tgz", + "integrity": "sha512-qTAAlrEsl8s4OiEQY69wDvcMIdQN6wdz5ojQiOy6YRMuynxenON0O5oCpJI6lshc6scgAY8qvJ2On/p+CXY0GA==", + "requires": { + "pg-int8": "1.0.1", + "postgres-array": "~2.0.0", + "postgres-bytea": "~1.0.0", + "postgres-date": "~1.0.4", + "postgres-interval": "^1.1.0" + } + }, + "pgpass": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/pgpass/-/pgpass-1.0.5.tgz", + "integrity": "sha512-FdW9r/jQZhSeohs1Z3sI1yxFQNFvMcnmfuj4WBMUTxOrAyLMaTcE1aAMBiTlbMNaXvBCQuVi0R7hd8udDSP7ug==", + "requires": { + "split2": "^4.1.0" + } + }, + "postgres-array": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/postgres-array/-/postgres-array-2.0.0.tgz", + "integrity": "sha512-VpZrUqU5A69eQyW2c5CA1jtLecCsN2U/bD6VilrFDWq5+5UIEVO7nazS3TEcHf1zuPYO/sqGvUvW62g86RXZuA==" + }, + "postgres-bytea": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/postgres-bytea/-/postgres-bytea-1.0.0.tgz", + "integrity": "sha512-xy3pmLuQqRBZBXDULy7KbaitYqLcmxigw14Q5sj8QBVLqEwXfeybIKVWiqAXTlcvdvb0+xkOtDbfQMOf4lST1w==" + }, + "postgres-date": { + "version": "1.0.7", + "resolved": "https://registry.npmjs.org/postgres-date/-/postgres-date-1.0.7.tgz", + "integrity": "sha512-suDmjLVQg78nMK2UZ454hAG+OAW+HQPZ6n++TNDUX+L0+uUlLywnoxJKDou51Zm+zTCjrCl0Nq6J9C5hP9vK/Q==" + }, + "postgres-interval": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/postgres-interval/-/postgres-interval-1.2.0.tgz", + "integrity": "sha512-9ZhXKM/rw350N1ovuWHbGxnGh/SNJ4cnxHiM0rxE4VN41wsg8P8zWn9hv/buK00RP4WvlOyr/RBDiptyxVbkZQ==", + "requires": { + "xtend": "^4.0.0" + } + }, + "proxy-addr": { + "version": "2.0.7", + "resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.7.tgz", + "integrity": "sha512-llQsMLSUDUPT44jdrU/O37qlnifitDP+ZwrmmZcoSKyLKvtZxpyV0n2/bD/N4tBAAZ/gJEdZU7KMraoK1+XYAg==", + "requires": { + "forwarded": "0.2.0", + "ipaddr.js": "1.9.1" + } + }, + "qs": { + "version": "6.11.0", + "resolved": "https://registry.npmjs.org/qs/-/qs-6.11.0.tgz", + "integrity": "sha512-MvjoMCJwEarSbUYk5O+nmoSzSutSsTwF85zcHPQ9OrlFoZOYIjaqBAJIqIXjptyD5vThxGq52Xu/MaJzRkIk4Q==", + "requires": { + "side-channel": "^1.0.4" + } + }, + "range-parser": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.2.1.tgz", + "integrity": "sha512-Hrgsx+orqoygnmhFbKaHE6c296J+HTAQXoxEF6gNupROmmGJRoyzfG3ccAveqCBrwr/2yxQ5BVd/GTl5agOwSg==" + }, + "raw-body": { + "version": "2.5.1", + "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.5.1.tgz", + "integrity": "sha512-qqJBtEyVgS0ZmPGdCFPWJ3FreoqvG4MVQln/kCgF7Olq95IbOp0/BWyMwbdtn4VTvkM8Y7khCQ2Xgk/tcrCXig==", + "requires": { + "bytes": "3.1.2", + "http-errors": "2.0.0", + "iconv-lite": "0.4.24", + "unpipe": "1.0.0" + } + }, + "safe-buffer": { + "version": "5.2.1", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz", + "integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==" + }, + "safer-buffer": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", + "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==" + }, + "send": { + "version": "0.18.0", + "resolved": "https://registry.npmjs.org/send/-/send-0.18.0.tgz", + "integrity": "sha512-qqWzuOjSFOuqPjFe4NOsMLafToQQwBSOEpS+FwEt3A2V3vKubTquT3vmLTQpFgMXp8AlFWFuP1qKaJZOtPpVXg==", + "requires": { + "debug": "2.6.9", + "depd": "2.0.0", + "destroy": "1.2.0", + "encodeurl": "~1.0.2", + "escape-html": "~1.0.3", + "etag": "~1.8.1", + "fresh": "0.5.2", + "http-errors": "2.0.0", + "mime": "1.6.0", + "ms": "2.1.3", + "on-finished": "2.4.1", + "range-parser": "~1.2.1", + "statuses": "2.0.1" + }, + "dependencies": { + "ms": { + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", + "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==" + } + } + }, + "serve-static": { + "version": "1.15.0", + "resolved": "https://registry.npmjs.org/serve-static/-/serve-static-1.15.0.tgz", + "integrity": "sha512-XGuRDNjXUijsUL0vl6nSD7cwURuzEgglbOaFuZM9g3kwDXOWVTck0jLzjPzGD+TazWbboZYu52/9/XPdUgne9g==", + "requires": { + "encodeurl": "~1.0.2", + "escape-html": "~1.0.3", + "parseurl": "~1.3.3", + "send": "0.18.0" + } + }, + "setprototypeof": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.2.0.tgz", + "integrity": "sha512-E5LDX7Wrp85Kil5bhZv46j8jOeboKq5JMmYM3gVGdGH8xFpPWXUMsNrlODCrkoxMEeNi/XZIwuRvY4XNwYMJpw==" + }, + "side-channel": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/side-channel/-/side-channel-1.0.4.tgz", + "integrity": "sha512-q5XPytqFEIKHkGdiMIrY10mvLRvnQh42/+GoBlFW3b2LXLE2xxJpZFdm94we0BaoV3RwJyGqg5wS7epxTv0Zvw==", + "requires": { + "call-bind": "^1.0.0", + "get-intrinsic": "^1.0.2", + "object-inspect": "^1.9.0" + } + }, + "split2": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/split2/-/split2-4.1.0.tgz", + "integrity": "sha512-VBiJxFkxiXRlUIeyMQi8s4hgvKCSjtknJv/LVYbrgALPwf5zSKmEwV9Lst25AkvMDnvxODugjdl6KZgwKM1WYQ==" + }, + "statuses": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/statuses/-/statuses-2.0.1.tgz", + "integrity": "sha512-RwNA9Z/7PrK06rYLIzFMlaF+l73iwpzsqRIFgbMLbTcLD6cOao82TaWefPXQvB2fOC4AjuYSEndS7N/mTCbkdQ==" + }, + "toidentifier": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/toidentifier/-/toidentifier-1.0.1.tgz", + "integrity": "sha512-o5sSPKEkg/DIQNmH43V0/uerLrpzVedkUh8tGNvaeXpfpuwjKenlSox/2O/BTlZUtEe+JG7s5YhEz608PlAHRA==" + }, + "type-is": { + "version": "1.6.18", + "resolved": "https://registry.npmjs.org/type-is/-/type-is-1.6.18.tgz", + "integrity": "sha512-TkRKr9sUTxEH8MdfuCSP7VizJyzRNMjj2J2do2Jr3Kym598JVdEksuzPQCnlFPW4ky9Q+iA+ma9BGm06XQBy8g==", + "requires": { + "media-typer": "0.3.0", + "mime-types": "~2.1.24" + } + }, + "unpipe": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz", + "integrity": "sha512-pjy2bYhSsufwWlKwPc+l3cN7+wuJlK6uz0YdJEOlQDbl6jo/YlPi4mb8agUkVC8BF7V8NuzeyPNqRksA3hztKQ==" + }, + "utils-merge": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz", + "integrity": "sha512-pMZTvIkT1d+TFGvDOqodOclx0QWkkgi6Tdoa8gC8ffGAAqz9pzPTZWAybbsHHoED/ztMtkv/VoYTYyShUn81hA==" + }, + "vary": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz", + "integrity": "sha512-BNGbWLfd0eUPabhkXUVm0j8uuvREyTh5ovRa/dyow/BqAbZJyC+5fU+IzQOzmAKzYqYRAISoRhdQr3eIZ/PXqg==" + }, + "xtend": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz", + "integrity": "sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ==" + } + } +} diff --git a/2-api/cyf-ecommerce-api/package.json b/2-api/cyf-ecommerce-api/package.json new file mode 100644 index 00000000..2a2ec166 --- /dev/null +++ b/2-api/cyf-ecommerce-api/package.json @@ -0,0 +1,15 @@ +{ + "name": "cyf-ecommerce-api", + "version": "1.0.0", + "description": "", + "main": "server.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "dependencies": { + "express": "^4.18.2", + "pg": "^8.9.0" + } +} diff --git a/2-api/cyf-ecommerce-api/server.js b/2-api/cyf-ecommerce-api/server.js new file mode 100644 index 00000000..0eb068ee --- /dev/null +++ b/2-api/cyf-ecommerce-api/server.js @@ -0,0 +1,31 @@ +const express = require("express"); +const app = express(); + +const { Pool } = require("pg"); + +const db = new Pool({ + user: "codeyourfuture", // replace with you username + host: "localhost", + database: "cyf_ecommerce", + password: "cyf123", + port: 5432, +}); + +app.get("/customers", function (req, res) { + db.query("SELECT * FROM customers", (error, result) => { + res.json(result.rows); + }); +}); + +app.get("/suppliers", function (req, res) { + db.query( + "select distinct s.supplier_name from customers c join orders o on (c.id = o.customer_id) join order_items oi on (o.id = oi.order_id) join product_availability pa on (oi.product_id = pa.prod_id) join products p on (pa.prod_id = p.id)join suppliers s on (s.id = pa.supp_id)", + (error, result) => { + res.json(result.rows); + } + ); +}); + +app.listen(3000, function () { + console.log("Server is listening on port 3000. Ready to accept requests!"); +}); From 97b410356d64c7c92f23d36af2858d8b1f96c241 Mon Sep 17 00:00:00 2001 From: Doris-Siu Date: Fri, 17 Feb 2023 20:39:38 +0000 Subject: [PATCH 2/6] Basic CRUD tasks --- 2-api/cyf-ecommerce-api/package.json | 1 + 2-api/cyf-ecommerce-api/server.js | 93 +++++++++++++++++++++++++++- 2 files changed, 92 insertions(+), 2 deletions(-) diff --git a/2-api/cyf-ecommerce-api/package.json b/2-api/cyf-ecommerce-api/package.json index 2a2ec166..4717778d 100644 --- a/2-api/cyf-ecommerce-api/package.json +++ b/2-api/cyf-ecommerce-api/package.json @@ -9,6 +9,7 @@ "author": "", "license": "ISC", "dependencies": { + "body-parser": "^1.20.1", "express": "^4.18.2", "pg": "^8.9.0" } diff --git a/2-api/cyf-ecommerce-api/server.js b/2-api/cyf-ecommerce-api/server.js index 0eb068ee..f7be7e8a 100644 --- a/2-api/cyf-ecommerce-api/server.js +++ b/2-api/cyf-ecommerce-api/server.js @@ -1,8 +1,11 @@ const express = require("express"); +const bodyParser = require("body-parser"); const app = express(); const { Pool } = require("pg"); +app.use(bodyParser.json()); + const db = new Pool({ user: "codeyourfuture", // replace with you username host: "localhost", @@ -10,6 +13,7 @@ const db = new Pool({ password: "cyf123", port: 5432, }); +// GET app.get("/customers", function (req, res) { db.query("SELECT * FROM customers", (error, result) => { @@ -17,6 +21,16 @@ app.get("/customers", function (req, res) { }); }); +app.get("/customers/:customerId", function (req, res) { + db.query( + "SELECT * FROM customers where id = $1", + [parseInt(req.params.customerId)], + (error, result) => { + res.json(result.rows); + } + ); +}); + app.get("/suppliers", function (req, res) { db.query( "select distinct s.supplier_name from customers c join orders o on (c.id = o.customer_id) join order_items oi on (o.id = oi.order_id) join product_availability pa on (oi.product_id = pa.prod_id) join products p on (pa.prod_id = p.id)join suppliers s on (s.id = pa.supp_id)", @@ -26,6 +40,81 @@ app.get("/suppliers", function (req, res) { ); }); -app.listen(3000, function () { - console.log("Server is listening on port 3000. Ready to accept requests!"); +app.get("/products", function (req, res) { + let queryParams = []; + let sql = + "select p.product_name, pa.unit_price, s.supplier_name from product_availability pa join products p on (pa.prod_id = p.id) join suppliers s on (s.id = pa.supp_id)"; + if (req.query.name) { + sql += + " where p.product_name ilike $1 || '%' or p.product_name ilike '%' || $1 or p.product_name ilike '%' || $1 || '%'"; + queryParams.push(req.query.name); + } + + db.query(sql, queryParams, (error, result) => { + res.json(result.rows); + }); +}); + +//POST + +app.post("/customers", function (req, res) { + const newName = req.body.name; + const newAdd = req.body.address; + const newCity = req.body.city; + const newCountry = req.body.country; + + const query = + "INSERT INTO customers (name, address, city, country) " + + "VALUES ($1, $2, $3, $4)" + + "RETURNING id"; + + db.query(query, [newName, newAdd, newCity, newCountry], (err, result) => { + res.send(`Customer ${result.rows[0].id} created.`); + }); +}); + +app.post("/products", function (req, res) { + const newProduct = req.body.name; + + const query = + "INSERT INTO products (product_name) " + "VALUES ($1)" + "RETURNING id"; + + db.query(query, [newProduct], (err, result) => { + res.send(`Product ${result.rows[0].id} created.`); + }); +}); + +//PUT +app.put("/customers/:customerId", function (req, res) { + const custId = req.params.customerId; + const newName = req.body.name; + const newAdd = req.body.address; + const newCity = req.body.city; + const newCountry = req.body.country; + + db.query( + "UPDATE customers SET name=$2, address = $3, city = $4, country = $5 WHERE id=$1", + [custId, newName, newAdd, newCity, newCountry] + ) + .then(() => res.send(`Customer ${custId} updated!`)) + .catch((err) => { + res.status(500).json({ error: err }); + }); +}); + +//DELETE +app.delete("/orders/:orderId", function (req, res) { + const Id = req.params.orderId; + + db.query("DELETE FROM order_items WHERE order_id=$1", [Id]) + .then(() => { + db.query("DELETE FROM orders WHERE id=$1", [Id]) + .then(() => res.send(`Order ${Id} deleted!`)) + .catch((e) => console.error(e)); + }) + .catch((e) => console.error(e)); +}); + +app.listen(3100, function () { + console.log("Server is listening on port 3100. Ready to accept requests!"); }); From 05b2c1afa0c13062a2a8eb7698becd4464e51ac2 Mon Sep 17 00:00:00 2001 From: Doris-Siu Date: Sat, 18 Feb 2023 12:17:06 +0000 Subject: [PATCH 3/6] Create validatingEx.js for syllabus exercises --- 2-api/cyf-ecommerce-api/remains | 7 +++++++ 2-api/cyf-ecommerce-api/validatingEx.js | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 2-api/cyf-ecommerce-api/remains create mode 100644 2-api/cyf-ecommerce-api/validatingEx.js diff --git a/2-api/cyf-ecommerce-api/remains b/2-api/cyf-ecommerce-api/remains new file mode 100644 index 00000000..718d7efe --- /dev/null +++ b/2-api/cyf-ecommerce-api/remains @@ -0,0 +1,7 @@ +Add a new POST endpoint /availability to create a new product availability (with a price and a supplier id). Check that the price is a positive integer and that both the product and supplier ID's exist in the database, otherwise return an error. + +Add a new POST endpoint /customers/:customerId/orders to create a new order (including an order date, and an order reference) for a customer. Check that the customerId corresponds to an existing customer or return an error. + +Add a new DELETE endpoint /customers/:customerId to delete an existing customer only if this customer doesn't have orders. + +Add a new GET endpoint /customers/:customerId/orders to load all the orders along with the items in the orders of a specific customer. Especially, the following information should be returned: order references, order dates, product names, unit prices, suppliers and quantities. diff --git a/2-api/cyf-ecommerce-api/validatingEx.js b/2-api/cyf-ecommerce-api/validatingEx.js new file mode 100644 index 00000000..02a0fd14 --- /dev/null +++ b/2-api/cyf-ecommerce-api/validatingEx.js @@ -0,0 +1,19 @@ +const express = require("express"); +const bodyParser = require("body-parser"); +const app = express(); + +const { Pool } = require("pg"); + +app.use(bodyParser.json()); + +const db = new Pool({ + user: "codeyourfuture", // replace with you username + host: "localhost", + database: "cyf_hotel", + password: "cyf123", + port: 5432, +}); + +app.listen(3110, function () { + console.log("Server is listening on port 3110. Ready to accept requests!"); +}); From 5a5648518da547dd752fb2e3701caedcd81780fd Mon Sep 17 00:00:00 2001 From: Doris-Siu Date: Sat, 18 Feb 2023 13:29:13 +0000 Subject: [PATCH 4/6] Complete syllabus exercises 3,4 - ValidatingEx.js --- 2-api/cyf-ecommerce-api/validatingEx.js | 59 +++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/2-api/cyf-ecommerce-api/validatingEx.js b/2-api/cyf-ecommerce-api/validatingEx.js index 02a0fd14..d328e589 100644 --- a/2-api/cyf-ecommerce-api/validatingEx.js +++ b/2-api/cyf-ecommerce-api/validatingEx.js @@ -14,6 +14,65 @@ const db = new Pool({ port: 5432, }); +// GET + +app.get("/customers", function (req, res) { + db.query("SELECT * FROM customers", (error, result) => { + res.json(result.rows); + }); +}); + +// Exercise 3 Q.2 and 3 +// Exercise 4 +app.post("/customers", function (req, res) { + const newName = req.body.name; + const newEmail = req.body.email; + const newPhone = req.body.phone; + const newAdd = req.body.address; + const newCity = req.body.city; + const newCode = req.body.postcode; + const newCountry = req.body.country; + + const query = + "INSERT INTO customers (name, email, phone, address, city, postcode, country) " + + "VALUES ($1, $2, $3, $4, $5, $6, $7) " + + "RETURNING id"; + + // First validate the phone number using a regular expression to check for valid characters + if ( + newPhone.replace(/[+\-()0-9 ]/g, "0") != // replace all valid chars with 0 + "0".padEnd(newPhone.length, "0") + ) { + // compare with all zeros same length as phone + return res + .status(400) + .send("The phone number must only contain 0-9, +, -, (, ) or space."); + } + + // Validate the new customer's email address by querying the existing customers table + // to return any rows that contain the same values + db.query( + "SELECT 1 FROM customers WHERE email=$1", + [newEmail], + (err, result) => { + if (result.rowCount > 0) { + // note the use of result.rowCount + return res + .status(400) + .send("A customer with that email address already exists!"); + } else { + db.query( + query, + [newName, newEmail, newPhone, newAdd, newCity, newCode, newCountry], + (err, result) => { + res.status(201).send(`Customer ${result.rows[0].id} created.`); + } + ); + } + } + ); +}); + app.listen(3110, function () { console.log("Server is listening on port 3110. Ready to accept requests!"); }); From 5ca7547d094f343120f59a7b70ba7eecef067dd2 Mon Sep 17 00:00:00 2001 From: Doris-Siu Date: Sat, 18 Feb 2023 20:22:13 +0000 Subject: [PATCH 5/6] Add a new POST endpoint /availability --- 2-api/cyf-ecommerce-api/remains | 2 -- 2-api/cyf-ecommerce-api/server.js | 36 +++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/2-api/cyf-ecommerce-api/remains b/2-api/cyf-ecommerce-api/remains index 718d7efe..13b027f0 100644 --- a/2-api/cyf-ecommerce-api/remains +++ b/2-api/cyf-ecommerce-api/remains @@ -1,5 +1,3 @@ -Add a new POST endpoint /availability to create a new product availability (with a price and a supplier id). Check that the price is a positive integer and that both the product and supplier ID's exist in the database, otherwise return an error. - Add a new POST endpoint /customers/:customerId/orders to create a new order (including an order date, and an order reference) for a customer. Check that the customerId corresponds to an existing customer or return an error. Add a new DELETE endpoint /customers/:customerId to delete an existing customer only if this customer doesn't have orders. diff --git a/2-api/cyf-ecommerce-api/server.js b/2-api/cyf-ecommerce-api/server.js index f7be7e8a..53c133a5 100644 --- a/2-api/cyf-ecommerce-api/server.js +++ b/2-api/cyf-ecommerce-api/server.js @@ -3,6 +3,7 @@ const bodyParser = require("body-parser"); const app = express(); const { Pool } = require("pg"); +const { query } = require("express"); app.use(bodyParser.json()); @@ -56,6 +57,41 @@ app.get("/products", function (req, res) { }); //POST +app.post("/availability", function (req, res) { + const prodId = req.body.prodId; + const suppId = req.body.suppId; + const price = req.body.price; + + if (price < 0 || typeof price !== "number") { + return res.status(400).send("Error: the price is not a positive number."); + } + + const subQuery1 = "select 1 from product_availability where prod_id = $1"; + const subQuery2 = "select 1 from product_availability where supp_id= $1"; + const query = + "INSERT INTO product_availability (prod_id, supp_id, unit_price) " + + "VALUES ($1, $2, $3) "; + + db.query(subQuery1, [prodId], (err, result) => { + if (result.rowCount <= 0) { + return res + .status(400) + .send("Error: the product Id is not exist in the database."); + } else { + db.query(subQuery2, [suppId], (err, result) => { + if (result.rowCount <= 0) { + return res + .status(400) + .send("Error: the supplier Id is not exist in the database."); + } else { + db.query(query, [prodId, suppId, price], (err, result) => { + res.send(`Product ${prodId} created.`); + }); + } + }); + } + }); +}); app.post("/customers", function (req, res) { const newName = req.body.name; From 41a5169c9d2b4d482aa7b2b46305bf2a5fcb877e Mon Sep 17 00:00:00 2001 From: Doris-Siu Date: Sat, 18 Feb 2023 22:31:48 +0000 Subject: [PATCH 6/6] Complete and update the remaining http requests --- 2-api/cyf-ecommerce-api/server.js | 65 ++++++++++++++++++++++++++++++- 1 file changed, 64 insertions(+), 1 deletion(-) diff --git a/2-api/cyf-ecommerce-api/server.js b/2-api/cyf-ecommerce-api/server.js index 53c133a5..813fdfe9 100644 --- a/2-api/cyf-ecommerce-api/server.js +++ b/2-api/cyf-ecommerce-api/server.js @@ -14,6 +14,7 @@ const db = new Pool({ password: "cyf123", port: 5432, }); + // GET app.get("/customers", function (req, res) { @@ -32,6 +33,25 @@ app.get("/customers/:customerId", function (req, res) { ); }); +app.get("/customers/:customerId/orders", async function (req, res) { + const custId = parseInt(req.params.customerId); + + const query1 = + "select * from customers c where exists (select 1 from orders o where o.customer_id = $1)"; + const query2 = + "select o.order_reference, o.order_date, p.product_name, pa.unit_price, s.supplier_name, oi.quantity from orders o join order_items oi on (o.id = oi.order_id) join product_availability pa on (oi.product_id = pa.prod_id) join products p on (pa.prod_id = p.id) join suppliers s on (s.id = pa.supp_id) where o.customer_id = $1"; + + let result = await db.query(query1, [custId]); + + if (result.rowCount > 0) { + db.query(query2, [custId], function (err, result) { + return res.status(200).send(result.rows); + }); + } else { + return res.status(400).send("Error: this customer does not have orders."); + } +}); + app.get("/suppliers", function (req, res) { db.query( "select distinct s.supplier_name from customers c join orders o on (c.id = o.customer_id) join order_items oi on (o.id = oi.order_id) join product_availability pa on (oi.product_id = pa.prod_id) join products p on (pa.prod_id = p.id)join suppliers s on (s.id = pa.supp_id)", @@ -120,6 +140,33 @@ app.post("/products", function (req, res) { }); }); +app.post("/customers/:customerId/orders", async function (req, res) { + const orderDate = req.body.orderDate; + const orderRef = req.body.orderRef; + const custId = parseInt(req.params.customerId); + + let result = await db.query("SELECT 1 from customers WHERE id = " + custId); + + if (result.rowCount === 0) { + return res.send( + "Error: customerId not corresponds to an existing customer." + ); + } else { + const query = + "INSERT INTO orders (order_date, order_reference, customer_id) " + + "VALUES ($1, $2, $3)" + + "RETURNING id"; + + db.query(query, [orderDate, orderRef, custId], (err, result) => { + if (err === undefined) { + res.status(201).send(`Order ${orderRef} created.`); + } else { + res.send(`${err}`); + } + }); + } +}); + //PUT app.put("/customers/:customerId", function (req, res) { const custId = req.params.customerId; @@ -148,7 +195,23 @@ app.delete("/orders/:orderId", function (req, res) { .then(() => res.send(`Order ${Id} deleted!`)) .catch((e) => console.error(e)); }) - .catch((e) => console.error(e)); + .catch((e) => res.send(`${e}`)); +}); + +app.delete("/customers/:customerId", async function (req, res) { + const custId = parseInt(req.params.customerId); + + const query = + "select * from customers c where exists (select 1 from orders o where o.customer_id = $1)"; + let result = await db.query(query, [custId]); + + if (result.rowCount > 0) { + return res.status(400).send("Error: this customer has orders."); + } else { + db.query("DELETE FROM customers WHERE id=$1", [custId]) + .then(() => res.send(`Customer ${custId} deleted!`)) + .catch((e) => res.send(`${e}`)); + } }); app.listen(3100, function () {