-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathlogic.ts
76 lines (74 loc) · 2.69 KB
/
logic.ts
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import axios from 'axios'
import chalk from 'chalk'
import * as ora from 'ora'
const url: string = "https://us-central1-myreddit-clone.cloudfunctions.net"
export const addContact = (answers: any) => {
(async () => {
try {
const spinner = ora('Adding contact ...').start();
let response = await axios.post(`${url}/addContact`,answers)
spinner.stop()
console.log(chalk.magentaBright('New contact added'))
} catch (error) {
console.log(error)
}
})()
}
export const getContact = (id: number) => {
( async ()=>{
try {
const spinner = ora('Fetching contact ...').start();
let response = await axios.get(`${url}/getContact/${id}/`)
spinner.clear()
spinner.stop()
var obj = response.data
for (var key in obj) {
console.log(chalk.blue('==============='))
console.log(chalk.greenBright(`id: ${id} \nFirstname: ${obj[key].firstname} \nLastname: ${obj[key].lastname} \nPhone Number: ${obj[key].phone} \nEmail: ${obj[key].email}`))
}
} catch (error) {
console.log(error)
}
})()
}
export const updateContact= (contact: any) => {
(async () => {
try {
const spinner = ora('Updating contact ...').start();
let response = await axios.put(`${url}/updateContact/${contact.id}` , contact)
spinner.stop()
console.log(chalk.cyanBright('Contact updated'))
} catch (error) {
console.log(error)
}
})()
}
export const deleteContact= (id: number) => {
(async () => {
try {
const spinner = ora('Deleting contact ...').start();
let response = await axios.delete(`${url}/deleteContact/${id}`)
spinner.stop()
console.log(chalk.bgMagentaBright('Contact deleted'))
} catch (error) {
console.log(error)
}
})()
}
export const getContactList= () => {
(async () => {
try {
const spinner = ora('Fetching All Contacts ...').start();
let response = await axios.get(`${url}/getContactList`)
spinner.stop()
var obj = response.data.res
console.log(chalk.green('**********=== Contacts List===**********'))
for (var key in obj) {
console.log(chalk.blue('==============='))
console.log(chalk.greenBright(`id: ${key} \nFirstname: ${obj[key].firstname} \nLastname: ${obj[key].lastname} \nPhone Number: ${obj[key].phone} \nEmail: ${obj[key].email}`))
}
} catch (error) {
console.log(error)
}
})()
}