diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index a9899d8..966ad3f 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -5,31 +5,30 @@ name: Node.js CI on: push: - branches: [ "main", "CI_CD" ] + branches: ['main', 'CI_CD'] pull_request: - branches: [ "main" ] + branches: ['main'] workflow_dispatch: jobs: build: - runs-on: ubuntu-latest strategy: matrix: - node-version: [ 22.x] + node-version: [22.x] mongodb-version: [8.0] steps: - - uses: actions/checkout@v4 - - name: Use Node.js ${{ matrix.node-version }} - uses: actions/setup-node@v4 - with: - node-version: ${{ matrix.node-version }} - cache: 'npm' - - name: Start MongoDB - uses: supercharge/mongodb-github-action@1.12.0 - with: - mongodb-version: ${{ matrix.mongodb-version }} - - run: | + - uses: actions/checkout@v4 + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v4 + with: + node-version: ${{ matrix.node-version }} + cache: 'npm' + - name: Start MongoDB + uses: supercharge/mongodb-github-action@1.12.0 + with: + mongodb-version: ${{ matrix.mongodb-version }} + - run: | echo "TZ=UTC" >> .env echo "PORT=8000" >> .env echo "HOST=localhost" >> .env @@ -38,6 +37,6 @@ jobs: echo "SESSION_DRIVER=cookie" >> .env echo "MONGO_URI=mongodb://localhost:27017/citium" >> .env echo "JWT_SECRET=$(openssl rand -base64 32)" >> .env - - run: npm ci - - run: node ace generate:key - - run: node ace test --force-exit + - run: npm ci + - run: node ace generate:key + - run: node ace test --force-exit diff --git a/README.md b/README.md index 23904e3..ec8faf4 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,7 @@ Innovative web platform for monitoring small construction sites in the city of Trento. This repository contains the backend services responsible for authentication, user management, construction site data, and real-time notifications. ## 🚀 Features + - 🔐 **Authentication & Authorization** – Secure stateful API authentication using MongoDB. - 🏗 **Construction Site Management** – CRUD operations for managing construction sites. - 🔔 **Real-time Notifications** – Subscription system for users to receive updates on construction sites. @@ -14,31 +15,39 @@ Innovative web platform for monitoring small construction sites in the city of T - 📦 **Scalability** – Built with AdonisJS for a modular and maintainable backend. ## 🛠️ Tech Stack + - **Framework**: AdonisJS - **Database**: MongoDB - **Authentication**: Stateful authentication with JWT - **Messaging**: WebSockets for real-time notifications ## 🚀 Getting Started + ### 1️⃣ Prerequisites + Make sure you have the following installed: + - [Node.js](https://nodejs.org/) (>= 16.x) - [MongoDB](https://www.mongodb.com/) - [NPM](https://www.npmjs.com/) ### 2️⃣ Installation + ```bash git clone https://github.com/fiatlinuxorg/citium.git cd citium ``` Install dependencies: + ```bash npm install ``` ### 3️⃣ Environment Setup + Create a `.env` file in the root directory and configure your environment variables: + ``` TZ=UTC PORT=8000 @@ -52,12 +61,15 @@ JWT_SECRET=YOUR_SECRET_KEY ``` ### 4️⃣ Run the Backend Server -development: + +development: + ```bash npm run dev ``` -production: +production: + ```bash npm run build cd build @@ -68,18 +80,22 @@ node bin/server.js The API will be available at `http://localhost:8000`. ## 📖 API Documentation + API documentation is available via Swagger: + ``` http://localhost:8000/api/docs ``` ## 🛡️ Security Considerations + - **JWT Authentication**: Protecting API endpoints - **CORS**: Configured to allow secure cross-origin requests - ## 📜 License + This project is licensed under the [MIT License](LICENSE). --- -💡 *Building a smarter city, one construction site at a time!* \ No newline at end of file + +💡 _Building a smarter city, one construction site at a time!_ diff --git a/adonisrc.ts b/adonisrc.ts index eb32924..20c3c62 100644 --- a/adonisrc.ts +++ b/adonisrc.ts @@ -30,7 +30,7 @@ export default defineConfig({ }, () => import('@adonisjs/core/providers/vinejs_provider'), () => import('@adonisjs/cors/cors_provider'), - () => import('@adonisjs/session/session_provider') + () => import('@adonisjs/session/session_provider'), ], /* diff --git a/app/controllers/construction_sites_controller.ts b/app/controllers/construction_sites_controller.ts index d8f3af6..397e899 100644 --- a/app/controllers/construction_sites_controller.ts +++ b/app/controllers/construction_sites_controller.ts @@ -39,7 +39,8 @@ export default class ConstructionSitesController { * @returns list with the construction site with the given id */ show({ params }: HttpContext) { - // S + let constructionSite = ConstructionSite.findById(params.id) + return constructionSite } /** @@ -77,9 +78,13 @@ export default class ConstructionSitesController { * @param params: id of the construction site, request: construction site data to update * @returns the updated construction site */ - update({ params, request }: HttpContext) { - let constructionSite = ConstructionSite.findByIdAndUpdate(params.id, request.all()) - return constructionSite + async update({ params, request, response }: HttpContext) { + try { + let constructionSite = await ConstructionSite.findByIdAndUpdate(params.id, request.all()) + return response.ok(constructionSite) + } catch (error) { + return response.notFound() + } } /** @@ -87,8 +92,14 @@ export default class ConstructionSitesController { * @param params: id of the construction site * @returns the deleted construction site */ - destroy({ params }: HttpContext) { - let constructionSite = ConstructionSite.findByIdAndDelete(params.id) - return constructionSite + async destroy({ params, response }: HttpContext) { + try { + await ConstructionSite.findByIdAndDelete(params.id) + // Also delete all subscriptions to this construction site + await Subscription.deleteMany({ construction_site_id: params.id }) + return response.noContent() + } catch (error) { + return response.notFound() + } } } diff --git a/config/session.ts b/config/session.ts index 30825aa..6a143aa 100644 --- a/config/session.ts +++ b/config/session.ts @@ -42,7 +42,7 @@ const sessionConfig = defineConfig({ */ stores: { cookie: stores.cookie(), - } + }, }) -export default sessionConfig \ No newline at end of file +export default sessionConfig