This repository has been archived by the owner on Oct 2, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Finish implementation of most API routes
Fixes #6 Partially implements #7 Signed-off-by: Ayane Satomi <[email protected]>
- Loading branch information
Showing
10 changed files
with
198 additions
and
21 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
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
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,18 @@ | ||
import { methods } from "../../../lib/methods"; | ||
import { NextApiRequest, NextApiResponse } from "next"; | ||
import { searchCollectionById } from "../../../lib/database"; | ||
|
||
|
||
export default methods({ | ||
get: async (req: NextApiRequest, res: NextApiResponse) => { | ||
const {query: {id}} = req; | ||
|
||
try { | ||
let dbResult = await searchCollectionById(parseInt(id[0])); | ||
|
||
res.status(200).json(dbResult); | ||
} catch (e) { | ||
res.status(404).json({code: res.statusCode.toString(), message: "Resource does not exist"}); | ||
} | ||
} | ||
}) |
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,39 @@ | ||
import { methods } from "../../../lib/methods"; | ||
import { NextApiRequest, NextApiResponse } from "next"; | ||
import { createCollection, getAllCollections } from "../../../lib/database"; | ||
import idGen from "../../../lib/idgen"; | ||
|
||
|
||
export default methods({ | ||
get: async (req: NextApiRequest, res: NextApiResponse) => { | ||
try { | ||
//TODO: paginate this in Frontend! | ||
let dbResult = await getAllCollections(); | ||
|
||
res.status(200).json(dbResult); | ||
} catch (e) { | ||
res.status(500).json({code: res.statusCode.toString(), message: `Something went wrong: ${e}`}); | ||
} | ||
}, | ||
post: { | ||
authorizationRequired: true, | ||
run: async (req: NextApiRequest, res: NextApiResponse) => { | ||
if (req.headers["content-type"] !== "application/json") res.end({code: 400, message: "Request body is not JSON."}); | ||
|
||
try { | ||
await createCollection({ | ||
id: idGen(), | ||
name: req.body.name, | ||
author: req.body.author, | ||
posts : req.body.posts, | ||
isNsfw: req.body.isNsfw, | ||
tags: req.body.tags | ||
}); | ||
|
||
res.status(204).end(); | ||
} catch (e) { | ||
res.status(500).json({code: res.statusCode.toString(), message: `Something went wrong: ${e}`}); | ||
} | ||
} | ||
} | ||
}) |
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,18 @@ | ||
import { methods } from "../../../lib/methods"; | ||
import { NextApiRequest, NextApiResponse } from "next"; | ||
import { searchPostById } from "../../../lib/database"; | ||
|
||
|
||
export default methods({ | ||
get: async (req: NextApiRequest, res: NextApiResponse) => { | ||
const {query: {id}} = req; | ||
|
||
try { | ||
let dbResult = await searchPostById(parseInt(id[0])); | ||
|
||
res.status(200).json(dbResult); | ||
} catch (e) { | ||
res.status(404).json({code: res.statusCode.toString(), message: "Resource does not exist"}); | ||
} | ||
} | ||
}) |
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,40 @@ | ||
import { methods } from "../../../lib/methods"; | ||
import { NextApiRequest, NextApiResponse } from "next"; | ||
import { createPost, getAllPosts } from "../../../lib/database"; | ||
import idGen from "../../../lib/idgen"; | ||
|
||
|
||
export default methods({ | ||
get : async (req: NextApiRequest, res: NextApiResponse) => { | ||
try { | ||
//TODO: Paginate this in Frontend! | ||
let dbResult = await getAllPosts(); | ||
|
||
res.status(200).json(dbResult); | ||
} catch (e) { | ||
res.status(500).json({code: res.statusCode.toString(), message: `Something went wrong: ${e}`}); | ||
} | ||
}, | ||
post: { | ||
authorizationRequired: true, | ||
run: async (req: NextApiRequest, res: NextApiResponse) => { | ||
if (req.headers["content-type"] !== "application/json") res.status(400).json({code: res.statusCode.toString, message: "Request body is not JSON."}); | ||
|
||
try { | ||
await createPost({ | ||
id: idGen(), | ||
author: req.body.id, | ||
caption: req.body.caption, | ||
cdnUrl: req.body.cdnUrl, | ||
tags: req.body.tags ?? [], | ||
dateCreated: new Date().toString(), | ||
isNsfw: req.body.isNsfw ?? false | ||
}); | ||
|
||
res.status(204).end(); | ||
} catch (e) { | ||
res.status(500).json({code: res.statusCode.toString(), message: `Something went wrong: ${e}`}); | ||
} | ||
} | ||
} | ||
}) |
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,25 @@ | ||
import { methods } from '../../../lib/methods'; | ||
import { NextApiRequest, NextApiResponse } from 'next'; | ||
import { searchUserById } from '../../../lib/database'; | ||
|
||
export default methods({ | ||
get: async (req: NextApiRequest, res: NextApiResponse) => { | ||
const {query: {id}} = req; | ||
|
||
try { | ||
let dbResult = await searchUserById(parseInt(id[0])); | ||
|
||
res.status(200).json(dbResult); | ||
} catch (e) { | ||
res.status(404).json({code: res.statusCode.toString(), message: "Resource does not exist"}); | ||
} | ||
}, | ||
patch:{ | ||
authorizationRequired: true, | ||
run: (req: NextApiRequest, res: NextApiResponse) => { | ||
const {query: {id}} = req; | ||
//TODO: add update function. | ||
res.status(503).json({code: res.statusCode.toString(), message: "PATCHs are not yet supported. stay tuned."}); | ||
} | ||
} | ||
}) |
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,38 @@ | ||
import {methods} from "../../../lib/methods"; | ||
import "../../../lib/database"; | ||
import { NextApiResponse, NextApiRequest } from "next"; | ||
import { createUser, getAllUsers } from "../../../lib/database"; | ||
import idGen from "../../../lib/idgen"; | ||
|
||
export default methods({ | ||
get: async (req: NextApiRequest, res: NextApiResponse) => { | ||
try { | ||
//TODO: Paginate this on Frontend! | ||
let dbResult = await getAllUsers(); | ||
res.status(200).json(dbResult); | ||
} catch (e) { | ||
res.status(500).json({code: res.statusCode.toString(), message: `Something went wrong: ${e}`}); | ||
} | ||
}, | ||
post: { | ||
authorizationRequired: true, | ||
run: async (req: NextApiRequest, res: NextApiResponse) => { | ||
if (req.headers["content-type"] !== "application/json") res.end({code: 400, message: "Request body is not JSON."}); | ||
|
||
try { | ||
await createUser({ | ||
id: idGen(), | ||
username: req.body.user, | ||
bio: req.body.bio ?? null, | ||
redditLink: req.body.redditLink, | ||
posts: [], | ||
collections: [], | ||
dateCreated: new Date().toString() | ||
}); | ||
res.status(204); | ||
} catch (e) { | ||
res.status(500).json({code: res.statusCode.toString(), message: `Something went wrong: ${e}`}); | ||
} | ||
} | ||
} | ||
}) |