parent
535ff867a9
commit
77a116938c
14 changed files with 400 additions and 116 deletions
@ -0,0 +1,48 @@ |
|||||||
|
const models = require('../models'); |
||||||
|
import * as api from '../../client/src/api'; |
||||||
|
import { EndpointError, EndpointHandler, catchUnhandledErrors } from './types'; |
||||||
|
|
||||||
|
export const CreateTagEndpointHandler: EndpointHandler = async (req: any, res: any) => { |
||||||
|
if (!api.checkCreateTagRequest(req)) { |
||||||
|
const e: EndpointError = { |
||||||
|
internalMessage: 'Invalid CreateTag request: ' + JSON.stringify(req.body), |
||||||
|
httpStatus: 400 |
||||||
|
}; |
||||||
|
throw e; |
||||||
|
} |
||||||
|
const reqObject: api.CreateTagRequest = req.body; |
||||||
|
|
||||||
|
const getTag = async (id: Number) => { |
||||||
|
const tag = await models.Tag.findAll({ |
||||||
|
where: { |
||||||
|
id: id |
||||||
|
} |
||||||
|
}); |
||||||
|
if (tag.length != 1) { |
||||||
|
const e: EndpointError = { |
||||||
|
internalMessage: 'There is no tag with id ' + id + '.', |
||||||
|
httpStatus: 400 |
||||||
|
}; |
||||||
|
throw e; |
||||||
|
} |
||||||
|
return tag[0]; |
||||||
|
} |
||||||
|
|
||||||
|
// If applicable, start retrieving the new parent tag.
|
||||||
|
const maybeNewParentPromise: Promise<any> | Promise<undefined> = |
||||||
|
(reqObject.parentId) ? getTag(reqObject.parentId) : (async () => { return undefined })(); |
||||||
|
|
||||||
|
(async () => { |
||||||
|
const maybeParent = await maybeNewParentPromise; |
||||||
|
const tag = await models.Tag.create({ |
||||||
|
name: reqObject.name |
||||||
|
}); |
||||||
|
reqObject.parentId && await tag.setParent(maybeParent); |
||||||
|
await tag.save(); |
||||||
|
const responseObject: api.CreateTagResponse = { |
||||||
|
id: tag.id |
||||||
|
}; |
||||||
|
res.status(200).send(responseObject); |
||||||
|
})() |
||||||
|
.catch(catchUnhandledErrors); |
||||||
|
} |
@ -0,0 +1,51 @@ |
|||||||
|
const models = require('../models'); |
||||||
|
import * as api from '../../client/src/api'; |
||||||
|
import { EndpointError, EndpointHandler, catchUnhandledErrors } from './types'; |
||||||
|
import tag from '../models/tag'; |
||||||
|
|
||||||
|
export const ModifyTagEndpointHandler: EndpointHandler = async (req: any, res: any) => { |
||||||
|
if (!api.checkModifySongRequest(req)) { |
||||||
|
const e: EndpointError = { |
||||||
|
internalMessage: 'Invalid ModifyTag request: ' + JSON.stringify(req.body), |
||||||
|
httpStatus: 400 |
||||||
|
}; |
||||||
|
throw e; |
||||||
|
} |
||||||
|
const reqObject: api.ModifyTagRequest = req.body; |
||||||
|
|
||||||
|
const getTag = async (id:Number) => { |
||||||
|
const tag = await models.Tag.findAll({ |
||||||
|
where: { |
||||||
|
id: id |
||||||
|
} |
||||||
|
}); |
||||||
|
if(tag.length != 1) { |
||||||
|
const e: EndpointError = { |
||||||
|
internalMessage: 'There is no tag with id ' + id + '.', |
||||||
|
httpStatus: 400 |
||||||
|
}; |
||||||
|
throw e; |
||||||
|
} |
||||||
|
return tag[0]; |
||||||
|
} |
||||||
|
|
||||||
|
// If applicable, start retrieving the new parent tag.
|
||||||
|
const maybeNewParentPromise:Promise<any>|Promise<undefined> =
|
||||||
|
(reqObject.parentId) ? getTag(reqObject.parentId) : (async () => { return undefined })(); |
||||||
|
|
||||||
|
// Start retrieving the tag to modify.
|
||||||
|
var tagInstancePromise: Promise<any> = getTag(req.params.id); |
||||||
|
|
||||||
|
// Upon finish retrieving artists and albums, modify the song.
|
||||||
|
await Promise.all([maybeNewParentPromise, tagInstancePromise]) |
||||||
|
.then(async (values: any) => { |
||||||
|
var [maybeParent, tag] = values; |
||||||
|
if(reqObject.name) { tag.setName(reqObject.name) }; |
||||||
|
if(reqObject.parentId) { tag.setParent(maybeParent) }; |
||||||
|
await tag.save(); |
||||||
|
}) |
||||||
|
.then(() => { |
||||||
|
res.status(200).send({}); |
||||||
|
}) |
||||||
|
.catch(catchUnhandledErrors); |
||||||
|
} |
@ -0,0 +1,39 @@ |
|||||||
|
const models = require('../models'); |
||||||
|
import * as api from '../../client/src/api'; |
||||||
|
import { EndpointError, EndpointHandler, catchUnhandledErrors } from './types'; |
||||||
|
|
||||||
|
export const TagDetailsEndpointHandler: EndpointHandler = async (req: any, res: any) => { |
||||||
|
if (!api.checkTagDetailsRequest(req)) { |
||||||
|
const e: EndpointError = { |
||||||
|
internalMessage: 'Invalid TagDetails request: ' + JSON.stringify(req.body), |
||||||
|
httpStatus: 400 |
||||||
|
}; |
||||||
|
throw e; |
||||||
|
} |
||||||
|
|
||||||
|
await models.Tag.findAll({ |
||||||
|
where: { |
||||||
|
id: req.params.id |
||||||
|
}, |
||||||
|
include: [{ |
||||||
|
model: models.Tag, |
||||||
|
as: 'parent' |
||||||
|
}] |
||||||
|
}) |
||||||
|
.then((tags: any[]) => { |
||||||
|
if (tags.length != 1) { |
||||||
|
const e: EndpointError = { |
||||||
|
internalMessage: 'There is no tag with id ' + req.params.id + '.', |
||||||
|
httpStatus: 400 |
||||||
|
}; |
||||||
|
throw e; |
||||||
|
} |
||||||
|
let tag = tags[0]; |
||||||
|
var response: api.TagDetailsResponse = { |
||||||
|
name: tag.name, |
||||||
|
}; |
||||||
|
if(tag.parent) { response['parentId'] = tag.parent.id; } |
||||||
|
res.send(response); |
||||||
|
}) |
||||||
|
.catch(catchUnhandledErrors); |
||||||
|
} |
@ -0,0 +1,14 @@ |
|||||||
|
module.exports = (sequelize, DataTypes) => { |
||||||
|
var Tag = sequelize.define('Tag', { |
||||||
|
name: DataTypes.STRING, |
||||||
|
}); |
||||||
|
|
||||||
|
Tag.associate = function (models) { |
||||||
|
models.Tag.hasOne(models.Tag, { as: 'parent' }); |
||||||
|
models.Tag.belongsToMany(models.Artist, { through: 'ArtistTags' }); |
||||||
|
models.Tag.belongsToMany(models.Album, { through: 'AlbumTags' }); |
||||||
|
models.Tag.belongsToMany(models.Song, { through: 'SongTags' }); |
||||||
|
}; |
||||||
|
|
||||||
|
return Tag; |
||||||
|
}; |
@ -1,107 +0,0 @@ |
|||||||
const chai = require('chai'); |
|
||||||
const chaiHttp = require('chai-http'); |
|
||||||
const express = require('express'); |
|
||||||
const models = require('../../../models'); |
|
||||||
import { SetupApp } from '../../../app'; |
|
||||||
import { expect } from 'chai'; |
|
||||||
import * as helpers from './helpers'; |
|
||||||
|
|
||||||
async function init() { |
|
||||||
chai.use(chaiHttp); |
|
||||||
const app = express(); |
|
||||||
SetupApp(app); |
|
||||||
await models.sequelize.sync({ force: true }); |
|
||||||
return app; |
|
||||||
} |
|
||||||
|
|
||||||
describe('POST /song with no title', () => { |
|
||||||
it('should fail', done => { |
|
||||||
init().then((app) => { |
|
||||||
chai |
|
||||||
.request(app) |
|
||||||
.post('/song') |
|
||||||
.send({}) |
|
||||||
.then((res) => { |
|
||||||
expect(res).to.have.status(400); |
|
||||||
done(); |
|
||||||
}); |
|
||||||
}) |
|
||||||
}); |
|
||||||
}); |
|
||||||
|
|
||||||
describe('POST /song with only a title', () => { |
|
||||||
it('should return the first available id', done => { |
|
||||||
init().then(async(app) => { |
|
||||||
chai |
|
||||||
.request(app) |
|
||||||
.post('/song') |
|
||||||
.send({ |
|
||||||
title: "MySong" |
|
||||||
}) |
|
||||||
.then((res) => { |
|
||||||
expect(res).to.have.status(200); |
|
||||||
expect(res.body).to.deep.equal({ |
|
||||||
id: 1 |
|
||||||
}); |
|
||||||
done(); |
|
||||||
}); |
|
||||||
}) |
|
||||||
}); |
|
||||||
}); |
|
||||||
|
|
||||||
describe('POST /song with a nonexistent artist Id', () => { |
|
||||||
it('should fail', done => { |
|
||||||
init().then(async (app) => { |
|
||||||
chai |
|
||||||
.request(app) |
|
||||||
.post('/song') |
|
||||||
.send({ |
|
||||||
title: "MySong", |
|
||||||
artistIds: [1] |
|
||||||
}) |
|
||||||
.then((res) => { |
|
||||||
expect(res).to.have.status(400); |
|
||||||
done(); |
|
||||||
}); |
|
||||||
}) |
|
||||||
}); |
|
||||||
}); |
|
||||||
|
|
||||||
describe('POST /song with an existing artist Id', () => { |
|
||||||
it('should succeed', done => { |
|
||||||
init().then((app) => { |
|
||||||
var req = chai.request(app).keepOpen(); |
|
||||||
helpers.createArtist(req, { name: "MyArtist" }, 200, { id: 1 }) |
|
||||||
.then(() => helpers.createSong(req, { title: "MySong", artistIds: [ 1 ] }, 200, { id: 1 }) ) |
|
||||||
.then(req.close) |
|
||||||
.then(done); |
|
||||||
}); |
|
||||||
}); |
|
||||||
}); |
|
||||||
|
|
||||||
describe('POST /song with two existing artist Ids', () => { |
|
||||||
it('should succeed', done => { |
|
||||||
init().then((app) => { |
|
||||||
var req = chai.request(app).keepOpen(); |
|
||||||
helpers.createArtist(req, { name: "Artist1" }, 200, { id: 1 }) |
|
||||||
.then(() => helpers.createArtist(req, { name: "Artist2" }, 200, { id: 2 }) ) |
|
||||||
.then(() => helpers.createSong(req, { title: "MySong", artistIds: [1, 2] }, 200, { id: 1 }) ) |
|
||||||
.then(req.close) |
|
||||||
.then(done); |
|
||||||
}); |
|
||||||
}); |
|
||||||
}); |
|
||||||
|
|
||||||
describe('POST /song with an existent and a nonexistent artist Id', () => { |
|
||||||
it('should fail', done => { |
|
||||||
init().then((app) => { |
|
||||||
var req = chai.request(app).keepOpen(); |
|
||||||
helpers.createArtist(req, { name: "Artist1" }, 200, { id: 1 }) |
|
||||||
.then(() => helpers.createSong(req, { title: "MySong", artistIds: [1, 2] }, 400) ) |
|
||||||
.then(req.close) |
|
||||||
.then(done); |
|
||||||
}); |
|
||||||
}); |
|
||||||
}); |
|
||||||
|
|
||||||
export { }; |
|
Loading…
Reference in new issue