Jucundus/backend/services/lotDb.js

75 lines
1.7 KiB
JavaScript

const { ObjectId } = require('mongodb');
const connectDb = require("./db");
const LotDb = class
{
constructor()
{
this.getCollection();
}
async getCollection()
{
const db = await connectDb();
if (!db) {
throw new Error('Database not connected');
}
this.collection = db.collection("Lots");
}
// CRUD
async get(id)
{
let result = await this.collection.findOne({_id: new ObjectId(id)});
return result;
}
async post(newDocument)
{
let result = await this.collection.insertOne(newDocument);
return result;
}
async put(id, data)
{
let result = await this.collection.updateOne({_id: new ObjectId(id)}, {$set: data});
return result;
}
async remove(id)
{
let result = await this.collection.deleteOne({_id: new ObjectId(id)});
return result;
}
// Fucntions
async getAll()
{
let result = await this.collection.find({}).toArray();
return result;
}
async getBySaleId(idSalePlatform, platformName)
{
console.log(platformName);
let result = await this.collection.find({sale_id: new ObjectId(idSalePlatform), platform: platformName});
return result.toArray();
}
async getByIDPlatform(idLotPlatform, platformName)
{
let result = await this.collection.findOne({idPlatform: String(idLotPlatform), platform: platformName});
return result;
}
async deleteAllLotBySaleId(sale_id){
let result = await this.collection.deleteMany({sale_id: new ObjectId(sale_id)});
return result;
}
}
module.exports = {LotDb};