diff --git a/backend-lambda/getMakes/index.mjs b/backend-lambda/getMakes/index.mjs index e69de29..22630a4 100644 --- a/backend-lambda/getMakes/index.mjs +++ b/backend-lambda/getMakes/index.mjs @@ -0,0 +1,36 @@ +import {MongoClient, ServerApiVersion} from "mongodb"; +import 'dotenv/config'; + +export const handler = async (event, content) => { + const uri = process.env.MONGO_URI; + const client = new MongoClient(uri, { + serverApi: { + version: ServerApiVersion.v1, + strict: true, + deprecationErrors: true + } + }); + const carYear = event['carYear']; + + return client.connect() + .then(async () => { + const database = client.db("vehicleDB"); + const configurations = database.collection("configurations"); + const makes = await configurations.aggregate([ + {$match: {year: parseInt(carYear)}}, + {$group: {_id: "$make", makes: {$addToSet: "$make"}}}, + {$sort: {_id: 1}} + ]).toArray(); + return makes.map(make => make._id); + }).then(makes => { + return { + statusCode: 200, + body: JSON.stringify(makes) + }; + }).catch(err => { + return { + statusCode: 500, + body: JSON.stringify({ message: `Internal Server Error: ${err.message}` }) + }; + }).finally(() => client.close()); +}; \ No newline at end of file diff --git a/backend-lambda/getYears/index.mjs b/backend-lambda/getYears/index.mjs index 78f7831..c2ef5a5 100644 --- a/backend-lambda/getYears/index.mjs +++ b/backend-lambda/getYears/index.mjs @@ -1,5 +1,5 @@ import { MongoClient, ServerApiVersion } from "mongodb"; -import 'dotenv/config' +import 'dotenv/config'; export const handler = async (event, context) => { @@ -21,18 +21,15 @@ export const handler = async (event, context) => { return configurations.aggregate([ { $group: { _id: "$year", years: { $addToSet: "$year" } }} ]).toArray(); - }) - .then(years => { + }).then(years => { return { statusCode: 200, body: years }; - }) - .catch(err => { + }).catch(err => { return { statusCode: 500, body: JSON.stringify({ message: "Internal Server Error" }) }; - }) - .finally(() => client.close()); + }).finally(() => client.close()); }; \ No newline at end of file