|
|
@ -1,6 +1,7 @@ |
|
|
|
const models = require('../models'); |
|
|
|
const models = require('../models'); |
|
|
|
import * as api from '../../client/src/api'; |
|
|
|
import * as api from '../../client/src/api'; |
|
|
|
import { EndpointError, EndpointHandler, catchUnhandledErrors } from './types'; |
|
|
|
import { EndpointError, EndpointHandler, catchUnhandledErrors } from './types'; |
|
|
|
|
|
|
|
const { Op } = require("sequelize"); |
|
|
|
|
|
|
|
|
|
|
|
export const CreateArtistEndpointHandler: EndpointHandler = async (req: any, res: any) => { |
|
|
|
export const CreateArtistEndpointHandler: EndpointHandler = async (req: any, res: any) => { |
|
|
|
if (!api.checkCreateArtistRequest(req)) { |
|
|
|
if (!api.checkCreateArtistRequest(req)) { |
|
|
@ -11,9 +12,38 @@ export const CreateArtistEndpointHandler: EndpointHandler = async (req: any, res |
|
|
|
throw e; |
|
|
|
throw e; |
|
|
|
} |
|
|
|
} |
|
|
|
const reqObject: api.CreateArtistRequest = req.body; |
|
|
|
const reqObject: api.CreateArtistRequest = req.body; |
|
|
|
await models.Artist.create(reqObject) |
|
|
|
|
|
|
|
|
|
|
|
// Start retrieving the tag instances to link the artist to.
|
|
|
|
|
|
|
|
var tagInstancesPromise = reqObject.tagIds && models.Tag.findAll({ |
|
|
|
|
|
|
|
where: { |
|
|
|
|
|
|
|
id: { |
|
|
|
|
|
|
|
[Op.in]: reqObject.tagIds |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Upon finish retrieving artists and albums, create the artist and associate it.
|
|
|
|
|
|
|
|
await Promise.all([tagInstancesPromise]) |
|
|
|
|
|
|
|
.then((values: any) => { |
|
|
|
|
|
|
|
var [tags] = values; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (reqObject.tagIds && tags.length !== reqObject.tagIds.length) { |
|
|
|
|
|
|
|
const e: EndpointError = { |
|
|
|
|
|
|
|
internalMessage: 'Not all atags exist for CreateArtist request: ' + JSON.stringify(req.body), |
|
|
|
|
|
|
|
httpStatus: 400 |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
throw e; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var artist = models.Artist.build({ |
|
|
|
|
|
|
|
name: reqObject.name, |
|
|
|
|
|
|
|
storeLinks: reqObject.storeLinks || [], |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
tags && artist.addTags(tags); |
|
|
|
|
|
|
|
return artist.save(); |
|
|
|
|
|
|
|
}) |
|
|
|
.then((artist: any) => { |
|
|
|
.then((artist: any) => { |
|
|
|
const responseObject: api.CreateArtistResponse = { |
|
|
|
const responseObject: api.CreateSongResponse = { |
|
|
|
id: artist.id |
|
|
|
id: artist.id |
|
|
|
}; |
|
|
|
}; |
|
|
|
res.status(200).send(responseObject); |
|
|
|
res.status(200).send(responseObject); |
|
|
|