generated from Arquisoft/lomap_0
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #79 from Arquisoft/develop
Develop
- Loading branch information
Showing
39 changed files
with
6,901 additions
and
4,395 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,16 +1,24 @@ | ||
const mongoose = require('mongoose') | ||
import { MongoClient } from 'mongodb'; | ||
import {Place} from '../../webapp/src/shared/shareddtypes'; | ||
|
||
|
||
const mongoose = require('mongoose') | ||
const { MONGODB, MONGODB_ATLAS_TEST, NODE_ENV } = process.env // saca del .env la ruta a la base de datos | ||
|
||
//const connectionString = NODE_ENV === 'test' ? MONGODB_ATLAS_TEST : MONGODB_ATLAS | ||
let connectionURL: string; // coge del .env el enlace a la base de datos y lo transforma en string | ||
connectionURL = MONGODB!; // esto falla otra vez | ||
|
||
const client = new MongoClient("mongodb+srv://lomap:[email protected]/Lomap-es3b?retryWrites=true&w=majority"); //Para conectarse a la base | ||
|
||
const connectionString = MONGODB // el enlace a la base de datos | ||
var db: any; | ||
var cliente: any; | ||
|
||
if (!connectionString) { | ||
if (!connectionURL) { // en caso de que falle el .env | ||
console.error('Yoy must define your connection string') | ||
} | ||
|
||
// Realiza la conexion con la base de datos | ||
/** | ||
const dbConnection = async () => { | ||
try { | ||
mongoose.set("strictQuery", false) | ||
|
@@ -32,4 +40,109 @@ const dbConnection = async () => { | |
mongoose.disconnect() | ||
}) | ||
} | ||
module.exports = { dbConnection } | ||
module.exports = { dbConnection } */ | ||
|
||
|
||
export async function connectToDatabase() { | ||
try { | ||
await client.connect(); | ||
console.log('Connected to database!'); | ||
db = client.db(); | ||
|
||
} catch (error) { | ||
console.error(error); | ||
throw new Error('Failed to connect to database'); | ||
} | ||
} | ||
|
||
export async function guardarLugar(lugar:Place) { | ||
await connectToDatabase(); | ||
|
||
const tablaDB = db.collection('places'); | ||
|
||
await tablaDB.insertOne(lugar); | ||
|
||
console.log('Lugar guardado con exito! Nombre: ', lugar.name ); | ||
|
||
client.close(); | ||
} | ||
|
||
export async function cogerLugar(lugarID:number) { | ||
await connectToDatabase(); | ||
|
||
const tablaDB = db.collection('places'); | ||
|
||
const lugar = await tablaDB.findOne({_id:lugarID}); | ||
|
||
client.close(); | ||
|
||
return lugar; | ||
} | ||
|
||
export async function cogerLugares(): Promise<Place[]> { | ||
await connectToDatabase(); | ||
|
||
const tablaDB = db.collection('places'); | ||
|
||
const lugares = await tablaDB.find().toArray(); | ||
|
||
console.log(lugares); | ||
|
||
client.close(); | ||
|
||
return lugares; | ||
} | ||
|
||
export async function borrarLugar(lugarID:number) { | ||
await connectToDatabase(); | ||
|
||
const tablaDB = db.collection('places'); | ||
|
||
const filtro = {id:lugarID}; | ||
|
||
await tablaDB.deleteOne(filtro); | ||
|
||
console.log('Lugar borrado con exito! Nombre: ', lugarID ); | ||
|
||
client.close(); | ||
} | ||
|
||
export async function borrarLugar2(lugar:Place) { | ||
await connectToDatabase(); | ||
|
||
const tablaDB = db.collection('places'); | ||
|
||
await tablaDB.deleteOne(lugar); | ||
|
||
console.log('Lugar borrado con exito! Nombre: ', lugar.name ); | ||
|
||
client.close(); | ||
} | ||
|
||
export async function nuevoComentario(lugarID:number, comment: string) { | ||
await connectToDatabase(); | ||
|
||
const tablaDB = db.collection('places'); | ||
|
||
const addComment = { $set: { comment: comment}}; | ||
|
||
await tablaDB.updateOne({_id:lugarID}, addComment); | ||
|
||
console.log('Comentario añadido con exito! ID: ', lugarID ); | ||
|
||
client.close(); | ||
} | ||
|
||
export async function nuevoComentario2(lugar:Place, comment: string) { | ||
await connectToDatabase(); | ||
|
||
const tablaDB = db.collection('places'); | ||
|
||
const addComment = { $set: { comment: comment}}; | ||
|
||
await tablaDB.updateOne(lugar, addComment); | ||
|
||
console.log('Comentario añadido con exito! Comentario: ', lugar.comments ); | ||
|
||
client.close(); | ||
} |
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
Oops, something went wrong.