Skip to content

Commit

Permalink
feat: add data to person, events and plots (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
5annaha authored May 28, 2024
1 parent 0335319 commit fc1d43e
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/models/person.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,17 @@ export const Person = Bookshelf.Model.extend({
'id',
'first_name',
'last_name',
'title',
'dynasty',
'political_party',
'religion',
'ship_id',
'status',
'home_planet',
'is_visible',
'card_id',
'character_group',
'shift',
'is_character',
],
withRelated: [{
Expand Down
34 changes: 32 additions & 2 deletions src/models/story-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,39 @@ export const StoryEventWithRelations = StoryEvent.extend({
});
export type StoryEventWithRelations = z.infer<typeof StoryEventWithRelations>;

export const listStoryEvents = async (): Promise<StoryEvent[]> => {
export const listStoryEvents = async (): Promise<StoryEventWithRelations[]> => {
const events = await knex('story_events').select('*');
return StoryEvent.array().parse(events);

const events_with_persons = await Promise.all(events.map(async event => {
const [artifacts, persons, messages, plots] = await Promise.all([
knex('story_artifact_events')
.join('artifact', 'story_artifact_events.artifact_id', 'artifact.id')
.select('artifact.name', 'artifact.id', 'artifact.catalog_id')
.where({ event_id: event.id }),
knex('story_person_events')
.join('person', 'story_person_events.person_id', 'person.id')
.select('person.id', 'person.is_character', knex.raw('TRIM(CONCAT(person.first_name, \' \', person.last_name)) as name'))
.where({ event_id: event.id }),
knex('story_event_messages')
.join('story_messages', 'story_event_messages.message_id', 'story_messages.id')
.select('story_messages.id', 'story_messages.name', 'story_messages.sent')
.where({ event_id: event.id }),
knex('story_event_plots')
.join('story_plots', 'story_event_plots.plot_id', 'story_plots.id')
.select('story_plots.id', 'story_plots.name')
.where({ event_id: event.id }),
]);

return StoryEventWithRelations.parse({
...event,
artifacts,
persons,
messages,
plots,
});
}));

return events_with_persons;
}

export const getStoryEvent = async (id: number): Promise<StoryEventWithRelations | null> => {
Expand Down
36 changes: 33 additions & 3 deletions src/models/story-plots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,40 @@ export const StoryPlotWithRelations = StoryPlot.extend({
});
export type StoryPlotWithRelations = z.infer<typeof StoryPlotWithRelations>;

export async function listStoryPlots(): Promise<StoryPlot[]> {
export async function listStoryPlots(): Promise<StoryPlotWithRelations[] | []> {
const plots = await knex('story_plots').select('*');
return StoryPlot.array().parse(plots);
}

const plots_with_persons = await Promise.all(plots.map(async plot => {
const [artifacts, events, messages, persons] = await Promise.all([
knex('story_artifact_plots')
.join('artifact', 'story_artifact_plots.artifact_id', 'artifact.id')
.select('artifact.name', 'artifact.id', 'artifact.catalog_id')
.where({ plot_id: plot.id }),
knex('story_event_plots')
.join('story_events', 'story_event_plots.event_id', 'story_events.id')
.select('story_events.id', 'story_events.name')
.where({ plot_id: plot.id }),
knex('story_plot_messages')
.join('story_messages', 'story_plot_messages.message_id', 'story_messages.id')
.select('story_messages.id', 'story_messages.name')
.where({ plot_id: plot.id }),
knex('story_person_plots')
.join('person', 'story_person_plots.person_id', 'person.id')
.select('person.id', knex.raw('TRIM(CONCAT(person.first_name, \' \', person.last_name)) as name'))
.where({ plot_id: plot.id }),
]);

return StoryPlotWithRelations.parse({
...plot,
persons,
artifacts,
messages,
events
});
}));

return plots_with_persons;
};

export async function getStoryPlot(id: number): Promise<StoryPlot | null> {
const plot = await knex('story_plots').select('*').where({ id }).first();
Expand Down

0 comments on commit fc1d43e

Please sign in to comment.