add a shared property to Document

This commit is contained in:
2019-09-13 13:41:13 +03:00
parent 68c34364f6
commit 0b68dc2963
4 changed files with 72 additions and 11 deletions

View File

@@ -15,6 +15,7 @@ export interface IDocumentJSON {
content: string;
createdAt: number;
editedAt: number;
shared: boolean;
}
@Entity()
@@ -37,13 +38,17 @@ export class Document extends BaseEntity {
@Column({ type: "timestamp", default: null })
public editedAt: Date;
constructor(user: User, name: string, content: string) {
@Column({ type: "boolean", default: false })
public shared: boolean;
constructor(user: User, name: string, content: string, shared: boolean) {
super();
this.createdAt = new Date();
this.editedAt = this.createdAt;
this.user = user;
this.name = name;
this.content = content;
this.shared = shared;
}
public toJSON(user: number): IDocumentJSON {
@@ -54,6 +59,7 @@ export class Document extends BaseEntity {
content: this.content,
createdAt: this.createdAt.getTime(),
editedAt: this.editedAt.getTime(),
shared: this.shared,
};
}
}

View File

@@ -1,5 +1,6 @@
import * as Router from "koa-router";
import { Document } from "~entity/Document";
import { User } from "~entity/User";
export const docsRouter = new Router();
@@ -10,16 +11,17 @@ docsRouter.post("/docs/new", async ctx => {
const { user } = ctx.state;
const { name, content } = (ctx.request as any).body as {
const { name, content, shared } = (ctx.request as any).body as {
name: string | undefined;
content: string | undefined;
shared: boolean | undefined;
};
if (!name) {
ctx.throw(400);
}
const document = new Document(user.id, name, content);
const document = new Document(user.id, name, content, shared);
try {
await document.save();
@@ -53,7 +55,7 @@ docsRouter.patch("/docs/byID/:id", async ctx => {
content: string | undefined;
};
const document = await Document.findOne({ id, user: user.id });
const document = await Document.findOne({ id, user });
if (!document) {
ctx.throw(404);
@@ -86,7 +88,7 @@ docsRouter.get("/docs/list", async ctx => {
const { user } = ctx.state;
const documents = await Document.find({ user: user.id });
const documents = await Document.find({ user });
ctx.body = {
error: false,
@@ -109,7 +111,7 @@ docsRouter.get("/docs/byID/:id", async ctx => {
const { user } = ctx.state;
const document = await Document.findOne({ id, user: user.id });
const document = await Document.findOne({ id, user });
if (!document) {
ctx.throw(404);
@@ -121,6 +123,38 @@ docsRouter.get("/docs/byID/:id", async ctx => {
};
});
docsRouter.get("/docs/shared/:username/:id", async ctx => {
const { id, username } = ctx.params as {
id: number | undefined;
username: string | undefined;
};
if (!id || !username) {
ctx.throw(400);
}
const user = await User.findOne({ username });
if (!user) {
ctx.throw(404);
}
const document = await Document.findOne({ id, user });
if (!document) {
ctx.throw(404);
}
if (!document.shared) {
ctx.throw(401);
}
ctx.body = {
error: false,
data: document.toJSON(user.id),
};
});
docsRouter.delete("/docs/byID/:id", async ctx => {
if (!ctx.state.user) {
ctx.throw(401);
@@ -136,7 +170,7 @@ docsRouter.delete("/docs/byID/:id", async ctx => {
const { user } = ctx.state;
const document = await Document.findOne({ id, user: user.id });
const document = await Document.findOne({ id, user });
if (!document) {
ctx.throw(404);

View File

@@ -88,11 +88,28 @@ describe("docs", () => {
const documents = response.body.data as IDocumentJSON[];
const userDocs = [seed.doc1.toJSON(seed.user1.id)];
const userDocs = [
seed.doc1.toJSON(seed.user1.id),
seed.doc2p.toJSON(seed.user1.id),
];
expect(documents).to.deep.equal(userDocs);
});
it("should get a shared document", async () => {
const response = await request(callback)
.get(`/docs/shared/${seed.user1.username}/${seed.doc2p.id}`)
.expect(200);
expect(response.body.error).to.be.false;
const document = response.body.data as IDocumentJSON;
const usedDoc = seed.doc2p.toJSON(seed.user1.id);
expect(document).to.deep.equal(usedDoc);
});
it("should get a document", async () => {
const response = await request(callback)
.get(`/docs/byID/${seed.doc1.id}`)

View File

@@ -5,6 +5,7 @@ export interface ISeed {
user1: User;
user2: User;
doc1: Document;
doc2p: Document;
}
export async function seedDB(): Promise<ISeed> {
@@ -19,8 +20,11 @@ export async function seedDB(): Promise<ISeed> {
await user2.setPassword("User2");
await user2.save();
const doc1 = new Document(user1, "Doc1", "Doc1");
await doc1.save();
const doc1 = new Document(user1, "Doc1", "Doc1", false);
const doc2p = new Document(user1, "Doc2", "Doc2", true);
return { user1, user2, doc1 };
await doc1.save();
await doc2p.save();
return { user1, user2, doc1, doc2p };
}