-
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.
added album and photo data to user object
- Loading branch information
Showing
13 changed files
with
192 additions
and
26 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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { AlbumsService } from './albums.service'; | ||
import { HttpModule } from '@nestjs/axios'; | ||
import { PhotosService } from './photos/photos.service'; | ||
import { AlbumResolver } from './albums.resolver'; | ||
|
||
@Module({ | ||
imports: [HttpModule], | ||
providers: [AlbumsService, AlbumResolver, PhotosService], | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Parent, ResolveField, Resolver } from "@nestjs/graphql"; | ||
import { PhotosService } from "./photos/photos.service"; | ||
import { Photo } from "src/graphql"; | ||
|
||
@Resolver('Album') | ||
export class AlbumResolver { | ||
|
||
constructor(private readonly photosService: PhotosService) {} | ||
|
||
@ResolveField('photos') | ||
async getAlbumPhotos(@Parent() album): Promise<Photo[]> { | ||
const { id } = album | ||
return await this.photosService.getPhotosByAlbumId(id) | ||
} | ||
|
||
} |
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 { AlbumsService } from './albums.service'; | ||
|
||
describe('AlbumsService', () => { | ||
let service: AlbumsService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [AlbumsService], | ||
}).compile(); | ||
|
||
service = module.get<AlbumsService>(AlbumsService); | ||
}); | ||
|
||
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,31 @@ | ||
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 { Album } from 'src/graphql'; | ||
|
||
@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') | ||
} | ||
|
||
async getUserAlbums(id: number): Promise<Album[]> { | ||
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!") | ||
}) | ||
) | ||
) | ||
return data.filter(o => o.userId === id) | ||
} | ||
} |
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 { PhotosService } from './photos.service'; | ||
|
||
describe('PhotosService', () => { | ||
let service: PhotosService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [PhotosService], | ||
}).compile(); | ||
|
||
service = module.get<PhotosService>(PhotosService); | ||
}); | ||
|
||
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,32 @@ | ||
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 { Photo } from 'src/graphql'; | ||
|
||
@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): 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!") | ||
}) | ||
) | ||
) | ||
|
||
return data.filter(o => o.albumId === albumId) | ||
} | ||
} |
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
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,11 +1,12 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { AuthorsResolver } from './users.resolver'; | ||
import { AuthorService } from './users.service'; | ||
import { UsersResolver } from './users.resolver'; | ||
import { UserService } from './users.service'; | ||
import { HttpModule } from '@nestjs/axios'; | ||
import { PostsModule } from 'src/posts/posts.module'; | ||
import { AlbumsModule } from 'src/albums/albums.module'; | ||
|
||
@Module({ | ||
imports: [HttpModule, PostsModule], | ||
providers: [AuthorsResolver, AuthorService] | ||
imports: [HttpModule, PostsModule, AlbumsModule], | ||
providers: [UsersResolver, UserService] | ||
}) | ||
export class AuthorModule {} |
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,30 +1,38 @@ | ||
|
||
import { Args, Parent, Query, ResolveField, Resolver } from "@nestjs/graphql"; | ||
import { Comment, Post, User } from "src/graphql"; | ||
import { AuthorService } from "./users.service"; | ||
import { Post, User } from "src/graphql"; | ||
import { UserService } from "./users.service"; | ||
import { PostsService } from "src/posts/posts.service"; | ||
import { AlbumsService } from "src/albums/albums.service"; | ||
|
||
@Resolver('User') | ||
export class AuthorsResolver { | ||
export class UsersResolver { | ||
|
||
constructor( | ||
private readonly authorService: AuthorService, | ||
private readonly postsService: PostsService | ||
private readonly userService: UserService, | ||
private readonly postsService: PostsService, | ||
private readonly albumService: AlbumsService | ||
){} | ||
|
||
@Query('users') | ||
async authors(): Promise<User[]> { | ||
return await this.authorService.getAuthors() | ||
return await this.userService.getUsers() | ||
} | ||
|
||
@Query('user') | ||
async author(@Args('id') id: number): Promise<User> { | ||
return await this.authorService.getAuthor(id) | ||
return await this.userService.getUserById(id) | ||
} | ||
|
||
@ResolveField('posts') | ||
async getPosts(@Parent() author): Promise<Post[]> { | ||
const { id } = author | ||
return await this.postsService.getAuthorPosts(id) | ||
async getPosts(@Parent() user): Promise<Post[]> { | ||
const { id } = user | ||
return await this.postsService.getUserPosts(id) | ||
} | ||
|
||
@ResolveField('albums') | ||
async getAlbums(@Parent() user) { | ||
const { id } = user | ||
return await this.albumService.getUserAlbums(id); | ||
} | ||
} |
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