Browse Source

Added book as obj

main
Jesus 2 years ago
parent
commit
027cfccdb2
3 changed files with 90 additions and 1 deletions
  1. +74
    -0
      src/main/java/com/jesuspinar/booksearch/dao/BookDao.java
  2. +15
    -0
      src/main/java/com/jesuspinar/booksearch/dao/IBookDao.java
  3. +1
    -1
      src/main/java/com/jesuspinar/booksearch/model/Book.java

+ 74
- 0
src/main/java/com/jesuspinar/booksearch/dao/BookDao.java View File

@ -0,0 +1,74 @@
package com.jesuspinar.booksearch.dao;
import com.jesuspinar.booksearch.config.AppConfig;
import com.jesuspinar.booksearch.controller.CsvParser;
import com.jesuspinar.booksearch.model.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
@Repository
public class BookDao implements IBookDao{
public List<Book> books = new ArrayList<>();
@Autowired
private AppConfig appConfig;
@PostConstruct
public void init() {
books = CsvParser.readFile(appConfig.getFile(), appConfig.getSeparator(), appConfig.getListSeparator());
}
@Override
public Book findById(long id) {
Optional<Book> result = books
.stream()
.filter(f -> f.getId() == id)
.findFirst();
return result.orElse(null);
}
@Override
public Collection<Book> findAll() {
return books;
}
@Override
public void insert(Book book) {
books.add(book);
}
@Override
public void edit(Book book) {
int index = getIndexOf(book.getId());
if (index != -1)
books.set(index, book);
}
@Override
public void delete(long id) {
int index = getIndexOf(id);
if (index != -1)
books.remove(index);
}
private int getIndexOf(long id) {
boolean find = false;
int index = 0;
while (!find && index < books.size()) {
if (books.get(index).getId() == id)
find = true;
else
index++;
}
return (find) ? index : -1;
}
}

+ 15
- 0
src/main/java/com/jesuspinar/booksearch/dao/IBookDao.java View File

@ -0,0 +1,15 @@
package com.jesuspinar.booksearch.dao;
import com.jesuspinar.booksearch.model.Book;
import java.util.Collection;
public interface IBookDao {
public Book findById(long id);
public Collection<Book> findAll();
public void insert(Book book);
public void edit(Book book);
public void delete(long id);
}

+ 1
- 1
src/main/java/com/jesuspinar/booksearch/model/Book.java View File

@ -15,7 +15,7 @@ public class Book {
private String publisher; private String publisher;
public Book( String title, List<String> authors, String genre,String height, String publisher) { public Book( String title, List<String> authors, String genre,String height, String publisher) {
this.id = AUTO_ID++;
this.id = ++AUTO_ID;
this.title = title; this.title = title;
this.authors = authors; this.authors = authors;
this.genre = genre; this.genre = genre;


Loading…
Cancel
Save