-
Notifications
You must be signed in to change notification settings - Fork 1
/
ShoutoutRESTService.cfc
73 lines (61 loc) · 2.16 KB
/
ShoutoutRESTService.cfc
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
component rest="true" restpath="shoutouts" {
import model.Shoutout;
remote Array function list() httpmethod="GET"
{
var resArray = [];
var shouts = entityLoad('Shoutout');
for(var shout in shouts)
arrayAppend(resArray,shout.toStruct());
return resArray;
}
remote any function find(string searchString restargsource="Path") httpmethod="GET" restpath="find/{searchString}"
{
var searchResult = ORMSearch(searchString&"*",'Shoutout',["content"]);
if(searchResult.MaxTotalCount){
// found some issues serializing the ORMSearch result via XML
// also found some issues
var entities = [];
for(var resEntity in searchResult.data)
{
arrayAppend(entities,resEntity.entity.toStruct());
}
return entities;
}
else{
return [];
}
}
remote any function read(string id restargsource="Path") httpmethod="GET" restpath="{id}"
{
var shout = entityLoad("Shoutout",id,true);
return shout.toStruct();
}
// create a new shoutout from the page form
remote any function create(
required string content restargsource="Form",
required numeric lat restargsource="Form",
required numeric lng restargsource="Form") httpmethod="POST"
{
var contentValid = REFindNoCase(".*",content);
var latValid = REFindNoCase("^-?\d+\.?\d?",lat);
var lngValid = REFindNoCase("^-?\d+\.?\d?",lng);
if(contentValid && latValid && lngValid)
{
var shout = entityNew('Shoutout');
shout.setContent(content);
shout.setLat(lat);
shout.setLng(lng);
entitySave(shout);
ormFlush();
// notify users of new shoutouts!
WSPublish("shoutout",{event='newUserShoutout',shoutout=shout.toStruct()});
return shout.toStruct();
}
else
{
throw(type="InvalidRequest", errorCode='503', detail='Invalid Data');
}
}
// update
// delete
}