Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ mysql> FLUSH PRIVILEGES;

Populate the database using knex, which should add tables automatically.<br>
```
knex --knexfile src/knexfile.ts migrate:latest
make knex-migrate
```

This should populate your database with the required tables.
Expand Down
1 change: 1 addition & 0 deletions src/db-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export type EntriesRecord = {
billable: number;
created_at: Date;
modified_at: Date;
version: number;
}

export type PeopleRecord = {
Expand Down
12 changes: 6 additions & 6 deletions src/entry/controller/item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ class Entry extends Controller {
const person = await personService.findById(
+ctx.params.personId,
);

ctx.response.body = hal.item(
await entryService.findById(
person,
+ctx.params.entryId,
)
const entry = await entryService.findById(
person,
+ctx.params.entryId,
);

ctx.response.body = hal.item(entry);
ctx.response.headers.set('ETag', '"' + entry.version + '"');
}

async put(ctx: Context) {
Expand Down
3 changes: 3 additions & 0 deletions src/entry/formats/hal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ export function item(entry: Entry) {
href: entry.person.href
}
},
_headers: {
ETag: '"' + entry.version + '"',
},
description: entry.description,
date: entry.date,
minutes: entry.minutes,
Expand Down
26 changes: 16 additions & 10 deletions src/entry/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,15 @@ export async function create(entry: NewEntry): Promise<Entry> {
description: entry.description,
billable: entry.billable,
created_at: new Date(),
modified_at: new Date()
modified_at: new Date(),
version: 1,
});

return {
...entry,
id: result[0],
href: `/person/${entry.person.id}/entry/${result[0]}`,
version: 1,
createdAt: new Date(),
modifiedAt: new Date()
};
Expand All @@ -150,14 +152,17 @@ export async function create(entry: NewEntry): Promise<Entry> {

export async function update(entry: Entry): Promise<void> {

await knex('entries').update({
project_id: entry.project.id,
date: entry.date,
minutes: entry.minutes,
description: entry.description,
billable: entry.billable,
modified_at: new Date()
}).where({id: entry.id});
await knex('entries')
.update({
project_id: entry.project.id,
date: entry.date,
minutes: entry.minutes,
description: entry.description,
billable: entry.billable,
modified_at: new Date(),
})
.increment('version', 1)
.where({id: entry.id});

}

Expand All @@ -180,7 +185,8 @@ function mapRecord(input: EntriesRecord, project: Project, person: Person): Entr
description: input.description,
billable: !!input.billable,
createdAt: input.created_at,
modifiedAt: input.modified_at
modifiedAt: input.modified_at,
version: input.version,
};

}
24 changes: 24 additions & 0 deletions src/migrations/20221027052445_entry_version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Knex } from 'knex';


export async function up(knex: Knex): Promise<void> {

await knex.schema.alterTable('entries', table => {

table.integer('version').unsigned().notNullable().defaultTo(1);

});

}


export async function down(knex: Knex): Promise<void> {

await knex.schema.alterTable('entries', table => {

table.dropColumn('version');

});

}

3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ export type Entry = {

modifiedAt: Date;
createdAt: Date;
version: number;

}

export type NewEntry =
Omit<Entry, 'id' | 'href' | 'modifiedAt' | 'createdAt'>;
Omit<Entry, 'id' | 'href' | 'modifiedAt' | 'createdAt' | 'version'>;