-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added persistence for chats IDs (#22)
* feat(persistence-chats): migrate to Yarn Package Manager * feat(persistence-chats): added persistence for chat IDs * feat(persistence-chats): bumped application version and updated README feat #4
- Loading branch information
Showing
10 changed files
with
5,412 additions
and
7,561 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,3 +71,4 @@ typings/ | |
.vscode/launch.json | ||
|
||
/dist | ||
bot.db |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import ChatRepository from '../../../infra/repository/chat/ChatRepository'; | ||
|
||
export default class ChatService { | ||
private readonly chatRepository: ChatRepository = new ChatRepository(); | ||
|
||
async create(chatId: number): Promise<void> { | ||
await this.chatRepository.insert(chatId); | ||
} | ||
|
||
list(): Array<number> { | ||
return this.chatRepository.list().map((chatId) => Number(chatId)); | ||
} | ||
|
||
async delete(chatId: number): Promise<void> { | ||
await this.chatRepository.delete(chatId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import Datastore from 'nedb'; | ||
import { promisify } from 'util'; | ||
|
||
export default class ChatRepository { | ||
private readonly db: Datastore; | ||
|
||
private readonly promisifyUpdate: any; | ||
|
||
private readonly promisifyRemove: any; | ||
|
||
constructor() { | ||
this.db = new Datastore({ filename: 'bot.db', autoload: true }); | ||
this.promisifyUpdate = promisify(this.db.update.bind(this.db)); | ||
this.promisifyRemove = promisify(this.db.remove).bind(this.db); | ||
} | ||
|
||
async insert(chatId: number) { | ||
await this.promisifyUpdate({ chatId }, { $set: { chatId } }, { upsert: true }); | ||
} | ||
|
||
list(): any[] { | ||
return this.db.getAllData().map((item) => item.chatId); | ||
} | ||
|
||
async delete(chatId: number) { | ||
await this.promisifyRemove({ chatId }, {}); | ||
} | ||
} |
Oops, something went wrong.