Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
public static async getUsers(ctx: BaseContext) {
// get a user repository to perform operations with user
const userRepository: Repository = getManager().getRepository(User);
// load all users
const users: User[] = await userRepository.find();
// return OK status code and loaded users array
ctx.status = 200;
ctx.body = users;
}
async createComment(ctx: Context) {
const { articleId, content, userId } = ctx.request.body
const articleRepository = getManager().getRepository(Article)
const commentRepository = getManager().getRepository(Comment)
const userRepository = getManager().getRepository(User)
// 查询目标文章和用户
const targetArticle = await articleRepository.findOne({ where: { id: articleId }, relations: ['comments'] })
const targetUser = await userRepository.findOne({ where: { id: userId }, relations: ['comments'] })
// 创建comment并存储
const newComment = commentRepository.create({
content
})
await commentRepository.save(newComment)
// 存储到文章和用户中去
targetArticle.comments = [...targetArticle.comments, newComment]
targetUser.comments = [...targetUser.comments, newComment]
await userRepository.save(targetUser)
await articleRepository.save(targetArticle)
// 查找最新评论发回去
const resComment = await commentRepository
import { bind } from 'decko';
import { Like, Repository, FindConditions, getManager, FindManyOptions, FindOneOptions } from 'typeorm';
import { CacheService } from '@services/cache';
import { User } from './model';
export class UserService {
private readonly defaultRelations: string[] = ['userRole', 'assignee', 'tasksWatched'];
private readonly cacheService: CacheService = new CacheService();
private readonly repo: Repository = getManager().getRepository(User);
/**
* Read all users from db
*
* @param options Find options
* @param cached Read users from cache
* @returns Returns an array of users
*/
@bind
public readUsers(options: FindManyOptions = {}, cached: boolean = false): Promise {
try {
if (Object.keys(options).length) {
return this.repo.find({
relations: this.defaultRelations,
...options
});
export function getUsersRepo(manager?: EntityManager) {
let entityManager = manager ? manager : getManager();
return entityManager.getCustomRepository(repositories.UserRepository);
}
constructor() {
super(getManager('query'));
}
constructor() {
this.logger = new Logger(__filename);
this.currencyRepository = getManager().getRepository(Currency);
this.userRepository = getManager().getRepository(User);
this.billRepository = getManager().getRepository(Bill);
this.additionalRepository = getManager().getRepository(Additional);
}
async createUser(username: string, email: string, password: string, roleNames?: string[]): Promise {
const { hash, salt } = PasswordHelper.encrypt(password);
const roles = _.isEmpty(roleNames) ? null : await this.roleRepository.findByNames(roleNames);
let user = await this.getUser({ username, email });
if (!user) {
user = this.userRepository.create({ email, username, isActive: true });
}
logger.log(`found user ${r(user)}`);
user.password = hash;
user.salt = salt;
user.roles = roles;
return getManager().save(user);
}
async countAllByOffer(userId: string): Promise> {
const results: Array<{ offer_id: string, cnt: number }> = await getManager().query(
`SELECT
orders.offer_id, COUNT(DISTINCT(orders.id)) as cnt
FROM orders
LEFT JOIN orders_contexts
ON orders.id = orders_contexts.order_id
WHERE
(status = $1 OR (status IN ($2) AND expiration_date > $3))
AND orders_contexts.user_id = ($4)
GROUP BY offer_id`, ["completed", ["pending"], new Date(), userId]);
const map = new Map();
for (const res of results) {
map.set(res.offer_id, res.cnt);
}
return map;
},
constructor() {
this.logger = new Logger(__filename);
this.languagesRepository = getManager().getRepository(Language);
}
constructor() {
super(getManager('query'));
}