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

Added likes endpoint #173

Merged
merged 1 commit into from
Oct 11, 2021
Merged
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
48 changes: 48 additions & 0 deletions src/controllers/vote.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,52 @@ export class VoteController {
async deleteById(@param.path.string('id') id: string): Promise<void> {
return this.voteRepository.deleteById(id);
}

@post('/likes', {
responses: {
'200': {
description: 'Vote model instance',
content: {'application/json': {schema: getModelSchemaRef(Vote)}},
},
},
})
async createLike(
@requestBody({
content: {
'application/json': {
schema: getModelSchemaRef(Vote, {
title: 'NewVote',
}),
},
},
})
vote: Omit<Vote, 'id'>,
): Promise<Vote> {
/* eslint-disable @typescript-eslint/no-explicit-any */
const collection = (
this.voteRepository.dataSource.connector as any
).collection(Vote.modelName);
const query = {
userId: vote.userId,
type: vote.type,
referenceId: vote.referenceId,
};
const update = {
$set: vote,
};
const options = {upsert: true, returnOriginal: false};

return collection.findOneAndUpdate(query, update, options);
}

@del('/likes/{id}', {
responses: {
'200': {
description: 'Vote DELETE success',
},
},
})
async deleteLikesById(@param.path.string('id') id: string): Promise<void> {
return this.voteRepository.deleteById(id);
}
}
4 changes: 4 additions & 0 deletions src/interfaces/metric.interface.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
// TODO: removed likes, comments and dislikes
export interface Metric {
likes?: number;
dislikes?: number;
comments?: number;
upvotes: number;
downvotes: number;
discussions?: number;
Expand Down
3 changes: 3 additions & 0 deletions src/models/post.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@ export class Post extends Entity {
@hasMany(() => Comment, {keyTo: 'referenceId'})
comments: Comment[];

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

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

Expand Down
3 changes: 0 additions & 3 deletions src/models/vote.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ import {User} from './user.model';
mongodb: {
collection: 'votes',
},
jsonSchema: {
require: ['userId'],
},
},
})
export class Vote extends Entity {
Expand Down
12 changes: 12 additions & 0 deletions src/repositories/post.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ export class PostRepository extends DefaultCrudRepository<
typeof Vote.prototype.id
>;

// TODO: Delete after revamp is finished
public readonly likes: HasManyRepositoryFactory<
Vote,
typeof Vote.prototype.id
>;

public readonly transactions: HasManyRepositoryFactory<
Transaction,
typeof Post.prototype.id
Expand Down Expand Up @@ -84,5 +90,11 @@ export class PostRepository extends DefaultCrudRepository<
voteRepositoryGetter,
);
this.registerInclusionResolver('votes', this.votes.inclusionResolver);
// TODO: Delete after revamp
this.likes = this.createHasManyRepositoryFactoryFor(
'likes',
voteRepositoryGetter,
);
this.registerInclusionResolver('likes', this.likes.inclusionResolver);
}
}
6 changes: 6 additions & 0 deletions src/services/metric.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ export class MetricService {
state: false,
});

// TODO: removed likes and dislikes
const metric: Metric = {
likes: upvote.count,
dislikes: downvote.count,
upvotes: upvote.count,
downvotes: downvote.count,
};
Expand All @@ -95,6 +98,9 @@ export class MetricService {
).count;
}

// TODO: removed comments
metric.comments = (metric.discussions ?? 0) + (metric.debates ?? 0);

return metric;
}

Expand Down