/
ChannelResource.groovy
94 lines (68 loc) · 2.1 KB
/
ChannelResource.groovy
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
package org.fogbeam.neddick.jaxrs
import static org.grails.jaxrs.response.Responses.*
import groovy.json.JsonSlurper
import java.text.SimpleDateFormat
import javax.ws.rs.Consumes
import javax.ws.rs.GET
import javax.ws.rs.POST
import javax.ws.rs.PUT
import javax.ws.rs.Path
import javax.ws.rs.PathParam
import javax.ws.rs.Produces
import javax.ws.rs.core.Response
import org.fogbeam.neddick.Channel
import org.fogbeam.neddick.jaxrs.collection.ChannelCollection
@Path('/api/channel')
@Consumes('application/json')
class ChannelResource
{
def channelService;
def userService;
// 2017-07-12T00:06:54.833Z
SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd'T'HH:MM:ss.SSS'Z'" );
@GET
@Produces('application/json')
public ChannelCollection getChannels()
{
ChannelCollection collection = new ChannelCollection();
List<Channel> channels = channelService.getAllChannels();
collection.addAll(channels);
return collection;
}
@GET
@Path('/{id}')
@Produces('application/json')
public Channel getChannel( @PathParam("id") long id )
{
Channel channel = channelService.findById( id );
return channel;
}
@PUT
@Produces('text/plain')
@Consumes('application/json')
public Response createChannel( final String inputData )
{
println "inputData: \n ${inputData}";
log.info("received data: \n" + inputData );
JsonSlurper jsonSlurper = new JsonSlurper();
def jsonObject = jsonSlurper.parseText(inputData);
Channel newChannel = new Channel();
newChannel.uuid = jsonObject.uuid;
newChannel.name = jsonObject.name;
newChannel.description = jsonObject.description;
String dateCreated = jsonObject.dateCreated;
log.info( "dateCreated: ${dateCreated}");
newChannel.dateCreated = sdf.parse( dateCreated );
newChannel.privateChannel = Boolean.parseBoolean(jsonObject.privateChannel);
newChannel.owner = userService.findUserByUserId( jsonObject.userId);
channelService.save( newChannel );
ok( "OK" );
}
@POST
@Produces('text/plain')
@Consumes('application/json')
public Response updateChannel( String inputData )
{
ok( "OK" );
}
}