Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge US08 -> Devel #56

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
.pnp.js
/back-motor-de-busca/data.ms/indexes
/back-usuarios/node_modules
.env
./frontend/env

# testing
/coverage
Expand Down
6 changes: 6 additions & 0 deletions back-usuarios/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/.git
/node_modules
.dockerignore
.env
Dockerfile
fly.toml
39 changes: 39 additions & 0 deletions back-usuarios/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# syntax = docker/dockerfile:1

# Adjust NODE_VERSION as desired
ARG NODE_VERSION=16.17.1
FROM node:${NODE_VERSION}-slim as base

LABEL fly_launch_runtime="Node.js"

# Node.js app lives here
WORKDIR /app

# Set production environment
ENV NODE_ENV="production"


# Throw-away build stage to reduce size of final image
FROM base as build

# Install packages needed to build node modules
RUN apt-get update -qq && \
apt-get install -y build-essential pkg-config python

# Install node modules
COPY --link package-lock.json package.json ./
RUN npm ci

# Copy application code
COPY --link . .


# Final stage for app image
FROM base

# Copy built application
COPY --from=build /app /app

# Start the server by default, this can be overwritten at runtime
EXPOSE 3000
CMD [ "npm", "run", "start" ]
22 changes: 22 additions & 0 deletions back-usuarios/fly.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# fly.toml app configuration file generated for back-usuarios on 2023-12-05T22:00:56-03:00
#
# See https://fly.io/docs/reference/configuration/ for information about how to use this file.
#

app = "back-usuarios"
primary_region = "gru"

[build]

[http_service]
internal_port = 8800
force_https = true
auto_stop_machines = true
auto_start_machines = true
min_machines_running = 0
processes = ["app"]

[[vm]]
cpu_kind = "shared"
cpus = 1
memory_mb = 1024
91 changes: 71 additions & 20 deletions back-usuarios/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,10 @@ app.use(bodyParser.json());
app.post('/create_favorite', async (req, res) => {
try {
const { favoritesId, userToken, documentId } = req.body;
console.log("req.body criar favorito", req.body);
// Validate input
if (!favoritesId || !userToken || !documentId) {
return res.status(300).json({ success: false, message: 'dados insuficientes' });
} else {
console.log("dados de favoritos salvos", req.body);
console.log("dados de favoritos salvos!!!", req.body);
}

await createFavorite(pool, favoritesId, userToken, documentId);
Expand All @@ -54,22 +52,7 @@ app.post('/create_favorite', async (req, res) => {
}

});

// // app.use("/",userRoutes);
// async function createFavorite(pool, favoritesId, userToken, documentId) {
// const result = await pool.execute(
// 'INSERT INTO favorites (favoritesId, userToken, documentId) VALUES (?, ?, ?)',
// [favoritesId, userToken, documentId]
// );

// if (result[0].affectedRows > 0) {
// console.log('Favorite added successfully!');
// } else {
// console.log('Failed to add favorite.');
// throw new Error('Failed to add favorite.');
// }
// }
async function createFavorite(pool, favoritesId, userToken, documentId) {
async function createFavorite(pool, favoritesId, userToken, documentId) {
const query = util.promisify(pool.query).bind(pool);

try {
Expand All @@ -81,14 +64,82 @@ async function createFavorite(pool, favoritesId, userToken, documentId) {
if (result.affectedRows > 0) {
console.log('Favorite added successfully!');
} else {
console.log('Failed to add favorite.');
console.log('Failed to add favorite.\n');
throw new Error('Failed to add favorite.');
}
} catch (error) {
console.error('Error adding favorite:', error.message);
throw error;
}
}
app.post('/list_user_favorites', async (req, res) => {
try {
const {userEmail} = req.body;

// Validate input
if (!userEmail) {
return res.status(400).json({ success: false, message: 'Invalid request body.' });
} else{
console.log("email do usuario: ", userEmail.userEmail);
}

const documents = await listUserFavorites(pool, userEmail.userEmail, res);

res.json({ success: true, documents });
} catch (error) {
console.error('Error:', error);
// res.status(500).json({ success: false, message: 'Internal server error.' });
}
});

async function listUserFavorites(pool, userEmail, res) {

console.log("\ncomeco da funcao assincrona", userEmail);
const query = util.promisify(pool.query).bind(pool);
try {
const result = await query(
'SELECT USER.email, DOCUMENT.docName, DOCUMENT.link, DOCUMENT.content, DOCUMENT.docKey, DOCUMENT.creationDate FROM favorites INNER JOIN USER ON favorites.userToken = USER.token INNER JOIN DOCUMENT ON favorites.documentId = DOCUMENT.docKey WHERE USER.email = (?) ORDER BY USER.email',
[userEmail]
);
if (result.affectedRows > 0) {
console.log('Favorite selection was successfull!');
} else {
// console.log('Failed to select favorite.');
console.log(result.affectedRows);
console.log("->>>", userEmail);
// throw new Error('Failed to selecttt favorite.');
}


const favorites = await pool.query(query, userEmail);

res.json({ success: true, documents: result });

return result;
} catch (error) {
console.log("é aqui o problema?")
console.error('Error executing query:', error);
// res.status(500).json({ success: false, error: 'Internal Server Error' });

}
}


// async function listUserFavorites(pool, userEmail) {
// const [favorites] = await pool.execute(
// `
// SELECT USER.email, DOCUMENT.docName, DOCUMENT.link, DOCUMENT.content, DOCUMENT.docKey, DOCUMENT.creationDate
// FROM favorites
// INNER JOIN USER ON favorites.userToken = USER.token
// INNER JOIN DOCUMENT ON favorites.documentId = DOCUMENT.docKey
// WHERE USER.email = ?
// ORDER BY USER.email
// `,
// [userEmail]
// );

// return favorites;
// }

app.listen(PORT, () =>{
console.log(`Server running on port ${PORT}\n`)
Expand Down
Loading