From 6b89fa6efe605bf2cd308b79bbc4b670e61c2443 Mon Sep 17 00:00:00 2001 From: Jose Alberto Delgado Robles Date: Thu, 15 Feb 2024 16:39:45 +0100 Subject: [PATCH 01/11] Update docker-image.yml --- .github/workflows/docker-image.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml index 068d4e8..2d94372 100644 --- a/.github/workflows/docker-image.yml +++ b/.github/workflows/docker-image.yml @@ -77,15 +77,15 @@ jobs: - name: Move to project folder run: | - ssh -o StrictHostKeyChecking=no -i key.pem ubuntu@3.82.157.13 'cd backend/ && sudo docker compose down' + ssh -o StrictHostKeyChecking=no -i key.pem ubuntu@3.228.164.55 'cd backend/ && sudo docker compose down' - name: Remove old image run: | - ssh -o StrictHostKeyChecking=no -i key.pem ubuntu@3.82.157.13 'sudo docker image rm -f backend-aup-backend:latest' + ssh -o StrictHostKeyChecking=no -i key.pem ubuntu@3.228.164.55 'sudo docker image rm -f backend-aup-backend:latest' - name: Run new containers run: | - ssh -o StrictHostKeyChecking=no -i key.pem ubuntu@3.82.157.13 'cd backend/ && git pull && sudo docker compose up -d' + ssh -o StrictHostKeyChecking=no -i key.pem ubuntu@3.228.164.55 'cd backend/ && git pull && sudo docker compose up -d' From 6470c09a74185d5479259155e4c70a7c71336bf2 Mon Sep 17 00:00:00 2001 From: Jose Alberto Delgado Robles Date: Thu, 15 Feb 2024 16:53:36 +0100 Subject: [PATCH 02/11] Update docker-image.yml --- .github/workflows/docker-image.yml | 100 ++++++++++++++--------------- 1 file changed, 48 insertions(+), 52 deletions(-) diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml index 2d94372..649f3c5 100644 --- a/.github/workflows/docker-image.yml +++ b/.github/workflows/docker-image.yml @@ -9,28 +9,8 @@ on: env: DOCKER_USER: ${{ secrets.DOCKER_USER }} DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }} - + jobs: - - # test: - # name: Jest Tests - # runs-on: ubuntu-latest - - # steps: - # - name: Checkout code - # uses: actions/checkout@v3 - - # - name: Set up NodeJS - # uses: actions/setup-node@v1 - # with: - # node-version: 20 - - # - name: Install all dependencies - # run: npm install - - # - name: Run Tests - # run: npm test - build: runs-on: ubuntu-latest @@ -40,52 +20,68 @@ jobs: with: fetch-depth: 0 + - name: Git Semantic Version + uses: PaulHatch/semantic-version@v5.4.0 + with: + major_pattern: "major:" + minor_pattern: "feat:" + version_format: "${major}.${minor}.${patch}-prerelease${increment}" + id: version + - name: Set up Docker Buildx uses: docker/setup-buildx-action@v1 - name: Docker auth + env: + NEW_VERSION: ${{ steps.version.outputs.version }} uses: docker/login-action@v1 with: username: ${{ env.DOCKER_USER }} password: ${{ env.DOCKER_PASSWORD }} - - name: Build and push Docker Image - uses: docker/build-push-action@v2 - with: - context: ./ - push: true - dockerfile: ./Dockerfile - tags: jaderodev/aup-backend:latest + - name: Print Version + run: | + echo "New version: $NEW_VERSION - deploy: - needs: build - runs-on: ubuntu-latest - steps: - - name: Checkout code - uses: actions/checkout@v3 - - name: Docker auth - uses: docker/login-action@v1 - with: - username: ${{ env.DOCKER_USER }} - password: ${{ env.DOCKER_PASSWORD }} - - name: Set permissions for private key - run: | - echo "${{ secrets.AWS_PRIVATE_KEY }}" > key.pem - chmod 600 key.pem + # - name: Build and push Docker Image + # uses: docker/build-push-action@v2 + # with: + # context: ./ + # push: true + # dockerfile: ./Dockerfile + # tags: jaderodev/aup-backend:latest - - name: Move to project folder - run: | - ssh -o StrictHostKeyChecking=no -i key.pem ubuntu@3.228.164.55 'cd backend/ && sudo docker compose down' + # deploy: + # needs: build + # runs-on: ubuntu-latest + # steps: + # - name: Checkout code + # uses: actions/checkout@v3 - - name: Remove old image - run: | - ssh -o StrictHostKeyChecking=no -i key.pem ubuntu@3.228.164.55 'sudo docker image rm -f backend-aup-backend:latest' + # - name: Docker auth + # uses: docker/login-action@v1 + # with: + # username: ${{ env.DOCKER_USER }} + # password: ${{ env.DOCKER_PASSWORD }} - - name: Run new containers - run: | - ssh -o StrictHostKeyChecking=no -i key.pem ubuntu@3.228.164.55 'cd backend/ && git pull && sudo docker compose up -d' + # - name: Set permissions for private key + # run: | + # echo "${{ secrets.AWS_PRIVATE_KEY }}" > key.pem + # chmod 600 key.pem + + # - name: Move to project folder + # run: | + # ssh -o StrictHostKeyChecking=no -i key.pem ubuntu@3.228.164.55 'cd backend/ && sudo docker compose down' + + # - name: Remove old image + # run: | + # ssh -o StrictHostKeyChecking=no -i key.pem ubuntu@3.228.164.55 'sudo docker image rm -f backend-aup-backend:latest' + + # - name: Run new containers + # run: | + # ssh -o StrictHostKeyChecking=no -i key.pem ubuntu@3.228.164.55 'cd backend/ && git pull && sudo docker compose up -d' From 6ee3651a94c8d332c497d7f35d81e67e3ccd116f Mon Sep 17 00:00:00 2001 From: Jose Alberto Delgado Robles Date: Thu, 15 Feb 2024 16:54:35 +0100 Subject: [PATCH 03/11] Automatic versioning test --- .github/workflows/docker-image.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml index 649f3c5..5fcb262 100644 --- a/.github/workflows/docker-image.yml +++ b/.github/workflows/docker-image.yml @@ -41,7 +41,7 @@ jobs: - name: Print Version run: | - echo "New version: $NEW_VERSION + echo "New version: $NEW_VERSION !!!!!!!!!!!!!!!!" From 7d0cae572434f4d14dc3122470ec32327a12e115 Mon Sep 17 00:00:00 2001 From: Jose Alberto Delgado Robles Date: Thu, 15 Feb 2024 16:56:45 +0100 Subject: [PATCH 04/11] Semantic version tests --- .github/workflows/docker-image.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml index 5fcb262..3c73605 100644 --- a/.github/workflows/docker-image.yml +++ b/.github/workflows/docker-image.yml @@ -25,7 +25,7 @@ jobs: with: major_pattern: "major:" minor_pattern: "feat:" - version_format: "${major}.${minor}.${patch}-prerelease${increment}" + format: "${major}.${minor}.${patch}-prerelease${increment}" id: version - name: Set up Docker Buildx From d88b03e1ead993f866601e8c06777e62964d9c59 Mon Sep 17 00:00:00 2001 From: Jose Alberto Delgado Robles Date: Thu, 15 Feb 2024 16:57:32 +0100 Subject: [PATCH 05/11] Semantic version tests --- .github/workflows/docker-image.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml index 3c73605..b2025ba 100644 --- a/.github/workflows/docker-image.yml +++ b/.github/workflows/docker-image.yml @@ -32,14 +32,14 @@ jobs: uses: docker/setup-buildx-action@v1 - name: Docker auth - env: - NEW_VERSION: ${{ steps.version.outputs.version }} uses: docker/login-action@v1 with: username: ${{ env.DOCKER_USER }} password: ${{ env.DOCKER_PASSWORD }} - name: Print Version + env: + NEW_VERSION: ${{ steps.version.outputs.version }} run: | echo "New version: $NEW_VERSION !!!!!!!!!!!!!!!!" From c7355b0807e503bfee378c8b761c00f1dbf026cb Mon Sep 17 00:00:00 2001 From: Jose Alberto Delgado Robles Date: Thu, 15 Feb 2024 16:59:14 +0100 Subject: [PATCH 06/11] Semantic version tests --- .github/workflows/docker-image.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml index b2025ba..dd0cf9f 100644 --- a/.github/workflows/docker-image.yml +++ b/.github/workflows/docker-image.yml @@ -25,7 +25,7 @@ jobs: with: major_pattern: "major:" minor_pattern: "feat:" - format: "${major}.${minor}.${patch}-prerelease${increment}" + version_format: "${major}.${minor}.${patch}-prerelease${increment}" id: version - name: Set up Docker Buildx From 94794f281d41e2c8a6cfdc50767d8d474b336546 Mon Sep 17 00:00:00 2001 From: Jose Alberto Delgado Robles Date: Thu, 15 Feb 2024 17:04:32 +0100 Subject: [PATCH 07/11] Semantic Versioning test with docker image --- .github/workflows/docker-image.yml | 76 +++++++++++++++--------------- 1 file changed, 39 insertions(+), 37 deletions(-) diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml index dd0cf9f..635e18f 100644 --- a/.github/workflows/docker-image.yml +++ b/.github/workflows/docker-image.yml @@ -37,51 +37,53 @@ jobs: username: ${{ env.DOCKER_USER }} password: ${{ env.DOCKER_PASSWORD }} - - name: Print Version + - name: Build and push Docker Image env: NEW_VERSION: ${{ steps.version.outputs.version }} - run: | - echo "New version: $NEW_VERSION !!!!!!!!!!!!!!!!" - - - - # - name: Build and push Docker Image - # uses: docker/build-push-action@v2 - # with: - # context: ./ - # push: true - # dockerfile: ./Dockerfile - # tags: jaderodev/aup-backend:latest + uses: docker/build-push-action@v2 + with: + context: ./ + push: true + dockerfile: ./Dockerfile + tags: jaderodev/aup-backend:$NEW_VERSION + + - name: Build and push Docker Image + uses: docker/build-push-action@v2 + with: + context: ./ + push: true + dockerfile: ./Dockerfile + tags: jaderodev/aup-backend:latest - # deploy: - # needs: build - # runs-on: ubuntu-latest - # steps: - # - name: Checkout code - # uses: actions/checkout@v3 + deploy: + needs: build + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v3 - # - name: Docker auth - # uses: docker/login-action@v1 - # with: - # username: ${{ env.DOCKER_USER }} - # password: ${{ env.DOCKER_PASSWORD }} + - name: Docker auth + uses: docker/login-action@v1 + with: + username: ${{ env.DOCKER_USER }} + password: ${{ env.DOCKER_PASSWORD }} - # - name: Set permissions for private key - # run: | - # echo "${{ secrets.AWS_PRIVATE_KEY }}" > key.pem - # chmod 600 key.pem + - name: Set permissions for private key + run: | + echo "${{ secrets.AWS_PRIVATE_KEY }}" > key.pem + chmod 600 key.pem - # - name: Move to project folder - # run: | - # ssh -o StrictHostKeyChecking=no -i key.pem ubuntu@3.228.164.55 'cd backend/ && sudo docker compose down' + - name: Move to project folder + run: | + ssh -o StrictHostKeyChecking=no -i key.pem ubuntu@3.228.164.55 'cd backend/ && sudo docker compose down' - # - name: Remove old image - # run: | - # ssh -o StrictHostKeyChecking=no -i key.pem ubuntu@3.228.164.55 'sudo docker image rm -f backend-aup-backend:latest' + - name: Remove old image + run: | + ssh -o StrictHostKeyChecking=no -i key.pem ubuntu@3.228.164.55 'sudo docker image rm -f backend-aup-backend:latest' - # - name: Run new containers - # run: | - # ssh -o StrictHostKeyChecking=no -i key.pem ubuntu@3.228.164.55 'cd backend/ && git pull && sudo docker compose up -d' + - name: Run new containers + run: | + ssh -o StrictHostKeyChecking=no -i key.pem ubuntu@3.228.164.55 'cd backend/ && git pull && sudo docker compose up -d' From 1123c8450169e869635538f2d0e4e22f9c235421 Mon Sep 17 00:00:00 2001 From: Jose Alberto Delgado Robles Date: Thu, 15 Feb 2024 17:06:47 +0100 Subject: [PATCH 08/11] Semantic versioning docker test --- .github/workflows/docker-image.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml index 635e18f..3197fdd 100644 --- a/.github/workflows/docker-image.yml +++ b/.github/workflows/docker-image.yml @@ -38,9 +38,9 @@ jobs: password: ${{ env.DOCKER_PASSWORD }} - name: Build and push Docker Image + uses: docker/build-push-action@v2 env: NEW_VERSION: ${{ steps.version.outputs.version }} - uses: docker/build-push-action@v2 with: context: ./ push: true @@ -49,6 +49,8 @@ jobs: - name: Build and push Docker Image uses: docker/build-push-action@v2 + env: + NEW_VERSION: ${{ steps.version.outputs.version }} with: context: ./ push: true From f7469b9e91dfb96d70425300a9bce1f0ea3cc32e Mon Sep 17 00:00:00 2001 From: Jose Alberto Delgado Robles Date: Thu, 15 Feb 2024 17:10:08 +0100 Subject: [PATCH 09/11] Semantic versioning docker test --- .github/workflows/docker-image.yml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml index 3197fdd..c5cee7b 100644 --- a/.github/workflows/docker-image.yml +++ b/.github/workflows/docker-image.yml @@ -45,12 +45,10 @@ jobs: context: ./ push: true dockerfile: ./Dockerfile - tags: jaderodev/aup-backend:$NEW_VERSION + tags: jaderodev/aup-backend:${{ env.NEW_VERSION }} - name: Build and push Docker Image uses: docker/build-push-action@v2 - env: - NEW_VERSION: ${{ steps.version.outputs.version }} with: context: ./ push: true From 80dab8ef8988ccd6a5b26fda6227fb3d5ecaee20 Mon Sep 17 00:00:00 2001 From: Jose Alberto Delgado Date: Thu, 15 Feb 2024 22:43:35 +0100 Subject: [PATCH 10/11] major: major version release --- docker-compose.yml | 84 +++++++++---------- .../migration.sql | 2 + prisma/schema.prisma | 1 + src/presentation/animals/controller.ts | 8 ++ src/presentation/animals/routes.ts | 6 ++ src/presentation/services/animal.service.ts | 21 ++++- 6 files changed, 77 insertions(+), 45 deletions(-) create mode 100644 prisma/migrations/20240215174349_numfavs_count_in_animal/migration.sql diff --git a/docker-compose.yml b/docker-compose.yml index 5975be2..bd45323 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -47,46 +47,46 @@ services: MAILER_SECRET_KEY: ${MAILER_SECRET_KEY} WEB_SERVICE_URL: ${WEB_SERVICE_URL} - # aup-backend: - # build: . - # restart: on-failure - # depends_on: - # rabbitmq: - # condition: service_healthy - # container_name: aup-backend - # ports: - # - ${PORT}:${PORT} - # environment: - # NODE_ENV: ${NODE_ENV} - # PORT: ${PORT} - # DB_PASSWORD: ${DB_PASSWORD} - # DB_NAME: ${DB_NAME} - # DB_HOST: ${DB_HOST} - # DB_PORT: ${DB_PORT} - # DB_USERNAME: ${DB_USERNAME} - # JWT_SEED: ${JWT_SEED} - # DATABASE_URL: ${DATABASE_URL} - # MAIL_SERVICE: ${MAIL_SERVICE} - # MAILER_EMAIL: ${MAILER_EMAIL} - # MAILER_SECRET_KEY: ${MAILER_SECRET_KEY} - # WEBSERVICE_URL: ${WEBSERVICE_URL} - # RABBITMQ_USER: ${RABBITMQ_USER} - # RABBITMQ_PASS: ${RABBITMQ_PASS} - # RABBITMQ_URL: ${RABBITMQ_URL} + aup-backend: + build: . + restart: on-failure + depends_on: + rabbitmq: + condition: service_healthy + container_name: aup-backend + ports: + - ${PORT}:${PORT} + environment: + NODE_ENV: ${NODE_ENV} + PORT: ${PORT} + DB_PASSWORD: ${DB_PASSWORD} + DB_NAME: ${DB_NAME} + DB_HOST: ${DB_HOST} + DB_PORT: ${DB_PORT} + DB_USERNAME: ${DB_USERNAME} + JWT_SEED: ${JWT_SEED} + DATABASE_URL: ${DATABASE_URL} + MAIL_SERVICE: ${MAIL_SERVICE} + MAILER_EMAIL: ${MAILER_EMAIL} + MAILER_SECRET_KEY: ${MAILER_SECRET_KEY} + WEBSERVICE_URL: ${WEBSERVICE_URL} + RABBITMQ_USER: ${RABBITMQ_USER} + RABBITMQ_PASS: ${RABBITMQ_PASS} + RABBITMQ_URL: ${RABBITMQ_URL} - # proxy: - # build: ./nginx - # depends_on: - # - aup-backend - # container_name: aup-backend-proxy - # restart: always - # ports: - # - 80:80 - # - 443:443 - # volumes: - # - ./nginx/nginx.conf:/etc/nginx/conf.d/nginx.conf:ro - # - ./nginx/passwd/:/etc/nginx/passwd:ro - # - ./nginx/letsencrypt/:/etc/letsencrypt/:ro - # - ./nginx/certificates/certs/:/etc/nginx/certs:ro - # - ./nginx/certificates/private/:/etc/nginx/private:ro - # - ./nginx/logs/:/var/log/nginx/ + proxy: + build: ./nginx + depends_on: + - aup-backend + container_name: aup-backend-proxy + restart: always + ports: + - 80:80 + - 443:443 + volumes: + - ./nginx/nginx.conf:/etc/nginx/conf.d/nginx.conf:ro + - ./nginx/passwd/:/etc/nginx/passwd:ro + - ./nginx/letsencrypt/:/etc/letsencrypt/:ro + - ./nginx/certificates/certs/:/etc/nginx/certs:ro + - ./nginx/certificates/private/:/etc/nginx/private:ro + - ./nginx/logs/:/var/log/nginx/ diff --git a/prisma/migrations/20240215174349_numfavs_count_in_animal/migration.sql b/prisma/migrations/20240215174349_numfavs_count_in_animal/migration.sql new file mode 100644 index 0000000..05a49d5 --- /dev/null +++ b/prisma/migrations/20240215174349_numfavs_count_in_animal/migration.sql @@ -0,0 +1,2 @@ +-- AlterTable +ALTER TABLE "Animal" ADD COLUMN "numFavs" INTEGER NOT NULL DEFAULT 0; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 0a33532..8878a6d 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -104,6 +104,7 @@ model Animal { cityId Int images String[] easyTrain Boolean + numFavs Int @default(0) energyLevel energy moltingAmount molting status status_pet @default(awaiting_home) diff --git a/src/presentation/animals/controller.ts b/src/presentation/animals/controller.ts index 4e0ce52..b1cfd14 100644 --- a/src/presentation/animals/controller.ts +++ b/src/presentation/animals/controller.ts @@ -102,4 +102,12 @@ export class AnimalController { res.status(HttpCodes.OK).json({ message: 'Images updated successfully' }); }; + + addToFavorites = async (req: Request, res: Response) => { + const { id } = req.params; + + const response = await this.animalService.addToFavorites(id, req.user.id!); + + res.status(HttpCodes.OK).json(response); + }; } diff --git a/src/presentation/animals/routes.ts b/src/presentation/animals/routes.ts index 1e73108..fe7dec0 100644 --- a/src/presentation/animals/routes.ts +++ b/src/presentation/animals/routes.ts @@ -78,6 +78,12 @@ export class AnimalRoutes { animalController.delete ); + router.post( + '/:id/add-favorite', + authMiddleware.authenticateUser, + animalController.addToFavorites + ); + return router; } } diff --git a/src/presentation/services/animal.service.ts b/src/presentation/services/animal.service.ts index e0cbe60..b354548 100644 --- a/src/presentation/services/animal.service.ts +++ b/src/presentation/services/animal.service.ts @@ -234,14 +234,12 @@ export class AnimalService { const updateQuery = this.buildQuery(updateAnimalDto); - console.log({ updateQuery }); - const updatedAnimal = await prisma.animal.update({ where: { id: animal.id }, data: updateQuery, }); - return 'Update Animal'; + return updatedAnimal; } public async delete(user: PayloadUser, term: string) { @@ -300,4 +298,21 @@ export class AnimalService { data: { images: resultImages }, }); } + + public async addToFavorites(animalId: string, userId: string) { + const animal = await prisma.animal.findUnique({ where: { id: animalId } }); + + if (!animal) throw new NotFoundError('Animal not found'); + + const newFav = await prisma.user.update({ + where: { id: userId }, + data: { + userFav: { + connect: { id: animalId }, + }, + }, + }); + + return newFav; + } } From 626ce6b1b554177bdacc74ab5029dc99f85abb73 Mon Sep 17 00:00:00 2001 From: Jose Alberto Delgado Date: Thu, 15 Feb 2024 22:57:22 +0100 Subject: [PATCH 11/11] fix: environment variables in docker compose --- docker-compose.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docker-compose.yml b/docker-compose.yml index bd45323..fcd1f01 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -73,6 +73,11 @@ services: RABBITMQ_USER: ${RABBITMQ_USER} RABBITMQ_PASS: ${RABBITMQ_PASS} RABBITMQ_URL: ${RABBITMQ_URL} + AWS_ACCESS_KEY_ID: ${AWS_ACCESS_KEY_ID} + AWS_SECRET_ACCESS_KEY: ${AWS_SECRET_ACCESS_KEY} + AWS_REGION: ${AWS_REGION} + AWS_BUCKET: ${AWS_BUCKET} + AWS_BUCKET_URL: ${AWS_BUCKET_URL} proxy: build: ./nginx