Skip to content

Commit

Permalink
feat(profile repo): implement typeorm profile repo
Browse files Browse the repository at this point in the history
  • Loading branch information
havrydotdev committed Oct 17, 2023
1 parent e23b25f commit 3029505
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 25 deletions.
13 changes: 6 additions & 7 deletions src/core/abstracts/profile.abstract.service.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import { CreateProfileDto, UpdateProfileDto } from '../dtos';
import { User } from '../entities';
import { Profile } from '../entities';

abstract class IProfileService {
abstract createProfile(dto: CreateProfileDto): Promise<void>;
abstract createProfile(profile: Profile): Promise<void>;

abstract updateProfile(dto: UpdateProfileDto): Promise<void>;
abstract updateProfile(profileId: number, profile: Profile): Promise<void>;

abstract deleteProfile(id: number): Promise<void>;
abstract deleteProfile(profileId: number): Promise<void>;

abstract findByUser(userId: number): Promise<void>;
abstract findByUser(userId: number): Promise<Profile>;

abstract findRecommended(user: User, skip: number): Promise<void>;
abstract findRecommended(user: Profile, skip: number): Promise<void>;
}

export { IProfileService };
4 changes: 2 additions & 2 deletions src/frameworks/game/typeorm/tests/game.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Test, TestingModule } from '@nestjs/testing';
import { TypeOrmGameService } from '../typeorm-game.service';
import { Game } from 'src/core';
import { Repository } from 'typeorm';
import { TypeOrmModule } from '@nestjs/typeorm';
import { TypeOrmModule, getRepositoryToken } from '@nestjs/typeorm';
import { MockDatabaseModule } from 'src/services/mock-database/mock-database.module';

describe('TypeOrmGameService', () => {
Expand All @@ -16,7 +16,7 @@ describe('TypeOrmGameService', () => {
}).compile();

service = module.get<TypeOrmGameService>(TypeOrmGameService);
repo = module.get<Repository<Game>>(Repository<Game>);
repo = module.get<Repository<Game>>(getRepositoryToken(Game));
});

it('should return an array of games', async () => {
Expand Down
2 changes: 2 additions & 0 deletions src/frameworks/profile/typeorm/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './typeorm-profile.module';
export * from './typeorm-profile.service';
16 changes: 16 additions & 0 deletions src/frameworks/profile/typeorm/typeorm-profile.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Module } from '@nestjs/common';
import { IProfileService, Profile } from 'src/core';
import { TypeOrmProfileService } from './typeorm-profile.service';
import { TypeOrmModule } from '@nestjs/typeorm';

@Module({
imports: [TypeOrmModule.forFeature([Profile])],
exports: [IProfileService],
providers: [
{
provide: IProfileService,
useClass: TypeOrmProfileService,
},
],
})
export class TypeOrmProfileModule {}
45 changes: 29 additions & 16 deletions src/frameworks/profile/typeorm/typeorm-profile.service.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,43 @@
import { Injectable } from '@nestjs/common';
import {
CreateProfileDto,
IProfileService,
UpdateProfileDto,
User,
} from 'src/core';
import { InjectRepository } from '@nestjs/typeorm';
import { IProfileService, Profile } from 'src/core';
import { Repository } from 'typeorm';

@Injectable()
export class TypeOrmProfileService implements IProfileService {
async createProfile(dto: CreateProfileDto): Promise<void> {
throw new Error('Method not implemented.');
constructor(
@InjectRepository(Profile)
private readonly profileRepo: Repository<Profile>,
) {}

async findByUser(userId: number): Promise<Profile> {
return this.profileRepo.findOne({
where: {
user: {
id: userId,
},
},
});
}

async updateProfile(dto: UpdateProfileDto): Promise<void> {
throw new Error('Method not implemented.');
async createProfile(profile: Profile): Promise<void> {
await this.profileRepo.insert(profile);
}

async deleteProfile(id: number): Promise<void> {
throw new Error('Method not implemented.');
async updateProfile(profileId: number, profile: Profile): Promise<void> {
await this.profileRepo.update(profileId, profile);
}

async findByUser(userId: number): Promise<void> {
throw new Error('Method not implemented.');
async deleteProfile(profileId: number): Promise<void> {
await this.profileRepo.delete(profileId);
}

async findRecommended(user: User, skip: number): Promise<void> {
throw new Error('Method not implemented.');
// TODO
async findRecommended(user: Profile, skip: number): Promise<void> {
await this.profileRepo.find({
skip,
take: 1,
order: {},
});
}
}

0 comments on commit 3029505

Please sign in to comment.