|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- import { Database } from 'sqlite3'
- import User from '../../models/User'
- import {v4} from 'uuid'
- import FolderService from '../Folder/service'
-
- export default class UserService {
- private readonly db = new Database(process.env.DATABASE_PATH)
- private readonly folderService = new FolderService()
-
- async getByUsername(username: string): Promise<User> {
- return new Promise((resolve, reject) => {
- const statement = this.db.prepare(`
- SELECT *
- FROM users
- WHERE name = (?)
- `)
-
- statement.get(username, (err, result) => {
- if (err) {
- reject(err)
- return
- }
- if (result) {
- resolve(User.resolve(result))
- return
- }
- resolve(null)
- })
- })
- }
-
- async createUser(username: string): Promise<User> {
- const now = new Date()
- const userFolder = await this.folderService.create(username)
- return new Promise((resolve, reject) => {
- const userId = v4()
- const userStatement = this.db.prepare(`
- INSERT INTO users (
- id,
- name,
- root_folder_id,
- created_at,
- updated_at,
- deleted_at
- ) VALUES (?, ?, ?, ?, ?, NULL)
- `)
-
- const createdAt = now.toISOString()
- const updatedAt = now.toISOString()
- const rootFolderId = userFolder.id
- userStatement.run(userId, username, rootFolderId, createdAt, updatedAt, (err) => {
- if (err) {
- reject(err)
- return
- }
- resolve(User.resolve({
- 'id': userId,
- 'name': username,
- 'root_folder_id': rootFolderId,
- 'created_at': now,
- 'updated_at': now,
- 'deleted_at': null,
- }))
- })
- })
- }
- }
|