Skip to content

Commit

Permalink
new changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Hadiqa11 committed Oct 4, 2023
1 parent 6e89ed0 commit 3848c1a
Show file tree
Hide file tree
Showing 32 changed files with 821 additions and 1 deletion.
9 changes: 9 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# .env

NODE_ENV=
DB_HOST=
DB_DIALECT=
DB_USER=
DB_PASSWORD=
DB_PORT=
DB_MIGRATIONS_PATH=
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
node_modules
node_modules
.env
package-lock.json
26 changes: 26 additions & 0 deletions config/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"development": {
"host": "localhost",
"dialect": "postgres",
"user":"hadiqasumbalarshad",
"password" :"11223344",
"port":"5432",
"migrationsPath": "./migrations"

},
"test": {
"host": "localhost",
"dialect": "postgres",
"user":"hadiqasumbalarshad",
"password" :"11223344",
"port":"5432"
},
"production": {
"host": "localhost",
"dialect": "postgres",
"user":"hadiqasumbalarshad",
"password" :"11223344",
"port":"5432"
}
}

35 changes: 35 additions & 0 deletions controllers/attachment.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Request, Response } from 'express';
// import AttachmentService from '../services/attachment.service';

// Function to create a new attachment
const createAttachment = async (req: Request, res: Response) => {
try {
const { fileUrl, fileName, messageId, creatorId } = req.body;
const attachment = await AttachmentService.createAttachment(
fileUrl,
fileName,
messageId,
creatorId
);

res.status(201).json(attachment);
} catch (error) {
res.status(500).json({ error: error.message });
}
};

// Function to get attachments by message ID
const getAttachmentsByMessageId = async (req: Request, res: Response) => {
try {
const { message_id } = req.params;
const attachments = await AttachmentService.getAttachmentsByMessageId(
message_id
);

res.status(200).json(attachments);
} catch (error) {
res.status(500).json({ error: error.message });
}
};

export { createAttachment, getAttachmentsByMessageId };
Empty file added controllers/auth.controller.ts
Empty file.
Empty file added controllers/group.controller.ts
Empty file.
Empty file.
Empty file added controllers/index.js
Empty file.
Empty file.
Empty file added controllers/user.controller.ts
Empty file.
40 changes: 40 additions & 0 deletions migrations/20231003065958-User.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict';
import { QueryInterface, DataTypes, Sequelize } from 'sequelize';

/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up (queryInterface: QueryInterface, sequelize: Sequelize) {
await queryInterface.createTable('Users', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: DataTypes.INTEGER
},
userName: {
type: DataTypes.STRING
},
fullName: {
type: DataTypes.STRING
},
email: {
type: DataTypes.STRING
},
password: {
type: DataTypes.STRING
},
createdAt: {
allowNull: false,
type: DataTypes.DATE
},
updatedAt: {
allowNull: false,
type: DataTypes.DATE
}
});
},

async down (queryInterface: QueryInterface, sequelize: Sequelize) {
await queryInterface.dropTable('Users');
}
};
44 changes: 44 additions & 0 deletions migrations/20231003075540-Message.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'use strict';

import { QueryInterface, DataTypes, Sequelize } from 'sequelize';
/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up (queryInterface: QueryInterface, sequelize: Sequelize) {
await queryInterface.createTable('Messages', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: DataTypes.INTEGER
},
text: {
type: DataTypes.STRING,
allowNull: false
},
sender_id : {
type: DataTypes.INTEGER.UNSIGNED ,
allowNull: false,
},
group_id : {
type: DataTypes.INTEGER.UNSIGNED ,
allowNull: false,
},
reciever_id : {
type: DataTypes.INTEGER.UNSIGNED ,
allowNull: false,
},
createdAt: {
allowNull: false,
type: DataTypes.DATE
},
updatedAt: {
allowNull: false,
type: DataTypes.DATE
}
});
},

async down (queryInterface: QueryInterface, sequelize: Sequelize) {
await queryInterface.dropTable('Messages');
}
};
36 changes: 36 additions & 0 deletions migrations/20231003075550-Group.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict';

import { QueryInterface, DataTypes, Sequelize } from 'sequelize';
/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up (queryInterface: QueryInterface, sequelize: Sequelize) {
await queryInterface.createTable('Groups', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: DataTypes.INTEGER
},
group_name: {
allowNull: false,
type: DataTypes.STRING
},
creator_id : {
type: DataTypes.INTEGER.UNSIGNED ,
allowNull: false,
},
createdAt: {
allowNull: false,
type: DataTypes.DATE
},
updatedAt: {
allowNull: false,
type: DataTypes.DATE
}
});
},

async down (queryInterface: QueryInterface, sequelize: Sequelize) {
await queryInterface.dropTable('Groups');
}
};
37 changes: 37 additions & 0 deletions migrations/20231003075602-Groupmember.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict';

import { QueryInterface, DataTypes, Sequelize } from 'sequelize';
/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up (queryInterface: QueryInterface, sequelize: Sequelize) {
await queryInterface.createTable('Group_participants', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: DataTypes.INTEGER
},
group_id : {
type: DataTypes.INTEGER.UNSIGNED ,
allowNull: false,
},

user_id : {
type: DataTypes.INTEGER.UNSIGNED ,
allowNull: false,
},
createdAt: {
allowNull: false,
type: DataTypes.DATE
},
updatedAt: {
allowNull: false,
type: DataTypes.DATE
}
});
},

async down (queryInterface: QueryInterface, sequelize: Sequelize) {
await queryInterface.dropTable('Groups');
}
};
43 changes: 43 additions & 0 deletions migrations/20231003075627-Attachment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict';

import { QueryInterface, DataTypes, Sequelize } from 'sequelize';
/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up (queryInterface: QueryInterface, sequelize: Sequelize) {
await queryInterface.createTable('Attachments', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: DataTypes.INTEGER
},
file_Url: {
allowNull: false,
type: DataTypes.STRING
},
file_Name: {
type: DataTypes.STRING
},
creator_id : {
type: DataTypes.INTEGER.UNSIGNED ,
allowNull: false,
},
message_id : {
type: DataTypes.INTEGER.UNSIGNED ,
allowNull: false,
},
createdAt: {
allowNull: false,
type: DataTypes.DATE
},
updatedAt: {
allowNull: false,
type: DataTypes.DATE
}
});
},

async down (queryInterface: QueryInterface, sequelize: Sequelize) {
await queryInterface.dropTable('Attachments');
}
};
43 changes: 43 additions & 0 deletions models/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import fs from 'fs';
import path from 'path';
import { Sequelize, DataTypes, Model } from 'sequelize';

const basename = path.basename(__filename);
const env = process.env.NODE_ENV || 'development';
const config = require(path.join(__dirname, '..', 'config', 'config.json'))[env];
const db: Record<string, Model> = {}; // Define the type for the `db` object

let sequelize: Sequelize;

// if (config.use_env_variable) {
// sequelize = new Sequelize(process.env[config.use_env_variable]!, config);
// } else {
// sequelize = new Sequelize(config.database!, config.username!, config.password!, config);
// }

fs
.readdirSync(__dirname)
.filter(file => {
return (
file.indexOf('.') !== 0 &&
file !== basename &&
file.slice(-3) === '.ts' &&
file.indexOf('.test.ts') === -1
);
})
.forEach(file => {
const model = require(path.join(__dirname, file)).default(sequelize, DataTypes);
db[model.name] = model;
});

Object.keys(db).forEach((model : any) => {
if (model.associate) {
model.associate!(db);
}
});


export { sequelize, DataTypes };

export default db;

Loading

0 comments on commit 3848c1a

Please sign in to comment.