/** GET /articles/:id -> get the "id" article. */ @RequestMapping( value = "/articles/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) @Timed public ResponseEntity<Article> getArticle(@PathVariable Long id) { log.debug("REST request to get Article : {}", id); Article article = articleRepository.findOne(id); if (article != null) { article.resolveTraduction(); if (article.getCategory() != null) article.getCategory().resolveTraduction(); } return Optional.ofNullable(article) .map(result -> new ResponseEntity<>(result, HttpStatus.OK)) .orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND)); }
/** POST /articles -> Create a new article. */ @RequestMapping( value = "/articles", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE) @Timed public ResponseEntity<Article> createArticle(@RequestBody Article article) throws URISyntaxException { log.debug("REST request to save Article : {}", article); if (article.getId() != null) { return ResponseEntity.badRequest() .headers( HeaderUtil.createFailureAlert( "article", "idexists", "A new article cannot already have an ID")) .body(null); } article.setI18nTitle(I18n.setTranslationText(article.getI18nTitle(), article.getTitle())); article.setI18nText(I18n.setTranslationText(article.getI18nText(), article.getText())); Article result = articleRepository.save(article); return ResponseEntity.created(new URI("/api/articles/" + result.getId())) .headers(HeaderUtil.createEntityCreationAlert("article", result.getId().toString())) .body(result); }
/** PUT /articles -> Updates an existing article. */ @RequestMapping( value = "/articles", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE) @Timed public ResponseEntity<Article> updateArticle(@RequestBody Article article) throws URISyntaxException { log.debug("REST request to update Article : {}", article); if (article.getId() == null) { return createArticle(article); } Article result = articleRepository.findOne(article.getId()); result.setI18nTitle(I18n.setTranslationText(result.getI18nTitle(), article.getTitle())); result.setI18nText(I18n.setTranslationText(result.getI18nText(), article.getText())); result.setTitle(article.getTitle()); result.setText(article.getText()); result = articleRepository.save(result); return ResponseEntity.ok() .headers(HeaderUtil.createEntityUpdateAlert("article", article.getId().toString())) .body(result); }