Skip to content

Commit

Permalink
add deleting
Browse files Browse the repository at this point in the history
  • Loading branch information
lastmjs committed May 3, 2024
1 parent 7ae5677 commit 081a30a
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 31 deletions.
24 changes: 22 additions & 2 deletions examples/sqlite/src/server/posts/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export function countPosts(db: Database): number {
}
}

export function createPost(db: Database, postCreate: PostCreate): number {
export function createPost(db: Database, postCreate: PostCreate): Post {
db.run(
'INSERT INTO posts (user_id, title, body) VALUES (:user_id, :title, :body)',
{
Expand All @@ -78,7 +78,13 @@ export function createPost(db: Database, postCreate: PostCreate): number {

const id = db.exec('SELECT last_insert_rowid()')[0].values[0][0] as number;

return id;
const post = getPost(db, id);

if (post === null) {
throw new Error(`createPost: could not post with id ${id}`);
}

return post;
}

export function updatePost(db: Database, postUpdate: PostUpdate): Post {
Expand All @@ -103,6 +109,20 @@ export function updatePost(db: Database, postUpdate: PostUpdate): Post {
return post;
}

export function deletePost(db: Database, id: number): number {
db.run(`DELETE FROM posts WHERE id = :id`, {
':id': id
});

const post = getPost(db, id);

if (post !== null) {
throw new Error(`deletePost: could not delete post with id ${id}`);
}

return id;
}

export function convertQueryExecResultToUser(sqlValues: SqlValue[]): Post {
return {
id: sqlValues[0] as number,
Expand Down
21 changes: 18 additions & 3 deletions examples/sqlite/src/server/posts/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@ import { v4 } from 'uuid';

import { db } from '../../';
import { createUser } from '../users/db';
import { countPosts, createPost, getPost, getPosts, updatePost } from './db';
import {
countPosts,
createPost,
deletePost,
getPost,
getPosts,
updatePost
} from './db';

export function getRouter(): Router {
const router = express.Router();
Expand Down Expand Up @@ -45,13 +52,13 @@ export function getRouter(): Router {
age: 33
});

const id = createPost(db, {
const post = createPost(db, {
user_id: user.id,
title,
body
});

res.json(id);
res.json(post);
}
);

Expand Down Expand Up @@ -80,6 +87,14 @@ export function getRouter(): Router {

router.patch('/', updateHandler);

router.delete('/', (req: Request<any, any, { id: number }>, res) => {
const { id } = req.body;

const deletedId = deletePost(db, id);

res.json(deletedId);
});

return router;
}

Expand Down
25 changes: 21 additions & 4 deletions examples/sqlite/src/server/users/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,13 @@ export function createUser(db: Database, userCreate: UserCreate): User {

const id = db.exec('SELECT last_insert_rowid()')[0].values[0][0] as number;

return {
...userCreate,
id
};
const user = getUser(db, id);

if (user === null) {
throw new Error(`updateUser: could not find user with id ${id}`);
}

return user;
}

export function updateUser(db: Database, userUpdate: UserUpdate): User {
Expand All @@ -88,6 +91,20 @@ export function updateUser(db: Database, userUpdate: UserUpdate): User {
return user;
}

export function deleteUser(db: Database, id: number): number {
db.run(`DELETE FROM users WHERE id = :id`, {
':id': id
});

const user = getUser(db, id);

if (user !== null) {
throw new Error(`deleteUser: could not delete user with id ${id}`);
}

return id;
}

export function convertQueryExecResultToUser(sqlValues: SqlValue[]): User {
return {
id: sqlValues[0] as number,
Expand Down
17 changes: 16 additions & 1 deletion examples/sqlite/src/server/users/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ import express, { Request, Response, Router } from 'express';
import { v4 } from 'uuid';

import { db } from '../../';
import { countUsers, createUser, getUser, getUsers, updateUser } from './db';
import {
countUsers,
createUser,
deleteUser,
getUser,
getUsers,
updateUser
} from './db';

export function getRouter(): Router {
const router = express.Router();
Expand Down Expand Up @@ -67,6 +74,14 @@ export function getRouter(): Router {

router.patch('/', updateHandler);

router.delete('/', (req: Request<any, any, { id: number }>, res) => {
const { id } = req.body;

const deletedId = deleteUser(db, id);

res.json(deletedId);
});

return router;
}

Expand Down
103 changes: 82 additions & 21 deletions examples/sqlite/test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,28 +37,28 @@ export function getTests(canisterId: string): Test[] {
...usersTestsBeforeBatch(origin),
...postsTestsBeforeBatch(origin),
{
name: '/users/batch/999',
name: '/users/batch/499',
test: async () => {
const response = await fetch(`${origin}/users/batch/999`, {
const response = await fetch(`${origin}/users/batch/499`, {
method: 'POST'
});
const responseJson = await response.json();

return {
Ok: responseJson.Success === '999 users created'
Ok: responseJson.Success === '499 users created'
};
}
},
{
name: '/posts/batch/499',
name: '/posts/batch/299',
test: async () => {
const response = await fetch(`${origin}/posts/batch/499`, {
const response = await fetch(`${origin}/posts/batch/299`, {
method: 'POST'
});
const responseJson = await response.json();

return {
Ok: responseJson.Success === '499 posts created'
Ok: responseJson.Success === '299 posts created'
};
}
},
Expand Down Expand Up @@ -95,7 +95,61 @@ export function getTests(canisterId: string): Test[] {
}
},
...usersTestsAfterBatch(origin),
...postsTestsAfterBatch(origin)
...postsTestsAfterBatch(origin),
{
name: '/users delete',
test: async () => {
const createUserResponse = await fetch(`${origin}/users`, {
method: 'POST',
headers: [['Content-Type', 'application/json']],
body: JSON.stringify({
username: 'lastmjs_about_to_delete',
age: 33
})
});
const createUserResponseJson = await createUserResponse.json();

const deleteUserResponse = await fetch(`${origin}/users`, {
method: 'DELETE',
headers: [['Content-Type', 'application/json']],
body: JSON.stringify({
id: createUserResponseJson.id
})
});
const deleteUserResponseJson = await deleteUserResponse.json();

return {
Ok: deleteUserResponseJson === createUserResponseJson.id
};
}
},
{
name: '/posts delete',
test: async () => {
const createPostResponse = await fetch(`${origin}/posts`, {
method: 'POST',
headers: [['Content-Type', 'application/json']],
body: JSON.stringify({
title: 'Post about to be deleted',
body: 'Body will be deleted with post'
})
});
const createPostResponseJson = await createPostResponse.json();

const deletePostResponse = await fetch(`${origin}/posts`, {
method: 'DELETE',
headers: [['Content-Type', 'application/json']],
body: JSON.stringify({
id: createPostResponseJson.id
})
});
const deletePostResponseJson = await deletePostResponse.json();

return {
Ok: deletePostResponseJson === createPostResponseJson.id
};
}
}
];
}

Expand Down Expand Up @@ -286,7 +340,14 @@ function postsTestsBeforeBatch(origin: string): Test[] {
const responseJson = await response.json();

return {
Ok: responseJson === 1
Ok:
responseJson.id === 1 &&
responseJson.title === 'Post 1' &&
responseJson.body ===
'It is a very intriguing post yes' &&
responseJson.user.id === 2 &&
typeof responseJson.user.username === 'string' &&
typeof responseJson.user.age === 'number'
};
}
},
Expand Down Expand Up @@ -393,30 +454,30 @@ function usersTestsAfterBatch(origin: string): Test[] {
const responseJson = await response.json();

return {
Ok: responseJson.length === 1_500
Ok: responseJson.length === 800
};
}
},
{
name: '/users/count 1_500',
name: '/users/count 800',
test: async () => {
const response = await fetch(`${origin}/users/count`);
const responseJson = await response.json();

return {
Ok: responseJson === 1_500
Ok: responseJson === 800
};
}
},
{
name: '/users/1500 not null',
name: '/users/800 not null',
test: async () => {
const response = await fetch(`${origin}/users/1500`);
const response = await fetch(`${origin}/users/800`);
const responseJson = await response.json();

return {
Ok:
responseJson.id === 1_500 &&
responseJson.id === 800 &&
typeof responseJson.username === 'string' &&
typeof responseJson.age === 'number'
};
Expand All @@ -430,34 +491,34 @@ function postsTestsAfterBatch(origin: string): Test[] {
{
name: '/posts not empty',
test: async () => {
const response = await fetch(`${origin}/posts?limit=400`);
const response = await fetch(`${origin}/posts?limit=300`);
const responseJson = await response.json();

return {
Ok: responseJson.length === 400
Ok: responseJson.length === 300
};
}
},
{
name: '/posts/count 500',
name: '/posts/count 300',
test: async () => {
const response = await fetch(`${origin}/posts/count`);
const responseJson = await response.json();

return {
Ok: responseJson === 500
Ok: responseJson === 300
};
}
},
{
name: '/posts/500 not null',
name: '/posts/300 not null',
test: async () => {
const response = await fetch(`${origin}/posts/500`);
const response = await fetch(`${origin}/posts/300`);
const responseJson = await response.json();

return {
Ok:
responseJson.id === 500 &&
responseJson.id === 300 &&
typeof responseJson.title === 'string' &&
typeof responseJson.body === 'string' &&
typeof responseJson.user.id === 'number' &&
Expand Down

0 comments on commit 081a30a

Please sign in to comment.