Coverage Summary for Class: TopController (io.github.mkuchin.controller)
Class | Class, % | Method, % | Line, % |
---|---|---|---|
TopController | 100% (1/ 1) | 100% (3/ 3) | 100% (5/ 5) |
1 package io.github.mkuchin.controller;
2
3 import io.github.mkuchin.service.word.WordCountService;
4 import lombok.RequiredArgsConstructor;
5 import org.springframework.web.bind.annotation.GetMapping;
6 import org.springframework.web.bind.annotation.PathVariable;
7 import org.springframework.web.bind.annotation.RequestMapping;
8 import org.springframework.web.bind.annotation.RestController;
9
10 import javax.servlet.http.HttpServletResponse;
11 import java.io.IOException;
12
13 @RestController
14 @RequestMapping("/top")
15 @RequiredArgsConstructor
16 public class TopController {
17
18 private final WordCountService wordCountService;
19 private static final String TEXT_CSV = "text/csv";
20
21 @GetMapping(value = "/{n}", produces = TEXT_CSV)
22 public void top(@PathVariable int n, HttpServletResponse response) throws IOException {
23 var writer = response.getWriter();
24 response.setContentType(TEXT_CSV);
25 wordCountService.topWords(n).forEach(wordCount ->
26 writer.println(String.format("%s|%s", wordCount.getWord(), wordCount.getCount()))
27 );
28 }
29 }