Deploy Bot commited on
Commit
cfb79bb
·
1 Parent(s): c60a122

Add Dynamic Banners

Browse files
Files changed (2) hide show
  1. src/api/routes.js +82 -0
  2. src/models/Banner.js +12 -0
src/api/routes.js CHANGED
@@ -44,10 +44,92 @@ router.get('/image/:fileId', async (req, res) => {
44
  // --- PROTECTED ROUTES ---
45
  const Category = require('../models/Category');
46
  const User = require('../models/User');
 
47
 
48
  // --- PROTECTED ROUTES ---
49
  router.use(auth);
50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  // Get Categories (For App Sidebar)
52
  router.get('/categories', async (req, res) => {
53
  try {
 
44
  // --- PROTECTED ROUTES ---
45
  const Category = require('../models/Category');
46
  const User = require('../models/User');
47
+ const Banner = require('../models/Banner');
48
 
49
  // --- PROTECTED ROUTES ---
50
  router.use(auth);
51
 
52
+ // --- FILE UPLOAD (New) ---
53
+ const multer = require('multer');
54
+ const upload = multer({ dest: 'uploads/' });
55
+ const fs = require('fs');
56
+ const FormData = require('form-data');
57
+
58
+ router.post('/upload', upload.single('image'), async (req, res) => {
59
+ try {
60
+ if (!req.file) return res.status(400).json({ error: 'No file uploaded' });
61
+
62
+ // Upload to Telegram to get file_id
63
+ const form = new FormData();
64
+ form.append('chat_id', config.ADMIN_IDS[0] || 12345); // Send to admin or dummy chat
65
+ form.append('document', fs.createReadStream(req.file.path));
66
+
67
+ // Use a method that returns file_id. sendDocument is good.
68
+ // NOTE: You need a chat_id where the bot can send messages.
69
+ // We will try sending to the first ADMIN_ID.
70
+
71
+ const tgRes = await axios.post(
72
+ `https://api.telegram.org/bot${config.BOT_TOKEN}/sendDocument`,
73
+ form,
74
+ { headers: form.getHeaders() }
75
+ );
76
+
77
+ // Cleanup local file
78
+ fs.unlinkSync(req.file.path);
79
+
80
+ if (tgRes.data && tgRes.data.ok) {
81
+ const fileId = tgRes.data.result.document.file_id;
82
+ res.json({ success: true, file_id: fileId });
83
+ } else {
84
+ res.status(500).json({ error: 'Telegram upload failed' });
85
+ }
86
+ } catch (e) {
87
+ if (req.file) fs.unlinkSync(req.file.path); // Cleanup on error
88
+ console.error("Upload Error:", e.message);
89
+ res.status(500).json({ error: e.message });
90
+ }
91
+ });
92
+
93
+ // --- BANNER ROUTES (New Phase 12) ---
94
+ // Get Banners
95
+ router.get('/banners', async (req, res) => {
96
+ try {
97
+ const banners = await Banner.find().sort({ createdAt: -1 });
98
+ res.json({ success: true, count: banners.length, data: banners });
99
+ } catch (e) {
100
+ res.status(500).json({ error: e.message });
101
+ }
102
+ });
103
+
104
+ // Add Banner (Admin)
105
+ router.post('/admin/banners', async (req, res) => {
106
+ try {
107
+ const { title, image_url, file_id, color } = req.body;
108
+ const newBanner = new Banner({
109
+ id: Date.now().toString(),
110
+ title,
111
+ image_url,
112
+ file_id,
113
+ color
114
+ });
115
+ await newBanner.save();
116
+ res.json({ success: true, data: newBanner });
117
+ } catch (e) {
118
+ res.status(500).json({ error: e.message });
119
+ }
120
+ });
121
+
122
+ // Delete Banner (Admin)
123
+ router.delete('/admin/banners/:id', async (req, res) => {
124
+ try {
125
+ await Banner.findOneAndDelete({ id: req.params.id });
126
+ res.json({ success: true, message: 'Banner deleted' });
127
+ } catch (e) {
128
+ res.status(500).json({ error: e.message });
129
+ }
130
+ });
131
+
132
+
133
  // Get Categories (For App Sidebar)
134
  router.get('/categories', async (req, res) => {
135
  try {
src/models/Banner.js ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const mongoose = require('mongoose');
2
+
3
+ const bannerSchema = new mongoose.Schema({
4
+ id: { type: String, required: true, unique: true },
5
+ title: { type: String, required: true },
6
+ image_url: { type: String }, // External URL or empty
7
+ file_id: { type: String }, // Telegram File ID
8
+ color: { type: String }, // Hex color (optional fallback)
9
+ createdAt: { type: Date, default: Date.now }
10
+ });
11
+
12
+ module.exports = mongoose.model('Banner', bannerSchema);