forked from Arquisoft/wiq_0
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a6f8bb8
commit 1cd7294
Showing
12 changed files
with
5,726 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Use an official Node.js runtime as a parent image | ||
FROM node:20 | ||
|
||
# Set the working directory in the container | ||
WORKDIR /usr/src/answerservice | ||
|
||
# Copy package.json and package-lock.json to the working directory | ||
COPY package*.json ./ | ||
|
||
# Install app dependencies | ||
RUN npm install | ||
|
||
# Copy the app source code to the working directory | ||
COPY . . | ||
|
||
# Expose the port the app runs on | ||
EXPOSE 8004 | ||
|
||
# Define the command to run your app | ||
CMD ["node", "answer-service.js"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const mongoose = require('mongoose'); | ||
|
||
const answerSchema = new mongoose.Schema({ | ||
type: { | ||
type: String, | ||
required: true, | ||
}, | ||
value: { | ||
type: String, | ||
required: true, | ||
}, | ||
}); | ||
|
||
const Answer = mongoose.model('Answer', answerSchema); | ||
|
||
module.exports = Answer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// answer-service.js | ||
const express = require('express'); | ||
const mongoose = require('mongoose'); | ||
const bodyParser = require('body-parser'); | ||
const Answer = require('./answer-model') | ||
|
||
const app = express(); | ||
const port = 8004; | ||
|
||
// Middleware to parse JSON in request body | ||
app.use(bodyParser.json()); | ||
|
||
// Connect to MongoDB | ||
const mongoUri = process.env.MONGODB_URI || 'mongodb://localhost:27017/answerdb'; | ||
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)) { | ||
throw new Error(`Missing required field: ${field}`); | ||
} | ||
} | ||
} | ||
|
||
app.post('/addanswer', async (req, res) => { | ||
try { | ||
// Check if required fields are present in the request body | ||
// validateRequiredFields(req, ['answername', 'password']); | ||
|
||
// Encrypt the password before saving it | ||
// const hashedPassword = await bcrypt.hash(req.body.password, 10); | ||
const newAnswer = new Answer({ | ||
type: req.body.type, | ||
value: req.body.value, | ||
}); | ||
|
||
await newAnswer.save(); | ||
res.json(newAnswer); | ||
} catch (error) { | ||
res.status(400).json({ error: error.message }); | ||
} | ||
}); | ||
|
||
app.post('/getanswer', async (req, res) => { | ||
try { | ||
// Check if required fields are present in the request body | ||
validateRequiredFields(req, ['type', 'value']); | ||
|
||
const { type, value } = req.body; | ||
|
||
// Find the answer by type in the database | ||
const answer = await Answer.findOne({ value }); | ||
|
||
// Check if the answer exists | ||
if (answer) { | ||
// Respond with the answer information | ||
res.json({ type: answer.type, value: answer.value }); | ||
} else { | ||
res.status(401).json({ error: 'Answer not found' }); | ||
} | ||
} catch (error) { | ||
res.status(500).json({ error: 'Internal Server Error' }); | ||
} | ||
}); | ||
|
||
const server = app.listen(port, () => { | ||
console.log(`Answer 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(); | ||
}); | ||
|
||
module.exports = server |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const request = require('supertest'); | ||
const { MongoMemoryServer } = require('mongodb-memory-server'); | ||
|
||
let mongoServer; | ||
let app; | ||
|
||
beforeAll(async () => { | ||
mongoServer = await MongoMemoryServer.create(); | ||
const mongoUri = mongoServer.getUri(); | ||
process.env.MONGODB_URI = mongoUri; | ||
app = require('./user-service'); | ||
}); | ||
|
||
afterAll(async () => { | ||
app.close(); | ||
await mongoServer.stop(); | ||
}); | ||
|
||
describe('User Service', () => { | ||
it('should add a new user on POST /adduser', async () => { | ||
const newUser = { | ||
username: 'testuser', | ||
password: 'testpassword', | ||
}; | ||
|
||
const response = await request(app).post('/adduser').send(newUser); | ||
expect(response.status).toBe(200); | ||
expect(response.body).toHaveProperty('username', 'testuser'); | ||
}); | ||
}); |
Oops, something went wrong.