From 6a8a0d2c6da1fe6351c410fbaa499a73ee05a3fb Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sun, 12 Jan 2020 20:24:35 +0100 Subject: [PATCH] Get the list of conversations --- ComunicTerm.pro | 10 ++++- conversations_screen.cpp | 22 ++++++++++ conversations_screen.h | 9 ++++ entities/conversation.cpp | 76 +++++++++++++++++++++++++++++++++ entities/conversation.h | 47 ++++++++++++++++++++ helpers/conversationshelper.cpp | 44 +++++++++++++++++++ helpers/conversationshelper.h | 22 ++++++++++ mainmenu.cpp | 4 +- 8 files changed, 230 insertions(+), 4 deletions(-) create mode 100644 conversations_screen.cpp create mode 100644 conversations_screen.h create mode 100644 entities/conversation.cpp create mode 100644 entities/conversation.h create mode 100644 helpers/conversationshelper.cpp create mode 100644 helpers/conversationshelper.h diff --git a/ComunicTerm.pro b/ComunicTerm.pro index c70a906..91d17d9 100644 --- a/ComunicTerm.pro +++ b/ComunicTerm.pro @@ -14,7 +14,10 @@ SOURCES += \ helpers/accounthelper.cpp \ helpers/userhelper.cpp \ entities/user.cpp \ - mainmenu.cpp + mainmenu.cpp \ + conversations_screen.cpp \ + helpers/conversationshelper.cpp \ + entities/conversation.cpp HEADERS += \ config.h \ @@ -25,4 +28,7 @@ HEADERS += \ helpers/accounthelper.h \ helpers/userhelper.h \ entities/user.h \ - mainmenu.h + mainmenu.h \ + conversations_screen.h \ + helpers/conversationshelper.h \ + entities/conversation.h diff --git a/conversations_screen.cpp b/conversations_screen.cpp new file mode 100644 index 0000000..1c44b4f --- /dev/null +++ b/conversations_screen.cpp @@ -0,0 +1,22 @@ +#include + +#include "conversations_screen.h" +#include "helpers/conversationshelper.h" +#include "entities/conversation.h" + + +using namespace std; + + +void showConversationsScreen() +{ + auto list = ConversationsHelper::GetList(); + + for(auto c : list) + cout << "Conv: " << c.name() << endl; + + + cout << "done" << endl; + string s; + cin >> s; +} diff --git a/conversations_screen.h b/conversations_screen.h new file mode 100644 index 0000000..c6d58d8 --- /dev/null +++ b/conversations_screen.h @@ -0,0 +1,9 @@ +/** + * Conversations screen + * + * @author Pierre HUBERT + */ + +#pragma once + +void showConversationsScreen(); diff --git a/entities/conversation.cpp b/entities/conversation.cpp new file mode 100644 index 0000000..40aa5dc --- /dev/null +++ b/entities/conversation.cpp @@ -0,0 +1,76 @@ +#include "conversation.h" + +Conversation::Conversation() +{ + +} + +int Conversation::iD() const +{ + return mID; +} + +void Conversation::setID(int iD) +{ + mID = iD; +} + +int Conversation::iD_Owner() const +{ + return mID_Owner; +} + +void Conversation::setID_Owner(int iD_Owner) +{ + mID_Owner = iD_Owner; +} + +int Conversation::lastActive() const +{ + return mLastActive; +} + +void Conversation::setLastActive(int lastActive) +{ + mLastActive = lastActive; +} + +std::string Conversation::name() const +{ + return mName; +} + +void Conversation::setName(const std::string &name) +{ + mName = name; +} + +bool Conversation::following() const +{ + return mFollowing; +} + +void Conversation::setFollowing(bool following) +{ + mFollowing = following; +} + +bool Conversation::sawLastMessage() const +{ + return mSawLastMessage; +} + +void Conversation::setSawLastMessage(bool sawLastMessage) +{ + mSawLastMessage = sawLastMessage; +} + +std::vector Conversation::members() const +{ + return mMembers; +} + +void Conversation::setMembers(const std::vector &members) +{ + mMembers = members; +} diff --git a/entities/conversation.h b/entities/conversation.h new file mode 100644 index 0000000..debfe67 --- /dev/null +++ b/entities/conversation.h @@ -0,0 +1,47 @@ +/** + * Information about a conversation + * + * @author Pierre HUBERT + */ + +#pragma once + + +#include +#include + +class Conversation +{ +public: + Conversation(); + + int iD() const; + void setID(int iD); + + int iD_Owner() const; + void setID_Owner(int iD_Owner); + + int lastActive() const; + void setLastActive(int lastActive); + + std::string name() const; + void setName(const std::string &name); + + bool following() const; + void setFollowing(bool following); + + bool sawLastMessage() const; + void setSawLastMessage(bool sawLastMessage); + + std::vector members() const; + void setMembers(const std::vector &members); + +private: + int mID; + int mID_Owner; + int mLastActive; + std::string mName; + bool mFollowing; + bool mSawLastMessage; + std::vector mMembers; +}; diff --git a/helpers/conversationshelper.cpp b/helpers/conversationshelper.cpp new file mode 100644 index 0000000..c77d36f --- /dev/null +++ b/helpers/conversationshelper.cpp @@ -0,0 +1,44 @@ +#include + +#include "conversationshelper.h" +#include "../api_request.h" +#include "../entities/conversation.h" + +using namespace std; + +ConversationsHelper::ConversationsHelper() +{ + +} + +vector ConversationsHelper::GetList() +{ + const auto response = ApiRequest("conversations/getList").exec(); + + if(response.code() != 200) + throw runtime_error("Get conversations list failed! (code: " + to_string(response.code())); + + vector list; + + for(auto el : response.array()) { + auto obj = el.as_object(); + + Conversation conv; + conv.setID(el["ID"].as_integer()); + conv.setID_Owner(el["ID_owner"].as_integer()); + conv.setLastActive(el["last_active"].as_integer()); + conv.setName(el["name"].is_boolean() ? "" : el["name"].as_string()); + conv.setFollowing(el["following"].as_integer() == 1); + conv.setSawLastMessage(el["saw_last_message"].as_integer() == 1); + + vector members; + for(auto m : el["members"].as_array()) + members.push_back(stoi(m.as_string())); + conv.setMembers(members); + + list.push_back(conv); + + } + + return list; +} diff --git a/helpers/conversationshelper.h b/helpers/conversationshelper.h new file mode 100644 index 0000000..c52d556 --- /dev/null +++ b/helpers/conversationshelper.h @@ -0,0 +1,22 @@ +/** + * Conversations helper + * + * @author Pierre HUBERT + */ + +#pragma once + +#include + +class Conversation; + +class ConversationsHelper +{ +public: + ConversationsHelper(); + + /** + * Get the list of conversations of the current user + */ + static std::vector GetList(); +}; diff --git a/mainmenu.cpp b/mainmenu.cpp index 078aab7..fe6c4e9 100644 --- a/mainmenu.cpp +++ b/mainmenu.cpp @@ -10,6 +10,7 @@ #include "loginscreen.h" #include "mainmenu.h" +#include "conversations_screen.h" using namespace std; @@ -120,8 +121,7 @@ while(true) { // Open user conversations if(o++ == action) { - cerr << "TODO : implement conversations" << endl; - exit(-1); + showConversationsScreen(); } // Logout