Skip to content

Commit

Permalink
completed the redis demo app (client side)
Browse files Browse the repository at this point in the history
  • Loading branch information
ksmooi committed Nov 2, 2024
1 parent aff76f9 commit 5f3688a
Show file tree
Hide file tree
Showing 3 changed files with 200 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/apps/redis_app/client/apiService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import axios, { AxiosInstance } from 'axios';

class APIService {
private client: AxiosInstance;

constructor(baseURL: string) {
this.client = axios.create({
baseURL,
headers: {
'Content-Type': 'application/json',
},
});
}

// Create or Update a key
public async setKey(key: string, value: string): Promise<void> {
try {
await this.client.post('/set', { key, value });
} catch (error: any) {
throw new Error(error.response?.data?.message || 'Error setting key');
}
}

// Get a key's value
public async getKey(key: string): Promise<string> {
try {
const response = await this.client.get(`/get/${key}`);
return response.data.value;
} catch (error: any) {
throw new Error(error.response?.data?.message || 'Error getting key');
}
}

// Delete a key
public async deleteKey(key: string): Promise<void> {
try {
await this.client.delete(`/delete/${key}`);
} catch (error: any) {
throw new Error(error.response?.data?.message || 'Error deleting key');
}
}

// Get all keys
public async getAllKeys(): Promise<string[]> {
try {
const response = await this.client.get('/keys');
return response.data.keys;
} catch (error: any) {
throw new Error(error.response?.data?.message || 'Error fetching keys');
}
}
}

export default APIService;
130 changes: 130 additions & 0 deletions src/apps/redis_app/client/cliClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import inquirer from 'inquirer';
import APIService from './apiService';

class CLIClient {
private apiService: APIService;

constructor(apiService: APIService) {
this.apiService = apiService;
}

public async start(): Promise<void> {
console.log('Welcome to the Redis CRUD CLI Client!\n');
let exit = false;

while (!exit) {
const { action } = await inquirer.prompt([
{
type: 'list',
name: 'action',
message: 'Select an action:',
choices: [
'Create/Update a Key',
'Read a Key',
'Delete a Key',
'List All Keys',
'Exit',
],
},
]);

switch (action) {
case 'Create/Update a Key':
await this.createOrUpdateKey();
break;
case 'Read a Key':
await this.readKey();
break;
case 'Delete a Key':
await this.deleteKey();
break;
case 'List All Keys':
await this.listAllKeys();
break;
case 'Exit':
exit = true;
console.log('Goodbye!');
break;
}
}
}

private async createOrUpdateKey(): Promise<void> {
const answers = await inquirer.prompt([
{
type: 'input',
name: 'key',
message: 'Enter the key:',
validate: (input) => (input ? true : 'Key cannot be empty.'),
},
{
type: 'input',
name: 'value',
message: 'Enter the value:',
validate: (input) => (input ? true : 'Value cannot be empty.'),
},
]);

try {
await this.apiService.setKey(answers.key, answers.value);
console.log(`\nKey '${answers.key}' set successfully.\n`);
} catch (error: any) {
console.error(`\nError: ${error.message}\n`);
}
}

private async readKey(): Promise<void> {
const { key } = await inquirer.prompt([
{
type: 'input',
name: 'key',
message: 'Enter the key to read:',
validate: (input) => (input ? true : 'Key cannot be empty.'),
},
]);

try {
const value = await this.apiService.getKey(key);
console.log(`\nValue for key '${key}': ${value}\n`);
} catch (error: any) {
console.error(`\nError: ${error.message}\n`);
}
}

private async deleteKey(): Promise<void> {
const { key } = await inquirer.prompt([
{
type: 'input',
name: 'key',
message: 'Enter the key to delete:',
validate: (input) => (input ? true : 'Key cannot be empty.'),
},
]);

try {
await this.apiService.deleteKey(key);
console.log(`\nKey '${key}' deleted successfully.\n`);
} catch (error: any) {
console.error(`\nError: ${error.message}\n`);
}
}

private async listAllKeys(): Promise<void> {
try {
const keys = await this.apiService.getAllKeys();
if (keys.length === 0) {
console.log('\nNo keys found in Redis.\n');
return;
}
console.log('\nKeys in Redis:\n');
keys.forEach((key, index) => {
console.log(`${index + 1}. ${key}`);
});
console.log('');
} catch (error: any) {
console.error(`\nError: ${error.message}\n`);
}
}
}

export default CLIClient;
16 changes: 16 additions & 0 deletions src/apps/redis_app/client/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// npx ts-node src/apps/redis_app/client/client.ts

import APIService from './apiService';
import CLIClient from './cliClient';
import dotenv from 'dotenv';

dotenv.config();

const main = async () => {
const baseURL = process.env.SERVER_BASE_URL || 'http://192.168.1.150:5002/api/redis';
const apiService = new APIService(baseURL);
const cliClient = new CLIClient(apiService);
await cliClient.start();
};

main();

0 comments on commit 5f3688a

Please sign in to comment.