Skip to content

Commit

Permalink
include votes in comment model
Browse files Browse the repository at this point in the history
  • Loading branch information
abdulhakim2902 committed Oct 28, 2021
1 parent 62b4b36 commit f127ed2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/__tests__/helpers/database.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export async function givenRepositories(testdb: any) {
async () => userRepository,
async () => transactionRepository,
async () => commentLinkRepository,
async () => voteRepository,
);
const transactionRepository: TransactionRepository =
new TransactionRepository(
Expand Down
4 changes: 4 additions & 0 deletions src/models/comment.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
model,
property,
} from '@loopback/repository';
import {Vote} from '.';
import {ReferenceType, SectionType} from '../enums';
import {Metric} from '../interfaces';
import {CommentLink} from './comment-link.model';
Expand Down Expand Up @@ -118,6 +119,9 @@ export class Comment extends Entity {
})
comments: Comment[];

@hasMany(() => Vote, {keyTo: 'referenceId'})
votes: Vote[];

@hasMany(() => Transaction, {keyTo: 'referenceId'})
transactions: Transaction[];

Expand Down
14 changes: 14 additions & 0 deletions src/repositories/comment.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import {
HasManyThroughRepositoryFactory,
repository,
} from '@loopback/repository';
import {VoteRepository} from '.';
import {MongoDataSource} from '../datasources';
import {
Comment,
CommentLink,
CommentRelations,
Transaction,
User,
Vote,
} from '../models';
import {CommentLinkRepository} from './comment-link.repository';
import {TransactionRepository} from './transaction.repository';
Expand All @@ -25,6 +27,11 @@ export class CommentRepository extends DefaultCrudRepository<
> {
public readonly user: BelongsToAccessor<User, typeof Comment.prototype.id>;

public readonly votes: HasManyRepositoryFactory<
Vote,
typeof Vote.prototype.id
>;

public readonly transactions: HasManyRepositoryFactory<
Transaction,
typeof Comment.prototype.id
Expand All @@ -45,6 +52,8 @@ export class CommentRepository extends DefaultCrudRepository<
protected transactionRepositoryGetter: Getter<TransactionRepository>,
@repository.getter('CommentLinkRepository')
protected commentLinkRepositoryGetter: Getter<CommentLinkRepository>,
@repository.getter('VoteRepository')
protected voteRepositoryGetter: Getter<VoteRepository>,
) {
super(Comment, dataSource);
this.comments = this.createHasManyThroughRepositoryFactoryFor(
Expand All @@ -63,5 +72,10 @@ export class CommentRepository extends DefaultCrudRepository<
'transactions',
this.transactions.inclusionResolver,
);
this.votes = this.createHasManyRepositoryFactoryFor(
'votes',
voteRepositoryGetter,
);
this.registerInclusionResolver('votes', this.votes.inclusionResolver);
}
}

0 comments on commit f127ed2

Please sign in to comment.