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.
112 lines
3.3 KiB
112 lines
3.3 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 { createArtist, deleteArtist, getArtist, modifyArtist } from '../db/Artist'; |
|
|
|
export const GetArtist: EndpointHandler = async (req: any, res: any, knex: Knex) => { |
|
const { id: userId } = req.user; |
|
let id = parseInt(req.params.id); |
|
|
|
try { |
|
let artist = await getArtist(id, userId, knex); |
|
await res.status(200).send(artist); |
|
} catch (e) { |
|
handleErrorsInEndpoint(e) |
|
} |
|
} |
|
|
|
export const PostArtist: EndpointHandler = async (req: any, res: any, knex: Knex) => { |
|
if (!api.checkPostArtistRequest(req.body)) { |
|
const e: EndpointError = { |
|
name: "EndpointError", |
|
message: 'Invalid PostArtist request: ' + JSON.stringify(req.body), |
|
httpStatus: 400 |
|
}; |
|
throw e; |
|
} |
|
const reqObject: api.PostArtistRequest = req.body; |
|
const { id: userId } = req.user; |
|
|
|
console.log("User ", userId, ": Create artist ", reqObject) |
|
|
|
try { |
|
const id = await createArtist(userId, reqObject, knex); |
|
await res.status(200).send({ id: id }); |
|
|
|
} catch (e) { |
|
handleErrorsInEndpoint(e); |
|
} |
|
} |
|
|
|
|
|
export const PutArtist: EndpointHandler = async (req: any, res: any, knex: Knex) => { |
|
if (!api.checkPutArtistRequest(req.body)) { |
|
const e: EndpointError = { |
|
name: "EndpointError", |
|
message: 'Invalid PutArtist request', |
|
httpStatus: 400 |
|
}; |
|
throw e; |
|
} |
|
const reqObject: api.PutArtistRequest = req.body; |
|
const { id: userId } = req.user; |
|
let id = parseInt(req.params.id); |
|
|
|
console.log("User ", userId, ": Put Artist ", reqObject); |
|
|
|
try { |
|
await modifyArtist(userId, id, reqObject, knex); |
|
res.status(200).send(); |
|
|
|
} catch (e) { |
|
handleErrorsInEndpoint(e); |
|
} |
|
} |
|
|
|
export const PatchArtist: EndpointHandler = async (req: any, res: any, knex: Knex) => { |
|
if (!api.checkPatchArtistRequest(req.body)) { |
|
const e: EndpointError = { |
|
name: "EndpointError", |
|
message: 'Invalid PatchArtist request', |
|
httpStatus: 400 |
|
}; |
|
throw e; |
|
} |
|
const reqObject: api.PatchArtistRequest = req.body; |
|
const { id: userId } = req.user; |
|
let id = parseInt(req.params.id); |
|
|
|
console.log("User ", userId, ": Patch Artist ", reqObject); |
|
|
|
try { |
|
await modifyArtist(userId, id, reqObject, knex); |
|
res.status(200).send(); |
|
|
|
} catch (e) { |
|
handleErrorsInEndpoint(e); |
|
} |
|
} |
|
|
|
export const DeleteArtist: EndpointHandler = async (req: any, res: any, knex: Knex) => { |
|
const { id: userId } = req.user; |
|
let id = parseInt(req.params.id); |
|
|
|
console.log("User ", userId, ": Delete Artist ", id); |
|
|
|
try { |
|
await deleteArtist(userId, id, knex); |
|
res.status(200).send(); |
|
|
|
} catch (e) { |
|
handleErrorsInEndpoint(e); |
|
} |
|
} |
|
|
|
export const artistEndpoints: [ string, string, boolean, EndpointHandler ][] = [ |
|
[ api.PostArtistEndpoint, 'post', true, PostArtist ], |
|
[ api.GetArtistEndpoint, 'get', true, GetArtist ], |
|
[ api.PutArtistEndpoint, 'put', true, PutArtist ], |
|
[ api.PatchArtistEndpoint, 'patch', true, PatchArtist ], |
|
[ api.DeleteArtistEndpoint, 'delete', true, DeleteArtist ], |
|
]; |