1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2024-11-03 21:04:03 +08:00
pigallery2/backend/model/mysql/UserManager.ts
2016-12-27 16:09:47 +01:00

83 lines
2.5 KiB
TypeScript

import {UserDTO, UserRoles} from "../../../common/entities/UserDTO";
import {IUserManager} from "../interfaces/IUserManager";
import {UserEntity} from "./enitites/UserEntity";
import {MySQLConnection} from "./MySQLConnection";
export class UserManager implements IUserManager {
constructor() {
}
public findOne(filter: any, cb: (error: any, result: UserDTO) => void) {
MySQLConnection.getConnection().then(async connection => {
let userRepository = connection.getRepository(UserEntity);
return cb(null, await userRepository.findOne(filter));
}).catch((error) => {
return cb(error, null);
});
}
public find(filter: any, cb: (error: any, result: Array<UserDTO>) => void) {
MySQLConnection.getConnection().then(async connection => {
let userRepository = connection.getRepository(UserEntity);
return cb(null, await userRepository.find(filter));
}).catch((error) => {
return cb(error, null);
});
}
public createUser(user: UserDTO, cb: (error: any, result: UserDTO) => void = (e, r) => {
}) {
MySQLConnection.getConnection().then(connection => {
let userRepository = connection.getRepository(UserEntity);
userRepository.persist(user).then(u => cb(null, u)).catch(err => cb(err, null));
}).catch((error) => {
return cb(error, null);
});
}
public deleteUser(id: number, cb: (error: any) => void) {
MySQLConnection.getConnection().then(connection => {
let userRepository = connection.getRepository(UserEntity);
userRepository.findOne({id: id}).then((user) => {
userRepository.remove(user).catch(err => cb(err));
}).catch(err => cb(err));
}).catch((error) => {
return cb(error);
});
}
public changeRole(id: number, newRole: UserRoles, cb: (error: any, result: string) => void) {
MySQLConnection.getConnection().then(async connection => {
let userRepository = connection.getRepository(UserEntity);
let user = await userRepository.findOne({id: id});
user.role = newRole;
await userRepository.persist(user);
return cb(null, "ok");
}).catch((error) => {
return cb(error, null);
});
}
public changePassword(request: any, cb: (error: any, result: string) => void) {
throw new Error("not implemented"); //TODO: implement
}
}