Skip to content

Commit

Permalink
Arreglos en cities-backend-db
Browse files Browse the repository at this point in the history
  • Loading branch information
sfaci committed Oct 20, 2024
1 parent db08b68 commit 351b17a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 22 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Repositorio con ejemplos de proyectos Node.js

- **cities-backend**: API sencilla hecha con nodejs. Sólo backend. Sin base de datos
- **cities-backend-db**: API sencilla hecha con nodejs. Sólo backend. Los datos están almacenados en una base de datos SQLite
- **cities-backend-frontend**: API sencilla hecha con nodejs. Incluye un frontend HTML + Javascript con llamadas al backend usando axios. Sin base de datos
- **cities-backend-frontend-crud**: API sencilla hecha con nodejs con operación de registro. Incluye un frontend HTML + Javascript con llamadas al backend usando axios. Sin base de datos
- **cities-backend**: API sencilla. Sólo backend. Sin base de datos
- **cities-backend-db**: API con CRUD completo. Sólo backend. Los datos están almacenados en una base de datos SQLite
- **cities-backend-frontend**: API sencilla. Incluye un frontend HTML + Javascript con llamadas al backend usando axios. Sin base de datos
- **cities-backend-frontend-crud**: API sencilla con operación de registro. Incluye un frontend HTML + Javascript con llamadas al backend usando axios. Sin base de datos
Binary file modified cities-backend-db/cities.db
Binary file not shown.
54 changes: 36 additions & 18 deletions cities-backend-db/src/app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import express from 'express';
import knex from 'knex';

// Configuración de la base de datos: tipo, ubicación y otros parámetros
const db = knex({
client: 'sqlite3',
connection: {
Expand All @@ -12,32 +13,49 @@ const db = knex({
const app = express();
app.use(express.json());

const cities = {
'Zaragoza': {
altitude: 199,
population: 673010
},
'Huesca': {
altitude: 488,
population: 53305
},
'Teruel': {
altitude: 915,
population: 25900
}
};

// Operación que devuelve todas las ciudades de la base de datos
app.get('/cities', async (req, res) => {
const result = await db('cities').select('*');
res.json(result);

res.status(200).json(result);
});

// Operación que devuelve una ciudad determinada
app.get('/cities/:city', async (req, res) => {
const result = await db('cities').select('*').where({name: req.params.city}).first();
res.json(result);

res.status(200).json(result);
});

// Operación que registra una nueva ciudad en la base de datos
app.post('/cities', async (req, res) => {
await db('cities').insert({
name: req.body.name,
population: req.body.population,
altitude: req.body.altitude
});

res.status(201).json({});
});

// Operación que modifica una ciudad en la base de datos
app.put('/cities/:city', async (req, res) => {
await db('cities').where({ name: req.params.city }).update({
population: req.body.population,
altitude: req.body.altitude
});

res.status(204).json({});
});

// Operación que elimina una ciudad de la base de datos
app.delete('/cities/:city', async (req, res) => {
const cityName = req.params.city;
await db('cities').del().where({name: cityName});

res.status(204).json({})
});

app.listen(8080, () => {
console.log('Iniciando el backend en el puerto 8080');
});
});
1 change: 1 addition & 0 deletions cities_collection.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"v":3,"name":"cities","folders":[],"requests":[{"v":"7","endpoint":"http://localhost:8080/cities","name":"cities","params":[{"key":"carId","value":"4","active":true,"description":""}],"headers":[],"method":"GET","auth":{"authType":"inherit","authActive":true},"preRequestScript":"","testScript":"","body":{"contentType":null,"body":null},"requestVariables":[]},{"v":"7","endpoint":"http://localhost:8080/cities/Zaragoza","name":"cities/:city","params":[],"headers":[],"method":"GET","auth":{"authType":"inherit","authActive":true},"preRequestScript":"","testScript":"","body":{"contentType":null,"body":null},"requestVariables":[]},{"v":"7","endpoint":"http://localhost:8080/cities","name":"cities","params":[],"headers":[],"method":"POST","auth":{"authType":"inherit","authActive":true},"preRequestScript":"","testScript":"","body":{"contentType":"application/json","body":"{\n \"name\": \"Madrid\",\n \"population\": 4500000,\n \"altitude\": 550\n}"},"requestVariables":[]},{"v":"7","endpoint":"http://localhost:8080/cities/Madrid","name":"cities/:city","params":[],"headers":[],"method":"DELETE","auth":{"authType":"inherit","authActive":true},"preRequestScript":"","testScript":"","body":{"contentType":null,"body":null},"requestVariables":[]},{"v":"7","endpoint":"http://localhost:8080/cities/Zaragoza","name":"cities/:city","params":[],"headers":[],"method":"PUT","auth":{"authType":"inherit","authActive":true},"preRequestScript":"","testScript":"","body":{"contentType":"application/json","body":"{\n \"population\": 700000,\n \"altitude\": 280\n}"},"requestVariables":[]}],"headers":[],"auth":{"authType":"none","authActive":true}}

0 comments on commit 351b17a

Please sign in to comment.