-
Notifications
You must be signed in to change notification settings - Fork 0
/
logic.ts
76 lines (70 loc) · 2.32 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 * as chalk from 'chalk';
import * as ora from 'ora';
import {UpdateContactForm, ContactForm} from './firefunctions/functions/src/interfaces';
const url: string = "https://us-central1-contactmanager-cli.cloudfunctions.net";
export const addContact = async(answers: ContactForm) => {
try {
const spinner = ora('Adding Contact').start();
await axios.post(`${url}/addContact`, answers);
spinner.stop();
console.log(chalk.magentaBright('New Contact added'));
}
catch(err) {
console.log({err});
}
}
export const getContact = async (id: number) => {
try {
const spinner = ora('Fetching Contact').start();
const response = await axios.get(`${url}/getContact/${id}`);
spinner.clear();
spinner.stop();
const obj = response.data;
for (const key in obj) {
console.log(chalk.blue('________________'));
console.log(chalk.greenBright(`email: ${obj[key].email}`))
}
}
catch(err) {
console.log({err});
}
};
export const updateContact = async (contact: UpdateContactForm) => {
try {
const spinner = ora('Updating Contact').start();
await axios.put(`${url}/updateContact/${contact.id}` , contact);
spinner.stop()
console.log(chalk.cyanBright('Contact updated'))
}
catch (error) {
console.log(error)
}
};
export const deleteContact = async (id: number) => {
try {
const spinner = ora('Deleting Contact').start();
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();
const response = await axios.get(`${url}/getContactList`);
spinner.stop()
const 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)
}
};