Fix tag associations

pull/7/head
Sander Vocke 5 years ago
parent 9d9b2e3ee0
commit 1a8d5dc525
  1. 8
      client/src/api.ts
  2. 17
      server/endpoints/ArtistDetailsEndpointHandler.ts
  3. 34
      server/endpoints/CreateArtistEndpointHandler.ts
  4. 19
      server/endpoints/CreateSongEndpointHandler.ts
  5. 16
      server/endpoints/SongDetailsEndpointHandler.ts
  6. 16
      server/test/integration/flows/ArtistFlow.js
  7. 14
      server/test/integration/flows/SongFlow.js
  8. 28
      server/test/integration/flows/helpers.js

@ -62,6 +62,7 @@ export interface SongDetailsResponse {
storeLinks: String[],
artistIds: Number[],
albumIds: Number[],
tagIds: Number[],
}
export function checkSongDetailsRequest(req: any): boolean {
return true;
@ -82,6 +83,7 @@ export const ArtistDetailsEndpoint = '/artist/:id';
export interface ArtistDetailsRequest { }
export interface ArtistDetailsResponse {
name: String,
tagIds: Number[],
storeLinks: String[],
}
export function checkArtistDetailsRequest(req: any): boolean {
@ -94,6 +96,7 @@ export interface CreateSongRequest {
title: String;
artistIds?: Number[];
albumIds?: Number[];
tagIds?: Number[];
storeLinks?: String[];
}
export interface CreateSongResponse {
@ -110,6 +113,7 @@ export interface ModifySongRequest {
title?: String;
artistIds?: Number[];
albumIds?: Number[];
tagIds?: Number[];
storeLinks?: String[];
}
export interface ModifySongResponse { }
@ -121,8 +125,7 @@ export function checkModifySongRequest(req:any): boolean {
export const CreateArtistEndpoint = '/artist';
export interface CreateArtistRequest {
name: String;
songIds?: Number[];
albumIds?: Number[];
tagIds?: Number[];
storeLinks?: String[];
}
export interface CreateArtistResponse {
@ -137,6 +140,7 @@ export function checkCreateArtistRequest(req:any): boolean {
export const ModifyArtistEndpoint = '/artist/:id';
export interface ModifyArtistRequest {
name?: String,
tagIds?: Number[];
storeLinks?: String[],
}
export interface ModifyArtistResponse { }

@ -11,12 +11,13 @@ export const ArtistDetailsEndpointHandler: EndpointHandler = async (req: any, re
throw e;
}
await models.Artist.findAll({
try {
const artists: any[] = await models.Artist.findAll({
where: {
id: req.params.id
}
})
.then((artists: any[]) => {
},
include: [models.Tag]
});
if (artists.length != 1) {
const e: EndpointError = {
internalMessage: 'There is no artist with id ' + req.params.id + '.',
@ -29,9 +30,11 @@ export const ArtistDetailsEndpointHandler: EndpointHandler = async (req: any, re
(artist.storeLinks ? [artist.storeLinks] : []);
const response: api.ArtistDetailsResponse = {
name: artist.name,
tagIds: artist.Tags.map((tag: any) => tag.id),
storeLinks: storeLinks,
};
res.send(response);
})
.catch(catchUnhandledErrors);
await res.send(response);
} catch (e) {
catchUnhandledErrors(e)
}
}

@ -1,6 +1,7 @@
const models = require('../models');
import * as api from '../../client/src/api';
import { EndpointError, EndpointHandler, catchUnhandledErrors } from './types';
const { Op } = require("sequelize");
export const CreateArtistEndpointHandler: EndpointHandler = async (req: any, res: any) => {
if (!api.checkCreateArtistRequest(req)) {
@ -11,9 +12,38 @@ export const CreateArtistEndpointHandler: EndpointHandler = async (req: any, res
throw e;
}
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) => {
const responseObject: api.CreateArtistResponse = {
const responseObject: api.CreateSongResponse = {
id: artist.id
};
res.status(200).send(responseObject);

@ -31,15 +31,25 @@ export const CreateSongEndpointHandler: EndpointHandler = async (req: any, res:
}
});
// Start retrieving the tag instances to link the song to.
var tagInstancesPromise = reqObject.tagIds && models.Tag.findAll({
where: {
id: {
[Op.in]: reqObject.tagIds
}
}
});
// Upon finish retrieving artists and albums, create the song and associate it.
await Promise.all([artistInstancesPromise, albumInstancesPromise])
await Promise.all([artistInstancesPromise, albumInstancesPromise, tagInstancesPromise])
.then((values: any) => {
var [artists, albums] = values;
var [artists, albums, tags] = values;
if ((reqObject.artistIds && artists.length !== reqObject.artistIds.length) ||
(reqObject.albumIds && albums.length !== reqObject.albumIds.length)) {
(reqObject.albumIds && albums.length !== reqObject.albumIds.length) ||
(reqObject.tagIds && tags.length !== reqObject.tagIds.length)) {
const e: EndpointError = {
internalMessage: 'Not all albums and/or artists exist for CreateSong request: ' + JSON.stringify(req.body),
internalMessage: 'Not all albums and/or artists and/or tags exist for CreateSong request: ' + JSON.stringify(req.body),
httpStatus: 400
};
throw e;
@ -51,6 +61,7 @@ export const CreateSongEndpointHandler: EndpointHandler = async (req: any, res:
});
artists && song.addArtists(artists);
albums && song.addAlbums(albums);
tags && song.addTags(tags);
return song.save();
})
.then((song: any) => {

@ -11,13 +11,13 @@ export const SongDetailsEndpointHandler: EndpointHandler = async (req: any, res:
throw e;
}
await models.Song.findAll({
include: [models.Artist, models.Album],
try {
const songs = await models.Song.findAll({
include: [models.Artist, models.Album, models.Tag],
where: {
id: req.params.id
}
})
.then((songs: any[]) => {
});
if (songs.length != 1) {
const e: EndpointError = {
internalMessage: 'There is no song with id ' + req.params.id + '.',
@ -30,9 +30,11 @@ export const SongDetailsEndpointHandler: EndpointHandler = async (req: any, res:
title: song.title,
artistIds: song.Artists.map((artist: any) => artist.id),
albumIds: song.Albums.map((album: any) => album.id),
tagIds: song.Tags.map((tag: any) => tag.id),
storeLinks: song.storeLinks,
}
res.send(response);
})
.catch(catchUnhandledErrors);
await res.send(response);
} catch (e) {
catchUnhandledErrors(e);
}
}

@ -74,7 +74,21 @@ describe('PUT /artist with an existing artist', () => {
var req = chai.request(app).keepOpen();
helpers.createArtist(req, { name: "MyArtist" }, 200, { id: 1 })
.then(() => helpers.modifyArtist(req, 1, { name: "MyNewArtist" }, 200))
.then(() => helpers.checkArtist(req, 1, 200, { name: "MyNewArtist", storeLinks: [] } ) )
.then(() => helpers.checkArtist(req, 1, 200, { name: "MyNewArtist", storeLinks: [], tagIds: [] }))
.then(req.close)
.then(done);
});
});
});
describe('POST /artist with tags', () => {
it('should succeed', done => {
init().then((app) => {
var req = chai.request(app).keepOpen();
helpers.createTag(req, { name: "Root" }, 200, { id: 1 })
.then(() => helpers.createTag(req, { name: "Leaf", parentId: 1 }, 200, { id: 2 }))
.then(() => helpers.createArtist(req, { name: "MyArtist", tagIds: [ 1, 2 ] }, 200, { id: 1 }))
.then(() => helpers.checkArtist(req, 1, 200, { name: "MyArtist", storeLinks: [], tagIds: [ 1, 2 ] }))
.then(req.close)
.then(done);
});

@ -239,3 +239,17 @@ describe('POST /song/query with several songs and filters', () => {
})
});
});
describe('POST /song with tags', () => {
it('should succeed', done => {
init().then((app) => {
var req = chai.request(app).keepOpen();
helpers.createTag(req, { name: "Root" }, 200, { id: 1 })
.then(() => helpers.createTag(req, { name: "Leaf", parentId: 1 }, 200, { id: 2 }))
.then(() => helpers.createSong(req, { title: "Song", tagIds: [ 1, 2 ] }, 200, { id: 1 }))
.then(() => helpers.checkSong(req, 1, 200, { title: "Song", storeLinks: [], tagIds: [ 1, 2 ], albumIds: [], artistIds: [] }))
.then(req.close)
.then(done);
});
});
});

@ -15,6 +15,34 @@ export async function createSong(
});
}
export async function modifySong(
req,
id = 1,
props = { name: "NewSong" },
expectStatus = undefined,
) {
await req
.put('/song/' + id)
.send(props)
.then((res) => {
expectStatus && expect(res).to.have.status(expectStatus);
});
}
export async function checkSong(
req,
id,
expectStatus = undefined,
expectResponse = undefined,
) {
await req
.get('/song/' + id)
.then((res) => {
expectStatus && expect(res).to.have.status(expectStatus);
expectResponse && expect(res.body).to.deep.equal(expectResponse);
})
}
export async function createArtist(
req,
props = { name: "Artist" },

Loading…
Cancel
Save