Skip to content

Commit

Permalink
getYears and getMakes lambda created & functional
Browse files Browse the repository at this point in the history
  • Loading branch information
Delaney H committed Apr 10, 2024
1 parent e5fb651 commit 458e8d0
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
36 changes: 36 additions & 0 deletions backend-lambda/getMakes/index.mjs
Original file line number Diff line number Diff line change
@@ -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());
};
11 changes: 4 additions & 7 deletions backend-lambda/getYears/index.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MongoClient, ServerApiVersion } from "mongodb";
import 'dotenv/config'
import 'dotenv/config';

export const handler = async (event, context) => {

Expand All @@ -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());
};

0 comments on commit 458e8d0

Please sign in to comment.