44import static edu .tamu .framework .enums .BusinessValidationType .CREATE ;
55import static edu .tamu .framework .enums .BusinessValidationType .EXISTS ;
66
7- import java .util .HashMap ;
8- import java .util .Iterator ;
9- import java .util .Map ;
10-
117import org .springframework .beans .factory .annotation .Autowired ;
128import org .springframework .data .domain .Page ;
13- import org .springframework .data .domain .Sort ;
14- import org .springframework .data .domain .Sort .Direction ;
15- import org .springframework .messaging .simp .SimpMessagingTemplate ;
169import org .springframework .transaction .annotation .Transactional ;
1710import org .springframework .web .bind .annotation .RestController ;
1811
19- import com .fasterxml .jackson .databind . JsonNode ;
20- import com .fasterxml .jackson .databind .node . ArrayNode ;
12+ import com .fasterxml .jackson .core . JsonProcessingException ;
13+ import com .fasterxml .jackson .databind .ObjectMapper ;
2114
2215import edu .tamu .app .model .Note ;
16+ import edu .tamu .app .model .Service ;
2317import edu .tamu .app .model .repo .NoteRepo ;
24- import edu .tamu .app .model .repo .ServiceRepo ;
2518import edu .tamu .app .model .request .FilteredPageRequest ;
2619import edu .tamu .framework .aspect .annotation .ApiCredentials ;
2720import edu .tamu .framework .aspect .annotation .ApiData ;
2821import edu .tamu .framework .aspect .annotation .ApiMapping ;
22+ import edu .tamu .framework .aspect .annotation .ApiModel ;
2923import edu .tamu .framework .aspect .annotation .ApiValidatedModel ;
3024import edu .tamu .framework .aspect .annotation .ApiValidation ;
3125import edu .tamu .framework .aspect .annotation .ApiVariable ;
@@ -39,20 +33,23 @@ public class NoteController {
3933
4034 @ Autowired
4135 private NoteRepo noteRepo ;
42-
43- @ Autowired
44- private ServiceRepo serviceRepo ;
4536
4637 @ Autowired
47- private SimpMessagingTemplate simpMessagingTemplate ;
38+ private ObjectMapper objectMapper ;
4839
4940 @ ApiMapping ("/all" )
5041 @ Auth (role = "ROLE_ANONYMOUS" )
5142 public ApiResponse getAllNotes () {
5243 return new ApiResponse (SUCCESS , noteRepo .findAll ());
5344 }
5445
55- @ ApiMapping ("/get/{id}" )
46+ @ ApiMapping ("/by-service" )
47+ @ Auth (role = "ROLE_ANONYMOUS" )
48+ public ApiResponse getAllNotesByService (@ ApiModel Service service ) {
49+ return new ApiResponse (SUCCESS , noteRepo .findAllByService (service ));
50+ }
51+
52+ @ ApiMapping ("/{id}" )
5653 @ Auth (role = "ROLE_ANONYMOUS" )
5754 public ApiResponse getNote (@ ApiVariable Long id ) {
5855 return new ApiResponse (SUCCESS , noteRepo .findOne (id ));
@@ -63,53 +60,27 @@ public ApiResponse getNote(@ApiVariable Long id) {
6360 @ ApiValidation (business = { @ ApiValidation .Business (value = CREATE ), @ ApiValidation .Business (value = EXISTS ) })
6461 public ApiResponse create (@ ApiValidatedModel Note note , @ ApiCredentials Credentials credentials ) {
6562 note = noteRepo .create (note , credentials );
66- ApiResponse response = new ApiResponse (SUCCESS , note );
67- simpMessagingTemplate .convertAndSend ("/channel/note/new" , response );
68- return response ;
63+ return new ApiResponse (SUCCESS , note );
6964 }
7065
7166 @ ApiMapping ("/update" )
7267 @ Auth (role = "ROLE_SERVICE_MANAGER" )
7368 public ApiResponse update (@ ApiValidatedModel Note note ) {
74- note = noteRepo .save (note );
75- ApiResponse response = new ApiResponse (SUCCESS , noteRepo .getOne (note .getId ()));
76- simpMessagingTemplate .convertAndSend ("/channel/note/" + note .getId (), response );
77- return response ;
69+ return new ApiResponse (SUCCESS , noteRepo .update (note ));
7870 }
7971
8072 @ Transactional
8173 @ ApiMapping ("/remove" )
8274 @ Auth (role = "ROLE_SERVICE_MANAGER" )
8375 public ApiResponse remove (@ ApiValidatedModel Note note ) {
8476 noteRepo .delete (note );
85- simpMessagingTemplate .convertAndSend ("/channel/service" + note .getService ().getId (), new ApiResponse (SUCCESS , serviceRepo .getOne (note .getService ().getId ())));
8677 return new ApiResponse (SUCCESS );
8778 }
88-
79+
8980 @ ApiMapping ("/page" )
90- @ Auth (role ="ROLE_ANONYMOUS" )
91- public ApiResponse page (@ ApiData JsonNode dataNode ) {
92- Direction sortDirection ;
93- if (dataNode .get ("direction" ).get ("direction" ).asText ().equals ("ASC" )) {
94- sortDirection = Sort .Direction .ASC ;
95- } else {
96- sortDirection = Sort .Direction .DESC ;
97- }
98-
99- Map <String , String []> filters = new HashMap <String , String []>();
100- filters .put ("title" , arrayNodeToStringArray ((ArrayNode ) dataNode .get ("filters" ).get ("title" )));
101- FilteredPageRequest filteredPageRequest = new FilteredPageRequest (dataNode .get ("page" ).get ("number" ).asInt (), dataNode .get ("page" ).get ("size" ).asInt (), sortDirection , dataNode .get ("direction" ).get ("properties" ).asText (), filters );
102- Page <Note > notes = noteRepo .findAll (filteredPageRequest );
103- return new ApiResponse (SUCCESS , notes );
104- }
105-
106- private String [] arrayNodeToStringArray (ArrayNode arrayNode ) {
107- String [] array = new String [arrayNode .size ()];
108- Iterator <JsonNode > arrayIterator = arrayNode .elements ();
109- int i = 0 ;
110- while (arrayIterator .hasNext ()) {
111- array [i ++] = arrayIterator .next ().asText ();
112- }
113- return array ;
81+ @ Auth (role = "ROLE_ANONYMOUS" )
82+ public ApiResponse page (@ ApiData FilteredPageRequest filteredPageRequest ) {
83+ return new ApiResponse (SUCCESS , noteRepo .findAll (filteredPageRequest .toPageRequest ()));
11484 }
85+
11586}
0 commit comments