Skip to content

Commit 2b56fb4

Browse files
committed
fix: article author relation
1 parent 20b92a1 commit 2b56fb4

File tree

6 files changed

+38
-22
lines changed

6 files changed

+38
-22
lines changed

src/article/article.entity.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Entity, PrimaryGeneratedColumn, Column, OneToOne, OneToMany, JoinColumn, AfterUpdate, BeforeUpdate } from 'typeorm';
1+
import { Entity, PrimaryGeneratedColumn, Column, OneToOne, ManyToOne, OneToMany, JoinColumn, AfterUpdate, BeforeUpdate } from 'typeorm';
22
import { UserEntity } from '../user/user.entity';
33
import { Comment } from './comment.entity';
44

@@ -14,10 +14,10 @@ export class ArticleEntity {
1414
@Column()
1515
title: string;
1616

17-
@Column()
17+
@Column({default: ''})
1818
description: string;
1919

20-
@Column()
20+
@Column({default: ''})
2121
body: string;
2222

2323
@Column({ type: 'timestamp', default: () => "CURRENT_TIMESTAMP"})
@@ -34,8 +34,7 @@ export class ArticleEntity {
3434
@Column('simple-array')
3535
tagList: string[];
3636

37-
@OneToOne(type => UserEntity)
38-
@JoinColumn()
37+
@ManyToOne(type => UserEntity, user => user.articles)
3938
author: UserEntity;
4039

4140
@OneToMany(type => Comment, comment => comment.article, {eager: true})

src/article/article.service.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ export class ArticleService {
2626
async findAll(query): Promise<ArticlesRO> {
2727

2828
const qb = await getRepository(ArticleEntity)
29-
.createQueryBuilder('article');
29+
.createQueryBuilder('article')
30+
.leftJoinAndSelect('article.author', 'author');
3031

3132
qb.where("1 = 1");
3233

@@ -83,7 +84,7 @@ export class ArticleService {
8384
}
8485

8586
const articles = await qb.getMany();
86-
87+
8788
return {articles, articlesCount};
8889
}
8990

@@ -162,16 +163,28 @@ export class ArticleService {
162163
}
163164

164165
async create(userId: number, articleData: CreateArticleDto): Promise<ArticleEntity> {
165-
const author = await this.userRepository.findOneById(userId);
166+
166167
let article = new ArticleEntity();
167168
article.title = articleData.title;
168169
article.description = articleData.description;
169170
article.slug = this.slugify(articleData.title);
170171
article.tagList = articleData.tagList || [];
171172
article.comments = [];
172-
article.author = author;
173173

174-
return await this.articleRepository.save(article);
174+
const newArticle = await this.articleRepository.save(article);
175+
176+
const author = await this.userRepository.findOneById(userId);
177+
178+
if (Array.isArray(author.articles)) {
179+
author.articles.push(article);
180+
} else {
181+
author.articles = [article];
182+
}
183+
184+
await this.userRepository.save(author);
185+
186+
return newArticle;
187+
175188
}
176189

177190
async update(slug: string, articleData: any): Promise<ArticleRO> {

src/profile/profile.controller.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ export class ProfileController {
2222
}
2323

2424
@Post(':username/follow')
25-
async follow(@User('id') userId: number, @Param('username') username: string): Promise<ProfileRO> {
26-
return await this.profileService.follow(userId, username);
25+
async follow(@User('email') email: string, @Param('username') username: string): Promise<ProfileRO> {
26+
return await this.profileService.follow(email, username);
2727
}
2828

2929
@Delete(':username/follow')

src/profile/profile.service.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,23 @@ export class ProfileService {
4747
return {profile};
4848
}
4949

50-
async follow(followerId: number, username: string): Promise<ProfileRO> {
51-
if (!followerId || !username) {
52-
throw new HttpException('FollowerId and username not provided.', HttpStatus.BAD_REQUEST);
50+
async follow(followerEmail: string, username: string): Promise<ProfileRO> {
51+
if (!followerEmail || !username) {
52+
throw new HttpException('Follower email and username not provided.', HttpStatus.BAD_REQUEST);
5353
}
5454

5555
const followingUser = await this.userRepository.findOne({username});
56+
const followerUser = await this.userRepository.findOne({email: followerEmail});
5657

57-
if (followingUser.id === followerId) {
58-
throw new HttpException('FollowerId and FollowingId cannot be equal.', HttpStatus.BAD_REQUEST);
58+
if (followingUser.email === followerEmail) {
59+
throw new HttpException('FollowerEmail and FollowingId cannot be equal.', HttpStatus.BAD_REQUEST);
5960
}
6061

61-
const _follows = await this.followsRepository.findOne( {followerId, followingId: followingUser.id});
62+
const _follows = await this.followsRepository.findOne( {followerId: followerUser.id, followingId: followingUser.id});
6263

6364
if (!_follows) {
6465
const follows = new FollowsEntity();
65-
follows.followerId = followerId;
66+
follows.followerId = followerUser.id;
6667
follows.followingId = followingUser.id;
6768
await this.followsRepository.save(follows);
6869
}

src/user/user.entity.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Entity, PrimaryGeneratedColumn, Column, BeforeInsert, JoinTable, ManyToMany} from "typeorm";
1+
import {Entity, PrimaryGeneratedColumn, Column, BeforeInsert, JoinTable, ManyToMany, OneToMany} from "typeorm";
22
import { IsEmail, Validate } from 'class-validator';
33
import * as crypto from 'crypto';
44
import { ArticleEntity } from '../article/article.entity';
@@ -30,8 +30,10 @@ export class UserEntity {
3030
this.password = crypto.createHmac('sha256', this.password).digest('hex');
3131
}
3232

33-
@ManyToMany(type => ArticleEntity, {eager: true})
33+
@ManyToMany(type => ArticleEntity)
3434
@JoinTable()
35-
favorites: ArticleEntity[]
35+
favorites: ArticleEntity[];
3636

37+
@OneToMany(type => ArticleEntity, article => article.author)
38+
articles: ArticleEntity[];
3739
}

src/user/user.service.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export class UserService {
5353
newUser.username = username;
5454
newUser.email = email;
5555
newUser.password = password;
56+
newUser.articles = [];
5657

5758
const errors = await validate(newUser);
5859
if (errors.length > 0) {

0 commit comments

Comments
 (0)