Skip to content

Commit

Permalink
Merge pull request #27 from ether/feat/database
Browse files Browse the repository at this point in the history
Feat(database)
  • Loading branch information
SamTV12345 authored Feb 27, 2024
2 parents e767e4f + 811dafd commit 5b2e29a
Show file tree
Hide file tree
Showing 56 changed files with 6,070 additions and 173 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,10 @@ next-env.d.ts
# Production configuration file
/settings.json
storybook-static/

# Var files
var/dirty.db

# API keys
/APIKEY.txt

180 changes: 180 additions & 0 deletions api/author/init.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
import { fastifyServer } from '@/server';
import {
createAuthor,
createAuthorIfNotExistsFor,
getAuthorName,
listPadsOfAuthor,
} from '@/service/pads/AuthorManager';
import {
BASE_PATH_AUTHOR_GET,
BASE_PATH_AUTHOR_PAD,
BASE_PATH_AUTHORS,
} from '@/api/constants';


export const initAuthor = () => {
fastifyServer.post<{
Body: {
authorName: string;
authorMapper?: string;
ifNotExists?: boolean;
};
}>(
BASE_PATH_AUTHORS,
{
schema: {
tags: ['author'],
body: {
type: 'object',
properties: {
authorName: { type: 'string' },
authorMapper: { type: 'string' },
ifNotExists: { type: 'boolean' },
},
required: ['authorName'],
},
response: {
200: {
type: 'object',
properties: {
code: { type: 'number' },
data: {
type: 'object',
properties: {},
},
},
},
500: {
type: 'object',
properties: {
code: { type: 'number' },
message: { type: 'string' },
},
},
},
},
},
async (req, res) => {
const { authorName, authorMapper, ifNotExists } = req.body;
try {
let author;

if (ifNotExists) {
author = await createAuthorIfNotExistsFor(authorMapper!, authorName);
} else {
author = await createAuthor(authorName);
}

return {
code: 0,
data: {
author,
},
message: 'ok',
};
} catch (e: any) {
res.status(500);
return {
code: 1,
message: e.message,
};
}
}
);

fastifyServer.get<{
Params: {
authorId?: string;
};
}>(
BASE_PATH_AUTHOR_PAD,
{
schema: {
tags: ['author'],
querystring: {
authorName: { type: 'string' },
},
response: {
200: {
type: 'object',
properties: {
code: { type: 'number' },
data: {
type: 'object',
properties: {},
},
},
},
},
},
},
async (req, res) => {
const { authorId } = req.params;

try {
const author = await listPadsOfAuthor(authorId!);
return {
code: 0,
data: {
author,
},
message: 'ok',
};
} catch (e: any) {
return {
code: 1,
message: e.message,
};
}
}
);

fastifyServer.get<{
Params: {
authorName: string;
};
}>(
BASE_PATH_AUTHOR_GET,
{
schema: {
tags: ['author'],
params: {
authorName: { type: 'string' },
},
response: {
200: {
type: 'object',
properties: {
code: { type: 'number' },
data: {
type: 'object',
properties: {},
},
},
},
},
},
},
async (req, res) => {
const { authorName } = req.params;
let author: string;
try {
author = await getAuthorName(authorName);

return {
code: 0,
data: {
author,
},
message: 'ok',
};
} catch (e: any) {
res.status(500);
return {
code: 1,
message: e.message,
};
}
}
);
};
17 changes: 17 additions & 0 deletions api/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const BASE_PATH = "/api/v1";
export const BASE_PATH_AUTHORS = `${BASE_PATH}/authors`;
export const BASE_PATH_GROUPS = `${BASE_PATH}/groups`;
export const BASE_PATH_PADS = `${BASE_PATH}/pads`;
export const BASE_PATH_SESSIONS = `${BASE_PATH}/sessions`;








// Special paths for authors

export const BASE_PATH_AUTHOR_PAD = `${BASE_PATH_AUTHORS}/:authorID/pads`;
export const BASE_PATH_AUTHOR_GET = `${BASE_PATH_AUTHORS}/:authorID`;
168 changes: 168 additions & 0 deletions api/groups/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
import { fastifyServer } from '@/server';
import {
createGroup,
listAllGroups,
createGroupIfNotExistsFor,
deleteGroup,
} from '@/service/pads/GroupManager';

const basePath = '/api/groups';
export const initGroups = () => {
fastifyServer.post<{
Querystring: {
ifNotExists?: boolean;
appGroupId?: string;
};
}>(
basePath,
{
schema: {
tags: ['group'],
querystring: {
ifNotExists: { type: 'boolean' },
appGroupId: { type: 'string' },
},
response: {
200: {
type: 'object',
properties: {
code: { type: 'number' },
data: {
type: 'object',
properties: {
groupId: { type: 'string' },
},
},
message: { type: 'string' },
},
},
},
security: [
{
apiKey: [],
},
],
},
},
async (req, res) => {
let groupId;

const { ifNotExists, appGroupId } = req.query;

if (ifNotExists) {
if (!appGroupId) {
return {
code: 1,
message: 'appGroupId is required',
};
}

groupId = await createGroupIfNotExistsFor(appGroupId!);
} else {
groupId = await createGroup();
}

return {
code: 0,
data: {
groupId,
},
message: 'ok',
};
}
);

fastifyServer.get(
basePath,
{
schema: {
tags: ['group'],
response: {
200: {
type: 'object',
properties: {
code: { type: 'number' },
data: {
type: 'object',
properties: {
groupId: {
type: 'array',
items: {
type: 'string',
},
},
},
},
message: { type: 'string' },
},
},
},
security: [
{
apiKey: [],
},
],
},
},
async () => {
const { groupIDs } = await listAllGroups();

return {
code: 0,
data: {
groupId: groupIDs,
},
message: 'ok',
};
}
);

fastifyServer.delete<{
Params: {
groupId: string;
};
}>(
`${basePath}/:groupId`,
{
schema: {
tags: ['group'],
params: {
groupId: { type: 'string' },
},
response: {
200: {
type: 'object',
properties: {
code: { type: 'number' },
message: { type: 'string' },
},
},
409: {
type: 'object',
properties: {
code: { type: 'number' },
message: { type: 'string' },
},
},
},
},
},
async (req, res) => {
const groupId = req.params.groupId;

try {
await deleteGroup(groupId);
return {
code: 0,
message: 'ok',
};
} catch (e: any) {
res.statusCode = 409;
return {
code: 1,
message: e.message,
};
}
}
);
};
11 changes: 11 additions & 0 deletions api/initAPIRoots.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { initGroups } from './groups/api';
import { initPads } from '@/api/pads/init';
import { initAuthor } from '@/api/author/init';
import { initSession } from '@/api/session/initSession';

export const initAPIRoots = () => {
initGroups();
initPads();
initAuthor();
initSession();
};
Loading

0 comments on commit 5b2e29a

Please sign in to comment.