Skip to content

Commit

Permalink
feat: fetch user upvotes
Browse files Browse the repository at this point in the history
  • Loading branch information
karimdaghari committed Jan 14, 2020
1 parent 6032809 commit 2fd3d48
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/modules/user/user.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import { TypeOrmModule } from '@nestjs/typeorm';
import { User } from './user.entity';
import { ProductModule } from '../product/product.module';
import { ProfileModule } from '../profile/profile.module';
import { VoteModule } from '../vote/vote.module';

@Module({
imports: [
TypeOrmModule.forFeature([User]),
forwardRef(() => ProductModule),
ProfileModule
ProfileModule,
VoteModule
],
providers: [UserService, UserResolver],
exports: [UserService]
Expand Down
5 changes: 5 additions & 0 deletions src/modules/user/user.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ export class UserResolver {
return await this.userService.fetchProfileByUserId(id);
}

@ResolveProperty('votes')
async votes(@Parent() { id }: User) {
return await this.userService.fetchVotesByUserId(id);
}

@ResolveProperty('notifications')
@UseGuards(GraphQLAuth)
async notifications(@Parent() { id }: User) {
Expand Down
13 changes: 11 additions & 2 deletions src/modules/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { ProductService } from '../product/product.service';
import { UserInput } from './dto/user.input';
import { SharedService } from '../shared/shared.service';
import { ProfileService } from '../profile/profile.service';
import { VoteService } from '../vote/vote.service';

@Injectable()
export class UserService {
Expand All @@ -20,7 +21,8 @@ export class UserService {
@Inject(forwardRef(() => ProductService))
private readonly productService: ProductService,
private readonly sharedService: SharedService,
private readonly profileService: ProfileService
private readonly profileService: ProfileService,
private readonly voteService: VoteService
) {}

async saveUser(user: User) {
Expand All @@ -32,7 +34,10 @@ export class UserService {
}

async findUserByOptions(options: any = {}): Promise<User> {
return await this.userRepository.findOneOrFail(options);
return await this.userRepository.findOneOrFail({
where: { options },
relations: ['products', 'comments']
});
}

async fetchMakersByIds(makersIds: number[]): Promise<User[]> {
Expand Down Expand Up @@ -134,4 +139,8 @@ export class UserService {
);
return profile;
}

async fetchVotesByUserId(id: number) {
return await this.voteService.fetchAllVotes({ where: { userId: id } });
}
}

0 comments on commit 2fd3d48

Please sign in to comment.