-
Notifications
You must be signed in to change notification settings - Fork 3
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
update #3
Merged
update #3
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { PartialType } from '@nestjs/mapped-types'; | ||
import { IsMongoId, IsNotEmpty, IsNumber, IsOptional, Min } from 'class-validator'; | ||
|
||
export class CrateRatingDto { | ||
@IsNotEmpty() | ||
@IsNumber() | ||
@Min(1) | ||
rating: number; | ||
|
||
@IsNotEmpty() | ||
@IsMongoId({ message: 'Project id is not valid' }) | ||
projectId: string; | ||
|
||
@IsNotEmpty() | ||
@IsOptional() | ||
comment: string; | ||
} | ||
|
||
export class UpdateRatingDto extends PartialType(CrateRatingDto) { | ||
@IsNotEmpty() | ||
@IsMongoId({ message: 'Rating id is not valid' }) | ||
ratingId: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { Body, Controller, Delete, Get, Param, Patch, Post, Query } from '@nestjs/common'; | ||
import { RatingService } from './rating.service'; | ||
import { LoggedInUserDecorator } from 'src/common/decorators/logged_in_user.decorator'; | ||
import { UserDocument } from 'src/module/v1/user/schemas/user.schema'; | ||
import { CrateRatingDto, UpdateRatingDto } from 'src/module/v1/rating/dto/rating.dto'; | ||
import { PaginationDto } from 'src/module/v1/repository/dto/repository.dto'; | ||
|
||
@Controller('rating') | ||
export class RatingController { | ||
constructor(private readonly ratingService: RatingService) {} | ||
|
||
@Post() | ||
async create(@Body() payload: CrateRatingDto, @LoggedInUserDecorator() user: UserDocument) { | ||
return await this.ratingService.create(user, payload); | ||
} | ||
|
||
@Patch() | ||
async update(@Body() payload: UpdateRatingDto, @LoggedInUserDecorator() user: UserDocument) { | ||
return await this.ratingService.updateRating(user, payload); | ||
} | ||
|
||
@Get('project/:projectId') | ||
async getRatingByProject(@Param('projectId') projectId: string, @Query() query: PaginationDto) { | ||
return await this.ratingService.getRatingByProject(projectId, query); | ||
} | ||
|
||
@Get(':id') | ||
async getRatingById(@Param('id') id: string) { | ||
return await this.ratingService.getRatingById(id); | ||
} | ||
|
||
@Delete(':ratingId') | ||
async deleteRating(@Param('ratingId') ratingId: string, @LoggedInUserDecorator() user: UserDocument) { | ||
return await this.ratingService.deleteRating(ratingId, user); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { RatingService } from './rating.service'; | ||
import { RatingController } from './rating.controller'; | ||
import { MongooseModule } from '@nestjs/mongoose'; | ||
import { Rating, RatingSchema } from 'src/module/v1/rating/schema/rating.schema'; | ||
import { ProjectModule } from 'src/module/v1/project/project.module'; | ||
import { RepositoryModule } from 'src/module/v1/repository/repository.module'; | ||
|
||
@Module({ | ||
imports: [ | ||
MongooseModule.forFeature([ | ||
{ | ||
name: Rating.name, | ||
schema: RatingSchema, | ||
}, | ||
]), | ||
RepositoryModule, | ||
ProjectModule, | ||
], | ||
controllers: [RatingController], | ||
providers: [RatingService], | ||
}) | ||
export class RatingModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
import { Injectable, NotFoundException } from '@nestjs/common'; | ||
import { InjectModel } from '@nestjs/mongoose'; | ||
import mongoose, { Model } from 'mongoose'; | ||
import { ProjectService } from 'src/module/v1/project/project.service'; | ||
import { CrateRatingDto, UpdateRatingDto } from 'src/module/v1/rating/dto/rating.dto'; | ||
import { Rating, RatingDocument } from 'src/module/v1/rating/schema/rating.schema'; | ||
import { PaginationDto } from 'src/module/v1/repository/dto/repository.dto'; | ||
import { RepositoryService } from 'src/module/v1/repository/repository.service'; | ||
import { UserDocument } from 'src/module/v1/user/schemas/user.schema'; | ||
|
||
@Injectable() | ||
export class RatingService { | ||
constructor( | ||
@InjectModel(Rating.name) private ratingModel: Model<RatingDocument>, | ||
private repositoryService: RepositoryService, | ||
private projectService: ProjectService, | ||
) {} | ||
|
||
async create(user: UserDocument, payload: CrateRatingDto) { | ||
const { projectId, rating, comment } = payload; | ||
const project = await this.projectService.getProjectById(projectId); | ||
|
||
if (!project) { | ||
throw new NotFoundException('Project not found'); | ||
} | ||
|
||
const ratingData = await this.ratingModel.create({ | ||
user: user._id, | ||
project: project._id, | ||
rating, | ||
comment, | ||
}); | ||
|
||
if (!ratingData) { | ||
throw new NotFoundException('Unable to rate project, please try again'); | ||
} | ||
|
||
const averageRating = await this.getAverageRating(projectId); | ||
if (averageRating > 0) { | ||
await this.projectService.updateQuery({ _id: project._id }, { averageRating }); | ||
} | ||
|
||
return ratingData; | ||
} | ||
|
||
async getAverageRating(projectId: string) { | ||
const averageRating = await this.ratingModel.aggregate([ | ||
{ | ||
$match: { | ||
project: new mongoose.Types.ObjectId(projectId), | ||
isDeleted: false, | ||
}, | ||
}, | ||
{ | ||
$group: { | ||
_id: '$project', | ||
averageRating: { $avg: '$rating' }, | ||
}, | ||
}, | ||
{ | ||
$project: { | ||
_id: 0, | ||
averageRating: 1, | ||
}, | ||
}, | ||
]); | ||
|
||
return averageRating[0]?.averageRating || 0; | ||
} | ||
|
||
async updateRating(user: UserDocument, payload: UpdateRatingDto) { | ||
const { ratingId } = payload; | ||
|
||
const updateRating = await this.ratingModel.findOneAndUpdate( | ||
{ | ||
_id: ratingId, | ||
user: user._id, | ||
}, | ||
{ | ||
...payload, | ||
}, | ||
Comment on lines
+79
to
+81
Check failure Code scanning / CodeQL Database query built from user-controlled sources High
This query object depends on a
user-provided value Error loading related location Loading |
||
{ new: true }, | ||
); | ||
|
||
if (!updateRating) { | ||
throw new NotFoundException('Rating not found'); | ||
} | ||
|
||
return updateRating; | ||
} | ||
|
||
async getRatingByProject(projectId: string, query: PaginationDto) { | ||
const [averageRating, rating] = await Promise.all([ | ||
this.getAverageRating(projectId), | ||
this.repositoryService.paginate(this.ratingModel, query, { project: projectId }, 'user project'), | ||
]); | ||
|
||
return { | ||
rating, | ||
averageRating, | ||
}; | ||
} | ||
|
||
async deleteRating(ratingId: string, user: UserDocument) { | ||
const deletedRating = await this.ratingModel.findOneAndUpdate( | ||
{ | ||
_id: ratingId, | ||
user: user._id, | ||
}, | ||
{ | ||
isDeleted: true, | ||
}, | ||
); | ||
|
||
if (!deletedRating) { | ||
throw new NotFoundException('Rating not found'); | ||
} | ||
|
||
// update project average rating | ||
const project = await this.projectService.getProjectById(deletedRating.project); | ||
if (project) { | ||
const averageRating = await this.getAverageRating(deletedRating.project); | ||
await this.projectService.updateQuery({ _id: project._id }, { averageRating }); | ||
} | ||
} | ||
|
||
async getRatingById(id: string) { | ||
return await this.ratingModel.findById(id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'; | ||
import mongoose, { Document } from 'mongoose'; | ||
import { Project } from 'src/module/v1/project/schema/project.schema'; | ||
import { User } from 'src/module/v1/user/schemas/user.schema'; | ||
|
||
export type RatingDocument = Rating & Document; | ||
|
||
@Schema({ timestamps: true }) | ||
export class Rating { | ||
@Prop({ type: mongoose.Schema.Types.ObjectId, ref: User.name }) | ||
user: string; | ||
|
||
@Prop({ type: mongoose.Schema.Types.ObjectId, ref: Project.name }) | ||
project: string; | ||
|
||
@Prop({ enum: [1, 2, 3, 4, 5], default: 0 }) | ||
rating: number; | ||
|
||
@Prop() | ||
comment: string; | ||
|
||
@Prop({ default: false }) | ||
isDeleted: boolean; | ||
} | ||
|
||
export const RatingSchema = SchemaFactory.createForClass(Rating); | ||
|
||
RatingSchema.index({ project: 1, user: 1 }, { unique: true }); | ||
|
||
RatingSchema.pre('find', function (next) { | ||
// remove deleted ratings | ||
this.where({ isDeleted: false }); | ||
|
||
next(); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check failure
Code scanning / CodeQL
Database query built from user-controlled sources High