|
4 | 4 | import static edu.tamu.framework.enums.BusinessValidationType.CREATE; |
5 | 5 | import static edu.tamu.framework.enums.BusinessValidationType.EXISTS; |
6 | 6 |
|
| 7 | +import java.util.HashMap; |
| 8 | +import java.util.Iterator; |
| 9 | +import java.util.Map; |
| 10 | + |
7 | 11 | import org.springframework.beans.factory.annotation.Autowired; |
| 12 | +import org.springframework.data.domain.Page; |
| 13 | +import org.springframework.data.domain.Sort; |
| 14 | +import org.springframework.data.domain.Sort.Direction; |
8 | 15 | import org.springframework.messaging.simp.SimpMessagingTemplate; |
9 | 16 | import org.springframework.web.bind.annotation.RestController; |
10 | 17 |
|
| 18 | +import com.fasterxml.jackson.databind.JsonNode; |
| 19 | +import com.fasterxml.jackson.databind.node.ArrayNode; |
| 20 | + |
11 | 21 | import edu.tamu.app.model.Note; |
12 | 22 | import edu.tamu.app.model.repo.NoteRepo; |
| 23 | +import edu.tamu.app.model.request.FilteredPageRequest; |
13 | 24 | import edu.tamu.framework.aspect.annotation.ApiCredentials; |
| 25 | +import edu.tamu.framework.aspect.annotation.ApiData; |
14 | 26 | import edu.tamu.framework.aspect.annotation.ApiMapping; |
15 | 27 | import edu.tamu.framework.aspect.annotation.ApiValidatedModel; |
16 | 28 | import edu.tamu.framework.aspect.annotation.ApiValidation; |
@@ -65,4 +77,33 @@ public ApiResponse remove(@ApiValidatedModel Note note) { |
65 | 77 | simpMessagingTemplate.convertAndSend("/channel/note", new ApiResponse(SUCCESS, noteRepo.findAll())); |
66 | 78 | return new ApiResponse(SUCCESS); |
67 | 79 | } |
| 80 | + |
| 81 | + @ApiMapping("/page") |
| 82 | + @Auth(role="ROLE_ANONYMOUS") |
| 83 | + public ApiResponse page(@ApiData JsonNode dataNode) { |
| 84 | + Direction sortDirection; |
| 85 | + if (dataNode.get("direction").get("direction").asText().equals("ASC")) { |
| 86 | + sortDirection = Sort.Direction.ASC; |
| 87 | + } else { |
| 88 | + sortDirection = Sort.Direction.DESC; |
| 89 | + } |
| 90 | + |
| 91 | + Map<String, String[]> filters = new HashMap<String, String[]>(); |
| 92 | + System.out.println("dataNode: " + dataNode.get("filters").get("title")); |
| 93 | + filters.put("title", arrayNodeToStringArray((ArrayNode) dataNode.get("filters").get("title"))); |
| 94 | + |
| 95 | + FilteredPageRequest filteredPageRequest = new FilteredPageRequest(dataNode.get("page").get("number").asInt(), dataNode.get("page").get("size").asInt(), sortDirection, dataNode.get("direction").get("properties").asText(), filters); |
| 96 | + Page<Note> notes = noteRepo.findAll(filteredPageRequest); |
| 97 | + return new ApiResponse(SUCCESS, notes); |
| 98 | + } |
| 99 | + |
| 100 | + private String[] arrayNodeToStringArray(ArrayNode arrayNode) { |
| 101 | + String[] array = new String[arrayNode.size()]; |
| 102 | + Iterator<JsonNode> arrayIterator = arrayNode.elements(); |
| 103 | + int i = 0; |
| 104 | + while (arrayIterator.hasNext()) { |
| 105 | + array[i++] = arrayIterator.next().asText(); |
| 106 | + } |
| 107 | + return array; |
| 108 | + } |
68 | 109 | } |
0 commit comments