-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from ArinNigam/chatroom
Chatroom
- Loading branch information
Showing
16 changed files
with
541 additions
and
14 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
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
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 @@ | ||
import mongoose from "mongoose"; | ||
|
||
const MessageSchema = new mongoose.Schema({ | ||
sender: { | ||
type: String, | ||
required: true | ||
}, | ||
content: { | ||
type: String, | ||
required: true | ||
}, | ||
timestamp: { | ||
type: Date, | ||
default: Date.now() | ||
} | ||
}); | ||
|
||
const Message = mongoose.model('Message', MessageSchema); | ||
|
||
export default Message; |
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 @@ | ||
import Message from '../../models/message.js'; | ||
import * as messages from "../../constants/messages.js"; | ||
import { Router} from "express"; | ||
const messageListRouter = Router(); | ||
|
||
messageListRouter.get("/", async (req, res) => { | ||
try { | ||
const messages = await Message.find({}); | ||
res.json(messages); | ||
} catch (err) { | ||
res.status(500).json({ message: messages.internalServerError }); | ||
} | ||
}); | ||
|
||
messageListRouter.post("/", async (req, res) => { | ||
const message = new Message({ | ||
sender: req.body.sender, | ||
content: req.body.content, | ||
timestamp: req.body.timestamp | ||
}); | ||
try { | ||
const newMessage = await message.save(); | ||
res.status(201).json(newMessage); | ||
} catch (err) { | ||
res.status(400).json({ message: messages.badRequest }); | ||
} | ||
}); | ||
|
||
messageListRouter.delete("/:id", async (req, res) => { | ||
try { | ||
const message = await Message.findById(req.params.id); | ||
console.log(message); | ||
if (message) { | ||
await message.deleteOne(); | ||
res.json({ message: messages.deleted }); | ||
} else { | ||
res.status(404).json({ message: messages.notFound }); | ||
} | ||
} catch (err) { | ||
res.status(500).json({ message: messages.internalServerError }); | ||
} | ||
}); | ||
|
||
export default messageListRouter; |
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
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
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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
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,30 @@ | ||
class Message { | ||
final String? id; | ||
final String sender; | ||
final String content; | ||
final DateTime? timestamp; | ||
|
||
Message({ | ||
this.id, | ||
required this.sender, | ||
required this.content, | ||
this.timestamp, | ||
}); | ||
|
||
factory Message.fromJson(Map<String, dynamic> json) { | ||
return Message( | ||
id: json['_id'], | ||
sender: json['sender'], | ||
content: json['content'], | ||
timestamp: DateTime.parse(json['timestamp']), | ||
); | ||
} | ||
|
||
Map<String, dynamic> toJson() { | ||
return { | ||
'sender': sender, | ||
'content': content, | ||
'timestamp': timestamp!.toIso8601String(), | ||
}; | ||
} | ||
} |
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,91 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_riverpod/flutter_riverpod.dart'; | ||
import 'package:logger/logger.dart'; | ||
import 'package:smart_insti_app/models/message.dart'; | ||
import 'package:smart_insti_app/repositories/chat_room_repository.dart'; | ||
|
||
import '../constants/constants.dart'; | ||
import '../constants/dummy_entries.dart'; | ||
|
||
final chatRoomProvider = | ||
StateNotifierProvider<ChatRoomStateNotifier, ChatRoomState>( | ||
(ref) => ChatRoomStateNotifier(ref)); | ||
|
||
class ChatRoomState { | ||
final List<Message> messageList; | ||
final TextEditingController messageController; | ||
final LoadingState loadingState; | ||
|
||
ChatRoomState({ | ||
required this.messageList, | ||
required this.messageController, | ||
required this.loadingState, | ||
}); | ||
|
||
ChatRoomState copyWith({ | ||
List<Message>? messageList, | ||
TextEditingController? messageController, | ||
LoadingState? loadingState, | ||
}) { | ||
return ChatRoomState( | ||
messageList: messageList ?? this.messageList, | ||
messageController: messageController ?? this.messageController, | ||
loadingState: loadingState ?? this.loadingState, | ||
); | ||
} | ||
} | ||
|
||
class ChatRoomStateNotifier extends StateNotifier<ChatRoomState> { | ||
ChatRoomStateNotifier(Ref ref) | ||
: _api = ref.read(chatRoomRepositoryProvider), | ||
super( | ||
ChatRoomState( | ||
messageList: DummyMessages.messages, | ||
messageController: TextEditingController(), | ||
loadingState: LoadingState.progress, | ||
), | ||
) { | ||
loadMessages(); | ||
} | ||
final ChatRoomRepository _api; | ||
final Logger _logger = Logger(); | ||
final ScrollController scrollController = ScrollController(); | ||
|
||
void loadMessages() async { | ||
final messages = await _api.getMessages(); | ||
state = state.copyWith( | ||
messageList: messages, | ||
loadingState: LoadingState.success, | ||
); | ||
scrollController.jumpTo(scrollController.position.maxScrollExtent); | ||
} | ||
|
||
void addMessage(String sender) async { | ||
final message = Message( | ||
sender: sender, | ||
content: state.messageController.text, | ||
timestamp: DateTime.now(), | ||
); | ||
await _api.addMessage(message); | ||
state.messageController.clear(); | ||
loadMessages(); | ||
} | ||
|
||
// void editMessage(int index, String newContent) { | ||
// final messages = state.messageList; | ||
// Message message = messages[index]; | ||
// deleteMessage(index); | ||
// addMessage() | ||
// } | ||
|
||
void deleteMessage(int index) { | ||
final messages = state.messageList; | ||
Message message = messages[index]; | ||
_api.deleteMessage(message.id!); | ||
messages.removeAt(index); | ||
state = state.copyWith(messageList: messages); | ||
for (var message in messages) { | ||
_logger.i(message.content); | ||
} | ||
} | ||
} |
Oops, something went wrong.