This repository contains Full stack for TODO app.
todo_backend/
: Solution to the backend in Go for managing todos.todo-mobile/
: Solution to the front-end in React Native for managing todos.doctor-list/
: Solution to the React app for displaying a list of doctors.most_common-chars/
: Solution to finding the most common characters in a string.
A Go backend that lets you add a todo and get the list of todos.
POST /addTodo
: Adds a new todo.GET /getTodos
: Retrieves the list of todos.
For more details, refer to the todo_backend README.
A React Native front-end that lets you add a todo and get the list of todos. The front-end calls the backend routes.
For more details, refer to the todo-mobile README.
- Navigate to the
todo-mobile
directory:cd todo-mobile
- Install dependencies:
npm install
- Run the React Native app:
npm start
A Go function that returns a set of the most common characters in a given string.
For the word "annoying", the most common character is {'n'}
. For the word "implementation", the most common characters are {'i', 'm', 'e', 't', 'n'}
.
For more details, refer to the most_common-chars README.
- Navigate to the
most_common-chars
directory:cd most_common-chars
- Run the Go file:
go run main.go
A simple React app that displays a list of doctors using TailwindCSS for styling. For more details, refer to the doctor-list README.
- Navigate to the
doctor-list
directory:cd doctor-list
- Install dependencies:
npm install
- Run the React app:
npm start
Here are some improvements:
- Async/Await: Use
async
/await
to handle asynchronous requests instead of directly assigningaxios(query)
tores
. - Parameter Validation: Validate and handle cases where
url
ortoken
might be missing or invalid. - Logging and Error Handling: Improve logging and error handling for better readability and debugging.
- Code Duplication: Refactor to avoid duplicating code, particularly in the
get
andpost
methods. - Extract Header Generation: Extract header generation into a separate function to avoid redundancy.
Here is the refactored code:
import axios from 'axios';
const generateHeaders = (token) => {
return {
Authorization: token ? `Bearer ${token}` : '',
Accept: 'application/json',
'X-Request-ID': Math.floor(Math.random() * 1000000),
};
};
const logQueryResponse = (query, res) => {
console.log('Query: ', query);
if (res.status.toString().startsWith('2')) {
console.log('Response: ', res);
} else {
console.error('Response: ', res);
}
};
const handleError = (error) => {
if (error.response) {
console.error('Response error: ', error.response);
} else {
console.error('Error: ', error.message);
}
};
export const Service = {
get: async (url, token, params = {}, log = false) => {
if (!url || !token) {
throw new Error('URL and token are required');
}
const headers = generateHeaders(token);
const query = {
method: 'get',
url,
headers,
...(params ? { params } : {}),
};
try {
const res = await axios(query);
if (log) {
logQueryResponse(query, res);
}
return res;
} catch (error) {
handleError(error);
throw error;
}
},
post: async (url, token, data, log = true) => {
if (!url || !token) {
throw new Error('URL and token are required');
}
const headers = {
...generateHeaders(token),
'Content-Type': 'application/json',
};
const query = {
method: 'post',
url,
headers,
data,
};
try {
const res = await axios(query);
if (log) {
logQueryResponse(query, res);
}
return res;
} catch (error) {
handleError(error);
throw error;
}
},
};