-
Notifications
You must be signed in to change notification settings - Fork 4
/
EventController.java
95 lines (79 loc) · 3.56 KB
/
EventController.java
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
package com.niit.controller;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.niit.dao.EventDAO;
import com.niit.model.Event;
@RestController
public class EventController {
private static final Logger logger = LoggerFactory.getLogger(EventController.class);
@Autowired
EventDAO eventDAO;
@RequestMapping(value="/events",method=RequestMethod.GET)
public ResponseEntity<List<Event>> listAllEvents(){
logger.debug("calling method listAllEvents");
List<Event> event=eventDAO.list();
if(event.isEmpty()){
return new ResponseEntity<List<Event>>(HttpStatus.NO_CONTENT);
}
return new ResponseEntity<List<Event>>(event,HttpStatus.OK);
}
@RequestMapping(value="/event/",method=RequestMethod.POST)
public ResponseEntity<Event> createEvent(@RequestBody Event event){
logger.debug("calling method createEvent" + event.getEvent_id());
if(eventDAO.get(event.getEvent_id())==null){
eventDAO.save(event);
}
logger.debug("event already exists with id:" + event.getEvent_id());
event.setErrorMessage("event already exists with id:" + event.getEvent_id());
return new ResponseEntity<Event>(event,HttpStatus.OK);
}
@RequestMapping(value="/event/{id}",method=RequestMethod.PUT)
public ResponseEntity<Event> updateEvent(@PathVariable("id") int event_id,@RequestBody Event event){
logger.debug("calling method updateEvent" + event.getEvent_id());
if(eventDAO.get(event_id)==null){
logger.debug("event does not exists with id:" + event.getEvent_id());
event=new Event();
event.setErrorMessage("event does not exists with id:" + event.getEvent_id());
return new ResponseEntity<Event> (event,HttpStatus.NOT_FOUND);
}
eventDAO.update(event);
logger.debug("event updated successfully");
return new ResponseEntity<Event> (event,HttpStatus.OK);
}
@RequestMapping(value="/event/{id}",method=RequestMethod.DELETE)
public ResponseEntity<Event> deleteEvent(@PathVariable("id") int event_id){
logger.debug("calling method deleteEvent for event id: " + event_id);
Event event=eventDAO.get(event_id);
if(event==null){
logger.debug("event does not exists with id:" + event_id);
event=new Event();
event.setErrorMessage("event does not exists with id:" + event_id);
return new ResponseEntity<Event> (event,HttpStatus.NOT_FOUND);
}
eventDAO.delete(event_id);
logger.debug("event deleted successfully");
return new ResponseEntity<Event> (event,HttpStatus.OK);
}
@RequestMapping(value="/event/{id}",method=RequestMethod.GET)
public ResponseEntity<Event> getEvent(@PathVariable("id") int event_id){
logger.debug("calling method getEvent for event id: " + event_id);
Event event=eventDAO.get(event_id);
if(event==null){
logger.debug("event does not exists with id:" + event_id);
event=new Event();
event.setErrorMessage("event does not exists with id:" + event_id);
return new ResponseEntity<Event> (event,HttpStatus.NOT_FOUND);
}
logger.debug("event exists with id:" + event_id);
return new ResponseEntity<Event> (event,HttpStatus.OK);
}
}