oatpp-mongocxxmapper
an official module if enough data is aquired to prove its reliability. So we are really happy about any reports
@bhorn -- sample DTO
// StyleDto.hpp
// crud
//
// Created by Leonid on 3/13/18.
// Modified by Ryan Rajpaul 01/31/2020
// Copyright © 2018 oatpp. All rights reserved.
//
class StyleDto : public oatpp::data::mapping::type::Object {
DTO_INIT(StyleDto, Object)
DTO_FIELD(Int32, StyleId);
DTO_FIELD(String, Manufacturer, "Manufacturer");
DTO_FIELD(String, Brand, "Brand");
DTO_FIELD(String, Category, "Category");
DTO_FIELD(String, Type, "Type");
DTO_FIELD(String, Description, "Description");
};
@bhorn -- method below StyleDto::ObjectWrapper Database::getStyleById(v_int32 id){
bsoncxx::stdx::optional<bsoncxx::v_noabi::document::value> result =
m_db[COLLECTION_NAME].find_one({document{} << "StyleId" << id << finalize});
auto docView = result->view();
auto style = m_mongomapper->readFromDocument(docView);
if(style) {
OATPP_LOGD(TAG, "Found style with id %d", id);
return style;
}
return style;
}
@bhorn -- Method that will try to get a mongodb document by StyleId
StyleDto::ObjectWrapper Database::getStyleById(v_int32 id){
bsoncxx::stdx::optional<bsoncxx::v_noabi::document::value> result =
m_db[COLLECTION_NAME].find_one({document{} << "StyleId" << id << finalize});
auto docView = result->view();
auto style = m_mongomapper->readFromDocument(docView);
if(style) {
OATPP_LOGD(TAG, "Found style with id %d", id);
return style;
}
return style;
}
@bhorn sample json definition of a style document from style collection
{
"StyleId": NumberInt(4400),
"Manufacturer": "Acme Inc",
"Category": "Boys",
"Type": "Footwear",
"Description": "Running shoes"
}
Hello @codderembedded ,
I want to take base64 data, convert it to string and save it in txt file. How can I do it?
#include "oatpp/encoding/Base64.hpp"
...
oatpp::String base64Data = "SGVsbG8gV29ybGQh";
oatpp::String plainData = oatpp::encoding::Base64::decode(base64Data);
plainData->saveToFile("/path/to/file.txt");
How can I convert to std::string from oatpp::String?
oatpp::String oatppString = "Hello World!";
std::string stdString = oatppString->std_str();
ConversionUtils.hpp
- https://github.com/oatpp/oatpp/blob/master/src/oatpp/core/utils/ConversionUtils.hpp#L140
Hello @rrajpaul ,
How do I pass parameter for unit testing for example to test the crud method getUserById
The recommended way to test oatpp controllers is described here.
The basic idea - is that you create an ApiClient
for you ApiController
and then you do regular HTTP calls to your server.
Please let me know if you have more questions.
Regards,
Leonid
Hey @Sanzona ,
Thanks for sharing this issue. We'll have to update the documentation!
Just pass m_objectMapper variable as is without .get()
:
std::shared_ptr<OutgoingResponse> handle(const std::shared_ptr<IncomingRequest>& request) override {
auto message = MessageDto::createShared();
message->statusCode = 1024;
message->message = "Hello DTO!";
return ResponseFactory::createResponse(Status::CODE_200, message, m_objectMapper);
}
oatpp::String json = oatpp::base::StrBuffer::loadFromFile("/path/to/file");
auto myDto = objectMapper->readFromString<MyDto>(json);