|
| 1 | + |
| 2 | +# Simple Azure Functions To-Do App |
| 3 | + |
| 4 | +This repository contains an Azure Functions application that performs CRUD operations for a To-Do list. The app is backed by MongoDB and deployed to Azure. |
| 5 | + |
| 6 | +## Table of Contents |
| 7 | +1. [Prerequisites](#prerequisites) |
| 8 | +2. [Setup Instructions](#setup-instructions) |
| 9 | +3. [How to Run Locally](#how-to-run-locally) |
| 10 | +4. [Postman API Requests](#postman-api-requests) |
| 11 | +5. [Azure CLI Deployment](#azure-cli-deployment) |
| 12 | +6. [Response Samples](#response-samples) |
| 13 | + |
| 14 | +## Prerequisites |
| 15 | +1. [Azure CLI](https://learn.microsoft.com/en-us/cli/azure/install-azure-cli) |
| 16 | +2. [MongoDB Account](https://www.mongodb.com/cloud/atlas) |
| 17 | +3. [Node.js](https://nodejs.org/) |
| 18 | +4. [Azure Functions Core Tools](https://docs.microsoft.com/en-us/azure/azure-functions/functions-run-local) |
| 19 | + |
| 20 | +## Setup Instructions |
| 21 | +1. Clone the repository: |
| 22 | + ```bash |
| 23 | + git clone <repository_url> |
| 24 | + cd <repository_folder> |
| 25 | + ``` |
| 26 | + |
| 27 | +2. Install dependencies: |
| 28 | + ```bash |
| 29 | + npm install |
| 30 | + ``` |
| 31 | + |
| 32 | +3. Create a `.env` file in the root directory and add the following variables: |
| 33 | + ``` |
| 34 | + MONGODB_URI=<your_mongodb_connection_string> |
| 35 | + ``` |
| 36 | + |
| 37 | +4. Install Azure Functions Core Tools if not already installed: |
| 38 | + ```bash |
| 39 | + npm install -g azure-functions-core-tools@3 --unsafe-perm true |
| 40 | + ``` |
| 41 | + |
| 42 | +## How to Run Locally |
| 43 | +1. Start the Azure Functions app locally: |
| 44 | + ```bash |
| 45 | + func start |
| 46 | + ``` |
| 47 | + |
| 48 | +2. This will start the server on `http://localhost:7071`. You can make requests to the API endpoints as described in the next section. |
| 49 | + |
| 50 | +## Azure Functions Used |
| 51 | + |
| 52 | +Below are the API request types and sample URLs: |
| 53 | + |
| 54 | +| **AZ Function** | **URL** | **Request Type** | **Description** | |
| 55 | +|-------------------------|----------------------------------------|------------------|----------------------------------------------| |
| 56 | +| **Create Todo** | `https://noob-functions.azurewebsites.net/api/create-todo-function` | `POST` | Creates a new To-Do entry. | |
| 57 | +| **Get Todos** | `https://noob-functions.azurewebsites.net/api/get-todos-function` | `GET` | Retrieves all To-Do entries. | |
| 58 | +| **Update Todo** | `https://noob-functions.azurewebsites.net/api/update-todo-function?id=67b5bc9238c337c4de6f9bfa` | `PUT` | Updates a specific To-Do entry by ID. | |
| 59 | +| **Delete Todo** | `https://noob-functions.azurewebsites.net/api/delete-todo-function?id=67b5bc9238c337c4de6f9bfa` | `DELETE` | Deletes a specific To-Do entry by ID. | |
| 60 | + |
| 61 | + |
| 62 | +## Postman API Requests |
| 63 | +### 1. **Create To-Do (POST)** |
| 64 | + - URL: `http://localhost:7071/api/todo-function` |
| 65 | + - Method: POST |
| 66 | + - Body: |
| 67 | + ```json |
| 68 | + { |
| 69 | + "title": "Learn Azure Functions", |
| 70 | + "description": "Learn how to deploy and use Azure Functions" |
| 71 | + } |
| 72 | + ``` |
| 73 | + - Response: |
| 74 | + ```json |
| 75 | + { |
| 76 | + "timestamp": "2025-02-19T00:00:00.000Z", |
| 77 | + "status": "success", |
| 78 | + "message": "To-Do Created", |
| 79 | + "code": 201, |
| 80 | + "todoId": "<generated_todo_id>" |
| 81 | + } |
| 82 | + ``` |
| 83 | + |
| 84 | +### 2. **Get All To-Dos (GET)** |
| 85 | + - URL: `http://localhost:7071/api/todo-function` |
| 86 | + - Method: GET |
| 87 | + - Response: |
| 88 | + ```json |
| 89 | + [ |
| 90 | + { |
| 91 | + "todoId": "<todo_id>", |
| 92 | + "title": "Learn Azure Functions", |
| 93 | + "description": "Learn how to deploy and use Azure Functions", |
| 94 | + "timestamp": "2025-02-19T00:00:00.000Z" |
| 95 | + } |
| 96 | + ] |
| 97 | + ``` |
| 98 | + |
| 99 | +### 3. **Update To-Do (PUT)** |
| 100 | + - URL: `http://localhost:7071/api/todo-function/<todo_id>` |
| 101 | + - Method: PUT |
| 102 | + - Body: |
| 103 | + ```json |
| 104 | + { |
| 105 | + "title": "Learn Azure Functions - Updated", |
| 106 | + "description": "Updated the description" |
| 107 | + } |
| 108 | + ``` |
| 109 | + - Response: |
| 110 | + ```json |
| 111 | + { |
| 112 | + "timestamp": "2025-02-19T00:00:00.000Z", |
| 113 | + "status": "success", |
| 114 | + "message": "To-Do Updated", |
| 115 | + "code": 200 |
| 116 | + } |
| 117 | + ``` |
| 118 | + |
| 119 | +### 4. **Delete To-Do (DELETE)** |
| 120 | + - URL: `http://localhost:7071/api/todo-function/<todo_id>` |
| 121 | + - Method: DELETE |
| 122 | + - Response: |
| 123 | + ```json |
| 124 | + { |
| 125 | + "timestamp": "2025-02-19T00:00:00.000Z", |
| 126 | + "status": "success", |
| 127 | + "message": "To-Do Deleted", |
| 128 | + "code": 200 |
| 129 | + } |
| 130 | + ``` |
| 131 | + |
| 132 | +## Azure CLI Deployment |
| 133 | +### 1. **Install Azure CLI** |
| 134 | + [Install Azure CLI](https://learn.microsoft.com/en-us/cli/azure/install-azure-cli) |
| 135 | + |
| 136 | +### 2. **Login to Azure** |
| 137 | + ```bash |
| 138 | + az login |
| 139 | + ``` |
| 140 | + |
| 141 | +### 3. **Create Resource Group** |
| 142 | + ```bash |
| 143 | + az group create --name <ResourceGroupName> --location <Region> |
| 144 | + ``` |
| 145 | + |
| 146 | +### 4. **Create Function App** |
| 147 | + ```bash |
| 148 | + az functionapp create --resource-group <ResourceGroupName> --consumption-plan-location <Region> --runtime node --runtime-version 14 --functions-version 3 --name <FunctionAppName> --storage-account <StorageAccountName> |
| 149 | + ``` |
| 150 | + |
| 151 | +### 5. **Publish the Function App** |
| 152 | + Navigate to your project folder and deploy the app: |
| 153 | + ```bash |
| 154 | + func azure functionapp publish <FunctionAppName> |
| 155 | + ``` |
| 156 | + |
| 157 | +### 6. **Done!** |
| 158 | + Your Azure Functions app should now be live. You can access it via the URL: |
| 159 | + ``` |
| 160 | + https://<FunctionAppName>.azurewebsites.net |
| 161 | + ``` |
| 162 | + |
| 163 | +## Response Samples |
| 164 | +### Sample Success Response: |
| 165 | +```json |
| 166 | +{ |
| 167 | + "timestamp": "2025-02-19T00:00:00.000Z", |
| 168 | + "status": "success", |
| 169 | + "message": "To-Do Created", |
| 170 | + "code": 201, |
| 171 | + "todoId": "<generated_todo_id>" |
| 172 | +} |
| 173 | +``` |
| 174 | + |
| 175 | +### Sample Error Response: |
| 176 | +```json |
| 177 | +{ |
| 178 | + "timestamp": "2025-02-19T00:00:00.000Z", |
| 179 | + "status": "error", |
| 180 | + "message": "To-Do Not Found", |
| 181 | + "code": 404 |
| 182 | +} |
| 183 | +``` |
| 184 | +## Creating A Simple HTTP Trigger Azure Function (Alternative Learning) |
| 185 | +### 1. **Install Azure CLI** |
| 186 | + [Install Azure CLI](https://learn.microsoft.com/en-us/cli/azure/install-azure-cli) |
| 187 | + |
| 188 | +### 2. **Login to Azure** |
| 189 | + ```bash |
| 190 | + az login |
| 191 | + ``` |
| 192 | + |
| 193 | +### 3. **Create Resource Group** |
| 194 | + ```bash |
| 195 | + az group create --name <ResourceGroupName> --location <Region> |
| 196 | + ``` |
| 197 | + |
| 198 | +### 4. **Create Function App** |
| 199 | + ```bash |
| 200 | + az functionapp create --resource-group <ResourceGroupName> --consumption-plan-location <Region> --runtime node --runtime-version 20 --functions-version 4 --name <FunctionAppName> |
| 201 | + ``` |
| 202 | + |
| 203 | + |
| 204 | +### 5. **Create an Azure Functions Project** |
| 205 | + Run the following command to create a new Azure Functions project: |
| 206 | + ```bash |
| 207 | + func init myAzureFunctionApp --worker-runtime node |
| 208 | + ``` |
| 209 | + |
| 210 | +### 6. **Create a New HTTP-Triggered Function** |
| 211 | + Generate an HTTP function: |
| 212 | + ```bash |
| 213 | + func new --name myHttpFunction --template "HTTP trigger" --authlevel "anonymous" |
| 214 | + ``` |
| 215 | + |
| 216 | +### 7. **Publish the Function App** |
| 217 | + Navigate to your project folder and deploy the app: |
| 218 | + ```bash |
| 219 | + func azure functionapp publish <FunctionAppName> |
| 220 | + ``` |
0 commit comments