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