Skip to content

Commit 145ab62

Browse files
committed
Articles pageable and Postman article file -> Done by Ivan
1 parent becdd86 commit 145ab62

File tree

2 files changed

+274
-0
lines changed

2 files changed

+274
-0
lines changed

Article.postman_collection.json

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
{
2+
"info": {
3+
"_postman_id": "dca10921-3263-431d-8938-99309ef2bc48",
4+
"name": Article
5+
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
6+
},
7+
"item": [
8+
{
9+
"name": "https://localhost:8443/api/articles",
10+
"request": {
11+
"method": "GET",
12+
"header": [],
13+
"url": {
14+
"raw": "https://localhost:8443/api/articles",
15+
"protocol": "https",
16+
"host": [
17+
"localhost"
18+
],
19+
"port": "8443",
20+
"path": [
21+
"api",
22+
"articles"
23+
]
24+
}
25+
},
26+
"response": []
27+
},
28+
{
29+
"name": "https://localhost:8443/api/articles/6",
30+
"request": {
31+
"method": "GET",
32+
"header": [],
33+
"url": {
34+
"raw": "https://localhost:8443/api/articles/6",
35+
"protocol": "https",
36+
"host": [
37+
"localhost"
38+
],
39+
"port": "8443",
40+
"path": [
41+
"api",
42+
"articles",
43+
"6"
44+
]
45+
}
46+
},
47+
"response": []
48+
},
49+
{
50+
"name": "https://localhost:8443/api/articles",
51+
"request": {
52+
"method": "POST",
53+
"header": [],
54+
"body": {
55+
"mode": "raw",
56+
"raw": "{\r\n \"title\": \"pulsera\",\r\n \"description\": \"Las mejores pulseras del mundo\",\r\n \"price\": \"9,99\",\r\n \"city\": \"Barcelona\",\r\n \"postal_code\": \"28754\",\r\n}",
57+
"options": {
58+
"raw": {
59+
"language": "json"
60+
}
61+
}
62+
},
63+
"url": {
64+
"raw": "https://localhost:8443/api/articles",
65+
"protocol": "https",
66+
"host": [
67+
"localhost"
68+
],
69+
"port": "8443",
70+
"path": [
71+
"api",
72+
"articles"
73+
]
74+
}
75+
},
76+
"response": []
77+
},
78+
{
79+
"name": "https://localhost:844/api/articles/8",
80+
"request": {
81+
"method": "PUT",
82+
"header": [],
83+
"body": {
84+
"mode": "raw",
85+
"raw": "{\r\n \"title\": \"Llaves allen\",\r\n \"description\": \"Llaves allen especiales\",\r\n \"price\": \"29,99\",\r\n \"city\": \"Islas Canarias\",\r\n \"postal_code\": \"20923\",\r\n}",
86+
"options": {
87+
"raw": {
88+
"language": "json"
89+
}
90+
}
91+
},
92+
"url": {
93+
"raw": "https://localhost:8443/api/articles/8",
94+
"protocol": "https",
95+
"host": [
96+
"localhost"
97+
],
98+
"port": "8443",
99+
"path": [
100+
"api",
101+
"articles",
102+
"8"
103+
]
104+
}
105+
},
106+
"response": []
107+
},
108+
{
109+
"name": "https://localhost:8443/api/articles/8",
110+
"request": {
111+
"method": "DELETE",
112+
"header": [],
113+
"url": {
114+
"raw": "https://localhost:8443/api/articles/8",
115+
"protocol": "https",
116+
"host": [
117+
"localhost"
118+
],
119+
"port": "8443",
120+
"path": [
121+
"api",
122+
"articles",
123+
"8"
124+
]
125+
}
126+
},
127+
"response": []
128+
},
129+
{
130+
"name": "https://localhost:8443/api/articles/7/image",
131+
"request": {
132+
"method": "GET",
133+
"header": [],
134+
"url": {
135+
"raw": "https://localhost:8443/api/articles/7/image",
136+
"protocol": "https",
137+
"host": [
138+
"localhost"
139+
],
140+
"port": "8443",
141+
"path": [
142+
"api",
143+
"articles",
144+
"7",
145+
"image"
146+
]
147+
}
148+
},
149+
"response": []
150+
},
151+
{
152+
"name": "https://localhost:8443/api/articles/7/image",
153+
"request": {
154+
"method": "DELETE",
155+
"header": [],
156+
"url": {
157+
"raw": "https://localhost:8443/api/articles/7/image",
158+
"protocol": "https",
159+
"host": [
160+
"localhost"
161+
],
162+
"port": "8443",
163+
"path": [
164+
"api",
165+
"articles",
166+
"7",
167+
"image"
168+
]
169+
}
170+
},
171+
"response": []
172+
},
173+
{
174+
"name": "https://localhost:8443/api/articles/7/image",
175+
"request": {
176+
"method": "POST",
177+
"header": [],
178+
"body": {
179+
"mode": "formdata",
180+
"formdata": [
181+
{
182+
"key": "imageFile",
183+
"type": "file",
184+
"src": "/C:/Users/Usuario/Desktop/DAW/DESPERDICIOS/linterna.jpg"
185+
}
186+
]
187+
},
188+
"url": {
189+
"raw": "https://localhost:8443/api/articles/7/image",
190+
"protocol": "https",
191+
"host": [
192+
"localhost"
193+
],
194+
"port": "8443",
195+
"path": [
196+
"api",
197+
"articles",
198+
"7",
199+
"image"
200+
]
201+
}
202+
},
203+
"response": []
204+
}
205+
]
206+
}

backend/src/main/java/es/codeurjc/wallypop/controller/api/ArticleRestController.java

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,74 @@ List<Article> all() {
5353
return articleService.findAll();
5454
}
5555

56+
@GetMapping(params = {"page"})
57+
public ResponseEntity<Map<String, Object>> articlesPagination(HttpServletRequest request,@RequestParam("page") int page) {
58+
if (page != -1) { // with pagination
59+
int pageSize = 10;
60+
Principal principal = request.getUserPrincipal();
61+
if(principal != null) {
62+
User user = userService.findByNAME(principal.getName()).get();
63+
try {
64+
65+
Map<String, Object> response = new HashMap<>();
66+
List<String> articles_info = new LinkedList<String>();
67+
Pageable paging = (Pageable) PageRequest.of(0, pageSize);
68+
Page<Article> pageTuts;
69+
70+
pageTuts = articleService.findByUSERS(user, paging.withPage(page));
71+
if(pageTuts.getNumberOfElements() == 0) {
72+
articles_info.add("Empty");
73+
}else {
74+
for (int i = 0; i < pageTuts.getNumberOfElements();i++) {
75+
articles_info.add(pageTuts.getContent().get(i).toString());
76+
}
77+
}
78+
response.put("totalPages", pageTuts.getTotalPages());
79+
response.put("currentPage", page);
80+
response.put("totalItemsUser", pageTuts.getTotalElements());
81+
response.put("totalItemsThisPage", pageTuts.getNumberOfElements());
82+
response.put("articles", articles_info);
83+
84+
85+
86+
return new ResponseEntity<>(response, HttpStatus.OK);
87+
} catch (Exception e) {
88+
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
89+
}
90+
} else {
91+
return ResponseEntity.notFound().build();
92+
}
93+
}else { // without pagination
94+
Principal principal = request.getUserPrincipal();
95+
if(principal != null) {
96+
User user = userService.findByNAME(principal.getName()).get();
97+
try {
98+
List<String> articles_info = new LinkedList<String>();
99+
100+
if(user.getARTICLES().size() == 0) {
101+
articles_info.add("Empty");
102+
}else {
103+
for (int i = 0; i < user.getARTICLES().size();i++) {
104+
articles_info.add(user.getARTICLES().get(i).toString());
105+
}
106+
}
107+
108+
Map<String, Object> response = new HashMap<>();
109+
110+
response.put("totalItemsUser", articles_info.size());
111+
response.put("articles", articles_info);
112+
113+
return new ResponseEntity<>(response, HttpStatus.OK);
114+
} catch (Exception e) {
115+
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
116+
}
117+
} else {
118+
return ResponseEntity.notFound().build();
119+
}
120+
}
121+
122+
}
123+
56124
@GetMapping("/me")
57125
public ResponseEntity<List<Article>> me(HttpServletRequest request) {
58126
Principal principal = request.getUserPrincipal();

0 commit comments

Comments
 (0)