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>