Skip to content

Commit a4f064a

Browse files
committed
Comments schema update
1 parent 1174563 commit a4f064a

File tree

6 files changed

+30
-18
lines changed

6 files changed

+30
-18
lines changed

src/app.controller.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,14 @@ export class AppController {
5151

5252
@Post('/comment')
5353
async addComment(@Body() dto: AddCommentDto) {
54-
const token = await this.appService.getTokenById(dto.tokenId)
54+
const token = await this.appService.getTokenByAddress(dto.tokenAddress)
5555
if(!token) {
5656
throw new NotFoundException('Token not found')
5757
}
58+
const user = await this.userService.getUserByAddress(dto.userAddress)
59+
if(!user) {
60+
throw new NotFoundException('User not found')
61+
}
5862
return await this.appService.addComment(dto)
5963
}
6064

src/app.service.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -241,13 +241,13 @@ export class AppService {
241241
return await this.dataSource.manager.find(Comment, {
242242
where: {
243243
token: {
244-
id: dto.tokenId
244+
address: dto.tokenAddress
245245
}
246246
},
247247
take: +dto.limit,
248248
skip: +dto.offset,
249249
order: {
250-
createdAt: 'desc'
250+
createdAt: 'asc'
251251
}
252252
})
253253
}
@@ -257,6 +257,7 @@ export class AppService {
257257
const query = this.dataSource.getRepository(Token)
258258
.createQueryBuilder('token')
259259
.leftJoinAndSelect('token.user', 'user')
260+
// .leftJoinAndSelect('token.comments', 'comments')
260261
.offset(offset)
261262
.limit(limit)
262263
.orderBy({
@@ -267,7 +268,6 @@ export class AppService {
267268
query.where('token.name = :name', { name: search })
268269
.orWhere('token.address = :address', { address: search })
269270
.orWhere('token.symbol = :symbol', { symbol: search })
270-
.orWhere('token.id = :id', { id: search })
271271
.orWhere('token.txnHash = :txnHash', { txnHash: search })
272272
}
273273

@@ -306,10 +306,12 @@ export class AppService {
306306
}
307307

308308
async addComment(dto: AddCommentDto): Promise<string> {
309-
const token = await this.getTokenById(dto.tokenId)
309+
const token = await this.getTokenByAddress(dto.tokenAddress)
310+
const user = await this.userService.getUserByAddress(dto.userAddress)
310311
const comment = this.dataSource.manager.create(Comment, {
311312
...dto,
312-
token
313+
token,
314+
user
313315
})
314316
const { identifiers } = await this.dataSource.manager.insert(Comment, comment)
315317
return identifiers[0].id

src/dto/comment.dto.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export class AddCommentDto {
77
@Transform((address) => address.value.trim().toLowerCase())
88
@Type(() => String)
99
@IsString()
10-
tokenId: string;
10+
tokenAddress: string;
1111

1212
@ApiProperty({ type: String, required: true })
1313
@Transform((address) => address.value.trim().toLowerCase())
@@ -27,7 +27,7 @@ export class GetCommentsDto {
2727
@Transform((address) => address.value.trim().toLowerCase())
2828
@Type(() => String)
2929
@IsString()
30-
tokenId: string;
30+
tokenAddress: string;
3131

3232
@ApiProperty({ type: Number, required: false, default: '100' })
3333
// @Transform((limit) => limit.value.toNumber())

src/entities/comment.entity.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,26 @@ import {
77
} from 'typeorm';
88
import { ApiProperty } from '@nestjs/swagger';
99
import {Token} from "./token.entity";
10+
import {UserAccount} from "./user-account.entity";
1011

1112
@Entity({ name: 'comments' })
1213
export class Comment {
1314
@ApiProperty()
14-
@PrimaryGeneratedColumn('uuid')
15-
id: string;
16-
17-
@ApiProperty()
18-
@Column()
19-
userAddress: string;
15+
@PrimaryGeneratedColumn()
16+
id: number;
2017

2118
@ApiProperty()
2219
@Column()
2320
text: string;
2421

25-
@ManyToOne(() => Token, (token) => token.comments)
22+
@ManyToOne(() => UserAccount, (userAccount) => userAccount.comments, {
23+
eager: true
24+
})
25+
user: UserAccount
26+
27+
@ManyToOne(() => Token, (token) => token.comments, {
28+
eager: true
29+
})
2630
token: Token
2731

2832
@ApiProperty()

src/entities/token.entity.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,7 @@ export class Token {
4848
@JoinTable()
4949
user: UserAccount
5050

51-
@OneToMany(() => Comment, (comment) => comment.token, {
52-
eager: true
53-
})
51+
@OneToMany(() => Comment, (comment) => comment.token)
5452
@JoinTable()
5553
comments: Comment[]
5654

src/entities/user-account.entity.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
} from 'typeorm';
1111
import { ApiProperty } from '@nestjs/swagger';
1212
import {Token} from "./token.entity";
13+
import {Comment} from "./comment.entity";
1314

1415
@Entity({ name: 'users' })
1516
export class UserAccount {
@@ -28,6 +29,9 @@ export class UserAccount {
2829
@OneToMany(() => Token, (token) => token.user)
2930
tokens: Token[]
3031

32+
@OneToMany(() => Comment, (comment) => comment.user)
33+
comments: Comment[]
34+
3135
@ApiProperty()
3236
@CreateDateColumn({ name: 'createdAt' })
3337
createdAt: Date;

0 commit comments

Comments
 (0)