22 #include "hardware/Buzzer_odb.h" 23 #include "hardware/LED_odb.h" 40 template <
typename ObjectT, const
char *ObjectIdKey,
typename SerializerT>
41 std::vector<WebSockAPI::ICRUDResourceHandler::ActionActionParam>
45 std::vector<CRUDResourceHandler::ActionActionParam> ret;
53 catch (json::out_of_range &e)
62 hardware_action_param);
66 hardware_action_param);
70 hardware_action_param);
74 hardware_action_param);
80 template <
typename ObjectT, const
char *ObjectIdKey,
typename SerializerT>
85 DBPtr db = ctx_.dbsrv->db();
86 odb::transaction t(db->begin());
88 auto new_led_or_buzzer = std::make_shared<ObjectT>();
89 SerializerT::unserialize(*new_led_or_buzzer, req.at(
"attributes"),
91 db->persist(new_led_or_buzzer);
93 rep[
"data"] = SerializerT::serialize(*new_led_or_buzzer, security_context());
102 template <
typename T>
129 template <
typename ObjectT, const
char *ObjectIdKey,
typename SerializerT>
130 boost::optional<json>
135 using Result = odb::result<ObjectT>;
136 DBPtr db = ctx_.dbsrv->db();
137 odb::transaction t(db->begin());
140 if (!led_or_buzzer_id.is_nil())
142 auto led_or_buzzer = find_led_or_buzzer_by_id<ObjectT>(led_or_buzzer_id, db);
143 rep[
"data"] = SerializerT::serialize(*led_or_buzzer, security_context());
147 Result result = db->query<ObjectT>();
148 rep[
"data"] = json::array();
149 auto current_user = ctx_.session->current_user();
152 for (
const auto &led_or_buzzer : result)
156 if (ctx_.session->security_context().check_permission(
159 rep[
"data"].push_back(
160 SerializerT::serialize(led_or_buzzer, security_context()));
168 template <
typename ObjectT, const
char *ObjectIdKey,
typename SerializerT>
169 boost::optional<json>
173 DBPtr db = ctx_.dbsrv->db();
174 odb::transaction t(db->begin());
176 auto led_or_buzzer = find_led_or_buzzer_by_id<ObjectT>(led_or_buzzer_id, db);
178 SerializerT::unserialize(*led_or_buzzer, req.at(
"attributes"),
181 db->update(led_or_buzzer);
182 rep[
"data"] = SerializerT::serialize(*led_or_buzzer, security_context());
187 template <
typename ObjectT, const
char *ObjectIdKey,
typename SerializerT>
188 boost::optional<json>
192 DBPtr db = ctx_.dbsrv->db();
193 odb::transaction t(db->begin());
195 auto led_or_buzzer = find_led_or_buzzer_by_id<ObjectT>(did, db);
196 db->erase(led_or_buzzer);
202 template <
typename ObjectT, const
char *ObjectIdKey,
typename SerializerT>
205 : CRUDResourceHandler(ctx)
207 static_assert(std::is_same<ObjectT, Hardware::LED>::value ||
208 std::is_same<ObjectT, Hardware::Buzzer>::value,
209 "This CRUDHandler can only work with either LED or Buzzer type");
212 template <
typename ObjectT, const
char *ObjectIdKey,
typename SerializerT>
boost::optional< WebSockAPI::json > delete_impl(const WebSockAPI::json &req) override
CRUD handler for Buzzer and LED.
static WebSockAPI::CRUDResourceHandlerUPtr instanciate(WebSockAPI::RequestContext)
Hardware::DeviceId device_id
This is the header file for a generated source file, GitSHA1.cpp.
An optional transaction is an object that behave like an odb::transaction if there is no currently ac...
std::unique_ptr< CRUDResourceHandler > CRUDResourceHandlerUPtr
std::shared_ptr< LED > LEDPtr
std::shared_ptr< T > find_led_or_buzzer_by_id(const Hardware::DeviceId &id, DBPtr db)
std::vector< ActionActionParam > required_permission(Verb verb, const WebSockAPI::json &req) const override
Thin wrapper around boost::uuids::uuid.
CRUDHandler(const WebSockAPI::RequestContext &ctx)
Permissions for hardware devices.
boost::optional< WebSockAPI::json > read_impl(const WebSockAPI::json &req) override
boost::optional< WebSockAPI::json > create_impl(const WebSockAPI::json &req) override
static constexpr const char buzzer_object_id_key[]
How to retrieve the object's id when instanciate the crud handler for buzzer.
std::shared_ptr< Buzzer > BuzzerPtr
odb::result< Tools::LogEntry > Result
Holds valuable pointer to provide context to a request.
std::shared_ptr< odb::database > DBPtr
static constexpr const char led_object_id_key[]
How to retrieve the object's id when instanciate the crud handler for led.
Abstraction of LED device attributes.
boost::optional< WebSockAPI::json > update_impl(const WebSockAPI::json &req) override
Abstraction of Buzzer device attributes.
Serializer code is copy-pasted from buzzer's serializer.