Created
April 23, 2020 14:43
-
-
Save nietzscheson/ddf4358d9c69a18fc5e7dd1f1ff45259 to your computer and use it in GitHub Desktop.
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
import { Injectable, NotFoundException } from '@nestjs/common'; | |
import { InjectRepository } from '@nestjs/typeorm'; | |
import { User } from './user.entity'; | |
import { Repository } from 'typeorm'; | |
import { Args } from '@nestjs/graphql'; | |
import { UsersArgs } from './dto/users.args'; | |
import { ResourceArgs } from '../common/dto/resource.args'; | |
import { UserArgs } from './dto/user.args'; | |
import { AuthArgs } from '../auth/dto/auth.args'; | |
@Injectable() | |
export class UserService { | |
constructor(@InjectRepository(User) private readonly repository: Repository<User>) {} | |
async one(@Args() resource: ResourceArgs){ | |
const entity = await this.repository.findOne(resource.id); | |
if (!entity) { | |
throw new NotFoundException(resource.id); | |
} | |
return entity; | |
} | |
async all(@Args() userArgs: UsersArgs){ | |
return this.repository.find(); | |
} | |
async persist(@Args() input: UserArgs): Promise<User> { | |
return await this.repository.save(input); | |
} | |
async delete(@Args() resource: ResourceArgs): Promise<User> { | |
const entity = await this.one(resource); | |
await this.repository.delete( entity ); | |
return entity; | |
} | |
async oneBy(args: object): Promise<User>{ | |
return await this.repository.findOneOrFail(args); | |
} | |
async oneByEmailOrUsername(args: AuthArgs): Promise<User>{ | |
return await this.repository.findOneOrFail( { where: [ { email: args.username }, { username: args.username } ] }) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment