Skip to content

Commit 4470172

Browse files
committed
Add discounts delete functionality
1 parent 426c5d5 commit 4470172

3 files changed

Lines changed: 285 additions & 186 deletions

File tree

client_demo/netlicensing_client_demo.cc

Lines changed: 97 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -6,63 +6,101 @@
66
#include "netlicensing/constants.h"
77

88
int main(int argc, char* argv[]) {
9-
using namespace netlicensing;
10-
11-
std::string licensee_number = "IR2Q7A5P3";
12-
if (argc > 1) {
13-
licensee_number = argv[1];
14-
}
15-
16-
std::mt19937 gen;
17-
gen.seed(time(0));
18-
std::stringstream ss;
19-
ss << "P" << gen();
20-
std::string productNumber = ss.str();
21-
22-
std::cout << "Hello, this is NetLicensing demo client\n";
23-
std::cout << "Product endpoint " << endpoint<Product>() << std::endl;
24-
std::cout << "Product test number " << productNumber << std::endl;
25-
26-
try {
27-
Context ctx;
28-
ctx.set_base_url("https://go.netlicensing.io/core/v2/rest/");
29-
ctx.set_username("demo");
30-
ctx.set_password("demo");
31-
32-
// product section
33-
Product p;
34-
p.setName("Test name");
35-
p.setNumber(productNumber);
36-
Product newp = ProductService::create(ctx, p);
37-
38-
newp.setName("Updated name");
39-
Product newp2 = ProductService::update(ctx, newp.getNumber(), newp);
40-
41-
std::list<Product> products = ProductService::list(ctx, "");
42-
std::cout << "before delete products count " << products.size() << std::endl;
43-
44-
ProductService::del(ctx, newp2.getNumber(), false);
45-
46-
products = ProductService::list(ctx, "");
47-
std::cout << "after delete products count " << products.size() << std::endl;
48-
49-
if (!licensee_number.empty()) {
50-
std::cout << "start validation for " << licensee_number << std::endl;
51-
ValidationResult vres = LicenseeService::validate(ctx, licensee_number);
52-
std::cout << "got validation results:\n" << vres.toString() << std::endl;
53-
}
54-
}
55-
catch (const RestException& e) {
56-
std::cerr << e.what() << " code " << e.http_code() << std::endl;
57-
for (auto det : e.get_details()) {
58-
std::cerr << det.to_string() << std::endl;
59-
}
60-
return 2;
61-
}
62-
catch (const std::runtime_error& err) {
63-
std::cerr << err.what() << std::endl;
64-
return 1;
65-
}
66-
67-
return 0;
9+
using namespace netlicensing;
10+
11+
std::string licensee_number = "IR2Q7A5P3";
12+
if (argc > 1) {
13+
licensee_number = argv[1];
14+
}
15+
16+
std::mt19937 gen;
17+
gen.seed(time(0));
18+
std::stringstream ss;
19+
ss << "P" << gen();
20+
std::string productNumber = ss.str();
21+
22+
std::cout << "Hello, this is NetLicensing demo client\n";
23+
std::cout << "Product endpoint " << endpoint<Product>() << std::endl;
24+
std::cout << "Product test number " << productNumber << std::endl;
25+
26+
try {
27+
Context ctx;
28+
ctx.set_base_url("https://go.netlicensing.io/core/v2/rest/");
29+
ctx.set_username("demo");
30+
ctx.set_password("demo");
31+
32+
// product section
33+
Product p;
34+
p.setName("Test name");
35+
p.setNumber(productNumber);
36+
37+
std::list<ProductDiscount> discounts;
38+
ProductDiscount discount;
39+
ProductDiscount discount2;
40+
41+
discount.setTotalPrice("20");
42+
discount.setCurrency("EUR");
43+
discount.setAmountFix("10");
44+
45+
discount2.setTotalPrice("25");
46+
discount2.setCurrency("EUR");
47+
discount2.setAmountPercent("10");
48+
49+
discounts.push_back(discount);
50+
discounts.push_back(discount2);
51+
52+
p.setProductsDiscounts(discounts);
53+
54+
Product newp = ProductService::create(ctx, p);
55+
56+
std::list<ProductDiscount> newpDiscounts = newp.getDiscounts();
57+
58+
std::cout << "product disounts size: " << newpDiscounts.size() << std::endl;
59+
60+
int i = 0;
61+
for (ProductDiscount newpDiscount : newpDiscounts) {
62+
std::string newpDiscountStr = newpDiscount.toString();
63+
std::cout << "product disount #" << i << ":" << newpDiscountStr << std::endl;
64+
i++;
65+
}
66+
67+
newp.setName("Updated name");
68+
69+
std::list<ProductDiscount> discountsEmpty;
70+
newp.setProductsDiscounts(discountsEmpty);
71+
std::cin.ignore();
72+
Product newp2 = ProductService::update(ctx, newp.getNumber(), newp);
73+
74+
std::list<ProductDiscount> newp2Discounts = newp2.getDiscounts();
75+
76+
std::cout << "product disounts size " << newp2Discounts.size() << std::endl;
77+
std::cin.ignore();
78+
std::list<Product> products = ProductService::list(ctx, "");
79+
std::cout << "before delete products count " << products.size() << std::endl;
80+
81+
ProductService::del(ctx, newp2.getNumber(), false);
82+
83+
products = ProductService::list(ctx, "");
84+
std::cout << "after delete products count " << products.size() << std::endl;
85+
86+
if (!licensee_number.empty()) {
87+
std::cout << "start validation for " << licensee_number << std::endl;
88+
ValidationResult vres = LicenseeService::validate(ctx, licensee_number);
89+
std::cout << "got validation results:\n" << vres.toString() << std::endl;
90+
}
91+
}
92+
catch (const RestException& e) {
93+
std::cerr << e.what() << " code " << e.http_code() << std::endl;
94+
for (auto det : e.get_details()) {
95+
std::cerr << det.to_string() << std::endl;
96+
}
97+
return 2;
98+
}
99+
catch (const std::runtime_error& err) {
100+
std::cerr << err.what() << std::endl;
101+
std::cin.ignore();
102+
return 1;
103+
}
104+
105+
return 0;
68106
}

include/netlicensing/converters.h

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,43 +9,49 @@
99

1010
namespace netlicensing {
1111

12-
template<typename T>
13-
parameters_type toParametersList(T value);
14-
15-
template<>
16-
inline parameters_type toParametersList<BaseEntity>(BaseEntity value) {
17-
parameters_type params;
18-
params.push_back(std::make_pair(NUMBER, value.getNumber()));
19-
params.push_back(std::make_pair(ACTIVE, value.getActive().toString()));
20-
for (const auto& prop : value.getProperties()) {
21-
params.push_back(prop);
22-
}
23-
return params;
24-
}
25-
26-
template<>
27-
inline parameters_type toParametersList<Product>(Product value) {
28-
parameters_type params = toParametersList<BaseEntity>(value);
29-
params.push_back(std::make_pair(NAME, value.getName()));
30-
params.push_back(std::make_pair(VERSION, value.getVersion()));
31-
params.push_back(std::make_pair(LICENSEE_AUTOCREATE, value.getLicenseeAutoCreate().toString()));
32-
params.push_back(std::make_pair(DESCRIPTION, value.getDescription()));
33-
params.push_back(std::make_pair(LICENSING_INFO, value.getLicensingInfo()));
34-
// TODO(2K): convert discounts
35-
/*
36-
for (const auto& discount : value.getDiscounts()) {
37-
// ...
38-
}
39-
*/
40-
return params;
41-
}
42-
43-
template<>
44-
inline parameters_type toParametersList<Licensee>(Licensee value) {
45-
parameters_type params = toParametersList<BaseEntity>(value);
46-
params.push_back(std::make_pair(NAME, value.getName()));
47-
return params;
48-
}
12+
template<typename T>
13+
parameters_type toParametersList(T value);
14+
15+
template<>
16+
inline parameters_type toParametersList<BaseEntity>(BaseEntity value) {
17+
parameters_type params;
18+
params.push_back(std::make_pair(NUMBER, value.getNumber()));
19+
params.push_back(std::make_pair(ACTIVE, value.getActive().toString()));
20+
for (const auto& prop : value.getProperties()) {
21+
params.push_back(prop);
22+
}
23+
return params;
24+
}
25+
26+
template<>
27+
inline parameters_type toParametersList<Product>(Product value) {
28+
parameters_type params = toParametersList<BaseEntity>(value);
29+
params.push_back(std::make_pair(NAME, value.getName()));
30+
params.push_back(std::make_pair(VERSION, value.getVersion()));
31+
params.push_back(std::make_pair(LICENSEE_AUTOCREATE, value.getLicenseeAutoCreate().toString()));
32+
params.push_back(std::make_pair(DESCRIPTION, value.getDescription()));
33+
params.push_back(std::make_pair(LICENSING_INFO, value.getLicensingInfo()));
34+
35+
if (!value.getDiscounts().empty()) {
36+
for (ProductDiscount discount : value.getDiscounts()) {
37+
params.push_back(std::make_pair(DISCOUNT, discount.toString()));
38+
}
39+
}
40+
else {
41+
if (value.isProductDiscountsToched()) {
42+
params.push_back(std::make_pair(DISCOUNT, ""));
43+
}
44+
}
45+
46+
return params;
47+
}
48+
49+
template<>
50+
inline parameters_type toParametersList<Licensee>(Licensee value) {
51+
parameters_type params = toParametersList<BaseEntity>(value);
52+
params.push_back(std::make_pair(NAME, value.getName()));
53+
return params;
54+
}
4955

5056
} // namespace netlicensing
5157

0 commit comments

Comments
 (0)