-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
240 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
|
||
|
||
# Node modules | ||
/node_modules/ | ||
|
||
# JavaScript files | ||
*.js | ||
|
||
# TypeScript source map files | ||
*.js.map | ||
|
||
# If you decide to exclude package-lock.json | ||
package-lock.json | ||
|
||
.env | ||
*.xml |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# express-sample |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// bin/serve.ts | ||
import { MainApp } from "../src/MainApp"; | ||
|
||
const port = process.env.PORT || 3000; | ||
const app = new MainApp().getApp(); | ||
app.listen(port, () => { | ||
console.log(`Server running on http://localhost:${port}`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
{ | ||
"name": "miko-store", | ||
"version": "1.0.0", | ||
"description": "miko web shop", | ||
"main": "bin/serve.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"start": "node bin/serve.js", | ||
"build": "tsc", | ||
"dev": "ts-node src/MainApp.ts" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/nkoljanin/miko-store.git" | ||
}, | ||
"author": "nk", | ||
"license": "ISC", | ||
"bugs": { | ||
"url": "https://github.com/nkoljanin/miko-store/issues" | ||
}, | ||
"homepage": "https://github.com/nkoljanin/miko-store#readme", | ||
"dependencies": { | ||
"cors": "^2.8.5", | ||
"dotenv": "^16.4.5", | ||
"express": "^4.18.3", | ||
"express-file-store": "latest", | ||
"express-fileupload": "^1.4.0", | ||
"mongoose": "^8.2.0", | ||
"node-telegram-bot-api": "^0.64.0" | ||
}, | ||
"devDependencies": { | ||
"@types/chai": "^4.3.12", | ||
"@types/chai-http": "^4.2.0", | ||
"@types/express": "^4.17.21", | ||
"@types/mocha": "^10.0.6", | ||
"@types/mongoose": "^5.11.97", | ||
"@types/node": "^20.11.24", | ||
"chai": "^5.1.0", | ||
"chai-http": "^4.4.0", | ||
"mocha": "^10.3.0", | ||
"ts-node": "^10.9.2", | ||
"typescript": "^5.3.3" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// src/MainApp.ts | ||
import express, { Express } from 'express'; | ||
import mongoose from 'mongoose'; | ||
import { IndexController } from './controllers/IndexController'; | ||
import { UserController } from './controllers/UserController'; // Import the UserController | ||
// import { TelegramController } from './controllers/TelegramController'; // Import the TelegramController | ||
import dotenv from 'dotenv'; | ||
|
||
dotenv.config(); | ||
export class MainApp { | ||
private app: Express; | ||
private indexController: IndexController; | ||
private userController: UserController; // Add a property for UserController | ||
// private telegramController: TelegramController; // Add a property for TelegramController | ||
|
||
constructor() { | ||
this.app = express(); | ||
this.setMongoConfig(); | ||
this.indexController = new IndexController(); | ||
this.userController = new UserController(); // Instantiate UserController | ||
// this.telegramController = new TelegramController(); // Instantiate TelegramController | ||
this.routes(); | ||
} | ||
|
||
private setMongoConfig() { | ||
mongoose.Promise = global.Promise; | ||
const uri =process.env.MONGO_URI; | ||
|
||
mongoose.connect(uri, {}).then(() => { | ||
console.log('Database connected'); | ||
}); | ||
} | ||
|
||
private routes(): void { | ||
this.app.use('/', this.indexController.router); | ||
this.app.use('/users', this.userController.router); // Use the UserController routes | ||
// this.app.use('/bot', this.telegramController.router); // Use the TelegramController routes | ||
} | ||
|
||
public getApp(): Express { | ||
return this.app; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Router, Request, Response } from 'express'; | ||
import { UserService } from '../services/UserService'; | ||
|
||
export class IndexController { | ||
public router: Router; | ||
private userService: UserService; | ||
|
||
constructor() { | ||
this.router = Router(); | ||
this.userService = new UserService(); | ||
this.routes(); | ||
} | ||
|
||
public routes() { | ||
this.router.get('/', async (req: Request, res: Response) => { | ||
const users = await this.userService.getUsers(); | ||
res.json(users); | ||
}); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// src/controllers/UserController.ts | ||
import { Router, Request, Response } from 'express'; | ||
import { UserService } from '../services/UserService'; | ||
|
||
export class UserController { | ||
public router: Router; | ||
private userService: UserService; | ||
|
||
constructor() { | ||
this.router = Router(); | ||
this.userService = new UserService(); | ||
this.routes(); | ||
} | ||
|
||
public routes() { | ||
// Example route for getting all users | ||
this.router.get('/users', async (req: Request, res: Response) => { | ||
const users = await this.userService.getUsers(); | ||
res.json(users); | ||
}); | ||
|
||
// Add more routes as needed | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// src/models/User.ts | ||
import mongoose, { Schema, Document } from 'mongoose'; | ||
|
||
export interface IUser extends Document { | ||
telegram_id: string; | ||
username: string; | ||
address: string; | ||
} | ||
|
||
const UserSchema: Schema<IUser> = new Schema({ | ||
telegram_id: { type: String, required: true, unique: true }, | ||
username: { type: String, required: true }, | ||
address: { type: String, required: true } | ||
}); | ||
|
||
export const User = mongoose.model<IUser>('User', UserSchema); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import TelegramBot from 'node-telegram-bot-api'; | ||
|
||
export class TelegramService { | ||
private bot: TelegramBot; | ||
|
||
constructor(token: string) { | ||
this.bot = new TelegramBot(token, { polling: true }); | ||
this.initializeMessageHandling(); | ||
} | ||
|
||
start() { | ||
this.bot.on('message', (msg) => { | ||
// Your bot logic here | ||
}); | ||
console.log('Telegram bot started'); | ||
} | ||
initializeMessageHandling(): void { | ||
this.bot.on('message', (msg) => { | ||
const chatId = msg.chat.id; | ||
const response = `Hello, ${msg.from?.first_name}!`; | ||
this.bot.sendMessage(chatId, response); | ||
}); | ||
} | ||
|
||
// You can add more methods to handle different bot functionalities | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// src/services/UserService.ts | ||
import { User } from '../models/User'; | ||
|
||
export class UserService { | ||
public async getUsers() { | ||
return User.find(); | ||
} | ||
|
||
public async getUserById(id: string) { | ||
return User.findById(id); | ||
} | ||
|
||
public async createUser(data: any) { | ||
return User.create(data); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "commonjs", | ||
"target": "es2017", | ||
"moduleResolution": "node", | ||
"sourceMap": true, | ||
"noImplicitAny": false, | ||
"suppressImplicitAnyIndexErrors": false, | ||
"declaration": false, | ||
"watch": false, | ||
"esModuleInterop": true, | ||
"experimentalDecorators": true, | ||
"skipLibCheck": true | ||
}, | ||
"exclude": [ | ||
"node_modules", | ||
"dist/src", | ||
] | ||
} | ||
|