Implementation of controllers

The following is the IndexController that caters the index URL:

@Controller
public class IndexController {

@GetMapping("/")
public String index() {
return "redirect:/article";
}
}

The index method is mapped to the URL /, which will redirect to the /article URL when a GET request is received at that endpoint. 

The following is the ArticleController, which is responsible for the CRUD operations of the Article domain model:

@Controller
@RequestMapping("/article")
public class ArticleController {

private final ArticleService articleService;
private final UserService userService;

public ArticleController(ArticleService articleService, UserService
userService) {
this.articleService = articleService;
this.userService = userService;
}

@GetMapping
public String index(Model model,
@AuthenticationPrincipal UserDetails
userDetails,
@RequestParam(required = false, value = "q")
String q,
@RequestParam(required = false, value = "page") Integer page,
@RequestParam(required = false, value = "size") Integer size)
{
if (q == null) {
model.addAttribute("articles",
articleService.getAll(getPageable(page, size)));
} else {
model.addAttribute("articles", articleService.search(q,
getPageable(page, size)));
}

return "article/index";
}

All the methods in the preceding controller will be grouped under the URL /articleArticleController.index, which is mapped to the GET method of the /article URL, expects @AuthenticationPrincipal UserDetails, and which returns the logged-in user's details and optional @QueryParams q, page, and size. Where q is used as a search query, page and size are used to enable pagination on the List Articles page. 

It uses the ArticleSearch.getAll method if a q query parameter is not specified and the ArticleService.search method when a q query parameter is specified using the search bar. It also uses the UserService.getByUsername method to get the currently logged-in User domain model. Finally, it will send all the data using Model to article/index.ftl. A Model is an auto injected into a method, a model usually allows us to submit any reference data to a page that is going to be loaded:

    @GetMapping("/show/{link}")
public String getPost(@AuthenticationPrincipal UserDetails
userDetails,
@PathVariable String link, Model model) {
Optional<Article> article = articleService.getByLink(link);
if (article.isPresent()) {
model.addAttribute("article", article.get());
} else {
throwNotFoundException(link);
}

return "article/show";
}

The ArticleController.getPost method is mapped to the GET method /article/show/{link} URL, which uses the @PathVariable link variable to get the Article using the ArticleService.getByLink method, which in turn uses the link property to get the correct Article to show. Finally, it will send the data using Model to article/show.ftl:

    @GetMapping("/new")
public String newPost() {
return "article/create";
}

The ArticleController.newPost method is mapped to the GET method /article/new, which displays the article/create.ftl page:

    @GetMapping("/edit/{id}")
public String editPost(@AuthenticationPrincipal UserDetails
userDetails, @PathVariable String id, Model model) {
Optional<Article> article = articleService.getById(id);
if (article.isPresent()) {
model.addAttribute("article", article.get());
} else {
return throwNotFoundException(id);
}

return "article/create";
}

private String throwNotFoundException(@PathVariable String id) {
throw new NotFoundException("Article Not Found for "+id);
}

The ArticleController.editPost method is mapped to the GET method /article/edit/{id} URL, which uses the @PathVariable id variable to get the Article using ArticleService.getByIdFinally, it will send all the data using Model to article/create.ftl:

    @PostMapping("/delete/{id}")
public String deletePost(@AuthenticationPrincipal UserDetails
userDetails, @PathVariable String id, Model model) {
articleService.deleteById(id);

model.addAttribute("message", "Article with id " + id + "
deleted successfully!"
);
model.addAttribute("articles", articleService.getAll(new
PageRequest(0, 10)));

return "article/index";
}

The ArticleController.deletePost method is mapped to the POST method /article/delete/{id} URL, which uses the @PathVariable id variable to delete the Article using the ArticleService.deleteById method. Finally, it will send the user to the article/index.ftl page to List Articles with a message for successfully deleting an Article:

    @PostMapping
public String savePost(@AuthenticationPrincipal UserDetails
userDetails, Article article, Model model) {
if (article.getId() == null || article.getId().length() == 0) {
User user =
userService.getByUsername(userDetails.getUsername());
article.setAuthor(user);
} else {
Optional<Article> optionalArticle =
articleService.getById(article.getId());
if (optionalArticle.isPresent()) {
article.setAuthor(optionalArticle.get().getAuthor());
}
}
articleService.save(article);

return "redirect:/article/show/"+article.getLink();
}

The ArticleController.savePost method is mapped to the POST method /article URL, which saves the Article using the ArticleService.save method. Finally, it will redirect to /article/show/{link}, where link is the link property of the newly saved Article.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.250.114