Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Message feature #6

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions app/controllers/messageController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const Message = require('./message');

//mesaj olusturmak icin fonksiyon
function createNewMessage(mesajID, gonderen, alici, icerik) {
const newMessage = new Message({
mesajID,
gonderen,
alici,
icerik
});

newMessage.save((err, savedMessage) => {
if (err) {
console.error(err);
} else {
console.log('Kaydedilen mesaj: ', savedMessage);
}
});
}

//mesajlari listelemek icin fonksiyon
function listAllMessages() {
Message.find((err, messages) => {
if (err) {
console.error(err);
} else {
console.log('Mesajlar: ', messages);
}
});
}

//mesajın detaylarını goruntulemek icin fonksiyon
function viewMessageById(message_id) {
Message.findById(message_id, (err, message) => {
if (err) {
console.error(err);
} else {
console.log('Mesaj: ', message);
}
});
}

//mesaji guncellemek icin fonksiyon
function updateMessageById(message_id, newContent) {
Message.findByIdAndUpdate(message_id, { icerik: newContent }, (err, message) => {
if (err) {
console.error(err);
} else {
console.log('Güncellenen mesaj: ', message);
}
});
}

//mesaji silmek icin fonksiyon
function deleteMessageById(message_id) {
Message.findByIdAndDelete(message_id, (err, message) => {
if (err) {
console.error(err);
} else {
console.log('Silinen mesaj: ', message);
}
});
}

32 changes: 32 additions & 0 deletions app/models/message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const mongoose = require('mongoose');

const MessageSchema = new mongoose.Schema({
mesajID: {
type: String,
required: true,
unique: true
},
gonderen: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
required: true
},
alici: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
required: true
},
icerik: {
type: String,
required: true
},
tarih: {
type: Date,
default: Date.now,
required: true
}
});

const Message = mongoose.model('Message', MessageSchema);

module.exports = Message;
21 changes: 21 additions & 0 deletions app/routes/messageRoutes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const express = require('express');
const messageController = require('../controllers/messageController');

const router = express.Router();

//yeni bir mesaj oluşturma
router.post('/messages', messageController.createMessage);

//tüm mesajlari listeleme
router.get('/messages', messageController.getAllMessages);

//belirli bir mesaji getirme
router.get('/messages/:messageId', messageController.getMessage);

//mesaj guncelleme
router.put('/messages/:messageId', messageController.updateMessage);

//mesaj silme
router.delete('/messages/:messageId', messageController.deleteMessage);

module.exports = router;
21 changes: 21 additions & 0 deletions config/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const mongoose = require('mongoose');

const connectDB = async () => {
try {
const dbURI = process.env.DB_URI;
const connectionOptions = {
useNewUrlParser: true,
useUnifiedTopology: true,
dbName: 'IkinciSans',
};

await mongoose.connect(dbURI, connectionOptions);
console.log('Database connected successfully.');
} catch (error) {
console.error('Database connection failed:', error.message);
process.exit(1);
}
};

module.exports = connectDB;

11 changes: 11 additions & 0 deletions connection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const mongoose = require('mongoose');

const url = 'mongodb+srv://<kullaniciadi>:<sifre>@cluster0.1pcp9yc.mongodb.net/?retryWrites=true&w=majority';

mongoose.connect(url, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log('MongoDB baglantisi basarili'))
.catch((err) => console.error('MongoDB baglantisi hatasi: ', err));

const Message = require('./Message');

module.exports = { Message };