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

DRAFT: Add API endpoints for POST requests #18

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
17 changes: 17 additions & 0 deletions src/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down Expand Up @@ -89,6 +91,21 @@ async function startServer(servicePort: number) {
app.listen(servicePort, () =>
console.log('App listining on port', servicePort)
)

app.post(
'/api/events/add',
Copy link
Member

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.

authorizeRequest,
async (req, res) => {
try {
const field = await calendarEventService.createCalendarEvent(req.body)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Käyttäjältä saatu data (req.body) pitää kyllä jotenkin validoida, ennen kuin sitä päästää eteenpäin. Suosittelisin ottamaan projektiin mukaan Zod-kirjaston ja käyttämään sitä.


return res.json(field)
} catch (e) {
console.error(e)
res.status(500).json({ error: 'internal server error' })
}
},
)
}

startServer(Number(process.env.SERVICE_PORT || 3001))
56 changes: 55 additions & 1 deletion src/services/calendarEventService.ts
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'

Expand Down Expand Up @@ -220,3 +220,57 @@ function parseUserEventsQueryResult(
): CalendarEvent & { price: string } {
return { ...parseQueryResult(row), price: row.price as string }
}

export async function createCalendarEvent(
props: {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nimi props on vähän Reactismi. Olisko parempi nimi vaikka event.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tämä tyyppi olisi varmaan hyvä eriyttää omaksi tyyppimäärittelykseen (type NewEvent = { ... }). Jos otat käyttöön Zod-kirjaston, voi sitä myös hyödyntää tässä.

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 = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Jos props-muuttujan sisältö tarkastetaan jo pyynnön käsittelijässä, kuten on hyvän käytännön mukaista, ei tässä kohta enään tarvitse tehdä tällaista, vaan tiedot voi antaa suoraan eteenpäin Knexille.

Copy link
Author

Choose a reason for hiding this comment

The 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
}