Skip to content

Commit

Permalink
final d01
Browse files Browse the repository at this point in the history
  • Loading branch information
Víctor Escalera García committed Mar 17, 2024
1 parent fca5d79 commit 323dab3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 32 deletions.
44 changes: 22 additions & 22 deletions Victor/api-VEG.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ module.exports = (app, db) => {
}
});

if ('id' in queryParams) {
if ('ranking' in queryParams) {
return res.sendStatus(400, "Bad Request");
}

db.find(query).sort({ id: 1 }).skip(offset).limit(limit).exec((err, data_VEG) => {
db.find(query).sort({ ranking: 1 }).skip(offset).limit(limit).exec((err, data_VEG) => {
if (err) {
res.sendStatus(500, "Internal Error");
} else {
Expand All @@ -74,12 +74,12 @@ module.exports = (app, db) => {
});
});

// GET para obtener un recurso por su ID
app.get(API_BASE + "/youtube-trends/:id", (req, res) => {
const resourceId = parseInt(req.params.id); // Obtener el ID del recurso de los parámetros de la ruta
// GET para obtener un recurso por su ranking
app.get(API_BASE + "/youtube-trends/:ranking", (req, res) => {
const resourceRanking = parseInt(req.params.ranking); // Obtener el Ranking del recurso de los parámetros de la ruta

// Buscar el recurso por su ID en la base de datos
db.findOne({ id: resourceId }, (err, resource) => {
// Buscar el recurso por su Ranking en la base de datos
db.findOne({ ranking: resourceRanking }, (err, resource) => {
if (err) {
console.error(err);
res.sendStatus(500); // Devolver un error 500 si hay un problema con la base de datos
Expand Down Expand Up @@ -182,7 +182,7 @@ module.exports = (app, db) => {
const nuevoDato = req.body;

// Verificar el orden de las propiedades recibidas
const expectedOrder = ['id', 'country', 'title', 'published_at', 'channel_title', 'category_id', 'trending_date', 'view_count', 'comment_count'];
const expectedOrder = ['ranking', 'country', 'title', 'published_at', 'channel_title', 'category_id', 'trending_date', 'view_count', 'comment_count'];
const receivedFields = Object.keys(nuevoDato);

// Verificar si el orden de las propiedades coincide con el esperado
Expand All @@ -195,7 +195,7 @@ module.exports = (app, db) => {
}

// Verificar si el objeto recibido tiene la estructura de campos esperada
const expectedFields = ['id', 'country', 'title', 'published_at', 'channel_title', 'category_id', 'trending_date', 'view_count', 'comment_count'];
const expectedFields = ['ranking', 'country', 'title', 'published_at', 'channel_title', 'category_id', 'trending_date', 'view_count', 'comment_count'];

// Verificar si todos los campos esperados están presentes
const isValidStructure = expectedFields.every(field => receivedFields.includes(field));
Expand All @@ -206,14 +206,14 @@ module.exports = (app, db) => {
return;
}

// Verificar si el campo 'id' está presente en el objeto recibido
if (!nuevoDato.hasOwnProperty('id')) {
// Verificar si el campo 'ranking' está presente en el objeto recibido
if (!nuevoDato.hasOwnProperty('ranking')) {
res.sendStatus(400, "Bad Request");
return;
}

// Verificar si el ID ya existe en la base de datos
db.findOne({ id: nuevoDato.id }, (err, existingData) => {
// Verificar si el Ranking ya existe en la base de datos
db.findOne({ ranking: nuevoDato.ranking }, (err, existingData) => {
if (err) {
res.sendStatus(500, "Internal Error");
return;
Expand Down Expand Up @@ -241,23 +241,23 @@ module.exports = (app, db) => {


//PUT para actualizar un dato existente
app.put(API_BASE + "/youtube-trends/:id", (req, res) => {
const idFromUrl = parseInt(req.params.id);
app.put(API_BASE + "/youtube-trends/:ranking", (req, res) => {
const rankingFromUrl = parseInt(req.params.ranking);
const newDato = req.body;

if (idFromUrl !== newDato.id) {
return res.status(400).send("Bad Request: El ID en la URL y en el cuerpo de la solicitud no coinciden");
if (rankingFromUrl !== newDato.ranking) {
return res.sendStatus(400,"Bad Request");
}

db.findOne({ id: idFromUrl }, (findErr, existingData) => {
db.findOne({ ranking: rankingFromUrl }, (findErr, existingData) => {
if (findErr) {
return res.sendStatus(500, "Internal Error");
}
if (!existingData) {
return res.sendStatus(404, "Not Found");
}

db.update({ id: idFromUrl }, { $set: newDato }, {}, (updateErr, numReplaced) => {
db.update({ ranking: rankingFromUrl }, { $set: newDato }, {}, (updateErr, numReplaced) => {
if (updateErr) {
return res.sendStatus(500, "Internal Error");
}
Expand All @@ -278,10 +278,10 @@ module.exports = (app, db) => {


// DELETE para eliminar un dato existente
app.delete(API_BASE + "/youtube-trends/:id", (req, res) => {
const id = parseInt(req.params.id);
app.delete(API_BASE + "/youtube-trends/:ranking", (req, res) => {
const ranking = parseInt(req.params.ranking);

db.remove({ id: id }, {}, (err, numRemoved) => {
db.remove({ ranking: ranking }, {}, (err, numRemoved) => {
if (err) {
return res.sendStatus(500, "Internal Error");
}
Expand Down
20 changes: 10 additions & 10 deletions Victor/index-VEG.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const data = [
{ id: 0, country: 'Albania', title: 'DON XHONI - KATILE', published_at: '2022-07-07T22:00:12Z', channel_title: 'DON XHONI', category_id: 24, trending_date: '22.11.07', view_count: 5130167, comment_count: 3641 },
{ id: 1, country: 'Albania', title: 'Dhurata Dora x Elvana Gjata - GAJDE (Official Video)', published_at: '2022-06-30T22:00:11Z', channel_title: 'Dhurata Dora', category_id: 10, trending_date: '22.11.07', view_count: 8886648, comment_count: 4414 },
{ id: 2, country: 'Albania', title: 'KIDA x BUTRINT IMERI - AM/PM', published_at: '2022-07-02T22:00:11Z', channel_title: 'KIDA', category_id: 10, trending_date: '22.11.07', view_count: 7230818, comment_count: 7518 },
{ id: 3, country: 'Albania', title: 'Dafina Zeqiri - Malli', published_at: '2022-07-07T22:00:12Z', channel_title: 'Dafina Zeqiri', category_id: 10, trending_date: '22.11.07', view_count: 1406078, comment_count: 827 },
{ id: 4, country: 'Albania', title: 'Tayna x Azet - Tequila', published_at: '2022-06-30T18:00:26Z', channel_title: 'Friends Entertainment', category_id: 10, trending_date: '22.11.07', view_count: 6397676, comment_count: 6063 },
{ id: 5, country: 'Albania', title: 'GJIKO x FIFI - DJEGI KREJT', published_at: '2022-07-07T17:45:11Z', channel_title: 'REDBOX Entertainment', category_id: 10, trending_date: '22.11.07', view_count: 1695364, comment_count: 1961 },
{ id: 6, country: 'Albania', title: 'Ardian Bujupi x Ledri Vula - GELATO (prod. by Bled)', published_at: '2022-06-30T22:00:11Z', channel_title: 'OfficialArdianBujupi', category_id: 10, trending_date: '22.11.07', view_count: 3316092, comment_count: 1391 },
{ id: 7, country: 'Albania', title: 'Capital T - Dukat (prod. Panda Music)', published_at: '2022-06-29T16:00:03Z', channel_title: 'Capital T', category_id: 10, trending_date: '22.11.07', view_count: 7110466, comment_count: 1230 },
{ id: 8, country: 'Albania', title: 'MatoLale x Jongmen KOKAINA', published_at: '2022-07-05T16:30:27Z', channel_title: 'MATOLALE', category_id: 10, trending_date: '22.11.07', view_count: 1157204, comment_count: 929 },
{ id: 9, country: "España", title: "España campeona del mundo", published_at: "2010-07-011T22:00:12Z", channel_title: "SEFutbol", category_id: 2, trending_date: "10.07.11", view_count: 514000, comment_count: 3651 }
{ ranking: 0, country: 'Albania', title: 'DON XHONI - KATILE', published_at: '2022-07-07T22:00:12Z', channel_title: 'DON XHONI', category_id: 24, trending_date: '22.11.07', view_count: 5130167, comment_count: 3641 },
{ ranking: 1, country: 'Albania', title: 'Dhurata Dora x Elvana Gjata - GAJDE (Official Video)', published_at: '2022-06-30T22:00:11Z', channel_title: 'Dhurata Dora', category_id: 10, trending_date: '22.11.07', view_count: 8886648, comment_count: 4414 },
{ ranking: 2, country: 'Albania', title: 'KIDA x BUTRINT IMERI - AM/PM', published_at: '2022-07-02T22:00:11Z', channel_title: 'KIDA', category_id: 10, trending_date: '22.11.07', view_count: 7230818, comment_count: 7518 },
{ ranking: 3, country: 'Albania', title: 'Dafina Zeqiri - Malli', published_at: '2022-07-07T22:00:12Z', channel_title: 'Dafina Zeqiri', category_id: 10, trending_date: '22.11.07', view_count: 1406078, comment_count: 827 },
{ ranking: 4, country: 'Albania', title: 'Tayna x Azet - Tequila', published_at: '2022-06-30T18:00:26Z', channel_title: 'Friends Entertainment', category_id: 10, trending_date: '22.11.07', view_count: 6397676, comment_count: 6063 },
{ ranking: 5, country: 'Albania', title: 'GJIKO x FIFI - DJEGI KREJT', published_at: '2022-07-07T17:45:11Z', channel_title: 'REDBOX Entertainment', category_id: 10, trending_date: '22.11.07', view_count: 1695364, comment_count: 1961 },
{ ranking: 6, country: 'Albania', title: 'Ardian Bujupi x Ledri Vula - GELATO (prod. by Bled)', published_at: '2022-06-30T22:00:11Z', channel_title: 'OfficialArdianBujupi', category_id: 10, trending_date: '22.11.07', view_count: 3316092, comment_count: 1391 },
{ ranking: 7, country: 'Albania', title: 'Capital T - Dukat (prod. Panda Music)', published_at: '2022-06-29T16:00:03Z', channel_title: 'Capital T', category_id: 10, trending_date: '22.11.07', view_count: 7110466, comment_count: 1230 },
{ ranking: 8, country: 'Albania', title: 'MatoLale x Jongmen KOKAINA', published_at: '2022-07-05T16:30:27Z', channel_title: 'MATOLALE', category_id: 10, trending_date: '22.11.07', view_count: 1157204, comment_count: 929 },
{ ranking: 9, country: "España", title: "España campeona del mundo", published_at: "2010-07-011T22:00:12Z", channel_title: "SEFutbol", category_id: 2, trending_date: "10.07.11", view_count: 514000, comment_count: 3651 }
];

// Función para calcular la media de view_count para un país específico
Expand Down

0 comments on commit 323dab3

Please sign in to comment.