Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
nkoljanin committed Apr 24, 2024
1 parent 4644422 commit b193b18
Show file tree
Hide file tree
Showing 12 changed files with 240 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .gitignore
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
5 changes: 5 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions README 2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# express-sample
8 changes: 8 additions & 0 deletions bin/serve.ts
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}`);
});
44 changes: 44 additions & 0 deletions package.json
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"
}
}
43 changes: 43 additions & 0 deletions src/MainApp.ts
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;
}
}
21 changes: 21 additions & 0 deletions src/controllers/IndexController.ts
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);
});
}

}
24 changes: 24 additions & 0 deletions src/controllers/UserController.ts
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
}
}
16 changes: 16 additions & 0 deletions src/models/User.ts
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);
26 changes: 26 additions & 0 deletions src/services/TelegramService.ts
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
}
16 changes: 16 additions & 0 deletions src/services/UserService.ts
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);
}
}
20 changes: 20 additions & 0 deletions tsconfig.json
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",
]
}

0 comments on commit b193b18

Please sign in to comment.