-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement put and delete episode handlers
This should be everything I need to close the loop on episode permalink pages and CRUD actions.
- Loading branch information
Showing
4 changed files
with
117 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* eslint-disable camelcase */ | ||
import SQL from '@nearform/sql' | ||
|
||
export async function deleteEpisode (fastify, opts) { | ||
fastify.delete('/', { | ||
preHandler: fastify.auth([fastify.verifyJWT]), | ||
schema: { | ||
params: { | ||
type: 'object', | ||
properties: { | ||
episode_id: { type: 'string', format: 'uuid' } | ||
}, | ||
required: ['episode_id'] | ||
} | ||
} | ||
}, | ||
async function deleteEpisodeHandler (request, reply) { | ||
const ownerID = request.user.id | ||
const episodeID = request.params.episode_id | ||
|
||
const query = SQL` | ||
delete from episodes | ||
where id = ${episodeID} | ||
and owner_id = ${ownerID} | ||
` | ||
|
||
// TODO: check results | ||
await fastify.pg.query(query) | ||
|
||
reply.status = 202 | ||
fastify.metrics.episodeDeleteCounter.inc() | ||
|
||
return { | ||
status: 'ok' | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
import { getEpisode } from './get-episode.js' | ||
// import { putEpisode } from './put-episode.js' | ||
// import { deleteEpisode } from './delete-episode.js' | ||
import { putEpisode } from './put-episode.js' | ||
import { deleteEpisode } from './delete-episode.js' | ||
|
||
export default async function episodeRoutes (fastify, opts) { | ||
await Promise.all([ | ||
getEpisode(fastify, opts) | ||
// putEpisode(fastify, opts), | ||
// deleteEpisode(fastify, opts) | ||
getEpisode(fastify, opts), | ||
putEpisode(fastify, opts), | ||
deleteEpisode(fastify, opts) | ||
]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* eslint-disable camelcase */ | ||
import SQL from '@nearform/sql' | ||
|
||
import { commonEpisodeProps } from '../episode-props.js' | ||
|
||
export async function putEpisode (fastify, opts) { | ||
fastify.put('/', { | ||
preHandler: fastify.auth([fastify.verifyJWT]), | ||
schema: { | ||
querystring: { | ||
sensitive: { | ||
type: 'boolean', | ||
default: false | ||
} | ||
}, | ||
params: { | ||
type: 'object', | ||
properties: { | ||
episode_id: { type: 'string', format: 'uuid' } | ||
}, | ||
required: ['id'] | ||
}, | ||
body: { | ||
type: 'object', | ||
properties: { | ||
...commonEpisodeProps | ||
}, | ||
minProperties: 1, | ||
additionalProperties: false | ||
} | ||
} | ||
}, | ||
async function putEpisodeHandler (request, reply) { | ||
return fastify.pg.transact(async client => { | ||
const ownerId = request.user.episode_id | ||
const { episode_id: episodeId } = request.params | ||
const episode = request.body | ||
|
||
const updates = [] | ||
|
||
if (episode.url != null) updates.push(SQL`url = ${episode.url}`) | ||
if (episode.title != null) updates.push(SQL`title = ${episode.title}`) | ||
if (episode.explicit != null) updates.push(SQL`explicit = ${episode.explicit}`) | ||
// TODO: description editing | ||
// TODO: change medium or type? | ||
|
||
if (updates.length > 0) { | ||
const query = SQL` | ||
update episodes | ||
set ${SQL.glue(updates, ' , ')} | ||
where id = ${episodeId} | ||
and owner_id =${ownerId}; | ||
` | ||
|
||
await client.query(query) | ||
} | ||
|
||
fastify.metrics.episodeEditCounter.inc() | ||
|
||
return { | ||
status: 'ok' | ||
} | ||
}) | ||
}) | ||
} |