commit
0105456ae2
|
@ -4,7 +4,7 @@ import java.util.List;
|
|||
|
||||
public interface BookService {
|
||||
|
||||
List<Book> findAll();
|
||||
List<Book> findAll();
|
||||
|
||||
void saveAll(List<Book> books);
|
||||
void saveAll(List<Book> books);
|
||||
}
|
||||
|
|
|
@ -5,25 +5,25 @@ import java.util.List;
|
|||
|
||||
public class BooksCreationDto {
|
||||
|
||||
private List<Book> books;
|
||||
private List<Book> books;
|
||||
|
||||
public BooksCreationDto() {
|
||||
this.books = new ArrayList<>();
|
||||
}
|
||||
public BooksCreationDto() {
|
||||
this.books = new ArrayList<>();
|
||||
}
|
||||
|
||||
public BooksCreationDto(List<Book> books) {
|
||||
this.books = books;
|
||||
}
|
||||
public BooksCreationDto(List<Book> books) {
|
||||
this.books = books;
|
||||
}
|
||||
|
||||
public List<Book> getBooks() {
|
||||
return books;
|
||||
}
|
||||
public List<Book> getBooks() {
|
||||
return books;
|
||||
}
|
||||
|
||||
public void setBooks(List<Book> books) {
|
||||
this.books = books;
|
||||
}
|
||||
public void setBooks(List<Book> books) {
|
||||
this.books = books;
|
||||
}
|
||||
|
||||
public void addBook(Book book) {
|
||||
this.books.add(book);
|
||||
}
|
||||
public void addBook(Book book) {
|
||||
this.books.add(book);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,14 +16,14 @@ public class Config implements WebMvcConfigurer {
|
|||
|
||||
@Override
|
||||
public void addViewControllers(ViewControllerRegistry registry) {
|
||||
registry.addViewController("/").setViewName("index");
|
||||
registry.addViewController("/")
|
||||
.setViewName("index");
|
||||
registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ITemplateResolver templateResolver() {
|
||||
ClassLoaderTemplateResolver resolver
|
||||
= new ClassLoaderTemplateResolver();
|
||||
ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver();
|
||||
resolver.setPrefix("templates/books/");
|
||||
resolver.setSuffix(".html");
|
||||
resolver.setTemplateMode(TemplateMode.HTML);
|
||||
|
|
|
@ -12,16 +12,16 @@ import java.util.stream.Collectors;
|
|||
@Service
|
||||
public class InMemoryBookService implements BookService {
|
||||
|
||||
static Map<Long, Book> booksDB = new HashMap<>();
|
||||
static Map<Long, Book> booksDB = new HashMap<>();
|
||||
|
||||
@Override
|
||||
public List<Book> findAll() {
|
||||
return new ArrayList(booksDB.values());
|
||||
}
|
||||
@Override
|
||||
public List<Book> findAll() {
|
||||
return new ArrayList<>(booksDB.values());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveAll(List<Book> books) {
|
||||
long nextId = getNextId();
|
||||
@Override
|
||||
public void saveAll(List<Book> books) {
|
||||
long nextId = getNextId();
|
||||
for (Book book : books) {
|
||||
if (book.getId() == 0) {
|
||||
book.setId(nextId++);
|
||||
|
@ -29,14 +29,14 @@ public class InMemoryBookService implements BookService {
|
|||
}
|
||||
|
||||
Map<Long, Book> bookMap = books.stream()
|
||||
.collect(Collectors.toMap(
|
||||
Book::getId, Function.identity()));
|
||||
.collect(Collectors.toMap(Book::getId, Function.identity()));
|
||||
|
||||
booksDB.putAll(bookMap);
|
||||
}
|
||||
booksDB.putAll(bookMap);
|
||||
}
|
||||
|
||||
private Long getNextId(){
|
||||
return booksDB.keySet().stream()
|
||||
private Long getNextId() {
|
||||
return booksDB.keySet()
|
||||
.stream()
|
||||
.mapToLong(value -> value)
|
||||
.max()
|
||||
.orElse(0) + 1;
|
||||
|
|
|
@ -5,9 +5,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
|
||||
|
||||
@SpringBootApplication(
|
||||
exclude = {SecurityAutoConfiguration.class,
|
||||
DataSourceAutoConfiguration.class})
|
||||
@SpringBootApplication(exclude = { SecurityAutoConfiguration.class, DataSourceAutoConfiguration.class })
|
||||
public class ListBindingApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
|
|
@ -15,45 +15,47 @@ import java.util.List;
|
|||
@RequestMapping("/books")
|
||||
public class MultipleBooksController {
|
||||
|
||||
@Autowired
|
||||
private BookService bookService;
|
||||
@Autowired
|
||||
private BookService bookService;
|
||||
|
||||
@GetMapping(value = "/all")
|
||||
public String showAll(Model model) {
|
||||
model.addAttribute("books", bookService.findAll());
|
||||
@GetMapping(value = "/all")
|
||||
public String showAll(Model model) {
|
||||
model.addAttribute("books", bookService.findAll());
|
||||
|
||||
return "allBooks";
|
||||
}
|
||||
return "allBooks";
|
||||
}
|
||||
|
||||
@GetMapping(value = "/create")
|
||||
public String showCreateForm(Model model) {
|
||||
BooksCreationDto booksForm = new BooksCreationDto();
|
||||
@GetMapping(value = "/create")
|
||||
public String showCreateForm(Model model) {
|
||||
BooksCreationDto booksForm = new BooksCreationDto();
|
||||
|
||||
for (int i = 1; i <= 3; i++) {
|
||||
booksForm.addBook(new Book());
|
||||
}
|
||||
for (int i = 1; i <= 3; i++) {
|
||||
booksForm.addBook(new Book());
|
||||
}
|
||||
|
||||
model.addAttribute("form", booksForm);
|
||||
model.addAttribute("form", booksForm);
|
||||
|
||||
return "createBooksForm";
|
||||
}
|
||||
return "createBooksForm";
|
||||
}
|
||||
|
||||
@GetMapping(value = "/edit")
|
||||
public String showEditForm(Model model) {
|
||||
List<Book> books = new ArrayList<>();
|
||||
bookService.findAll().iterator().forEachRemaining(books::add);
|
||||
@GetMapping(value = "/edit")
|
||||
public String showEditForm(Model model) {
|
||||
List<Book> books = new ArrayList<>();
|
||||
bookService.findAll()
|
||||
.iterator()
|
||||
.forEachRemaining(books::add);
|
||||
|
||||
model.addAttribute("form", new BooksCreationDto(books));
|
||||
model.addAttribute("form", new BooksCreationDto(books));
|
||||
|
||||
return "editBooksForm";
|
||||
}
|
||||
return "editBooksForm";
|
||||
}
|
||||
|
||||
@PostMapping(value = "/save")
|
||||
public String saveBooks(@ModelAttribute BooksCreationDto form, Model model) {
|
||||
bookService.saveAll(form.getBooks());
|
||||
@PostMapping(value = "/save")
|
||||
public String saveBooks(@ModelAttribute BooksCreationDto form, Model model) {
|
||||
bookService.saveAll(form.getBooks());
|
||||
|
||||
model.addAttribute("books", bookService.findAll());
|
||||
model.addAttribute("books", bookService.findAll());
|
||||
|
||||
return "redirect:/books/all";
|
||||
}
|
||||
return "redirect:/books/all";
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue