Skip to content

Instantly share code, notes, and snippets.

@nietzscheson
Created April 23, 2020 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nietzscheson/ddf4358d9c69a18fc5e7dd1f1ff45259 to your computer and use it in GitHub Desktop.
Save nietzscheson/ddf4358d9c69a18fc5e7dd1f1ff45259 to your computer and use it in GitHub Desktop.
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