-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgreen-serv.c
121 lines (95 loc) · 3.09 KB
/
green-serv.c
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
120
121
#include "config.h"
#include "db.h"
#include "models/scope.h"
#include "json.h"
#include "flags.h"
#include "models/comment.h"
#include "models/marker.h"
#include <string.h>
/* Check for flags. */
void parseArgs(int argc, const char * argv[], struct gs_scope * campaign, MYSQL * conn){
int i;
for(i=0; i < argc; ++i){
if ( strncasecmp(argv[i], SCOPE_ID_SHORT, strlen(SCOPE_ID_SHORT)) == 0 || strncasecmp(argv[i], SCOPE_ID_LONG, strlen(SCOPE_ID_LONG)) == 0)
if ( argc >= i+1 )
db_getScopeById(atol(argv[++i]), campaign,conn);
}
}
int main(int argc, const char* argv[]) {
MYSQL * conn;
struct gs_scope campaign;
struct gs_comment testComment;
struct gs_comment * commentPage;
struct gs_marker testMarker;
struct gs_marker * markerPage;
Decimal latitude;
Decimal longitude;
char json[512];
bzero(json,512);
int numComments;
int numMarkers;
int i;
conn = _getMySQLConnection();
if(!conn){
fprintf(stderr, "%s\n", "Could not connect to mySQL");
return 1;
}
/* Setup Campaign for all querying. */
db_getScopeById(CAMPAIGN_ID, &campaign, conn);
parseArgs(argc,argv,&campaign,conn);
gsScopeToJSON(campaign,json);
printf("%s\n", json);
/* Test comment here */
gs_comment_ZeroStruct(&testComment);
gs_comment_setContent("Test Comment", &testComment);
gs_comment_setScopeId(campaign.id, &testComment);
db_insertComment(&testComment,conn);
bzero(json,512);
gsCommentToJSON(testComment,json);
printf("%s\n", json);
/* test getting comment by id */
db_getCommentById(testComment.id,&testComment,conn);
gsCommentToJSON(testComment,json);
printf("%s\n", json);
commentPage = malloc(RESULTS_PER_PAGE * sizeof(struct gs_comment));
if(commentPage != NULL){
numComments = db_getComments(0, campaign.id,commentPage, conn);
for(i=0; i < numComments; ++i){
bzero(json,512);
gsCommentToJSON(commentPage[i],json);
printf("%s\n", json);
}
free(commentPage);
}else{
fprintf(stderr, "%s\n", "Could not allocate enough memory for comment page");
}
/* Test markers */
createDecimalFromString(&latitude, "-44.50");
createDecimal(-44, 70, &longitude);
gs_marker_ZeroStruct(&testMarker);
gs_marker_setCommentId(testComment.id, &testMarker);
gs_marker_setScopeId(campaign.id, &testMarker);
gs_marker_setLongitude(longitude, &testMarker);
gs_marker_setLatitude(latitude, &testMarker);
db_insertMarker(&testMarker, conn);
gsMarkerToJSON(testMarker, json);
printf("%s\n", json);
db_getMarkerById(testMarker.id, &testMarker, conn);
gsMarkerToJSON(testMarker, json);
printf("%s\n", json);
markerPage = malloc(RESULTS_PER_PAGE * sizeof(struct gs_marker));
if(markerPage != NULL){
numMarkers = db_getMarkers(0, campaign.id, markerPage, conn);
for(i=0; i < numMarkers; ++i){
bzero(json,512);
gsMarkerToJSON(markerPage[i], json);
printf("%s\n", json);
}
free(markerPage);
}else{
fprintf(stderr, "%s\n", "Could not allocate enough memory for marker page");
}
/*Clean Up database connection*/
mysql_close(conn);
mysql_library_end();
}