diff --git a/src/Infrastructure/Network/NetworkClient.cc b/src/Infrastructure/Network/NetworkClient.cc index a4d1d305..0c8708d3 100644 --- a/src/Infrastructure/Network/NetworkClient.cc +++ b/src/Infrastructure/Network/NetworkClient.cc @@ -6,6 +6,7 @@ #include #include #include +#include namespace evento { @@ -59,76 +60,291 @@ Task> NetworkClient::getUserInfo() { co_return Ok(entity); } Task> NetworkClient::refreshAccessToken(std::string const& refreshToken) { - // TODO: implement me! - co_return Error(Error::Unknown); + auto result = co_await this->request(http::verb::post, + endpoint("/refresh-token"), + {{"refreshtoken", refreshToken}}); + if (result.isErr()) + co_return Err(result.unwrapErr()); + + try { + this->tokenBytes = result.unwrap()["accessToken"].get(); + } catch (const nlohmann::json::exception& e) { + this->tokenBytes = std::nullopt; + co_return Err(Error(Error::JsonDes, e.what())); + } + + co_return Ok(); } -Task> NetworkClient::getActiveEventList() { - // TODO: implement me! - co_return Error(Error::Unknown); +Task> NetworkClient::getActiveEventList() { + auto result = co_await this->request(http::verb::get, + endpoint("/v2/client/event/query", + {{"active", "true"}})); + if (result.isErr()) + co_return Err(result.unwrapErr()); + + EventQueryRes entity; + try { + nlohmann::from_json(result.unwrap(), entity); + } catch (const nlohmann::json::exception& e) { + co_return Err(Error(Error::JsonDes, e.what())); + } + + co_return Ok(entity); +} + +Task> NetworkClient::getLatestEventList() { + auto result = co_await this->request(http::verb::get, + endpoint("/v2/client/event/query", + {{"start", "now"}})); + if (result.isErr()) + co_return Err(result.unwrapErr()); + + EventQueryRes entity; + try { + nlohmann::from_json(result.unwrap(), entity); + } catch (const nlohmann::json::exception& e) { + co_return Err(Error(Error::JsonDes, e.what())); + } + + co_return Ok(entity); +} + +Task> NetworkClient::getHistoryEventList(int page, int size) { + auto result = co_await this->request(http::verb::get, + endpoint("/v2/client/event/query", + {{"page", std::to_string(page)}, + {"size", std::to_string(size)}, + {"end", "now"}})); + if (result.isErr()) + co_return Err(result.unwrapErr()); + + EventQueryRes entity; + try { + nlohmann::from_json(result.unwrap(), entity); + } catch (const nlohmann::json::exception& e) { + co_return Err(Error(Error::JsonDes, e.what())); + } + + co_return Ok(entity); } -Task> NetworkClient::getLatestEventList() { - // TODO: implement me! - co_return Error(Error::Unknown); +Task> NetworkClient::getDepartmentEventList(std::string const& larkDepartment, + int page, + int size) { + auto result = co_await this->request(http::verb::get, + endpoint("/v2/client/event/query", + {{"page", std::to_string(page)}, + {"size", std::to_string(size)}, + {"larkDepartmentName", + larkDepartment}})); + if (result.isErr()) + co_return Err(result.unwrapErr()); + + EventQueryRes entity; + try { + nlohmann::from_json(result.unwrap(), entity); + } catch (const nlohmann::json::exception& e) { + co_return Err(Error(Error::JsonDes, e.what())); + } + + co_return Ok(entity); } -Task> NetworkClient::getHistoryEventList(int page, int size) { - // TODO: implement me! - co_return Error(Error::Unknown); +Task> NetworkClient::getEventList(std::initializer_list params) { + auto result = co_await this->request(http::verb::get, + endpoint("/v2/client/event/query", params)); + if (result.isErr()) + co_return Err(result.unwrapErr()); + + EventQueryRes entity; + try { + nlohmann::from_json(result.unwrap(), entity); + } catch (const nlohmann::json::exception& e) { + co_return Err(Error(Error::JsonDes, e.what())); + } + + co_return Ok(entity); } Task> NetworkClient::getAttachment(int eventId) { - // TODO: implement me! - co_return Error(Error::Unknown); + auto result = co_await this->request( + http::verb::get, endpoint(std::format("api/v2/client/event/{}/attachments", eventId))); + if (result.isErr()) + co_return Err(result.unwrapErr()); + + AttachmentEntity entity; + try { + nlohmann::from_json(result.unwrap(), entity); + } catch (const nlohmann::json::exception& e) { + co_return Err(Error(Error::JsonDes, e.what())); + } + + co_return Ok(entity); } -Task> NetworkClient::getUserFeedback(int eventId) { - // TODO: implement me! - co_return Error(Error::Unknown); +Task>> NetworkClient::getUserFeedback(int eventId) { + auto result = co_await this + ->request(http::verb::get, + endpoint(std::format("/v2/client/event/{}/feedback", + eventId))); + if (result.isErr()) + co_return Err(result.unwrapErr()); + + std::optional entity = std::nullopt; + + if (result.unwrap().is_null()) { + co_return Ok(entity); + } + + try { + entity = result.unwrap().get(); + } catch (const nlohmann::json::exception& e) { + co_return Err(Error(Error::JsonDes, e.what())); + } + + co_return Ok(entity); } -Task> NetworkClient::addUserFeedback(int eventId, +Task> NetworkClient::addUserFeedback(int eventId, int rating, std::string const& content) { - // TODO: implement me! - co_return Error(Error::Unknown); + auto result = co_await this->request( + http::verb::post, + endpoint(std::format("/v2/client/event/{}/feedback", eventId), + {{"rating", std::to_string(rating)}, {"content", content}})); + if (result.isErr()) + co_return Err(result.unwrapErr()); + + co_return Ok(true); } -Task> NetworkClient::checkInEvent(int eventId, std::string const& code) { - // TODO: implement me! - co_return Error(Error::Unknown); +Task> NetworkClient::checkInEvent(int eventId, std::string const& code) { + auto result = co_await this->request( + http::verb::post, + endpoint(std::format("/v2/client/event/{}/check-in", eventId), {{"code", code}})); + if (result.isErr()) + co_return Err(result.unwrapErr()); + + if (result.unwrap().is_boolean()) + co_return Ok(result.unwrap().get()); + + co_return Err(Error(Error::Data, "response data type error")); } -Task> NetworkClient::subscribeEvent(int eventId, bool subscribe) { - // TODO: implement me! - co_return Error(Error::Unknown); +Task> NetworkClient::subscribeEvent(int eventId, bool subscribe) { + std::string subscribeStr = subscribe ? "true" : "false"; + auto result = co_await this + ->request(http::verb::post, + endpoint(std::format("/v2/client/event/{}/subscribe", + eventId), + {{"subscribe", subscribeStr}})); + if (result.isErr()) + co_return Err(result.unwrapErr()); + + if (result.unwrap().is_boolean()) + co_return Ok(result.unwrap().get()); + + co_return Err(Error(Error::Data, "response data type error")); } -Task> NetworkClient::subscribeDepartment(std::string const& larkDepartment, +Task> NetworkClient::subscribeDepartment(std::string const& larkDepartment, bool subscribe) { - // TODO: implement me! - co_return Error(Error::Unknown); + std::string subscribeStr = subscribe ? "true" : "false"; + auto result = co_await this + ->request(http::verb::post, + endpoint(std::format("/v2/client/event/{}/subscribe", + larkDepartment), + {{"subscribe", subscribeStr}})); + if (result.isErr()) + co_return Err(result.unwrapErr()); + + if (result.unwrap().is_boolean()) + co_return Ok(result.unwrap().get()); + + co_return Err(Error(Error::Data, "response data type error")); } Task> NetworkClient::getParticipatedEvent() { - // TODO: implement me! - co_return Error(Error::Unknown); + auto result = co_await this->request(http::verb::get, + endpoint("/v2/client/event/participated")); + if (result.isErr()) + co_return Err(result.unwrapErr()); + + EventEntityList entity; + try { + nlohmann::from_json(result.unwrap(), entity); + } catch (const nlohmann::json::exception& e) { + co_return Err(Error(Error::JsonDes, e.what())); + } + + co_return Ok(entity); } Task> NetworkClient::getSubscribedEvent() { - // TODO: implement me! - co_return Error(Error::Unknown); + auto result = co_await this->request(http::verb::get, + endpoint("/v2/client/event/subscribed")); + if (result.isErr()) + co_return Err(result.unwrapErr()); + + EventEntityList entity; + try { + nlohmann::from_json(result.unwrap(), entity); + } catch (const nlohmann::json::exception& e) { + co_return Err(Error(Error::JsonDes, e.what())); + } + + co_return Ok(entity); } Task> NetworkClient::getHomeSlide() { - // TODO: implement me! - co_return Error(Error::Unknown); + auto result = co_await this->request(http::verb::get, + endpoint("/v2/client/event/slide")); + if (result.isErr()) + co_return Err(result.unwrapErr()); + + SlideEntityList entity; + try { + nlohmann::from_json(result.unwrap(), entity); + } catch (const nlohmann::json::exception& e) { + co_return Err(Error(Error::JsonDes, e.what())); + } + + co_return Ok(entity); } Task> NetworkClient::getEventSlide(int eventId) { - // TODO: implement me! - co_return Error(Error::Unknown); + auto result = co_await this->request(http::verb::get, + endpoint( + std::format("/v2/client/event/{}/slide", + eventId))); + if (result.isErr()) + co_return Err(result.unwrapErr()); + + SlideEntityList entity; + try { + nlohmann::from_json(result.unwrap(), entity); + } catch (const nlohmann::json::exception& e) { + co_return Err(Error(Error::JsonDes, e.what())); + } + + co_return Ok(entity); +} + +Task> NetworkClient::getDepartmentList() { + auto result = co_await this->request(http::verb::get, + endpoint("v2/client/lark/department")); + if (result.isErr()) + co_return Err(result.unwrapErr()); + + DepartmentEntityList entity; + try { + nlohmann::from_json(result.unwrap(), entity); + } catch (const nlohmann::json::exception& e) { + co_return Err(Error(Error::JsonDes, e.what())); + } + + co_return Ok(entity); } urls::url NetworkClient::endpoint(std::string_view endpoint) { diff --git a/src/Infrastructure/Network/NetworkClient.h b/src/Infrastructure/Network/NetworkClient.h index da3e354e..5ec509a9 100644 --- a/src/Infrastructure/Network/NetworkClient.h +++ b/src/Infrastructure/Network/NetworkClient.h @@ -14,6 +14,8 @@ #include #include #include +#include +#include namespace evento { @@ -23,8 +25,9 @@ namespace net = boost::asio; // from namespace urls = boost::urls; // from using JsonResult = Result>; -using EventEntityList = std::vector; using SlideEntityList = std::vector; +using EventEntityList = std::vector; +using DepartmentEntityList = std::vector; using ContributorList = std::vector; template @@ -41,23 +44,29 @@ class NetworkClient { Task> refreshAccessToken(std::string const& refreshToken); - Task> getActiveEventList(); + Task> getActiveEventList(); + + Task> getLatestEventList(); - Task> getLatestEventList(); + Task> getHistoryEventList(int page, int size = 10); - Task> getHistoryEventList(int page, int size = 10); + Task> getDepartmentEventList(std::string const& larkDepartment, + int page, + int size = 10); + + Task> getEventList(std::initializer_list params); Task> getAttachment(int eventId); - Task> getUserFeedback(int eventId); + Task>> getUserFeedback(int eventId); - Task> addUserFeedback(int eventId, int rating, std::string const& content); + Task> addUserFeedback(int eventId, int rating, std::string const& content); - Task> checkInEvent(int eventId, std::string const& code); + Task> checkInEvent(int eventId, std::string const& code); - Task> subscribeEvent(int eventId, bool subscribe); + Task> subscribeEvent(int eventId, bool subscribe); - Task> subscribeDepartment(std::string const& larkDepartment, bool subscribe); + Task> subscribeDepartment(std::string const& larkDepartment, bool subscribe); Task> getParticipatedEvent(); @@ -67,6 +76,8 @@ class NetworkClient { Task> getEventSlide(int eventId); + Task> getDepartmentList(); + Task> getContributors(); Task> getLatestRelease(); diff --git a/src/Infrastructure/Network/Response/DepartmentEntity.h b/src/Infrastructure/Network/Response/DepartmentEntity.h new file mode 100644 index 00000000..99622023 --- /dev/null +++ b/src/Infrastructure/Network/Response/DepartmentEntity.h @@ -0,0 +1,16 @@ +// IWYU pragma: private, include +#pragma once + +#include +#include + +namespace evento { + +struct DepartmentEntity { + std::string id; + std::string name; + + NLOHMANN_DEFINE_TYPE_INTRUSIVE(DepartmentEntity, id, name); +}; + +} // namespace evento diff --git a/src/Infrastructure/Network/Response/EventEntity.h b/src/Infrastructure/Network/Response/EventEntity.h index 08308996..c8cb8d2b 100644 --- a/src/Infrastructure/Network/Response/EventEntity.h +++ b/src/Infrastructure/Network/Response/EventEntity.h @@ -18,7 +18,7 @@ struct EventEntity { std::string larkDepartmentName; State state; bool isSubscribed; // 已订阅 - bool isChecked; // 已签到 + bool isCheckedIn; // 已签到 NLOHMANN_DEFINE_TYPE_INTRUSIVE(EventEntity, id, @@ -32,7 +32,7 @@ struct EventEntity { larkDepartmentName, state, isSubscribed, - isChecked); + isCheckedIn); }; } // namespace evento \ No newline at end of file diff --git a/src/Infrastructure/Network/Response/EventQueryRes.h b/src/Infrastructure/Network/Response/EventQueryRes.h new file mode 100644 index 00000000..9baf3c88 --- /dev/null +++ b/src/Infrastructure/Network/Response/EventQueryRes.h @@ -0,0 +1,17 @@ +// IWYU pragma: private, include +#pragma once + +#include "EventEntity.h" +#include + +namespace evento { + +struct EventQueryRes { + std::vector elements; + int current; + int total; + + NLOHMANN_DEFINE_TYPE_INTRUSIVE(EventQueryRes, elements, current, total); +}; + +} // namespace evento \ No newline at end of file diff --git a/src/Infrastructure/Network/ResponseStruct.h b/src/Infrastructure/Network/ResponseStruct.h index e6b18ea5..d5916c3c 100644 --- a/src/Infrastructure/Network/ResponseStruct.h +++ b/src/Infrastructure/Network/ResponseStruct.h @@ -4,9 +4,11 @@ #include "Response/AttachmentEntity.h" // IWYU pragma: export #include "Response/ContributorEntity.h" // IWYU pragma: export +#include "Response/DepartmentEntity.h" // IWYU pragma: export #include "Response/EventEntity.h" // IWYU pragma: export +#include "Response/EventQueryRes.h" // IWYU pragma: export #include "Response/FeedbackEntity.h" // IWYU pragma: export #include "Response/LoginResEntity.h" // IWYU pragma: export #include "Response/ReleaseEntity.h" // IWYU pragma: export #include "Response/SlideEntity.h" // IWYU pragma: export -#include "Response/UserInfoEntity.h" // IWYU pragma: export \ No newline at end of file +#include "Response/UserInfoEntity.h" // IWYU pragma: export