SOIL C++
C++ Unified Device Interface
Object.cpp
Go to the documentation of this file.
1#include "Object.h"
2
3
4SOIL::Object::Object(std::shared_ptr<Element> parent, std::string uuid, std::string name, std::string description, std::string ontology) : Element(parent, uuid, name, description, ontology)
5{
6 if (uuid.substr(0, 3) != "OBJ")
7 {
8 throw std::logic_error("UUIDs for objects must start with OBJ!");
9 }
10 allowed_methods = { HTTP::Methods::GET, HTTP::Methods::OPTIONS, HTTP::Methods::HEAD, HTTP::Methods::PUT };
11}
12
13
15{
16}
17
18std::shared_ptr<SOIL::Object> SOIL::Object::create(std::shared_ptr<Element> parent, std::string uuid, std::string name, std::string description, std::string ontology)
19{
20 Object* object = new Object(parent, uuid, name, description, ontology);
21 return object->ptr();
22}
23
25{
26
27 HTTP::Json json_root = Element::wjson();
28 json_root[U("children")] = HTTP::Json::array();
29 int i = 0;
30 for (auto& child : children)
31 {
32 json_root[U("children")][i] = HTTP::Json();
33 json_root[U("children")][i][U("uuid")] = HTTP::Json::string(utility::conversions::to_string_t(child.first));
34 json_root[U("children")][i][U("name")] = HTTP::Json::string(utility::conversions::to_string_t(child.second->name));
35 i++;
36 }
37 return json_root;
38}
39
41{
42}
43
45{
46 throw std::logic_error("This function has not been implemented!");
47}
48
50{
51
52}
53
55{
56 this->read();
57 HTTP::Response response;
58 response.set_body(this->wjson());
59 response.set_status_code(HTTP::Status::OK);
60 return response;
61}
62
64{
65 auto task = message.extract_json();
66 task.wait();
67 HTTP::Json body = task.get();
68 HTTP::Json json = this->insert(body);
69 HTTP::Response response;
70 response.set_body(json);
71 response.set_status_code(HTTP::Status::Created);
72 return response;
73}
74
76{
77 this->remove();
78 parent->remove(this->uuid);
79 HTTP::Response response;
80 response.set_status_code(HTTP::Status::OK);
81 return response;
82}
nlohmann::json json
std::vector< web::http::method > allowed_methods
Allowed methods.
Definition: Resource.h:42
SOIL Base Element.
Definition: Element.h:24
std::string uuid
Local UUID.
Definition: Element.h:53
virtual HTTP::Json wjson(void)
HTTP JSON.
Definition: Element.cpp:130
Object Class.
Definition: Object.h:20
virtual void read(void)
Read callback.
Definition: Object.cpp:40
HTTP::Response handle_get(HTTP::Request message, std::smatch match=std::smatch()) override
HTTP GET Handler.
Definition: Object.cpp:54
static std::shared_ptr< Object > create(std::shared_ptr< Element > parent, std::string uuid, std::string name, std::string description, std::string ontology="")
Create new Object.
Definition: Object.cpp:18
HTTP::Response handle_delete(HTTP::Request message, std::smatch match=std::smatch()) override
HTTP DELETE Handler.
Definition: Object.cpp:75
virtual void remove(void)
Remove callback.
Definition: Object.cpp:49
virtual HTTP::Json insert(HTTP::Json body)
Insert callback.
Definition: Object.cpp:44
~Object()
Destructor.
Definition: Object.cpp:14
HTTP::Response handle_put(HTTP::Request message, std::smatch match=std::smatch()) override
HTTP PUT Handler.
Definition: Object.cpp:63
Object(std::shared_ptr< Element > parent, std::string uuid, std::string name, std::string description, std::string ontology="")
Constructor.
Definition: Object.cpp:4
web::json::value wjson(void)
HTTP JSON.
Definition: Object.cpp:24
web::json::value Json
HTTP JSON.
Definition: Types.h:39
web::http::http_request Request
HTTP Request.
Definition: Types.h:11
web::http::http_response Response
HTTP Response.
Definition: Types.h:18