Skip to content

Commit

Permalink
added album query and filter bug
Browse files Browse the repository at this point in the history
  • Loading branch information
enochval committed Jun 8, 2024
1 parent 16449fa commit 8f80169
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 51 deletions.
8 changes: 6 additions & 2 deletions src/albums/albums.module.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { Module } from '@nestjs/common';
import { Module, forwardRef } from '@nestjs/common';
import { AlbumsService } from './albums.service';
import { HttpModule } from '@nestjs/axios';
import { PhotosService } from './photos/photos.service';
import { AlbumResolver } from './albums.resolver';
import { UserModule } from 'src/users/users.module';

@Module({
imports: [HttpModule],
imports: [
forwardRef(() => UserModule),
HttpModule
],
providers: [AlbumsService, AlbumResolver, PhotosService],
exports: [AlbumsService, PhotosService]
})
Expand Down
21 changes: 18 additions & 3 deletions src/albums/albums.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
import { Args, Parent, ResolveField, Resolver } from "@nestjs/graphql";
import { Args, Parent, Query, ResolveField, Resolver } from "@nestjs/graphql";
import { PhotosService } from "./photos/photos.service";
import { Photo } from "src/graphql";
import { Album, Photo, User } from "src/graphql";
import { AlbumsService } from "./albums.service";
import { UserService } from "src/users/users.service";

@Resolver('Album')
export class AlbumResolver {

constructor(private readonly photosService: PhotosService) {}
constructor(
private readonly photosService: PhotosService,
private readonly albumsService: AlbumsService,
private readonly userService: UserService
) {}

@Query('albums')
async getAlbums(@Args() args): Promise<Album[]> {
return await this.albumsService.getAlbums(args)
}

@ResolveField('photos')
async getAlbumPhotos(@Parent() album, @Args() args): Promise<Photo[]> {
const { id } = album
return await this.photosService.getPhotosByAlbumId(id, args)
}

@ResolveField('user')
async getAlbumUser(@Parent() album): Promise<User> {
return await this.userService.getUserById(album.userId)
}
}
32 changes: 22 additions & 10 deletions src/albums/albums.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,33 @@ export class AlbumsService {
this.baseUrl = this.configService.get<string>('api.baseurl')
}

async getUserAlbums(id: number, args: any): Promise<Album[]> {
async getAlbums(args: any): Promise<Album[]> {
const slashAlbumId = `${args.albumId ? '/' + args.albumId : ''}`
const queryUserId = `${args.userId ? '?userId=' + args.userId : ''}`
const uri = `${this.baseUrl}/albums${slashAlbumId}${queryUserId}`

const { data } = await firstValueFrom(
this.httpService.get<any[]>(`${this.baseUrl}/albums`).pipe(
catchError((err: AxiosError) => {
this.logger.error(err.response.data)
throw new BadRequestException("An error happened!")
})
)
this.httpService.get<any|Album[]>(uri)
.pipe(
catchError((err: AxiosError) => {
this.logger.error(err.response.data)
throw new BadRequestException('An error happened!')
})
)
)
const userAlbums = data.filter(o => o.userId === id)

if(args.albumId) {
const album: Array<Album> = []
album.push(data)
return album
}

let rsp: Array<any> = data
if (args.first) {
return userAlbums.slice(0, args.first)
const length = (args.first > rsp.length) ? rsp.length : args.first
rsp = rsp.slice(0, length)
}

return userAlbums;
return rsp
}
}
3 changes: 2 additions & 1 deletion src/albums/photos/photos.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ export class PhotosService {
const photos: Photo[] = data.filter(o => o.albumId === albumId)

if (args.first) {
return photos.slice(0, args.first)
const length = (args.first > photos.length) ? photos.length : args.first
return photos.slice(0, length)
}

return photos;
Expand Down
2 changes: 2 additions & 0 deletions src/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ export abstract class IQuery {
abstract posts(first?: Nullable<number>, postId?: Nullable<number>, userId?: Nullable<number>): Nullable<Nullable<Post>[]> | Promise<Nullable<Nullable<Post>[]>>;

abstract comments(first?: Nullable<number>, commentId?: Nullable<number>, postId?: Nullable<number>): Nullable<Nullable<Comment>[]> | Promise<Nullable<Nullable<Comment>[]>>;

abstract albums(first?: Nullable<number>, albumId?: Nullable<number>, userId?: Nullable<number>): Nullable<Nullable<Album>[]> | Promise<Nullable<Nullable<Album>[]>>;
}

type Nullable<T> = T | null;
3 changes: 2 additions & 1 deletion src/posts/comments/comments.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ export class CommentsService {
let rsp: Array<Comment> = data

if (args.first) {
rsp = rsp.slice(0, args.first)
const length = (args.first > rsp.length) ? rsp.length : args.first
rsp = rsp.slice(0, length)
}

return rsp
Expand Down
26 changes: 5 additions & 21 deletions src/posts/posts.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class PostsService {
}

async getPosts(args: any): Promise<Post[]> {
const slashPostId = `${args.commentId ? '/'+args.commentId : ''}`
const slashPostId = `${args.postId ? '/'+args.postId : ''}`
const queryUserId = `${args.userId ? '?userId=' + args.userId : ''}`
const uri = `${this.baseUrl}/posts${slashPostId}${queryUserId}`

Expand All @@ -40,7 +40,8 @@ export class PostsService {
let rsp: Array<any> = data

if (args.first) {
rsp = rsp.slice(0, args.first)
const length = (args.first > rsp.length) ? rsp.length : args.first
rsp = rsp.slice(0, length)
}

return rsp
Expand All @@ -58,24 +59,6 @@ export class PostsService {
return data
}

async getUserPosts(userId: number, args: any): Promise<Post[]> {
const { data } = await firstValueFrom(
this.httpService.get<any[]>(`${this.baseUrl}/posts`).pipe(
catchError((err: AxiosError) => {
this.logger.error(err.response.data)
throw new BadRequestException("An error happened!")
})
)
)
const userPosts = data.filter(a => a.userId === userId)

if (args.first) {
return userPosts.slice(0, args.first)
}

return userPosts
}

async getPostComments(postId: number, args: any): Promise<Comment[]> {
const { data } = await firstValueFrom(
this.httpService.get<Comment[]>(`${this.baseUrl}/post/${postId}/comments`)
Expand All @@ -88,7 +71,8 @@ export class PostsService {
)

if (args.first) {
return data.slice(0, args.first)
const length = (args.first > data.length) ? data.length : args.first
return data.slice(0, length)
}

return data
Expand Down
3 changes: 2 additions & 1 deletion src/todos/todos.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ export class TodosService {
const todos = data.filter(o => o.userId === userId)

if (args.first) {
return todos.slice(0, args.first)
const length = (args.first > todos.length) ? todos.length : args.first
return todos.slice(0, length)
}

return todos
Expand Down
1 change: 1 addition & 0 deletions src/users/users.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,5 @@ type Query {
userById(id: Int!): User
posts(first: Int, postId: Int, userId: Int): [Post]
comments(first: Int, commentId: Int, postId: Int): [Comment]
albums(first: Int, albumId: Int, userId: Int): [Album]
}
3 changes: 2 additions & 1 deletion src/users/users.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import { TodosModule } from 'src/todos/todos.module';
@Module({
imports: [
forwardRef(() => PostsModule),
HttpModule, AlbumsModule, TodosModule],
forwardRef(() => AlbumsModule),
HttpModule, TodosModule],
providers: [UsersResolver, UserService],
exports: [UserService]
})
Expand Down
8 changes: 4 additions & 4 deletions src/users/users.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { UserService } from "./users.service";
import { PostsService } from "src/posts/posts.service";
import { AlbumsService } from "src/albums/albums.service";
import { TodosService } from "src/todos/todos.service";
import { Inject, forwardRef } from "@nestjs/common";

@Resolver('User')
export class UsersResolver {
Expand All @@ -30,13 +29,14 @@ export class UsersResolver {
@ResolveField('posts')
async getPosts(@Parent() user, @Args() args): Promise<Post[]> {
const { id } = user
return await this.postsService.getUserPosts(id, args)
args.userId = id
return await this.postsService.getPosts(args)
}

@ResolveField('albums')
async getAlbums(@Parent() user, @Args() args): Promise<Album[]> {
const { id } = user
return await this.albumService.getUserAlbums(id, args);
args.userId = user.id
return await this.albumService.getAlbums(args);
}

@ResolveField('todos')
Expand Down
19 changes: 12 additions & 7 deletions src/users/users.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { HttpService } from '@nestjs/axios';
import { BadRequestException, Injectable, Logger } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { AxiosError, AxiosResponse } from 'axios';
import { AxiosError } from 'axios';
import { catchError, firstValueFrom } from 'rxjs';
import { User } from 'src/graphql';

Expand All @@ -18,26 +18,31 @@ export class UserService {
}

async getUsers(args: any): Promise<User[]> {
const slashUserId = `${args.userId ? '/'+args.userId : ''}`
const uri = `${this.baseUrl}/users${slashUserId}`

const { data } = await firstValueFrom(
this.httpService.get<any|User[]>(`${this.baseUrl}/users/${args.userId ? args.userId : ''}`).pipe(
this.httpService.get<any|User[]>(uri).pipe(
catchError((error: AxiosError) => {
this.logger.error(error.response.data)
throw new BadRequestException('An error happened!')
})
)
)

if (args.first && Array.isArray(data)) {
return data.slice(0, args.first)
}

if (args.userId) {
const user: Array<User> = []
user.push(data)
return user
}

return data
let rsp: Array<any> = data
if (args.first) {
const length = (args.first > rsp.length) ? rsp.length : args.first
rsp = rsp.slice(0, length)
}

return rsp
}

async getUserById(id: number): Promise<User> {
Expand Down

0 comments on commit 8f80169

Please sign in to comment.