Skip to content

Commit

Permalink
refactor: ♻️ MongooseUserRepository to MongooseUsersRepository
Browse files Browse the repository at this point in the history
  • Loading branch information
zhumeisongsong committed Nov 19, 2024
1 parent b37adaa commit c725fce
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { getModelToken } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { MongooseUserRepository } from './mongoose-user.repository';
import { MongooseUsersRepository } from './mongoose-users.repository';
import { UserDocument } from './user.schema';
import { User } from '@users/domain';

describe('MongooseUserRepository', () => {
let repository: MongooseUserRepository;
describe('MongooseUsersRepository', () => {
let repository: MongooseUsersRepository;
let userModel: Model<UserDocument>;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
MongooseUserRepository,
MongooseUsersRepository,
{
provide: getModelToken(UserDocument.name),
useValue: {
Expand All @@ -22,7 +22,7 @@ describe('MongooseUserRepository', () => {
],
}).compile();

repository = module.get<MongooseUserRepository>(MongooseUserRepository);
repository = module.get<MongooseUsersRepository>(MongooseUsersRepository);
userModel = module.get<Model<UserDocument>>(getModelToken(UserDocument.name));
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { User, UserRepository } from '@users/domain';
import { User, UsersRepository } from '@users/domain';
import { Model } from 'mongoose';

import { UserDocument } from './user.schema';

export class MongooseUserRepository implements UserRepository {
@Injectable()
export class MongooseUsersRepository implements UsersRepository {
constructor(
@InjectModel(UserDocument.name) private userModel: Model<UserDocument>,
) {}

async findById(id: string): Promise<User | null> {
const userDoc = await this.userModel.findById(id).exec();
return userDoc ? new User(userDoc.id, userDoc.name) : null;

if (!userDoc) {
return null;
}
return new User(userDoc.id, userDoc.name);
}
}

0 comments on commit c725fce

Please sign in to comment.