0% found this document useful (0 votes)
88 views1 page

Color Trading API with MongoDB

This document outlines a simple Express.js application that connects to a MongoDB database for managing color trading. It includes a schema for colors with attributes like name, RGB values, price, and owner, and provides routes for retrieving colors and executing trades. Additionally, it features a mechanism to periodically update color prices by a random percentage every minute.

Uploaded by

kparth575
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views1 page

Color Trading API with MongoDB

This document outlines a simple Express.js application that connects to a MongoDB database for managing color trading. It includes a schema for colors with attributes like name, RGB values, price, and owner, and provides routes for retrieving colors and executing trades. Additionally, it features a mechanism to periodically update color prices by a random percentage every minute.

Uploaded by

kparth575
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

const express = require('express');

const mongoose = require('mongoose');


const cors = require('cors');

const app = express();


[Link](cors());
[Link]([Link]());

// Connect to MongoDB (replace with your connection string)


[Link]('mongodb://localhost:27017/colorTrading', { useNewUrlParser: true,
useUnifiedTopology: true });

// Models
const ColorSchema = new [Link]({
name: String,
rgb: [Number, Number, Number], // e.g., [255, 0, 0] for red
price: Number,
owner: String
});
const Color = [Link]('Color', ColorSchema);

// Routes
[Link]('/colors', async (req, res) => {
const colors = await [Link]();
[Link](colors);
});

[Link]('/trade', async (req, res) => {


const { colorId, buyer, seller, price } = [Link];
// Simple "100% correct" logic: Price must match current value (simulate
accuracy)
const color = await [Link](colorId);
if ([Link] !== price) return [Link](400).json({ error: 'Price mismatch -
trade not 100% correct!' });

// Update ownership
[Link] = buyer;
await [Link]();
[Link]({ message: 'Trade successful!' });
});

// Simulate price updates (run periodically)


setInterval(async () => {
const colors = await [Link]();
[Link](async (color) => {
// "100% correct" trend: Increase price by 1-10% randomly
[Link] *= (1 + [Link]() * 0.1);
await [Link]();
});
}, 60000); // Every minute

[Link](3001, () => [Link]('Server running on port 3001'));

You might also like