Skip to content

Commit

Permalink
💩 extended description with a markdown bio (really sloppy CSS; hasty …
Browse files Browse the repository at this point in the history
…addition)
  • Loading branch information
MattMoony committed May 2, 2024
1 parent c869840 commit 53ca72a
Show file tree
Hide file tree
Showing 9 changed files with 153 additions and 23 deletions.
19 changes: 17 additions & 2 deletions core/src/models/Organ.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { pool } from '../db';
import fs from 'fs';

/**
* Represents a member of an organization. That member
Expand All @@ -7,14 +8,17 @@ import { pool } from '../db';
*/
class Organ {
public id: number;
public bio: string;

public constructor (id: number) {
public constructor (id: number, bio: string) {
this.id = id;
this.bio = bio;
}

public json (): Object {
return {
id: this.id,
bio: this.bio,
};
}

Expand All @@ -26,7 +30,18 @@ class Organ {
const client = await pool.connect();
const result = await client.query('INSERT INTO organ DEFAULT VALUES RETURNING oid');
client.release();
return +result.rows[0].oid;
const oid = +result.rows[0].oid;
fs.writeFileSync(`${process.env.DATA_DIR}/${oid}.md`, '');
return oid;
}

public static async get (id: number): Promise<Organ|null> {
const client = await pool.connect();
const res = await client.query('SELECT * FROM organ WHERE oid = $1', [id]);
client.release();
if (res.rows.length === 0) return null;
if (!fs.existsSync(`${process.env.DATA_DIR}/${id}.md`)) fs.writeFileSync(`${process.env.DATA_DIR}/${id}.md`, '');
return new Organ(res.rows[0].oid, fs.readFileSync(`${process.env.DATA_DIR}/${id}.md`, 'utf8'));
}
}

Expand Down
4 changes: 2 additions & 2 deletions core/src/models/Organization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ class Organization extends Organ {
public established?: Date;
public dissolved?: Date;

public constructor (id: number, name: string, established?: Date, dissolved?: Date) {
super(id);
public constructor (id: number, bio: string, name: string, established?: Date, dissolved?: Date) {
super(id, bio);
this.name = name;
this.established = established;
this.dissolved = dissolved;
Expand Down
48 changes: 29 additions & 19 deletions core/src/models/Person.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ class Person extends Organ {
public birthdate?: Date;
public deathdate?: Date;

public constructor (id: number, firstname: string, lastname: string, birthdate?: Date, deathdate?: Date) {
super(id);
public constructor (id: number, bio: string, firstname: string, lastname: string, birthdate?: Date, deathdate?: Date) {
super(id, bio);
this.firstname = firstname;
this.lastname = lastname;
this.birthdate = birthdate;
Expand Down Expand Up @@ -159,13 +159,14 @@ class Person extends Organ {
[this.id, RELATION_TYPES['parent']],
);
client.release();
return res.rows.map((row) => new Relation(this, new Person(
return Promise.all(res.rows.map(async (row) => new Relation(this, new Person(
row.pid,
(await Organ.get(row.pid))!.bio,
row.firstname,
row.lastname,
row.birthdate,
row.deathdate,
), row.since, row.until));
), row.since, row.until)));
}

public async getChildren (): Promise<Relation[]> {
Expand All @@ -179,13 +180,14 @@ class Person extends Organ {
[this.id, RELATION_TYPES['parent']],
);
client.release();
return res.rows.map((row) => new Relation(this, new Person(
return Promise.all(res.rows.map(async (row) => new Relation(this, new Person(
row.pid,
(await Organ.get(row.pid))!.bio,
row.firstname,
row.lastname,
row.birthdate,
row.deathdate,
), row.since, row.until));
), row.since, row.until)));
}

public async getRomantic (): Promise<Relation[]> {
Expand All @@ -198,13 +200,14 @@ class Person extends Organ {
AND relation.relation = $2`,
[this.id, RELATION_TYPES['romantic']],
);
const ret = res.rows.map((row) => new Relation(this, new Person(
const ret = await Promise.all(res.rows.map(async (row) => new Relation(this, new Person(
row.pid,
(await Organ.get(row.pid))!.bio,
row.firstname,
row.lastname,
row.birthdate,
row.deathdate,
), row.since, row.until));
), row.since, row.until)));
const res2 = await client.query(
`SELECT person.*, relation.since, relation.until
FROM relation
Expand All @@ -214,13 +217,14 @@ class Person extends Organ {
[this.id, RELATION_TYPES['romantic']],
);
client.release();
return ret.concat(res2.rows.map((row) => new Relation(this, new Person(
return ret.concat(await Promise.all(res2.rows.map(async (row) => new Relation(this, new Person(
row.pid,
(await Organ.get(row.pid))!.bio,
row.firstname,
row.lastname,
row.birthdate,
row.deathdate,
), row.since, row.until)));
), row.since, row.until))));
}

public async getFriends (): Promise<Relation[]> {
Expand All @@ -233,13 +237,14 @@ class Person extends Organ {
AND relation.relation = $2`,
[this.id, RELATION_TYPES['friend']],
);
const ret = res.rows.map((row) => new Relation(this, new Person(
const ret = await Promise.all(res.rows.map(async (row) => new Relation(this, new Person(
row.pid,
(await Organ.get(row.pid))!.bio,
row.firstname,
row.lastname,
row.birthdate,
row.deathdate,
), row.since, row.until));
), row.since, row.until)));
const res2 = await client.query(
`SELECT person.*, relation.since, relation.until
FROM relation
Expand All @@ -249,27 +254,30 @@ class Person extends Organ {
[this.id, RELATION_TYPES['friend']],
);
client.release();
return ret.concat(res2.rows.map((row) => new Relation(this, new Person(
return ret.concat(await Promise.all(res2.rows.map(async (row) => new Relation(this, new Person(
row.pid,
(await Organ.get(row.pid))!.bio,
row.firstname,
row.lastname,
row.birthdate,
row.deathdate,
), row.since, row.until)));
), row.since, row.until))));
}

public static async create (firstname: string, lastname: string, birthdate?: Date, deathdate?: Date): Promise<Person> {
public static async create (firstname: string, lastname: string, birthdate?: Date, deathdate?: Date, bio?: string): Promise<Person> {
const id = await Organ.create();
const client = await pool.connect();
await client.query(
'INSERT INTO person (pid, firstname, lastname, birthdate, deathdate) VALUES ($1, $2, $3, $4, $5)',
[id, firstname, lastname, birthdate, deathdate,],
);
client.release();
return new Person(id, firstname, lastname, birthdate, deathdate);
return new Person(id, bio||'', firstname, lastname, birthdate, deathdate);
}

public static async get (id: number): Promise<Person|null> {
const organ = await super.get(id);
if (!organ) return null;
const client = await pool.connect();
const res = await client.query(
'SELECT * FROM person WHERE pid = $1',
Expand All @@ -278,7 +286,8 @@ class Person extends Organ {
client.release();
if (res.rows.length === 0) return null;
return new Person(
res.rows[0].pid,
organ.id,
organ.bio,
res.rows[0].firstname,
res.rows[0].lastname,
res.rows[0].birthdate,
Expand All @@ -296,13 +305,14 @@ class Person extends Organ {
[`%${query}%`],
);
client.release();
return res.rows.map((row) => new Person(
return Promise.all(res.rows.map(async (row) => new Person(
row.pid,
(await Organ.get(row.pid))!.bio,
row.firstname,
row.lastname,
row.birthdate,
row.deathdate,
));
)));
}
}

Expand Down
57 changes: 57 additions & 0 deletions web/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,20 @@
"@fortawesome/vue-fontawesome": "^3.0.5",
"@vue/apollo-composable": "^4.0.1",
"@vueuse/core": "^10.7.2",
"dompurify": "^3.1.2",
"graphql": "^16.8.1",
"graphql-tag": "^2.12.6",
"marked": "^12.0.2",
"pinia": "^2.1.7",
"v-network-graph": "^0.9.14",
"vue": "^3.4.15",
"vue-dompurify-html": "^5.0.1",
"vue-router": "^4.2.5"
},
"devDependencies": {
"@rushstack/eslint-patch": "^1.7.2",
"@types/dompurify": "^3.0.5",
"@types/marked": "^6.0.0",
"@types/node": "^20.11.10",
"@vitejs/plugin-vue": "^5.0.3",
"@vue/eslint-config-prettier": "^9.0.0",
Expand Down
Loading

0 comments on commit 53ca72a

Please sign in to comment.