Skip to content
This repository has been archived by the owner on Oct 2, 2020. It is now read-only.

Commit

Permalink
Finish implementation of most API routes
Browse files Browse the repository at this point in the history
Fixes #6

Partially implements #7

Signed-off-by: Ayane Satomi <[email protected]>
  • Loading branch information
sr229 committed Apr 26, 2020
1 parent 8852558 commit f992cba
Show file tree
Hide file tree
Showing 10 changed files with 198 additions and 21 deletions.
11 changes: 6 additions & 5 deletions lib/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const config: ConnectionOptions = {
"./entities/*.ts"
],
migrations: [
"./migrations/*.ts"
"./mdfigrations/*.ts"
]
}

Expand Down Expand Up @@ -113,17 +113,18 @@ export async function searchCollectionByKeyword(keyword: string) {
export async function searchUserById(id: number) {
let connection = await createConnection(config);

return await connection.getRepository(User).find({id: id});
return await connection.getRepository(User).findOneOrFail({id: id});
}

export async function searchPostById(id: number) {
let connection = await createConnection(config);

return await connection.getRepository(Post).find({id: id});
return await connection.getRepository(Post).findOneOrFail({id: id});
}

export async function searchCollectionById(id: number) {
let connection = await createConnection(config);

return await connection.getRepository(Post).find({id: id});
}
return await connection.getRepository(Post).findOneOrFail({id: id});

}
6 changes: 3 additions & 3 deletions lib/entities/Collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import {Tag} from './Tag';
@Entity()
export class Collection {
@PrimaryColumn("int", {unique: true})
public id: number;
public id: Number;

@Column("int")
@ManyToOne(type => User, user => user.collections)
public author: number;
public author: Number;

@Column("string", {length: 24})
public name: string;
public name: String;

@Column("array")
@ManyToMany(type => Post, post => post.id)
Expand Down
13 changes: 6 additions & 7 deletions lib/entities/Post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,22 @@ import {Tag} from './Tag';
@Entity()
export class Post {
@PrimaryColumn("int", {unique: true})
public id: number;
public id: Number;

@ManyToOne(type => User, user => user.posts)
public author: number;
public author: Number;

@Column("string", {length: 24})
public caption: string;
public caption: String;

@Column("string")
public cdnUrl: string;
public cdnUrl: String;

@Column("bool")
public isNsfw: boolean;
public isNsfw: Boolean;

@Column("timestamp")
@CreateDateColumn()
public dateCreated: string;
public dateCreated: String;

@ManyToMany(type => Tag, tags => tags.posts)
tags: Tag[];
Expand Down
11 changes: 5 additions & 6 deletions lib/entities/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ import {Collection} from "./Collection";
@Entity()
export class User {
@PrimaryColumn("int", {unique: true})
public id: number;
public id: Number;

@Column("string", {length: 24})
public username: string;
public username: String;

@Column("string")
public redditLink: string;
public redditLink: String;

@Column("string", {length: 100})
public bio: string;
public bio: String;

@Column("array")
@OneToMany(type => Post, post => post.id )
Expand All @@ -25,6 +25,5 @@ export class User {
public collections: Collection[];

@Column("timestamp")
@CreateDateColumn()
public dateCreated: string;
public dateCreated: String;
}
18 changes: 18 additions & 0 deletions pages/api/collections/[id].ts
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"});
}
}
})
39 changes: 39 additions & 0 deletions pages/api/collections/index.ts
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}`});
}
}
}
})
18 changes: 18 additions & 0 deletions pages/api/post/[id].ts
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"});
}
}
})
40 changes: 40 additions & 0 deletions pages/api/post/index.ts
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}`});
}
}
}
})
25 changes: 25 additions & 0 deletions pages/api/user/[id].ts
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."});
}
}
})
38 changes: 38 additions & 0 deletions pages/api/user/index.ts
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}`});
}
}
}
})

0 comments on commit f992cba

Please sign in to comment.