Skip to content

Commit

Permalink
Add markdown database model files.
Browse files Browse the repository at this point in the history
  • Loading branch information
NickPhura committed Nov 25, 2024
1 parent 7b3fc1c commit 77c0f97
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 29 deletions.
36 changes: 36 additions & 0 deletions api/src/database-models/markdown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { z } from 'zod';

/**
* Markdown Model.
*
* @description Data model for `markdown`.
*/
export const MarkdownModel = z.object({
markdown_id: z.number(),
markdown_type_id: z.number(),
data: z.string().nullable(),
score: z.number(),
record_end_date: z.string(),
create_date: z.string(),
create_user: z.number(),
update_date: z.string().nullable(),
update_user: z.number().nullable(),
revision_count: z.number()
});

export type MarkdownModel = z.infer<typeof MarkdownModel>;

/**
* Markdown Record.
*
* @description Data record for `markdown`.
*/
export const MarkdownRecord = MarkdownModel.omit({
create_date: true,
create_user: true,
update_date: true,
update_user: true,
revision_count: true
});

export type MarkdownRecord = z.infer<typeof MarkdownRecord>;
34 changes: 34 additions & 0 deletions api/src/database-models/markdown_type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { z } from 'zod';

/**
* Markdown Type Model.
*
* @description Data model for `markdown_type`.
*/
export const MarkdownTypeModel = z.object({

Check warning on line 8 in api/src/database-models/markdown_type.ts

View check run for this annotation

Codecov / codecov/patch

api/src/database-models/markdown_type.ts#L8

Added line #L8 was not covered by tests
markdown_type_id: z.number(),
name: z.string(),
description: z.string(),
create_date: z.string(),
create_user: z.number(),
update_date: z.string().nullable(),
update_user: z.number().nullable(),
revision_count: z.number()
});

export type MarkdownTypeModel = z.infer<typeof MarkdownTypeModel>;

/**
* Markdown Type Record.
*
* @description Data record for `markdown_type`.
*/
export const MarkdownTypeRecord = MarkdownTypeModel.omit({

Check warning on line 26 in api/src/database-models/markdown_type.ts

View check run for this annotation

Codecov / codecov/patch

api/src/database-models/markdown_type.ts#L26

Added line #L26 was not covered by tests
create_date: true,
create_user: true,
update_date: true,
update_user: true,
revision_count: true
});

export type MarkdownTypeRecord = z.infer<typeof MarkdownTypeRecord>;
34 changes: 34 additions & 0 deletions api/src/database-models/markdown_user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { z } from 'zod';

/**
* Markdown User Model.
*
* @description Data model for `markdown_user`.
*/
export const MarkdownUserModel = z.object({
markdown_user_id: z.number(),
system_user_id: z.number(),
markdown_id: z.number(),
create_date: z.string(),
create_user: z.number(),
update_date: z.string().nullable(),
update_user: z.number().nullable(),
revision_count: z.number()
});

export type MarkdownUserModel = z.infer<typeof MarkdownUserModel>;

/**
* Markdown User Record.
*
* @description Data record for `markdown_user`.
*/
export const MarkdownUserRecord = MarkdownUserModel.omit({
create_date: true,
create_user: true,
update_date: true,
update_user: true,
revision_count: true
});

export type MarkdownUserRecord = z.infer<typeof MarkdownUserRecord>;
18 changes: 6 additions & 12 deletions api/src/models/markdown-view.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
import { z } from 'zod';
import { MarkdownRecord } from '../database-models/markdown';

export const MarkdownObject = z.object({
markdown_id: z.number(),
markdown_type_id: z.number(),
data: z.string(),
export const MarkdownObject = MarkdownRecord.pick({
markdown_id: true,
markdown_type_id: true,
data: true
}).extend({
participated: z.boolean()
});

export type MarkdownObject = z.infer<typeof MarkdownObject>;

export const MarkdownUserObject = z.object({
markdown_user_id: z.number(),
system_user_id: z.number(),
markdown_id: z.number()
});

export type MarkdownUserObject = z.infer<typeof MarkdownUserObject>;

export interface MarkdownQueryObject {
system_user_id: number;
markdown_type_name: string;
Expand Down
12 changes: 10 additions & 2 deletions api/src/openapi/schemas/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,28 @@ export const markdownSchema: OpenAPIV3.SchemaObject = {
type: 'object',
description: 'Schema for get markdown response',
additionalProperties: false,
required: ['markdown'],
properties: {
markdown: {
type: 'object',
description: 'Markdown record',
required: ['markdown_id', 'markdown_type_id', 'data', 'participated'],
additionalProperties: false,
properties: {
markdown_id: { type: 'number', description: 'Primary key of the markdown record', minimum: 1 },
markdown_id: {
type: 'number',
description: 'Primary key of the markdown record',
minimum: 1
},
markdown_type_id: {
type: 'number',
description: 'Type of the markdown record, used to identify which records correspond to which dialogs',
minimum: 1
},
data: { type: 'string', description: 'Markdown string to display' },
data: {
type: 'string',
description: 'Markdown string to display'
},
participated: {
type: 'boolean',
description: 'True if the user has already scored the markdown record, otherwise false.'
Expand Down
4 changes: 1 addition & 3 deletions api/src/paths/markdown/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { RequestHandler } from 'express';
import { Operation } from 'express-openapi';
import { SYSTEM_ROLE } from '../../constants/roles';
import { getDBConnection } from '../../database/db';
import { markdownSchema } from '../../openapi/schemas/markdown';
import { authorizeRequestHandler } from '../../request-handlers/security/authorization';
Expand All @@ -14,8 +13,7 @@ export const GET: Operation = [
return {

Check warning on line 13 in api/src/paths/markdown/index.ts

View check run for this annotation

Codecov / codecov/patch

api/src/paths/markdown/index.ts#L13

Added line #L13 was not covered by tests
and: [
{
validSystemRoles: [SYSTEM_ROLE.PROJECT_CREATOR, SYSTEM_ROLE.SYSTEM_ADMIN, SYSTEM_ROLE.DATA_ADMINISTRATOR],
discriminator: 'SystemRole'
discriminator: 'SystemUser'
}
]
};
Expand Down
13 changes: 8 additions & 5 deletions api/src/paths/markdown/{markdownId}/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { RequestHandler } from 'express';
import { Operation } from 'express-openapi';
import { SYSTEM_ROLE } from '../../../constants/roles';
import { getDBConnection } from '../../../database/db';
import { authorizeRequestHandler } from '../../../request-handlers/security/authorization';
import { MarkdownService } from '../../../services/markdown-service';
Expand All @@ -13,8 +12,7 @@ export const POST: Operation = [
return {

Check warning on line 12 in api/src/paths/markdown/{markdownId}/index.ts

View check run for this annotation

Codecov / codecov/patch

api/src/paths/markdown/{markdownId}/index.ts#L12

Added line #L12 was not covered by tests
and: [
{
validSystemRoles: [SYSTEM_ROLE.PROJECT_CREATOR, SYSTEM_ROLE.SYSTEM_ADMIN, SYSTEM_ROLE.DATA_ADMINISTRATOR],
discriminator: 'SystemRole'
discriminator: 'SystemUser'
}
]
};
Expand All @@ -37,7 +35,8 @@ POST.apiDoc = {
description: 'Primary key of a markdown record to submit a score for',
required: true,
schema: {
type: 'integer'
type: 'integer',
minimum: 1
}
}
],
Expand All @@ -51,7 +50,11 @@ POST.apiDoc = {
additionalProperties: false,
required: ['score'],
properties: {
score: { type: 'number', description: 'Score to add to the markdown record', enum: [-1, 1] }
score: {
type: 'number',
description: 'Score to add to the markdown record',
enum: [-1, 1]
}
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions api/src/repositories/markdown-repository.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import SQL from 'sql-template-strings';
import { MarkdownObject, MarkdownQueryObject, MarkdownUserObject } from '../models/markdown-view';
import { MarkdownUserRecord } from '../database-models/markdown_user';
import { MarkdownObject, MarkdownQueryObject } from '../models/markdown-view';
import { BaseRepository } from './base-repository';

/**
Expand Down Expand Up @@ -66,10 +67,10 @@ export class MarkdownRepository extends BaseRepository {
*
* @param {number} markdownId
* @param {number} systemUserId
* @return {*} {Promise<MarkdownUserObject | null>}
* @return {*} {Promise<MarkdownUserRecord | null>}
* @memberof MarkdownRepository
*/
async getUserParticipation(markdownId: number, systemUserId: number): Promise<MarkdownUserObject | null> {
async getUserParticipation(markdownId: number, systemUserId: number): Promise<MarkdownUserRecord | null> {
const sqlStatement = SQL`
SELECT
markdown_user_id,
Expand All @@ -83,7 +84,7 @@ export class MarkdownRepository extends BaseRepository {
system_user_id = ${systemUserId};
`;

const response = await this.connection.sql(sqlStatement, MarkdownUserObject);
const response = await this.connection.sql(sqlStatement, MarkdownUserRecord);

return response.rows?.[0] ?? null;
}
Expand Down
7 changes: 4 additions & 3 deletions api/src/services/markdown-service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { MarkdownUserRecord } from '../database-models/markdown_user';
import { IDBConnection } from '../database/db';
import { MarkdownObject, MarkdownQueryObject, MarkdownUserObject } from '../models/markdown-view';
import { MarkdownObject, MarkdownQueryObject } from '../models/markdown-view';
import { MarkdownRepository } from '../repositories/markdown-repository';
import { DBService } from './db-service';

Expand Down Expand Up @@ -66,10 +67,10 @@ export class MarkdownService extends DBService {
*
* @param {number} markdownId
* @param {number} systemUserId
* @return {*} Promise<MarkdownUserObject | null>
* @return {*} Promise<MarkdownUserRecord | null>
* @memberof MarkdownService
*/
async getUserParticipation(markdownId: number, systemUserId: number): Promise<MarkdownUserObject | null> {
async getUserParticipation(markdownId: number, systemUserId: number): Promise<MarkdownUserRecord | null> {
return this.markdownRepository.getUserParticipation(markdownId, systemUserId);
}

Expand Down

0 comments on commit 77c0f97

Please sign in to comment.