You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
113 lines
3.2 KiB
113 lines
3.2 KiB
import * as api from '../../client/src/api/api'; |
|
import { EndpointError, EndpointHandler, handleErrorsInEndpoint } from './types'; |
|
import Knex from 'knex'; |
|
import asJson from '../lib/asJson'; |
|
import { AlbumWithDetails } from '../../client/src/api/api'; |
|
import { createAlbum, deleteAlbum, getAlbum, modifyAlbum } from '../db/Album'; |
|
import { GetArtist } from './Artist'; |
|
|
|
export const GetAlbum: EndpointHandler = async (req: any, res: any, knex: Knex) => { |
|
const { id: userId } = req.user; |
|
|
|
try { |
|
const maybeAlbum: api.GetAlbumResponse | null = |
|
await getAlbum(req.params.id, userId, knex); |
|
|
|
if (maybeAlbum) { |
|
await res.send(maybeAlbum); |
|
} else { |
|
await res.status(404).send({}); |
|
} |
|
|
|
} catch (e) { |
|
handleErrorsInEndpoint(e); |
|
} |
|
} |
|
|
|
export const PostAlbum: EndpointHandler = async (req: any, res: any, knex: Knex) => { |
|
if (!api.checkPostAlbumRequest(req.body)) { |
|
const e: EndpointError = { |
|
name: "EndpointError", |
|
message: 'Invalid PostAlbum request: ' + JSON.stringify(req.body), |
|
httpStatus: 400 |
|
}; |
|
throw e; |
|
} |
|
const reqObject: api.PostAlbumRequest = req.body; |
|
const { id: userId } = req.user; |
|
|
|
console.log("User ", userId, ": Post Album ", reqObject); |
|
|
|
try { |
|
let id = await createAlbum(userId, reqObject, knex); |
|
res.status(200).send({ id: id }); |
|
} catch (e) { |
|
handleErrorsInEndpoint(e); |
|
} |
|
} |
|
|
|
export const PutAlbum: EndpointHandler = async (req: any, res: any, knex: Knex) => { |
|
if (!api.checkPutAlbumRequest(req.body)) { |
|
const e: EndpointError = { |
|
name: "EndpointError", |
|
message: 'Invalid PutAlbum request', |
|
httpStatus: 400 |
|
}; |
|
throw e; |
|
} |
|
const reqObject: api.PutAlbumRequest = req.body; |
|
const { id: userId } = req.user; |
|
|
|
console.log("User ", userId, ": Put Album ", reqObject); |
|
|
|
try { |
|
await modifyAlbum(userId, req.params.id, reqObject, knex); |
|
res.status(200).send(); |
|
} catch (e) { |
|
handleErrorsInEndpoint(e); |
|
} |
|
} |
|
|
|
export const PatchAlbum: EndpointHandler = async (req: any, res: any, knex: Knex) => { |
|
if (!api.checkPatchAlbumRequest(req.body)) { |
|
const e: EndpointError = { |
|
name: "EndpointError", |
|
message: 'Invalid PatchAlbum request', |
|
httpStatus: 400 |
|
}; |
|
throw e; |
|
} |
|
const reqObject: api.PatchAlbumRequest = req.body; |
|
const { id: userId } = req.user; |
|
|
|
console.log("User ", userId, ": Patch Album ", reqObject); |
|
|
|
try { |
|
await modifyAlbum(userId, req.params.id, reqObject, knex); |
|
res.status(200).send(); |
|
} catch (e) { |
|
handleErrorsInEndpoint(e); |
|
} |
|
} |
|
|
|
export const DeleteAlbum: EndpointHandler = async (req: any, res: any, knex: Knex) => { |
|
const { id: userId } = req.user; |
|
|
|
console.log("User ", userId, ": Delete Album ", req.params.id); |
|
|
|
try { |
|
await deleteAlbum(userId, req.params.id, knex); |
|
res.status(200).send(); |
|
|
|
} catch (e) { |
|
handleErrorsInEndpoint(e); |
|
} |
|
} |
|
|
|
export const albumEndpoints: [ string, string, boolean, EndpointHandler ][] = [ |
|
[ api.PostAlbumEndpoint, 'post', true, PostAlbum ], |
|
[ api.GetAlbumEndpoint, 'get', true, GetAlbum ], |
|
[ api.PutAlbumEndpoint, 'put', true, PutAlbum ], |
|
[ api.PatchAlbumEndpoint, 'patch', true, PatchAlbum ], |
|
[ api.DeleteAlbumEndpoint, 'delete', true, DeleteAlbum ], |
|
]; |