-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from ether/feat/database
Feat(database)
- Loading branch information
Showing
56 changed files
with
6,070 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
} | ||
} | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
} | ||
} | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}; |
Oops, something went wrong.