Skip to content

Commit

Permalink
Unit test review service
Browse files Browse the repository at this point in the history
  • Loading branch information
lycamnguyen committed Nov 18, 2024
1 parent 220d208 commit 9670852
Show file tree
Hide file tree
Showing 11 changed files with 326 additions and 37 deletions.
16 changes: 10 additions & 6 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import type {Config} from '@jest/types'
import type { Config } from "@jest/types";
import { config as dotenvConfig } from "dotenv";
dotenvConfig();

const config: Config.InitialOptions = {
preset: 'ts-jest',
testEnvironment: 'node',
verbose: true,
}
preset: "ts-jest",
testEnvironment: "node",
verbose: true,
moduleFileExtensions: ["ts", "js", "json"],
roots: ["<rootDir>/src"],
};

export default config
export default config;
16 changes: 16 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"node": ">=16.0.0"
},
"dependencies": {
"@faker-js/faker": "^9.2.0",
"bcrypt": "^5.1.1",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.1",
Expand Down
2 changes: 1 addition & 1 deletion src/entity/Comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class Comment {
@PrimaryGeneratedColumn("increment", { type: "bigint" })
id!: number;

@ManyToOne(() => Review)
@ManyToOne(() => Review, (review) => review.comments)
@JoinColumn({ name: "review_id" })
review!: Review;

Expand Down
36 changes: 18 additions & 18 deletions src/entity/Course.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@ import {
UpdateDateColumn,
JoinColumn,
OneToMany,
} from 'typeorm';
import { User } from './User';
import { Section } from './Section';
import { Category } from './Category';
import { Cart } from './Cart';
import { CourseStatus } from '../enum/course.enum';
} from "typeorm";
import { User } from "./User";
import { Section } from "./Section";
import { Category } from "./Category";
import { Cart } from "./Cart";
import { CourseStatus } from "../enum/course.enum";

@Entity('courses')
@Entity("courses")
export class Course {
@PrimaryGeneratedColumn('increment', { type: 'bigint' })
@PrimaryGeneratedColumn("increment", { type: "bigint" })
id!: number;

@ManyToOne(() => User)
@JoinColumn({ name: 'professor_id' })
@JoinColumn({ name: "professor_id" })
professor!: User;

@ManyToOne(() => Category, (category) => category.courses)
@JoinColumn({ name: 'category_id' })
@JoinColumn({ name: "category_id" })
category!: Category;

@OneToMany(() => Section, (section) => section.course)
Expand All @@ -33,32 +33,32 @@ export class Course {
@Column()
name!: string;

@Column('double')
@Column("double")
price!: number;

@Column('text')
@Column("text")
description!: string;

@Column('float')
@Column({ type: "float", nullable: true })
average_rating!: number;

@Column({ type: 'bigint' })
@Column({ type: "bigint" })
professor_id!: number;

@Column({ type: 'bigint' })
@Column({ type: "bigint" })
category_id!: number;

@Column({
type: 'enum',
type: "enum",
enum: CourseStatus,
default: CourseStatus.DRAFT,
})
status!: string;

@CreateDateColumn({ type: 'datetime' })
@CreateDateColumn({ type: "datetime" })
created_at!: Date;

@UpdateDateColumn({ type: 'datetime' })
@UpdateDateColumn({ type: "datetime" })
updated_at!: Date;

@OneToMany(() => Cart, (cart) => cart.course)
Expand Down
4 changes: 2 additions & 2 deletions src/entity/Review.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ export class Review {
@Column("integer")
rating!: number;

@Column({ type: "bigint" })
@Column({ type: "bigint", nullable: true })
user_id!: number;

@Column({ type: "bigint" })
@Column({ type: "bigint", nullable: true })
course_id!: number;

@CreateDateColumn({ type: "datetime" })
Expand Down
4 changes: 4 additions & 0 deletions src/entity/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Entity,
} from 'typeorm';
import { UserRoleType, UserGenderType } from '../enum/user.enum';
import { Cart } from './Cart';
import { Review } from './Review';

@Entity('users')
export class User {
Expand Down Expand Up @@ -66,6 +67,9 @@ export class User {
@OneToMany(() => Cart, (cart) => cart.user)
cart!: Cart[];

@OneToMany(() => Review, (review) => review.user)
review!: Review[];

constructor(userData?: Partial<User>) {
userData && Object.assign(this, userData);
}
Expand Down
4 changes: 4 additions & 0 deletions src/entity/dto/course.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ export class CreateCourseDto {
@Min(0)
@Max(5)
average_rating?: number;

@IsNumber()
@IsOptional()
professor_id?: number
}

export class UpdateCourseDto {
Expand Down
24 changes: 18 additions & 6 deletions src/repos/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,25 @@ import { DataSource } from "typeorm";
import { config } from "dotenv";
config();

const {
DB_HOST,
DB_PORT,
DB_USERNAME,
DB_PASSWORD,
DB_DATABASE,
DB_TEST_DATABASE,
} = process.env;

const database =
process.env.NODE_ENV === "test" ? DB_TEST_DATABASE : DB_DATABASE;

export const AppDataSource = new DataSource({
type: process.env.DB_TYPE as "mysql",
host: process.env.DB_HOST,
port: parseInt(process.env.DB_PORT ?? "3306"),
username: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
type: "mysql",
host: DB_HOST,
port: parseInt(DB_PORT ?? "3306"),
username: DB_USERNAME,
password: DB_PASSWORD,
database: database,
synchronize: true,
logging: false,
entities: ["src/entity/*.ts"],
Expand Down
12 changes: 8 additions & 4 deletions src/service/review.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ const reviewRepository = AppDataSource.getRepository(Review);
export const getReviewByCourseId = (courseId: number) => {
return reviewRepository.find({
where: { course_id: courseId },
relations: ["user", "comments"],
relations: ["course", "user", "comments"],
});
};

export const getReviewsWithDetails = async () => {
return await reviewRepository.find({
relations: {
user: true,
course: true,
user: true,
course: true,
comments: {
user: true,
user: true,
},
},
select: {
Expand All @@ -38,6 +38,7 @@ export const getReviewsWithDetails = async () => {
},
},
},
order: { created_at: "DESC" },
});
};

Expand All @@ -46,6 +47,9 @@ export const createReview = (
rating: number,
course_id: number
) => {
if (course_id <= 0) {
return Promise.reject(new Error("Invalid course ID"));
}
return reviewRepository.save({
user_id,
rating,
Expand Down
Loading

0 comments on commit 9670852

Please sign in to comment.