Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/issue 13 event tagging and filtering #14

Merged
merged 3 commits into from
Oct 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@
## Overview
Serverless APIs for [www.analogstudios.net](https://github.com/AnalogStudiosRI/www.analogstudios.net) using [arc.codes](https://arc.codes/).

### Supported APIs
* `events` - Structured events content sourced from [**Contentful**](https://contentful.com/)
## Supported APIs
### `events`
Structured events content sourced from [**Contentful**](https://contentful.com/) around the **Event** content type.

_Options:_
- `?id=n` - Filter by the ID of the event
- `?tag=xxx` - Filter by tags of the event

> _You can only pass one or the other_

## Setup

Expand Down
17 changes: 13 additions & 4 deletions src/http/get-events/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ const client = contentful.createClient({

// learn more about HTTP functions here: https://arc.codes/http
export async function handler (req) {
const { queryStringParameters } = req;
const { queryStringParameters = {} } = req;
const { id, tag } = queryStringParameters;

let events = (await client.getEntries('Event'))
.items.map((event) => {
const { id, description, endTime, startTime, title } = event.fields;
const { tags = [] } = event.metadata;

return {
id,
Expand All @@ -31,12 +33,19 @@ export async function handler (req) {
}
}),
startTime: new Date(startTime).getTime() / 1000,
endTime: new Date(endTime).getTime() / 1000
endTime: new Date(endTime).getTime() / 1000,
tags: tags.map((tag) => {
return tag.sys.id;
})
};
});

if (queryStringParameters && queryStringParameters.id) {
events = events.filter(event => parseInt(event.id, 10) === parseInt(queryStringParameters.id, 10));
if (queryStringParameters) {
if (id) {
events = events.filter(event => parseInt(event.id, 10) === parseInt(queryStringParameters.id, 10));
} else if (tag) {
events = events.filter(event => event.tags.includes(tag));
}
}

return {
Expand Down