-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathC2SCalculatorRestMethodPrototype.cpp
119 lines (98 loc) · 4.54 KB
/
C2SCalculatorRestMethodPrototype.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include "C2SCalculatorRestMethodPrototype.h"
#include "C2SHttpEntityStreamerPlainText.h"
#include "DateTimeLogger.h"
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
using boost::multiprecision::cpp_int;
using boost::multiprecision::cpp_dec_float_50;
namespace c2s
{
namespace calculator
{
const std::string C2SCalculatorRestMethodPrototype::sPath = "calculator";
const std::set<std::string> C2SCalculatorRestMethodPrototype::m_mOperations = {"add","sub","div"
,"mul","mod","sqrt"
,"mempls","memmin","memrcl"};
C2SCalculatorRestMethodPrototype::C2SCalculatorRestMethodPrototype()
: C2SRestMethodPrototypeGET<std::string>( sPath ),
m_pLogger( new io::DateTimeLogger( "calculator" ) ),
m_sCNumber(),m_sPNumber(),m_sCMemory(),m_sOperation()
{
C2SRestMethodPrototypeGET<std::string>::installEntityStreamer( new C2SHttpEntityStreamerPlainText() );
C2SRestMethodPrototypeGET<std::string>::addQueryParameter( "cnumber" , &m_sCNumber , "invalid" );
C2SRestMethodPrototypeGET<std::string>::addQueryParameter( "pnumber" , &m_sPNumber , "invalid" );
C2SRestMethodPrototypeGET<std::string>::addQueryParameter( "cmemory" , &m_sCMemory , "invalid" );
C2SRestMethodPrototypeGET<std::string>::addQueryParameter( "operation" , &m_sOperation , "invalid" );
}
C2SCalculatorRestMethodPrototype::~C2SCalculatorRestMethodPrototype()
{
delete m_pLogger;
}
inline bool C2SCalculatorRestMethodPrototype::isInt(std::string &number){
try {
cpp_int number(number);
}
catch (const std::exception& e) {
return false;
}
return true;
}
inline bool C2SCalculatorRestMethodPrototype::isFloat(std::string &number){
try {
cpp_dec_float_50 number(number);
}
catch (const std::exception& e) {
return false;
}
return true;
}
inline bool C2SCalculatorRestMethodPrototype::isNumber(std::string &number){
return isInt(number) & isFloat(number);
}
C2SHttpResponse *C2SCalculatorRestMethodPrototype::process()
{
bool isFloat = false;
if(m_sCNumber.compare("invalid")!=0 && isNumber(m_sCNumber)){
if(!isInt(m_sCNumber)){
isFloat = true;
}
} else {
m_sFinalResponseEntity = std::string("Current number is not specified in query.")+m_sCNumber;
return C2SRestMethodPrototypeGET<std::string>::buildResponse( BadRequest , m_sFinalResponseEntity );
}
if(m_sPNumber.compare("invalid")!=0 && isNumber(m_sPNumber)){
if(!isInt(m_sPNumber)){
isFloat = true;
}
} else {
m_sFinalResponseEntity = std::string("Previous number is not specified in query.");
return C2SRestMethodPrototypeGET<std::string>::buildResponse( BadRequest , m_sFinalResponseEntity );
}
if(m_sCMemory.compare("invalid")==0 || !isNumber(m_sCMemory)){
m_sFinalResponseEntity = std::string("Current memory is not specified in query.");
return C2SRestMethodPrototypeGET<std::string>::buildResponse( BadRequest , m_sFinalResponseEntity );
}
if(m_sOperation.compare("invalid")==0 || m_mOperations.find(m_sOperation) == m_mOperations.end()){
m_sFinalResponseEntity = std::string("Operation not specified in query.");
return C2SRestMethodPrototypeGET<std::string>::buildResponse( BadRequest , m_sFinalResponseEntity );
}
C2SCalculatorController cController(m_sCNumber,m_sPNumber,m_sOperation,m_sCMemory,isFloat);
try{
cController.perform();
m_sFinalResponseEntity = "{\"CurrentNumber\":"+cController.getCurrentNumber()+","
+"\"PreviousNumber\":"+cController.getPreviousNumber()+","
+"\"Operation\":"+"\""+cController.getOperation()+"\""+","
+"\"MemoryValue\":"+cController.getMemoryValue()+
"}";
}catch (const std::exception& e) {
m_sFinalResponseEntity = std::string("Operation failed!");
return C2SRestMethodPrototypeGET<std::string>::buildResponse( BadRequest , m_sFinalResponseEntity );
}
return C2SRestMethodPrototypeGET<std::string>::buildResponse( OK , m_sFinalResponseEntity );
}
C2SCalculatorRestMethodPrototype *C2SCalculatorRestMethodPrototype::clone() const
{
return new C2SCalculatorRestMethodPrototype();
}
}
}