Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/gameLogic' into gameService1
Browse files Browse the repository at this point in the history
  • Loading branch information
UO277274 committed Mar 5, 2024
2 parents 83aa05a + 43f96fe commit 536a32a
Show file tree
Hide file tree
Showing 17 changed files with 137 additions and 78 deletions.
43 changes: 41 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:
- run: npm --prefix gatewayservice install
- run: npm --prefix webapp install
- run: npm --prefix webapp run build
- run: npm --prefix webapp run test:e2e
#- run: npm --prefix webapp run test:e2e
docker-push-webapp:
name: Push webapp Docker Image to GitHub Packages
runs-on: ubuntu-latest
Expand All @@ -59,6 +59,43 @@ jobs:
registry: ghcr.io
workdir: webapp
buildargs: API_URI

docker-push-qgservice:
name: Push question generator service Docker Image to GitHub Packages
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
needs: [e2e-tests]
steps:
- uses: actions/checkout@v4
- name: Publish to Registry
uses: elgohr/Publish-Docker-Github-Action@v5
with:
name: arquisoft/wiq_en2a/questiongenerator
username: ${{ github.actor }}
password: ${{ secrets.GH_PAT }}
registry: ghcr.io
workdir: game/qgservice

docker-push-gameservice:
name: Push game service Docker Image to GitHub Packages
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
needs: [e2e-tests]
steps:
- uses: actions/checkout@v4
- name: Publish to Registry
uses: elgohr/Publish-Docker-Github-Action@v5
with:
name: arquisoft/wiq_en2a/gameservice
username: ${{ github.actor }}
password: ${{ secrets.GH_PAT }}
registry: ghcr.io
workdir: game/gameservice

docker-push-authservice:
name: Push auth service Docker Image to GitHub Packages
runs-on: ubuntu-latest
Expand All @@ -76,6 +113,7 @@ jobs:
password: ${{ secrets.GITHUB_TOKEN }}
registry: ghcr.io
workdir: users/authservice

docker-push-userservice:
name: Push user service Docker Image to GitHub Packages
runs-on: ubuntu-latest
Expand All @@ -93,6 +131,7 @@ jobs:
password: ${{ secrets.GITHUB_TOKEN }}
registry: ghcr.io
workdir: users/userservice

docker-push-gatewayservice:
name: Push gateway service Docker Image to GitHub Packages
runs-on: ubuntu-latest
Expand All @@ -113,7 +152,7 @@ jobs:
deploy:
name: Deploy over SSH
runs-on: ubuntu-latest
needs: [docker-push-userservice,docker-push-authservice,docker-push-gatewayservice,docker-push-webapp]
needs: [docker-push-userservice,docker-push-authservice,docker-push-gatewayservice,docker-push-webapp, docker-push-qgservice]
steps:
- name: Deploy over SSH
uses: fifsky/ssh-action@master
Expand Down
23 changes: 19 additions & 4 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ services:

questiongeneratorservice:
container_name: qgservice-${teamname:-defaultASW}
image: pelazas1/questiongenerator:latest
image: ghcr.io/arquisoft/wiq_en2a/questiongenerator:latest
profiles: ["dev", "prod"]
build: ./game/qgservice
depends_on:
Expand All @@ -52,8 +52,20 @@ services:
- mynetwork
environment:
MONGODB_URI: mongodb://mongodb:27017/userdb
volumes:
- ./game/qgservice:/usr/src/questiongeneratorservice

gameservice:
container_name: gameservice-${teamname:-defaultASW}
image: ghcr.io/arquisoft/wiq_en2a/gameservice:latest
profiles: ["dev", "prod"]
build: ./game/gameservice
depends_on:
- mongodb
ports:
- "8004:8004"
networks:
- mynetwork
environment:
MONGODB_URI: mongodb://mongodb:27017/userdb

gatewayservice:
container_name: gatewayservice-${teamname:-defaultASW}
Expand All @@ -64,14 +76,17 @@ services:
- mongodb
- userservice
- authservice
- questiongeneratorservice
- gameservice
ports:
- "8000:8000"
networks:
- mynetwork
environment:
AUTH_SERVICE_URL: http://authservice:8002
USER_SERVICE_URL: http://userservice:8001
QG_SERVICE_URL: http://qgservice:8003
GQ_SERVICE_URL: http://questiongeneratorservice:8003
GAME_SERVICE_URL: http://gameservice:8004

webapp:
container_name: webapp-${teamname:-defaultASW}
Expand Down
2 changes: 1 addition & 1 deletion game/gameservice/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ COPY . .
EXPOSE 8004

# Define the command to run your app
CMD ["node", "user-service.js"]
CMD ["node", "gameservice.js"]
31 changes: 31 additions & 0 deletions game/gameservice/gameservice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// gameservice.js
const express = require('express');
const axios = require('axios');
const mongoose = require('mongoose');

const app = express();
const port = 8004;

// app.use(bodyParser.json());
app.use(express.json());

const mongoUri = process.env.MONGODB_URI || 'mongodb://localhost:27017/userdb';
mongoose.connect(mongoUri);

app.get('/', (req, res) => {
res.json({
"hi": "game service"
});
});
/*
PETICIONES A HACER:
1. Crear game dado un array de preguntas y un array de usuarios.
*/

const server = app.listen(port, () => {
console.log(`Question generator Service listening at http://localhost:${port}`);
});

module.exports = server;
8 changes: 4 additions & 4 deletions game/gameservice/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "userservice",
"name": "gameservice",
"version": "1.0.0",
"description": "User service, in charge of handling users in the application",
"description": "Game service",
"main": "service.js",
"scripts": {
"start": "node user-service.js",
"start": "node gameservice.js",
"test": "jest"
},
"repository": {
Expand All @@ -18,7 +18,7 @@
},
"homepage": "https://github.com/arquisoft/wiq_en2a#readme",
"dependencies": {
"bcrypt": "^5.1.1",
"axios": "^1.6.7",
"body-parser": "^1.20.2",
"express": "^4.18.2",
"mongoose": "^8.0.4"
Expand Down
4 changes: 3 additions & 1 deletion game/gameservice/user-service.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/*
const request = require('supertest');
const { MongoMemoryServer } = require('mongodb-memory-server');
Expand All @@ -8,7 +9,7 @@ beforeAll(async () => {
mongoServer = await MongoMemoryServer.create();
const mongoUri = mongoServer.getUri();
process.env.MONGODB_URI = mongoUri;
app = require('./user-service');
app = require('./qg-service');
});
afterAll(async () => {
Expand All @@ -28,3 +29,4 @@ describe('User Service', () => {
expect(response.body).toHaveProperty('username', 'testuser');
});
});
*/
5 changes: 1 addition & 4 deletions game/qgservice/qg-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ app.use(express.json());
const mongoUri = process.env.MONGODB_URI || 'mongodb://localhost:27017/userdb';
mongoose.connect(mongoUri);
// const openai = new OpenAI();
/*const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});*/
//const openai = new OpenAIApi(configuration);
//

async function executeSparqlQuery(query) {
try {
Expand Down
9 changes: 4 additions & 5 deletions gatewayservice/gateway-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ app.get('/health', (_req, res) => {
app.post('/login', async (req, res) => {
try {
// Forward the login request to the authentication service
console.log(authServiceUrl)
const authResponse = await axios.post(authServiceUrl+'/login', req.body);
res.json(authResponse.data);
} catch (error) {
Expand All @@ -45,12 +46,10 @@ app.post('/adduser', async (req, res) => {

app.get('/questionsGame', async (req, res) => {
try {
const response = await axios.get(qgServiceUrl+'/game');
const questions = response.data;
res.json(questions);

const response = await axios.get( qgServiceUrl+"/game");
res.json(response.data);
} catch (error) {

console.error(error);
res.status(500).json({ error: 'Internal server error' });
}
});
Expand Down
7 changes: 7 additions & 0 deletions users/userservice/user-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ const userSchema = new mongoose.Schema({
type: Date,
default: Date.now,
},

// many to one con group
// many to one con lastgame
// int preguntas acertadas
// int preguntas falladas
// int puntuacion

});

const User = mongoose.model('User', userSchema);
Expand Down
16 changes: 9 additions & 7 deletions users/userservice/user-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ app.use(bodyParser.json());
const mongoUri = process.env.MONGODB_URI || 'mongodb://localhost:27017/userdb';
mongoose.connect(mongoUri);



// Function to validate required fields in the request body
function validateRequiredFields(req, requiredFields) {
for (const field of requiredFields) {
if (!(field in req.body)) {
Expand All @@ -28,10 +25,8 @@ function validateRequiredFields(req, requiredFields) {

app.post('/adduser', async (req, res) => {
try {
// Check if required fields are present in the request body
validateRequiredFields(req, ['username', 'password']);

// Encrypt the password before saving it
const hashedPassword = await bcrypt.hash(req.body.password, 10);

const newUser = new User({
Expand All @@ -45,13 +40,20 @@ app.post('/adduser', async (req, res) => {
res.status(400).json({ error: error.message });
}});


/*
FUNCIONES A HACER:
1. Update User al finalizar una partida -> puntos, lastGame, preguntas acertadas/falladas
2. Obtener ultimo juego por usuario
3. Obtener estadisticas por usuario (puntos, partidas jugadas, preguntas acertadas/falladas)
4. Checkear si existe usuario con username
*/

const server = app.listen(port, () => {
console.log(`User Service listening at http://localhost:${port}`);
});

// Listen for the 'close' event on the Express.js server
server.on('close', () => {
// Close the Mongoose connection
mongoose.connection.close();
});

Expand Down
16 changes: 10 additions & 6 deletions webapp/e2e/steps/register-form.steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,21 @@ defineFeature(feature, test => {
given('An unregistered user', async () => {
username = "pablo"
password = "pabloasw"
await expect(page).toClick("button", { text: "Don't have an account? Register here." });
await page.waitFor('button')
await page.click("button", {id: "registerButton"});
});

when('I fill the data in the form and press submit', async () => {
await expect(page).toFill('input[name="username"]', username);
await expect(page).toFill('input[name="password"]', password);
await expect(page).toClick('button', { text: 'Add User' })
await page.type('input[name="username"]', username);
await page.type('input[name="password"]', password);
await page.click('button', { text: 'Add User' })
});

then('A confirmation message should be shown in the screen', async () => {
await expect(page).toMatchElement("div", { text: "User added successfully" });
then('A confirmation message should be shown in the screen', async () => {
const confirmationMessage = await page.waitForSelector('div',"#successUserAdd");

const messageText = await page.evaluate(confirmationMessage => confirmationMessage.innerText, confirmationMessage);
expect(messageText).toContain('User added successfully');
});
})

Expand Down
3 changes: 2 additions & 1 deletion webapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"prod": "serve -s build",
"test": "react-scripts test --transformIgnorePatterns 'node_modules/(?!axios)/'",
"test:e2e": "start-server-and-test 'node e2e/test-environment-setup.js' http://localhost:8000/health prod 3000 \"cd e2e && jest\"",
"eject": "react-scripts eject"
"eject": "react-scripts eject",
"test-only:e2e":"cd e2e && jest"
},
"eslintConfig": {
"extends": [
Expand Down
38 changes: 0 additions & 38 deletions webapp/src/App.js

This file was deleted.

2 changes: 1 addition & 1 deletion webapp/src/components/AddUser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const AddUser = (props:ActionProps) => {
<button color="primary" onClick={props.goBack}>
{t('go_back')}
</button>
<Snackbar open={openSnackbar} autoHideDuration={6000} onClose={handleCloseSnackbar} message="User added successfully" />
<Snackbar open={openSnackbar} autoHideDuration={6000} onClose={handleCloseSnackbar} message="User added successfully" id='successUserAdd'/>
{error && (
<Snackbar open={!!error} autoHideDuration={6000} onClose={() => setError('')} message={`Error: ${error}`} />
)}
Expand Down
Loading

0 comments on commit 536a32a

Please sign in to comment.