67 lines
1.4 KiB
JavaScript
67 lines
1.4 KiB
JavaScript
|
|
const { ObjectId } = require('mongodb');
|
|
const connectDb = require("./db");
|
|
|
|
const SaleDb = class
|
|
{
|
|
constructor()
|
|
{
|
|
this.getCollection();
|
|
}
|
|
|
|
async getCollection()
|
|
{
|
|
const db = await connectDb();
|
|
if (!db) {
|
|
throw new Error('Database not connected');
|
|
}
|
|
this.collection = db.collection("Sales");
|
|
}
|
|
|
|
// 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 getByUrl(url)
|
|
{
|
|
let result = await this.collection.findOne({url: url});
|
|
return result;
|
|
}
|
|
|
|
async getByIDPlatform(idSalePlatform, platformName)
|
|
{
|
|
let result = await this.collection.findOne({idPlatform: String(idSalePlatform), platform: String(platformName)});
|
|
return result;
|
|
}
|
|
}
|
|
|
|
module.exports = { SaleDb }; |