-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
abstracted jsonplaceholder api communication to a service
- Loading branch information
Showing
21 changed files
with
216 additions
and
254 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,17 @@ | ||
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'; | ||
import { JsonplaceholderModule } from 'src/jsonplaceholder/jsonplaceholder.module'; | ||
import { PhotosResolver } from './photos/photos.resolver'; | ||
|
||
@Module({ | ||
imports: [ | ||
forwardRef(() => UserModule), | ||
HttpModule | ||
JsonplaceholderModule | ||
], | ||
providers: [AlbumsService, AlbumResolver, PhotosService], | ||
providers: [AlbumsService, AlbumResolver, PhotosService, PhotosResolver], | ||
exports: [AlbumsService, PhotosService] | ||
}) | ||
export class AlbumsModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,23 @@ | ||
import { HttpService } from '@nestjs/axios'; | ||
import { BadRequestException, Injectable, Logger } from '@nestjs/common'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { AxiosError } from 'axios'; | ||
import { catchError, firstValueFrom } from 'rxjs'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { Album } from 'src/graphql'; | ||
import { JsonplaceholderService } from 'src/jsonplaceholder/jsonplaceholder.service'; | ||
|
||
@Injectable() | ||
export class AlbumsService { | ||
private readonly baseUrl: string | ||
private readonly logger: Logger = new Logger(AlbumsService.name) | ||
|
||
constructor( | ||
private readonly httpService: HttpService, | ||
private readonly configService: ConfigService | ||
) { | ||
this.baseUrl = this.configService.get<string>('api.baseurl') | ||
} | ||
private readonly jsonPlaceHolderService: JsonplaceholderService<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|Album[]>(uri) | ||
.pipe( | ||
catchError((err: AxiosError) => { | ||
this.logger.error(err.response.data) | ||
throw new BadRequestException('An error happened!') | ||
}) | ||
) | ||
) | ||
|
||
if(args.albumId) { | ||
const album: Array<Album> = [] | ||
album.push(data) | ||
return album | ||
} | ||
const slashAlbumId: string = `${args.albumId ? '/' + args.albumId : ''}` | ||
const queryUserId: string = `${args.userId ? '?userId=' + args.userId : ''}` | ||
const uri: string = `/albums${slashAlbumId}${queryUserId}` | ||
|
||
let rsp: Array<any> = data | ||
if (args.first) { | ||
const length = (args.first > rsp.length) ? rsp.length : args.first | ||
rsp = rsp.slice(0, length) | ||
if (args.albumId) { | ||
args.isOne = true | ||
} | ||
|
||
return rsp | ||
return await this.jsonPlaceHolderService.handleGetRequest(uri, args) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Args, Parent, Query, ResolveField, Resolver } from "@nestjs/graphql"; | ||
import { Album, Photo } from "src/graphql"; | ||
import { AlbumsService } from "../albums.service"; | ||
import { PhotosService } from "./photos.service"; | ||
|
||
|
||
@Resolver('Photo') | ||
export class PhotosResolver { | ||
|
||
constructor( | ||
private readonly albumService: AlbumsService, | ||
private readonly photosService: PhotosService | ||
) {} | ||
|
||
@Query('photos') | ||
async getPhotos(@Args() args): Promise<Photo[]> { | ||
return await this.photosService.getPhotos(args) | ||
} | ||
|
||
@ResolveField('album') | ||
async getPhotoAlbum(@Parent() photo): Promise<Album> { | ||
const args: any = { albumId: photo.albumId } | ||
const rsp = await this.albumService.getAlbums(args) | ||
return rsp.find(() => true) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,23 @@ | ||
import { HttpService } from '@nestjs/axios'; | ||
import { BadRequestException, Injectable, Logger } from '@nestjs/common'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { AxiosError } from 'axios'; | ||
import { catchError, firstValueFrom } from 'rxjs'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { Photo } from 'src/graphql'; | ||
import { JsonplaceholderService } from 'src/jsonplaceholder/jsonplaceholder.service'; | ||
|
||
@Injectable() | ||
export class PhotosService { | ||
private readonly logger: Logger = new Logger(PhotosService.name) | ||
private readonly baseUrl: string | ||
|
||
constructor( | ||
private readonly httpService: HttpService, | ||
private readonly configService: ConfigService | ||
){ | ||
this.baseUrl = this.configService.get<string>('api.baseurl') | ||
} | ||
|
||
async getPhotosByAlbumId(albumId: number, args: any): Promise<Photo[]> { | ||
const { data } = await firstValueFrom( | ||
this.httpService.get<any[]>(`${this.baseUrl}/photos`).pipe( | ||
catchError((err: AxiosError) => { | ||
this.logger.error(err.response.data) | ||
throw new BadRequestException("An error happened!") | ||
}) | ||
) | ||
) | ||
private readonly jsonplaceholdService: JsonplaceholderService<Photo> | ||
){} | ||
|
||
const photos: Photo[] = data.filter(o => o.albumId === albumId) | ||
async getPhotos(args: any): Promise<Photo[]> { | ||
const slashPhotoId: string = `${args.photoId ? '/'+args.photoId : ''}` | ||
const queryAlbumId: string = `${args.albumId ? '?albumId=' + args.albumId : ''}` | ||
const uri: string = `/photos${slashPhotoId}${queryAlbumId}` | ||
|
||
if (args.first) { | ||
const length = (args.first > photos.length) ? photos.length : args.first | ||
return photos.slice(0, length) | ||
if (args.photoId) { | ||
args.isOne = true | ||
} | ||
|
||
return photos; | ||
return await this.jsonplaceholdService.handleGetRequest(uri, args) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { JsonplaceholderService } from './jsonplaceholder.service'; | ||
import { HttpModule } from '@nestjs/axios'; | ||
|
||
@Module({ | ||
imports: [HttpModule], | ||
providers: [JsonplaceholderService], | ||
exports: [JsonplaceholderService] | ||
}) | ||
export class JsonplaceholderModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { JsonplaceholderService } from './jsonplaceholder.service'; | ||
|
||
describe('JsonplaceholderService', () => { | ||
let service: JsonplaceholderService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [JsonplaceholderService], | ||
}).compile(); | ||
|
||
service = module.get<JsonplaceholderService>(JsonplaceholderService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { HttpService } from '@nestjs/axios'; | ||
import { BadRequestException, Injectable, Logger } from '@nestjs/common'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { AxiosError } from 'axios'; | ||
import { catchError, firstValueFrom } from 'rxjs'; | ||
|
||
@Injectable() | ||
export class JsonplaceholderService<T> { | ||
|
||
private readonly baseUrl: string | ||
private readonly logger: Logger = new Logger(JsonplaceholderService.name) | ||
|
||
constructor( | ||
private readonly httpService: HttpService, | ||
private readonly configService: ConfigService | ||
) { | ||
this.baseUrl = this.configService.get<string>('api.baseurl') | ||
} | ||
|
||
async handleGetRequest(uri: string, args?: any): Promise<any> { | ||
const url: string = `${this.baseUrl}${uri}` | ||
|
||
const { data } = await firstValueFrom( | ||
this.httpService.get<any|T[]>(url) | ||
.pipe( | ||
catchError((err: AxiosError) => { | ||
this.logger.error(err.response.data) | ||
throw new BadRequestException('An error happened!') | ||
}) | ||
) | ||
) | ||
|
||
if(args && args.isOne) { | ||
const resp: Array<T> = [] | ||
resp.push(data) | ||
return resp | ||
} | ||
|
||
if (!Array.isArray(data)) { | ||
return data | ||
} | ||
|
||
let rsp: Array<T> = data | ||
if (args.first) { | ||
const length = (args.first > rsp.length) ? rsp.length : args.first | ||
rsp = rsp.slice(0, length) | ||
} | ||
|
||
return rsp | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,28 @@ | ||
import { HttpService } from '@nestjs/axios'; | ||
import { BadRequestException, Injectable, Logger } from '@nestjs/common'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { AxiosError } from 'axios'; | ||
import { catchError, firstValueFrom } from 'rxjs'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { Comment } from 'src/graphql'; | ||
import { JsonplaceholderService } from 'src/jsonplaceholder/jsonplaceholder.service'; | ||
|
||
@Injectable() | ||
export class CommentsService { | ||
private readonly logger: Logger = new Logger(CommentsService.name) | ||
private baseUrl: string | ||
|
||
constructor( | ||
private readonly httpService: HttpService, | ||
private readonly configService: ConfigService | ||
){ | ||
this.baseUrl = this.configService.get<string>('api.baseurl') | ||
} | ||
private readonly jsonplaceholderService: JsonplaceholderService<Comment> | ||
){} | ||
|
||
async getComments(args: any): Promise<Comment[]> { | ||
const slashCommentId = `${args.commentId ? '/'+args.commentId : ''}` | ||
const queryPostId = `${args.postId ? '?postId=' + args.postId : ''}` | ||
const uri = `${this.baseUrl}/comments${slashCommentId}${queryPostId}` | ||
|
||
const { data } = await firstValueFrom( | ||
this.httpService.get<any|Comment[]>(uri).pipe( | ||
catchError((err: AxiosError) => { | ||
this.logger.error(err.response.data) | ||
throw new BadRequestException("An error happened!") | ||
}) | ||
) | ||
) | ||
const slashCommentId: string = `${args.commentId ? '/'+args.commentId : ''}` | ||
const queryPostId: string = `${args.postId ? '?postId=' + args.postId : ''}` | ||
const uri: string = `/comments${slashCommentId}${queryPostId}` | ||
|
||
if (args.commentId) { | ||
const res: Array<Comment> = [] | ||
res.push(data) | ||
return res | ||
args.isOne = true | ||
} | ||
|
||
let rsp: Array<Comment> = data | ||
return this.jsonplaceholderService.handleGetRequest(uri, args) | ||
} | ||
|
||
if (args.first) { | ||
const length = (args.first > rsp.length) ? rsp.length : args.first | ||
rsp = rsp.slice(0, length) | ||
} | ||
|
||
return rsp | ||
async getPostComments(postId: number, args: any): Promise<Comment[]> { | ||
const uri: string = `/post/${postId}/comments` | ||
return await this.jsonplaceholderService.handleGetRequest(uri, args) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,22 @@ | ||
import { Module, forwardRef } from '@nestjs/common'; | ||
import { PostsService } from './posts.service'; | ||
import { HttpModule } from '@nestjs/axios'; | ||
import { PostsResolver } from './posts.resolver'; | ||
import { UserModule } from 'src/users/users.module'; | ||
import { CommentsService } from './comments/comments.service'; | ||
import { CommentsResolver } from './comments/comments.resolver'; | ||
import { JsonplaceholderModule } from 'src/jsonplaceholder/jsonplaceholder.module'; | ||
|
||
@Module({ | ||
imports: [HttpModule, forwardRef(() => UserModule)], | ||
providers: [PostsService, PostsResolver, CommentsService, CommentsResolver], | ||
imports: [ | ||
forwardRef(() => UserModule), | ||
JsonplaceholderModule | ||
], | ||
providers: [ | ||
PostsService, | ||
PostsResolver, | ||
CommentsService, | ||
CommentsResolver | ||
], | ||
exports: [PostsService] | ||
}) | ||
export class PostsModule {} |
Oops, something went wrong.