|
| 1 | +import { Injectable } from '@angular/core'; |
| 2 | +import { HttpClient } from '@angular/common/http'; |
| 3 | + |
| 4 | +import { News } from '../models/new.model'; |
| 5 | +import { Observable, throwError } from 'rxjs'; |
| 6 | +import { catchError } from 'rxjs/operators'; |
| 7 | + |
| 8 | +const URL = '/api/news/'; |
| 9 | + |
| 10 | +@Injectable({ providedIn: 'root' }) |
| 11 | +export class NewsService { |
| 12 | + |
| 13 | + constructor(private httpClient: HttpClient) { } |
| 14 | + |
| 15 | + |
| 16 | + /*@GetMapping("/") |
| 17 | + public List<News> getNews() { |
| 18 | +
|
| 19 | + return ns.findAll(); |
| 20 | + }*/ |
| 21 | + |
| 22 | + getNews(): Observable<News[]> { |
| 23 | + return this.httpClient.get(URL).pipe( |
| 24 | + //catchError(error => this.handleError(error)) |
| 25 | + ) as Observable<News[]>; |
| 26 | + } |
| 27 | + |
| 28 | + /*@GetMapping("/pages") |
| 29 | + public Page<News> findNewsPage(@RequestParam("page") int page) { |
| 30 | +
|
| 31 | + int size = 9; |
| 32 | + Page<News> news = ns.findAll(PageRequest.of(page, size)); |
| 33 | +
|
| 34 | + return news; |
| 35 | + }*/ |
| 36 | + |
| 37 | + findNewsPage() { |
| 38 | + return this.httpClient.get(URL + "pages"); |
| 39 | + } |
| 40 | + |
| 41 | + /*@GetMapping("/{id}") |
| 42 | + public ResponseEntity<News> getNew(@PathVariable long id) { |
| 43 | + Optional<News> newx = ns.findById(id); |
| 44 | + if (newx.isPresent()) { |
| 45 | + News ns = newx.get(); |
| 46 | + return new ResponseEntity<>(ns, HttpStatus.OK); |
| 47 | + } else { |
| 48 | + return new ResponseEntity<>(HttpStatus.NOT_FOUND); |
| 49 | + } |
| 50 | +
|
| 51 | + }*/ |
| 52 | + |
| 53 | + //getNewById |
| 54 | + getNew(id: number): Observable<News> { |
| 55 | + return this.httpClient.get(URL + id).pipe( |
| 56 | + //catchError(error => this.handleError(error)) |
| 57 | + ) as Observable<News>; |
| 58 | + } |
| 59 | + |
| 60 | + /*@PostMapping("/") |
| 61 | + @ResponseStatus(HttpStatus.CREATED) |
| 62 | + public ResponseEntity<News> createVideogame(@RequestBody News news) { |
| 63 | +
|
| 64 | + ns.save(news); |
| 65 | +
|
| 66 | + URI location = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}").buildAndExpand(news.getId()) |
| 67 | + .toUri(); |
| 68 | +
|
| 69 | + return ResponseEntity.created(location).body(news); |
| 70 | +
|
| 71 | + }*/ |
| 72 | + |
| 73 | + //addNew |
| 74 | + createNew(news: News) { |
| 75 | + |
| 76 | + if (!news.id) { //if dont exists, this method creates a new book |
| 77 | + return this.httpClient.post(URL, news) |
| 78 | + .pipe( |
| 79 | + //catchError(error => this.handleError(error)) |
| 80 | + ); |
| 81 | + } else { //else, if exists, this method update the existing book |
| 82 | + return this.httpClient.put(URL + news.id, news).pipe( //put(location, object to be located in the location) (en lo primero mete lo segundo, vamos) |
| 83 | + //catchError(error => this.handleError(error)) |
| 84 | + ); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + /* |
| 89 | + @PutMapping("/{id}") |
| 90 | + public ResponseEntity<News> updateNew(@PathVariable long id, @RequestBody News updatedNew) |
| 91 | + throws SQLException { |
| 92 | +
|
| 93 | + if (ns.exist(id)) { |
| 94 | + if (updatedNew.getImage()) { |
| 95 | + News newx = ns.findById(id).orElseThrow(); |
| 96 | + if (newx.getImage()) { |
| 97 | + updatedNew.setImageFile(BlobProxy.generateProxy(newx.getImageFile().getBinaryStream(), |
| 98 | + newx.getImageFile().length())); |
| 99 | + } |
| 100 | + } |
| 101 | +
|
| 102 | + updatedNew.setId(id); |
| 103 | + ns.save(updatedNew); |
| 104 | +
|
| 105 | + return new ResponseEntity<>(updatedNew, HttpStatus.OK); |
| 106 | + } else { |
| 107 | + return new ResponseEntity<>(HttpStatus.NOT_FOUND); |
| 108 | + } |
| 109 | + } |
| 110 | + */ |
| 111 | + |
| 112 | + updateNew(news: News){ |
| 113 | + return this.httpClient.put(URL + news.id, news).pipe( |
| 114 | + //catchError(error => this.handleError(error)) |
| 115 | + ); |
| 116 | + } |
| 117 | + |
| 118 | + /* |
| 119 | + // Deletes a new |
| 120 | + @DeleteMapping("/{id}") |
| 121 | + public ResponseEntity<News> deleteNew(@PathVariable long id) { |
| 122 | +
|
| 123 | + try { |
| 124 | + ns.delete(id); |
| 125 | + return new ResponseEntity<>(null, HttpStatus.OK); |
| 126 | +
|
| 127 | + } catch (EmptyResultDataAccessException e) { |
| 128 | + return new ResponseEntity<>(null, HttpStatus.NOT_FOUND); |
| 129 | + } |
| 130 | + } |
| 131 | + */ |
| 132 | + |
| 133 | + deleteNew(news: News) { |
| 134 | + return this.httpClient.delete(URL + news.id).pipe( |
| 135 | + //catchError(error => this.handleError(error)) |
| 136 | + ); |
| 137 | + } |
| 138 | + |
| 139 | + /* |
| 140 | + // Uploads an image to new image |
| 141 | + @PostMapping("/{id}/image") |
| 142 | + public ResponseEntity<News> uploadNewImage(@PathVariable long id, |
| 143 | + @RequestParam MultipartFile imageFile) |
| 144 | + throws IOException { |
| 145 | +
|
| 146 | + News newx = ns.findById(id).orElseThrow(); |
| 147 | +
|
| 148 | + URI location = ServletUriComponentsBuilder.fromCurrentRequest().build().toUri(); |
| 149 | +
|
| 150 | + newx.setImage(true); |
| 151 | + newx.setImageFile(BlobProxy.generateProxy(imageFile.getInputStream(), imageFile.getSize())); |
| 152 | + ns.save(newx); |
| 153 | +
|
| 154 | + return ResponseEntity.created(location).build(); |
| 155 | + } |
| 156 | + */ |
| 157 | + |
| 158 | + uploadNewImage(news: News, formData: FormData) { |
| 159 | + return this.httpClient.post(URL + news.id + '/image', formData) |
| 160 | + .pipe( |
| 161 | + //catchError(error => this.handleError(error)) |
| 162 | + ); |
| 163 | + } |
| 164 | + |
| 165 | + /* |
| 166 | + // Downloads an image of new image |
| 167 | + @GetMapping("/{id}/image") |
| 168 | + public ResponseEntity<Object> downloadNewImage(@PathVariable long id) throws SQLException { |
| 169 | +
|
| 170 | + News newx = ns.findById(id).orElseThrow(); |
| 171 | +
|
| 172 | + if (newx.getImageFile() != null) { |
| 173 | +
|
| 174 | + Resource file = new InputStreamResource(newx.getImageFile().getBinaryStream()); |
| 175 | +
|
| 176 | + return ResponseEntity.ok().header(HttpHeaders.CONTENT_TYPE, "image/jpeg") |
| 177 | + .contentLength(newx.getImageFile().length()).body(file); |
| 178 | +
|
| 179 | + } else { |
| 180 | + return ResponseEntity.notFound().build(); |
| 181 | + } |
| 182 | + } |
| 183 | + */ |
| 184 | + |
| 185 | + downloadNewImage(news: News) { |
| 186 | + return this.httpClient.get(URL + news.id + '/image') |
| 187 | + .pipe( |
| 188 | + //catchError(error => this.handleError(error)) |
| 189 | + ); |
| 190 | + } |
| 191 | + |
| 192 | + /* |
| 193 | + // Deletes an image of a new image |
| 194 | + @DeleteMapping("/{id}/image") |
| 195 | + public ResponseEntity<Object> deleteNewImage(@PathVariable long id) { |
| 196 | +
|
| 197 | + News newx = ns.findById(id).orElseThrow(); |
| 198 | +
|
| 199 | + newx.setImageFile(null); |
| 200 | + newx.setImage(false); |
| 201 | +
|
| 202 | + ns.save(newx); |
| 203 | +
|
| 204 | + return ResponseEntity.noContent().build(); |
| 205 | + } |
| 206 | + */ |
| 207 | + |
| 208 | + deleteNewImage(news: News) { |
| 209 | + return this.httpClient.delete(URL + news.id + '/image') |
| 210 | + .pipe( |
| 211 | + //catchError(error => this.handleError(error)) |
| 212 | + ); |
| 213 | + } |
| 214 | + |
| 215 | + //Duda terminar |
| 216 | + |
| 217 | + /* |
| 218 | + // Asign a purchase to a user |
| 219 | + @PutMapping("/{id}/read/{usId}") |
| 220 | + public ResponseEntity<Object> readNew(@PathVariable long id, @PathVariable long usId) { |
| 221 | +
|
| 222 | + Usero user = us.findById(usId).orElseThrow(); |
| 223 | + Optional<News> news = ns.findById(id); |
| 224 | +
|
| 225 | + if (news.isPresent()) { |
| 226 | + news.get().setOneReadNew(user); |
| 227 | + news.get().setId(id); |
| 228 | + ns.save(news.get()); |
| 229 | +
|
| 230 | + return new ResponseEntity<>(user.getReadNotices(), HttpStatus.OK); |
| 231 | +
|
| 232 | + } else { |
| 233 | +
|
| 234 | + return new ResponseEntity<>(HttpStatus.NOT_FOUND); |
| 235 | + } |
| 236 | +
|
| 237 | + } |
| 238 | + */ |
| 239 | + |
| 240 | + /*private handleError(error: any) { |
| 241 | + console.log("ERROR:"); |
| 242 | + console.error(error); |
| 243 | + return throwError("Server error (" + error.status + "): " + error.text()) |
| 244 | + }*/ |
| 245 | + |
| 246 | +} |
0 commit comments