-
Notifications
You must be signed in to change notification settings - Fork 0
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
DRAFT: Add API endpoints for POST requests #18
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,12 +3,14 @@ dotenv.config() | |
import express from 'express' | ||
import * as calendarEventService from './services/calendarEventService' | ||
import morgan from 'morgan' | ||
import bodyParser from 'body-parser' | ||
|
||
async function startServer(servicePort: number) { | ||
const logger = morgan(':method :url :status - :response-time ms') | ||
const app = express() | ||
|
||
app.use(logger) | ||
app.use(bodyParser.urlencoded({ extended: false })) | ||
|
||
// Ping route | ||
app.get('/ping', (_, res) => res.send('Hello there')) | ||
|
@@ -89,6 +91,21 @@ async function startServer(servicePort: number) { | |
app.listen(servicePort, () => | ||
console.log('App listining on port', servicePort) | ||
) | ||
|
||
app.post( | ||
'/api/events/add', | ||
authorizeRequest, | ||
async (req, res) => { | ||
try { | ||
const field = await calendarEventService.createCalendarEvent(req.body) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Käyttäjältä saatu data ( |
||
|
||
return res.json(field) | ||
} catch (e) { | ||
console.error(e) | ||
res.status(500).json({ error: 'internal server error' }) | ||
} | ||
}, | ||
) | ||
} | ||
|
||
startServer(Number(process.env.SERVICE_PORT || 3001)) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import knex from 'knex' | ||
import { pick, map } from 'remeda' | ||
import { pick, map, prop } from 'remeda' | ||
import moment from 'moment' | ||
import { parse } from 'url' | ||
|
||
|
@@ -220,3 +220,57 @@ function parseUserEventsQueryResult( | |
): CalendarEvent & { price: string } { | ||
return { ...parseQueryResult(row), price: row.price as string } | ||
} | ||
|
||
export async function createCalendarEvent( | ||
props: { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nimi There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tämä tyyppi olisi varmaan hyvä eriyttää omaksi tyyppimäärittelykseen ( |
||
name: string, // Looks weird on absence | ||
user_id: number | null, | ||
created: Date | null, | ||
starts: Date, // members.tko-aly.fi throws error on absence | ||
registration_starts: Date | null, | ||
registration_ends: Date | null, // If registration_starts is given but not _ends, members throws error | ||
cancellation_starts: Date | null, | ||
cancellation_ends: Date | null, | ||
location: string | null, | ||
category: string | null, | ||
description: string | null, | ||
price: string | null, | ||
map: string | null, | ||
membership_required: boolean | null, | ||
outsiders_allowed: boolean | null, | ||
template: boolean, // members.tko-aly.fi won't render on absence | ||
responsible: string | null, | ||
show_responsible: boolean | null, | ||
avec: boolean | null, | ||
deleted: boolean, // members.tko-aly.fi won't render on absence | ||
alcohol_meter: number | null, | ||
} | ||
): Promise<knex.Knex.QueryBuilder<any, number[]>> { | ||
const calendarEvent = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Jos There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tää on muute erinomaisen hyvä pointti :D |
||
name: props.name, | ||
user_id: props.user_id, | ||
created: props.created, | ||
starts: props.starts, | ||
registration_starts: props.registration_starts, | ||
registration_ends: props.registration_ends, | ||
cancellation_starts: props.cancellation_starts, | ||
cancellation_ends: props.cancellation_ends, | ||
location: props.location, | ||
category: props.category, | ||
description: props.description, | ||
price: props.price, | ||
map: props.map, | ||
membership_required: props.membership_required, | ||
outsiders_allowed: props.outsiders_allowed, | ||
template: props.template, | ||
responsible: props.responsible, | ||
show_responsible: props.show_responsible, | ||
avec: props.avec, | ||
deleted: props.deleted, | ||
alcohol_meter: props.alcohol_meter, | ||
} | ||
|
||
const query = db('calendar_events').insert(calendarEvent) | ||
|
||
return query | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pelkkä
/api/events
olisi enemmän REST-mäista.