File size: 1,312 Bytes
8738ae8
 
 
 
 
5efe61a
8738ae8
 
5efe61a
 
8738ae8
 
5efe61a
8738ae8
 
 
 
 
 
 
 
 
 
 
 
 
5efe61a
 
 
 
 
 
 
 
 
 
 
 
c7775be
5efe61a
 
 
 
 
 
 
 
 
 
 
 
 
 
8738ae8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { UserModel, UserInputDTO } from "../model";
import { BaseDatabase } from "./base-database";

export class UserDatabase extends BaseDatabase {

  createUser = async (name: string, password: string, email: string) => {
    try {
      await this.getConnection()
      await new UserModel({
        name, password, email
      }).save()
    } catch (error) {
      console.log(error)
      throw new Error(error.message)
    }
  }

  getUserByEmail = async (email: string) => {
    try {
      await this.getConnection()
      return await UserModel.findOne({ email }).exec()
    } catch (error) {
      throw new Error(error.message)
    }
  }

  getUsers = async () => {
    try {
      await this.getConnection()
      return await UserModel.find({}).exec()
    } catch (error) {
      throw new Error(error.message)
    }
  }

  getUserById = async (id: string) => {
    try {
      await this.getConnection()
      return await UserModel.findById(id, 'name email balance').exec()
    } catch (error) {
      throw new Error(error.message)
    }
  }

  updateBalance = async (id: string, newBalance: number) => {
    try {
      await this.getConnection()
      return await UserModel.findByIdAndUpdate(id, { balance: newBalance })
    } catch (error) {
      throw new Error(error.message)
    }
  }

}