1
0
Fork 0
mirror of https://github.com/muerwre/vk-tg-bot.git synced 2025-04-25 06:56:40 +07:00

#4 implemented postgres events

This commit is contained in:
Fedor Katurov 2021-05-04 13:58:05 +07:00
parent 50db549cbe
commit e73ab3cb4f
3 changed files with 55 additions and 51 deletions

View file

@ -1,6 +1,6 @@
import { VkEvent } from "../vk/types"; import { VkEvent } from "../vk/types";
import { StoredEvent, StoredLike } from "./types";
import { Like } from "./postgres/entities/Like"; import { Like } from "./postgres/entities/Like";
import { Event } from "./postgres/entities/Event";
export interface Storage { export interface Storage {
getEvent( getEvent(
@ -8,17 +8,13 @@ export interface Storage {
id: number, id: number,
groupId: number, groupId: number,
channel: string channel: string
): Promise<StoredEvent>; ): Promise<Event>;
createEvent(event: Partial<StoredEvent>): Promise<StoredEvent>; createEvent(event: Partial<Event>): Promise<Event>;
createOrUpdateLike(like: Partial<StoredLike>): Promise<Like>; createOrUpdateLike(like: Partial<Like>): Promise<Like>;
getLikesFor(channel: string, messageId: number): Promise<StoredLike[]>; getLikesFor(channel: string, messageId: number): Promise<Like[]>;
getLikeBy( getLikeBy(channel: string, messageId: number, author: number): Promise<Like>;
channel: string,
messageId: number,
author: number
): Promise<StoredLike>;
} }

View file

@ -1,15 +1,19 @@
import { Storage } from "../index"; import { Storage } from "../index";
import { VkEvent } from "../../vk/types"; import { VkEvent } from "../../vk/types";
import { StoredEvent } from "../types";
import { PostgresConfig } from "./types"; import { PostgresConfig } from "./types";
import { Connection, createConnection } from "typeorm"; import { Connection, createConnection, Repository } from "typeorm";
import logger from "../../logger"; import logger from "../../logger";
import path from "path"; import path from "path";
import { Like } from "./entities/Like";
import { Event } from "./entities/Event";
const entities = [path.join(__dirname, "./entities/*")]; const entities = [path.join(__dirname, "./entities/*")];
export class PostgresDB implements Storage { export class PostgresDB implements Storage {
private connection: Connection; private connection: Connection;
private events: Repository<Event>;
private likes: Repository<Like>;
constructor(private config: PostgresConfig) {} constructor(private config: PostgresConfig) {}
connect = async () => { connect = async () => {
@ -23,6 +27,9 @@ export class PostgresDB implements Storage {
await this.connection.synchronize(); await this.connection.synchronize();
this.events = this.connection.getRepository(Event);
this.likes = this.connection.getRepository(Like);
logger.info(`db connected to ${this.config.uri}`); logger.info(`db connected to ${this.config.uri}`);
}; };
@ -32,28 +39,50 @@ export class PostgresDB implements Storage {
groupId: number, groupId: number,
channel: string channel: string
) => { ) => {
return { return await this.events.findOne({ type, id, groupId, channel });
type,
id,
groupId,
channel,
tgMessageId: 0,
createdAt: new Date(),
updatedAt: new Date(),
};
}; };
createEvent = async (event: StoredEvent) => event; createEvent = async (event) => {
const result = this.events.create({
...event,
});
getLikesFor = async (channel: string, messageId: number) => []; return result[0];
};
getLikeBy = async (channel: string, messageId: number, author: number) => ({ getLikesFor = async (channel, messageId) => {
id: 0, return await this.likes.find({
where: { channel, messageId },
});
};
getLikeBy = async (channel, messageId, author) => {
return this.likes.findOne({
channel,
messageId,
author,
});
};
createOrUpdateLike = async ({
channel, channel,
messageId,
author, author,
text: "", text,
createdAt: new Date(), messageId,
updatedAt: new Date(), }: Partial<Like>) => {
}); const like = await this.likes.findOne({ channel, author, messageId });
if (like) {
like.text = text;
return await this.likes.save(like);
} else {
const created = await this.likes.create({
channel,
author,
text,
messageId,
});
return created[0];
}
};
} }

View file

@ -1,21 +0,0 @@
import { VkEvent } from "../vk/types";
export interface StoredEvent {
type: VkEvent;
id: number;
groupId: number;
channel: string;
tgMessageId: number;
createdAt: Date;
updatedAt: Date;
}
export interface StoredLike {
id: number;
messageId: number;
channel: string;
text: string;
author: number;
createdAt: Date;
updatedAt: Date;
}