Commit cb853633 by Patryk Czarnik

BlogService i dependency injection

parent f68d8e1c
package com.example.demo;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
......@@ -12,7 +10,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/blog")
public class BlogController {
private List<String> teksty = new ArrayList<>();
@Autowired
private BlogService blogService;
@GetMapping
public String rozmowaGet() {
......@@ -22,9 +21,9 @@ public class BlogController {
@PostMapping
public String rozmowaPost(String tekst, Model model) {
if(tekst != null && !tekst.isBlank()) {
teksty.add(tekst);
blogService.addTekst(tekst);
}
model.addAttribute("teksty", teksty);
model.addAttribute("teksty", blogService.getTeksty());
return "blog.html";
}
......
package com.example.demo;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/blog.json")
public class BlogRestController {
@Autowired
private BlogService blogService;
@GetMapping
public List<String> getTeksty() {
return blogService.getTeksty();
}
}
package com.example.demo;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.springframework.stereotype.Component;
@Component
public class BlogService {
private List<String> teksty = Collections.synchronizedList(new ArrayList<>());
public List<String> getTeksty() {
return Collections.unmodifiableList(teksty);
}
public void addTekst(String tekst) {
teksty.add(tekst);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment