Skip to content

Commit

Permalink
Added user service with basic methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ashleigh committed Oct 2, 2018
1 parent c7729cd commit 38e333b
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/user/user.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import {Injectable} from '@nestjs/common';
import {InjectRepository} from '@nestjs/typeorm';
import { Repository, UpdateResult } from 'typeorm';
import {UserEntity as User, UserEntity} from './../entities';
import { Pagination, PaginationOptionsInterface } from './../paginate';
import { UserModel } from 'models';

@Injectable()
export class UserService {
constructor(@InjectRepository(User) private readonly userRepository: Repository<User>) {}

async paginate(options: PaginationOptionsInterface): Promise<Pagination<User>> {
const [results, total] = await this.userRepository.findAndCount({
take: options.take,
skip: options.page,
});

return new Pagination<User>({
results,
total,
});
}

async create(user: UserModel): Promise<User> {
return await this.userRepository.save(this.userRepository.create(user));
}

async update(user: UserEntity): Promise<UpdateResult> {
return await this.userRepository.update(user.id, user);
}

async findByEmail(email: string): Promise<User|null> {
return await this.userRepository.findOne({
where: {
email,
},
});
}
}

0 comments on commit 38e333b

Please sign in to comment.