Discord bot
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

service.ts 1.5 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { Database } from 'sqlite3'
  2. import User from '../../models/User'
  3. import {v4} from 'uuid'
  4. import FolderService from '../Folder/service'
  5. export default class UserService {
  6. private readonly db = new Database(process.env.DATABASE_PATH)
  7. private readonly folderService = new FolderService()
  8. async getByUsername(username: string): Promise<User> {
  9. return new Promise((resolve, reject) => {
  10. const statement = this.db.prepare(`
  11. SELECT *
  12. FROM users
  13. WHERE name = (?)
  14. `)
  15. statement.get(username, (err, result) => {
  16. if (err) {
  17. reject(err)
  18. return
  19. }
  20. if (result) {
  21. resolve(User.resolve(result))
  22. return
  23. }
  24. resolve(null)
  25. })
  26. })
  27. }
  28. async createUser(username: string): Promise<User> {
  29. const now = new Date()
  30. const userFolder = await this.folderService.create(username)
  31. return new Promise((resolve, reject) => {
  32. const userId = v4()
  33. const userStatement = this.db.prepare(`
  34. INSERT INTO users (
  35. id,
  36. name,
  37. root_folder_id,
  38. created_at,
  39. updated_at,
  40. deleted_at
  41. ) VALUES (?, ?, ?, ?, ?, NULL)
  42. `)
  43. const createdAt = now.toISOString()
  44. const updatedAt = now.toISOString()
  45. const rootFolderId = userFolder.id
  46. userStatement.run(userId, username, rootFolderId, createdAt, updatedAt, (err) => {
  47. if (err) {
  48. reject(err)
  49. return
  50. }
  51. resolve(User.resolve({
  52. 'id': userId,
  53. 'name': username,
  54. 'root_folder_id': rootFolderId,
  55. 'created_at': now,
  56. 'updated_at': now,
  57. 'deleted_at': null,
  58. }))
  59. })
  60. })
  61. }
  62. }