From b193b18c2946de1844f4c114db8754529b4dab6a Mon Sep 17 00:00:00 2001 From: Noel Koljanin <82367504+nkoljanin@users.noreply.github.com> Date: Wed, 24 Apr 2024 10:59:37 +0200 Subject: [PATCH] Initial commit --- .gitignore | 16 +++++++++++ .idea/.gitignore | 5 ++++ README 2.md | 1 + bin/serve.ts | 8 ++++++ package.json | 44 ++++++++++++++++++++++++++++++ src/MainApp.ts | 43 +++++++++++++++++++++++++++++ src/controllers/IndexController.ts | 21 ++++++++++++++ src/controllers/UserController.ts | 24 ++++++++++++++++ src/models/User.ts | 16 +++++++++++ src/services/TelegramService.ts | 26 ++++++++++++++++++ src/services/UserService.ts | 16 +++++++++++ tsconfig.json | 20 ++++++++++++++ 12 files changed, 240 insertions(+) create mode 100644 .gitignore create mode 100644 .idea/.gitignore create mode 100644 README 2.md create mode 100644 bin/serve.ts create mode 100644 package.json create mode 100644 src/MainApp.ts create mode 100644 src/controllers/IndexController.ts create mode 100644 src/controllers/UserController.ts create mode 100644 src/models/User.ts create mode 100644 src/services/TelegramService.ts create mode 100644 src/services/UserService.ts create mode 100644 tsconfig.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fc26d9b --- /dev/null +++ b/.gitignore @@ -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 diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..b58b603 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,5 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/README 2.md b/README 2.md new file mode 100644 index 0000000..0f58eb5 --- /dev/null +++ b/README 2.md @@ -0,0 +1 @@ +# express-sample \ No newline at end of file diff --git a/bin/serve.ts b/bin/serve.ts new file mode 100644 index 0000000..e34e342 --- /dev/null +++ b/bin/serve.ts @@ -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}`); +}); diff --git a/package.json b/package.json new file mode 100644 index 0000000..c77a902 --- /dev/null +++ b/package.json @@ -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" + } +} diff --git a/src/MainApp.ts b/src/MainApp.ts new file mode 100644 index 0000000..eb8aa2f --- /dev/null +++ b/src/MainApp.ts @@ -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; + } +} diff --git a/src/controllers/IndexController.ts b/src/controllers/IndexController.ts new file mode 100644 index 0000000..c52cdbe --- /dev/null +++ b/src/controllers/IndexController.ts @@ -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); + }); + } + +} diff --git a/src/controllers/UserController.ts b/src/controllers/UserController.ts new file mode 100644 index 0000000..e65bbf0 --- /dev/null +++ b/src/controllers/UserController.ts @@ -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 + } +} diff --git a/src/models/User.ts b/src/models/User.ts new file mode 100644 index 0000000..956da78 --- /dev/null +++ b/src/models/User.ts @@ -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 = 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('User', UserSchema); diff --git a/src/services/TelegramService.ts b/src/services/TelegramService.ts new file mode 100644 index 0000000..c7953e1 --- /dev/null +++ b/src/services/TelegramService.ts @@ -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 +} diff --git a/src/services/UserService.ts b/src/services/UserService.ts new file mode 100644 index 0000000..2d80723 --- /dev/null +++ b/src/services/UserService.ts @@ -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); + } +} diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..de2feaf --- /dev/null +++ b/tsconfig.json @@ -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", + ] +} +