change folder structure
This commit is contained in:
parent
a332e4d779
commit
a09c75a998
167 changed files with 262 additions and 246 deletions
|
@ -1,132 +0,0 @@
|
|||
import { dataSource } from "../../data-source";
|
||||
import { role } from "../../entity/user/role";
|
||||
import { user } from "../../entity/user/user";
|
||||
import DatabaseActionException from "../../exceptions/databaseActionException";
|
||||
import InternalException from "../../exceptions/internalException";
|
||||
|
||||
export default abstract class UserService {
|
||||
/**
|
||||
* @description get users
|
||||
* @returns {Promise<Array<user>>}
|
||||
*/
|
||||
static async getAll(): Promise<Array<user>> {
|
||||
return await dataSource
|
||||
.getRepository(user)
|
||||
.createQueryBuilder("user")
|
||||
.leftJoinAndSelect("user.roles", "roles")
|
||||
.leftJoinAndSelect("user.permissions", "permissions")
|
||||
.leftJoinAndSelect("roles.permissions", "role_permissions")
|
||||
.orderBy("firstname", "ASC")
|
||||
.addOrderBy("lastname", "ASC")
|
||||
.getMany()
|
||||
.then((res) => {
|
||||
return res;
|
||||
})
|
||||
.catch((err) => {
|
||||
throw new DatabaseActionException("SELECT", "user", err);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @description get user by id
|
||||
* @param id string
|
||||
* @returns {Promise<user>}
|
||||
*/
|
||||
static async getById(id: string): Promise<user> {
|
||||
return await dataSource
|
||||
.getRepository(user)
|
||||
.createQueryBuilder("user")
|
||||
.leftJoinAndSelect("user.roles", "roles")
|
||||
.leftJoinAndSelect("user.permissions", "permissions")
|
||||
.leftJoinAndSelect("roles.permissions", "role_permissions")
|
||||
.where("user.id = :id", { id: id })
|
||||
.getOneOrFail()
|
||||
.then((res) => {
|
||||
return res;
|
||||
})
|
||||
.catch((err) => {
|
||||
throw new DatabaseActionException("SELECT", "user", err);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @description get user by username
|
||||
* @param username string
|
||||
* @returns {Promise<user>}
|
||||
*/
|
||||
static async getByUsername(username: string): Promise<user> {
|
||||
return await dataSource
|
||||
.getRepository(user)
|
||||
.createQueryBuilder("user")
|
||||
.select()
|
||||
.where("user.username = :username", { username: username })
|
||||
.getOneOrFail()
|
||||
.then((res) => {
|
||||
return res;
|
||||
})
|
||||
.catch((err) => {
|
||||
throw new DatabaseActionException("SELECT", "user", err);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @description get users by mail or username
|
||||
* @param username string
|
||||
* @param mail string
|
||||
* @returns {Promise<Array<user>>}
|
||||
*/
|
||||
static async getByMailOrUsername(mail?: string, username?: string): Promise<Array<user>> {
|
||||
return await dataSource
|
||||
.getRepository(user)
|
||||
.createQueryBuilder("user")
|
||||
.select()
|
||||
.where("user.mail = :mail", { mail: mail })
|
||||
.orWhere("user.username = :username", { username: username })
|
||||
.getMany()
|
||||
.then((res) => {
|
||||
return res;
|
||||
})
|
||||
.catch((err) => {
|
||||
throw new DatabaseActionException("SELECT", "user", err);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @description get count of users
|
||||
* @returns {Promise<number>}
|
||||
*/
|
||||
static async count(): Promise<number> {
|
||||
return await dataSource
|
||||
.getRepository(user)
|
||||
.createQueryBuilder("user")
|
||||
.select()
|
||||
.getCount()
|
||||
.then((res) => {
|
||||
return res;
|
||||
})
|
||||
.catch((err) => {
|
||||
throw new DatabaseActionException("COUNT", "users", err);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @description get roles assigned to user
|
||||
* @param userId string
|
||||
* @returns {Promise<Array<role>>}
|
||||
*/
|
||||
static async getAssignedRolesByUserId(userId: string): Promise<Array<role>> {
|
||||
return await dataSource
|
||||
.getRepository(user)
|
||||
.createQueryBuilder("user")
|
||||
.leftJoinAndSelect("user.roles", "roles")
|
||||
.leftJoinAndSelect("roles.permissions", "role_permissions")
|
||||
.where("user.id = :id", { id: userId })
|
||||
.getOneOrFail()
|
||||
.then((res) => {
|
||||
return res.roles;
|
||||
})
|
||||
.catch((err) => {
|
||||
throw new DatabaseActionException("SELECT", "userRoles", err);
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue