Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Chimoney Discord Bot #79

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions submissions/chimoney-discord-bot/api/Database/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require("dotenv").config();
const mongoose = require("mongoose");

const connectDB = async () => {
try {
// Get mongoURI from environment variables
const mongoUri = process.env.MONGO_URI;

// Connect to database
await mongoose.connect(mongoUri);
console.log("successfully connected to db");
} catch (error) {
console.log(error.message);
}
};

module.exports = { connectDB };
23 changes: 23 additions & 0 deletions submissions/chimoney-discord-bot/api/Models/transaction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const mongoose = require("mongoose");
const { Schema } = mongoose;

const TransactionSchema = new Schema({
discordSender: {
type: String,
required: true,
},
discordReceiver: {
type: String,
required: true,
},
_id: {
type: String,
required: true,
},
isRedeemed: {
type: Boolean,
default: false,
},
});

module.exports = mongoose.model("Transaction", TransactionSchema);
30 changes: 30 additions & 0 deletions submissions/chimoney-discord-bot/api/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require("dotenv").config();
const express = require("express");
const app = express();
const morgan = require("morgan");
const helmet = require("helmet");
const { connectDB } = require("./Database");
const apiRouter = require("./routes/index");

// Setup middleware
app.use(express.json());
app.use(helmet());
app.use(morgan("tiny"));

// Routes
app.use("/api", apiRouter);

// 404 route handler
app.use((req, res, next) => {
res.status(404).json({
message: "Resource Not Found",
success: false,
});
});

// Start express server
const port = process.env.PORT || 8080;
app.listen(port, async () => {
console.log(`Listening on port ${port}`);
await connectDB();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const {
getAllTransactions,
getTransactionByID,
createTransaction,
editTransaction,
} = require("../services/transaction.service");
const { handleAsync } = require("../utils/helpers");

const handleGetTransactions = handleAsync(async (req, res) => {
const filter = req.query;

// Get all matching transactions
const transactions = await getAllTransactions(filter);

res.status(200).json({ transactions, success: true });
});

const handleGetTransactionById = handleAsync(async (req, res) => {
const { transactionId } = req.params;

// Get transaction from the database
const transaction = await getTransactionByID(transactionId);

res.status(200).json({ transaction, success: true });
});

const handleCreateTransaction = handleAsync(async (req, res) => {
// Create a new transaction
const transaction = await createTransaction(req.body);

res.status(200).json({ transaction, success: true });
});

const handleEditTransaction = handleAsync(async (req, res) => {
const { transactionId } = req.params;

// Update transaction in the database
const transaction = await editTransaction(transactionId, req.body);

res.status(200).json({ transaction, success: true });
});

module.exports = {
handleCreateTransaction,
handleEditTransaction,
handleGetTransactionById,
handleGetTransactions,
};
13 changes: 13 additions & 0 deletions submissions/chimoney-discord-bot/api/dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM node:alpine

WORKDIR /app

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 8080

CMD [ "npm", "start" ]
Loading