Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Candidate/maciek wasiak #58

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,100 changes: 2,100 additions & 0 deletions ConduitUpdated.postman_collection.json

Large diffs are not rendered by default.

174 changes: 115 additions & 59 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"@nestjs/microservices": "^7.0.5",
"@nestjs/platform-express": "^7.0.5",
"@nestjs/swagger": "^4.4.0",
"@nestjs/testing": "^7.0.5",
"@nestjs/typeorm": "^7.0.0",
"@nestjs/websockets": "^7.0.5",
"argon2": "^0.26.2",
Expand All @@ -37,7 +36,7 @@
"crypto": "^1.0.1",
"crypto-js": "^4.0.0",
"jsonwebtoken": "^8.5.1",
"mysql": "^2.18.1",
"mysql2": "^2.2.5",
"passport-jwt": "^4.0.0",
"reflect-metadata": "^0.1.13",
"rxjs": "^6.5.5",
Expand All @@ -47,6 +46,7 @@
"typescript": "^3.8.3"
},
"devDependencies": {
"@nestjs/testing": "^7.6.15",
"@types/jest": "^25.2.1",
"@types/node": "^13.13.4",
"atob": ">=2.1.0",
Expand Down
102 changes: 102 additions & 0 deletions src/article/article.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { Test, TestingModule } from "@nestjs/testing";
import { ArticleController } from "./article.controller";
import { ArticleService } from "./article.service";
import { UserService } from "../user/user.service";
import { UserController } from "../user/user.controller";
import { UserEntity } from "../user/user.entity";
import { TypeOrmModule } from "@nestjs/typeorm";
import { ArticleEntity } from "./article.entity";
import { UserData, UserRO } from "../user/user.interface";
import { Comment } from "./comment.entity";
import { FollowsEntity } from "../profile/follows.entity";
import { ExecutionContext } from "@nestjs/common";
import * as jwt from "jsonwebtoken";
import { SECRET } from "../config";

describe("ArticleController", () => {
let articleController: ArticleController;
let articleService: ArticleService;
let userController: UserController;
let userService: UserService;
let user: UserEntity;

beforeAll(async () => {
const app: TestingModule = await Test.createTestingModule({
imports: [
TypeOrmModule.forRoot(),
TypeOrmModule.forFeature([
ArticleEntity,
Comment,
UserEntity,
FollowsEntity,
]),
],
controllers: [ArticleController, UserController],
providers: [ArticleService, UserService],
}).compile();

articleController = app.get<ArticleController>(ArticleController);
userController = app.get<UserController>(UserController);
articleService = app.get<ArticleService>(ArticleService);
userService = app.get<UserService>(UserService);

let timestamp = Date.now();
let email = `test${timestamp}@test.io`;
let password = "testPASS123";
let username = `user${timestamp}`;

let userRO = await userController.create({
email,
password,
username,
});

userRO = await userController.login({
email,
password,
});

user = jwt.verify(userRO.user.token, SECRET);
});

describe("root", () => {
it("should return article list", () => {
expect(articleController.getFeed(user.id, null)).not.toBeNull();
});
});

describe("root", () => {
it("should create an article", async () => {
await articleController.create(user.id, {
title: "string",
description: "string",
body: "string",
tagList: [],
isMature: false
});

let results = await articleController.findAll({ author: user.username });

expect(results).not.toBeNull();
expect(results.articlesCount).toEqual(1);
});
});

describe("root", () => {
it("should create an article and check if isMature is present", async () => {
const newArticle = await articleController.create(user.id, {
title: "string",
description: "string",
body: "string",
tagList: [],
isMature: true
});

let results = await articleController.findOne(newArticle.slug);

expect(results).not.toBeNull();
expect(results.article).toHaveProperty('isMature');
expect(results.article.isMature).toEqual(true);
});
});
});
9 changes: 9 additions & 0 deletions src/article/article.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,13 @@ export class ArticleController {
return await this.articleService.unFavorite(userId, slug);
}

@Post(':slug/readlater')
async readLater(@User('id') userId: number, @Param('slug') slug) {
return await this.articleService.readLater(userId, slug);
}

@Delete(':slug/readlater')
async unReadLater(@User('id') userId: number, @Param('slug') slug) {
return await this.articleService.unReadLater(userId, slug);
}
}
6 changes: 6 additions & 0 deletions src/article/article.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,10 @@ export class ArticleEntity {

@Column({default: 0})
favoriteCount: number;

@Column({default: false})
isMature: boolean;

@Column({default: 0})
readLaterCount: number;
}
35 changes: 35 additions & 0 deletions src/article/article.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,40 @@ export class ArticleService {
return {article};
}

async readLater(id: number, slug: string): Promise<ArticleRO> {
let article = await this.articleRepository.findOne({slug});
const user = await this.userRepository.findOne(id);

const isReadLater = user.readLater.findIndex(_article => _article.id === article.id) < 0;
if (isReadLater) {
user.readLater.push(article);
article.readLaterCount++;

await this.userRepository.save(user);
article = await this.articleRepository.save(article);
}

return {article};
}

async unReadLater(id: number, slug: string): Promise<ArticleRO> {
let article = await this.articleRepository.findOne({slug});
const user = await this.userRepository.findOne(id);

const deleteIndex = user.readLater.findIndex(_article => _article.id === article.id);

if (deleteIndex >= 0) {

user.readLater.splice(deleteIndex, 1);
article.readLaterCount--;

await this.userRepository.save(user);
article = await this.articleRepository.save(article);
}

return {article};
}

async findComments(slug: string): Promise<CommentsRO> {
const article = await this.articleRepository.findOne({slug});
return {comments: article.comments};
Expand All @@ -175,6 +209,7 @@ export class ArticleService {
article.slug = this.slugify(articleData.title);
article.tagList = articleData.tagList || [];
article.comments = [];
article.isMature = articleData.isMature;

const newArticle = await this.articleRepository.save(article);

Expand Down
1 change: 1 addition & 0 deletions src/article/dto/create-article.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export class CreateArticleDto {
readonly description: string;
readonly body: string;
readonly tagList: string[];
readonly isMature: boolean;
}
3 changes: 3 additions & 0 deletions src/user/user.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,7 @@ export class UserEntity {

@OneToMany(type => ArticleEntity, article => article.author)
articles: ArticleEntity[];

@OneToMany(type => ArticleEntity, article => article.author)
readLater: ArticleEntity[];
}