diff --git a/mivan/src/main/java/mivan/Mivan.java b/mivan/src/main/java/mivan/Mivan.java index f8b851a..d367cc8 100644 --- a/mivan/src/main/java/mivan/Mivan.java +++ b/mivan/src/main/java/mivan/Mivan.java @@ -1,37 +1,14 @@ package mivan; -import java.io.Console; - import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import mivan.model.User; -import mivan.repository.UserRepository; -import mivan.repository.UserRepositoryImpl; - @SpringBootApplication public class Mivan { public static void main(String[] args) { - - String firstName = "Paolo"; - String lastName = "Verdi"; - String city = "Milano"; - String username = "p.verdi1"; - String email = "p.verdi1@campus.unimib.it"; - String password = "pverdipassword"; - - User user = new User(firstName, lastName, city, username, email, password); - - - UserRepository userRepository = new UserRepositoryImpl(); - userRepository.addUser(user); - - int size = userRepository.getSize(); - - System.out.print(size); - + SpringApplication.run(Mivan.class, args); - } + } } diff --git a/mivan/src/main/java/mivan/model/Author.java b/mivan/src/main/java/mivan/model/Author.java index 3df1f6d..0aa05e1 100644 --- a/mivan/src/main/java/mivan/model/Author.java +++ b/mivan/src/main/java/mivan/model/Author.java @@ -21,20 +21,22 @@ public class Author implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id", unique = true) - private long id; - - @Column(name = "name", nullable=false) - + private long id; + + @Column(name = "name", nullable = false) + private String name; - - @ManyToMany(mappedBy="authors") + + @ManyToMany(mappedBy = "authors") private List books; - - public Author() {} + + public Author() { + } + public Author(String name) { super(); this.name = name; - this.books = new ArrayList (); + this.books = new ArrayList(); } public long getId() { @@ -51,24 +53,23 @@ public class Author implements Serializable { public void setName(String name) { this.name = name; - } - + } + public List getBooks() { return this.books; } - + public void addBook(Book book) { if (!this.books.contains(book)) { this.books.add(book); book.addAuthor(this); } } - + public void removeBook(Book book) { if (this.books.contains(book)) this.books.remove(book); - book.removeAuthor(this); + book.removeAuthor(this); } - - + } diff --git a/mivan/src/main/java/mivan/model/Book.java b/mivan/src/main/java/mivan/model/Book.java index 0c00668..6932874 100644 --- a/mivan/src/main/java/mivan/model/Book.java +++ b/mivan/src/main/java/mivan/model/Book.java @@ -16,29 +16,26 @@ import javax.persistence.Table; @Entity @Table(name = "book") public class Book implements Serializable { - + private static final long serialVersionUID = 19L; - + @Id - @Column(name = "isbn", nullable=false, unique=true) + @Column(name = "isbn", nullable = false, unique = true) private long isbn; - @Column(name = "title", nullable=false) + @Column(name = "title", nullable = false) private String title; @OneToOne private Book prequel; - + @ManyToMany - @JoinTable( - name = "book_has_author", - joinColumns=@JoinColumn(name="isbn", referencedColumnName="isbn"), - inverseJoinColumns=@JoinColumn(name="id", referencedColumnName="id") ) + @JoinTable(name = "book_has_author", joinColumns = @JoinColumn(name = "isbn", referencedColumnName = "isbn"), inverseJoinColumns = @JoinColumn(name = "id", referencedColumnName = "id")) private List authors; - + public Book() { } - + public Book(long isbn, String title, Book prequel) { this.isbn = isbn; this.title = title; @@ -73,7 +70,7 @@ public class Book implements Serializable { public List getAuthors() { return this.authors; } - + public void addAuthor(Author author) { if (!this.authors.contains(author)) { @@ -81,13 +78,13 @@ public class Book implements Serializable { author.addBook(this); } } - + public void removeAuthor(Author author) { if (this.authors.contains(author)) { this.authors.remove(author); author.removeBook(this); } - + } - + } diff --git a/mivan/src/main/java/mivan/model/Item.java b/mivan/src/main/java/mivan/model/Item.java index 856d073..16f2117 100644 --- a/mivan/src/main/java/mivan/model/Item.java +++ b/mivan/src/main/java/mivan/model/Item.java @@ -13,49 +13,50 @@ import javax.persistence.ManyToOne; import javax.persistence.Table; @Entity -@Table(name="item") -public class Item implements Serializable{ - private static final long serialVersionUID = 1993L; - - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - @Column(name = "id", unique = true) - private long id; - - @ManyToOne(fetch=FetchType.LAZY) - @JoinColumn(name="isbn") - private Book book; - - @ManyToOne(fetch=FetchType.LAZY) - @JoinColumn(name="location") - private Location location; - - public Item () {} - public Item(Book book, Location location){ - super(); - this.book = book; - this.location = location; - } - - public long getId() { - return this.id; - } - - public Book getBook(){ - return this.book; - } - - public void setBook(Book book){ - this.book = book; - } - - public Location getLocation() { - return this.location; - } - - public void setLocation(Location location) { - this.location = location; - } - +@Table(name = "item") +public class Item implements Serializable { + private static final long serialVersionUID = 1993L; + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "id", unique = true) + private long id; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "isbn") + private Book book; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "location") + private Location location; + + public Item() { + } + + public Item(Book book, Location location) { + super(); + this.book = book; + this.location = location; + } + + public long getId() { + return this.id; + } + + public Book getBook() { + return this.book; + } + + public void setBook(Book book) { + this.book = book; + } + + public Location getLocation() { + return this.location; + } + + public void setLocation(Location location) { + this.location = location; + } } \ No newline at end of file diff --git a/mivan/src/main/java/mivan/model/Person.java b/mivan/src/main/java/mivan/model/Person.java index 9c9d46a..59f28cb 100644 --- a/mivan/src/main/java/mivan/model/Person.java +++ b/mivan/src/main/java/mivan/model/Person.java @@ -7,27 +7,25 @@ import javax.persistence.*; @MappedSuperclass public class Person implements Serializable { - private static final long serialVersionUID = -5124436115031696628L; - - + @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id", unique = true) - private long id; - - @Column(name = "firstName", nullable=false) - private String firstName; - - @Column(name = "lastName", nullable=false) - private String lastName; - + private long id; + + @Column(name = "firstName", nullable = false) + private String firstName; + + @Column(name = "lastName", nullable = false) + private String lastName; + @Column(name = "city") private String city; - - - public Person() {} - + + public Person() { + } + public Person(String firstName, String lastName, String city) { super(); this.firstName = firstName; @@ -36,28 +34,28 @@ public class Person implements Serializable { } public long getId() { - return id; - } - - public void setId(int id) { - this.id = id; - } - - public String getFirstName() { - return firstName; - } - - public void setFirstName(String firstName) { - this.firstName = firstName; - } - - public String getLastName() { - return lastName; - } - - public void setLastName(String lastName) { - this.lastName = lastName; - } + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } public String getCity() { return city; @@ -72,6 +70,4 @@ public class Person implements Serializable { return "Person [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", city=" + city + "]"; } - - } diff --git a/mivan/src/main/java/mivan/repository/AuthorRepository.java b/mivan/src/main/java/mivan/repository/AuthorRepository.java index b5fc0d2..007820e 100644 --- a/mivan/src/main/java/mivan/repository/AuthorRepository.java +++ b/mivan/src/main/java/mivan/repository/AuthorRepository.java @@ -7,14 +7,14 @@ public interface AuthorRepository extends Repository { public void deleteAuthorById(Long id); public void addAuthor(Author author); - + public void updateAuthor(Long id, String name); - + public Author searchAuthorByName(String name); - + public void addBook(Long isbn, Long id); - + public void removeBook(Long isbn, Long id); - + public int getSize(); } diff --git a/mivan/src/main/java/mivan/repository/AuthorRepositoryImpl.java b/mivan/src/main/java/mivan/repository/AuthorRepositoryImpl.java index ea0d85b..57cbd3c 100644 --- a/mivan/src/main/java/mivan/repository/AuthorRepositoryImpl.java +++ b/mivan/src/main/java/mivan/repository/AuthorRepositoryImpl.java @@ -17,7 +17,7 @@ public class AuthorRepositoryImpl implements AuthorRepository { public AuthorRepositoryImpl() { this.entityManagerFactory = Persistence.createEntityManagerFactory("mivan"); } - + @Override public Optional findById(Long id) { final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); @@ -60,7 +60,7 @@ public class AuthorRepositoryImpl implements AuthorRepository { entityManager.persist(author); entityManager.getTransaction().commit(); entityManager.close(); - + } catch (Exception ex) { entityManager.getTransaction().rollback(); } @@ -89,8 +89,8 @@ public class AuthorRepositoryImpl implements AuthorRepository { Author author = null; try { - author = (Author) entityManager.createQuery("FROM Author u WHERE lower(u.name) = '" + name.toLowerCase() + "'") - .getSingleResult(); + author = (Author) entityManager + .createQuery("FROM Author u WHERE lower(u.name) = '" + name.toLowerCase() + "'").getSingleResult(); entityManager.close(); } catch (Exception ex) { @@ -99,7 +99,7 @@ public class AuthorRepositoryImpl implements AuthorRepository { return author; } - + @Override public int getSize() { final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); @@ -113,7 +113,7 @@ public class AuthorRepositoryImpl implements AuthorRepository { } return size; } - + @Override public void addBook(Long isbn, Long id) { final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); @@ -130,7 +130,7 @@ public class AuthorRepositoryImpl implements AuthorRepository { entityManager.close(); } catch (Exception ex) { entityManager.getTransaction().rollback(); - } + } } @Override @@ -149,8 +149,7 @@ public class AuthorRepositoryImpl implements AuthorRepository { entityManager.close(); } catch (Exception ex) { entityManager.getTransaction().rollback(); - } + } } - } diff --git a/mivan/src/main/java/mivan/repository/BookRepository.java b/mivan/src/main/java/mivan/repository/BookRepository.java index ba772ae..e60335e 100644 --- a/mivan/src/main/java/mivan/repository/BookRepository.java +++ b/mivan/src/main/java/mivan/repository/BookRepository.java @@ -2,22 +2,21 @@ package mivan.repository; import mivan.model.Book; -public interface BookRepository extends Repository{ - - //public Optional findUserAddressById(Long id); +public interface BookRepository extends Repository { + + // public Optional findUserAddressById(Long id); public void deleteBookById(Long isbn); public void addBook(Book book); - + public void updateBook(Long isbn, String title, Book prequel); - + public Book searchBookByTitle(String title); - + public void addAuthor(Long isbn, Long id); - + public void removeAuthor(Long isbn, Long id); public int getSize(); } - diff --git a/mivan/src/main/java/mivan/repository/BookRepositoryImpl.java b/mivan/src/main/java/mivan/repository/BookRepositoryImpl.java index 4c0006e..d83885c 100644 --- a/mivan/src/main/java/mivan/repository/BookRepositoryImpl.java +++ b/mivan/src/main/java/mivan/repository/BookRepositoryImpl.java @@ -14,7 +14,7 @@ public class BookRepositoryImpl implements BookRepository { private EntityManagerFactory entityManagerFactory; - public BookRepositoryImpl(){ + public BookRepositoryImpl() { this.entityManagerFactory = Persistence.createEntityManagerFactory("mivan"); } @@ -51,7 +51,7 @@ public class BookRepositoryImpl implements BookRepository { } @Override - public void addBook(Book book) { + public void addBook(Book book) { final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); try { if (!entityManager.getTransaction().isActive()) { @@ -60,7 +60,7 @@ public class BookRepositoryImpl implements BookRepository { entityManager.persist(book); entityManager.getTransaction().commit(); entityManager.close(); - + } catch (Exception ex) { entityManager.getTransaction().rollback(); } @@ -117,8 +117,6 @@ public class BookRepositoryImpl implements BookRepository { return size; } - - @Override public void addAuthor(Long isbn, Long id) { final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); @@ -135,7 +133,7 @@ public class BookRepositoryImpl implements BookRepository { entityManager.close(); } catch (Exception ex) { entityManager.getTransaction().rollback(); - } + } } @Override @@ -154,9 +152,7 @@ public class BookRepositoryImpl implements BookRepository { entityManager.close(); } catch (Exception ex) { entityManager.getTransaction().rollback(); - } + } } - - } diff --git a/mivan/src/main/java/mivan/repository/ItemRepository.java b/mivan/src/main/java/mivan/repository/ItemRepository.java index e011ab2..924b528 100644 --- a/mivan/src/main/java/mivan/repository/ItemRepository.java +++ b/mivan/src/main/java/mivan/repository/ItemRepository.java @@ -4,10 +4,10 @@ import mivan.model.Item; public interface ItemRepository extends Repository { - public void deleteItemById(Long id); + public void deleteItemById(Long id); - public void addItem(Item item); + public void addItem(Item item); - public int getSize(); + public int getSize(); } \ No newline at end of file diff --git a/mivan/src/main/java/mivan/repository/ItemRepositoryImpl.java b/mivan/src/main/java/mivan/repository/ItemRepositoryImpl.java index 6c89526..3e3c89e 100644 --- a/mivan/src/main/java/mivan/repository/ItemRepositoryImpl.java +++ b/mivan/src/main/java/mivan/repository/ItemRepositoryImpl.java @@ -1,4 +1,5 @@ package mivan.repository; + import java.util.List; import java.util.Optional; @@ -9,30 +10,31 @@ import javax.persistence.Persistence; import mivan.model.Item; public class ItemRepositoryImpl implements ItemRepository { - private EntityManagerFactory entityManagerFactory; + private EntityManagerFactory entityManagerFactory; + + public ItemRepositoryImpl() { + this.entityManagerFactory = Persistence.createEntityManagerFactory("mivan"); + } - public ItemRepositoryImpl() { - this.entityManagerFactory = Persistence.createEntityManagerFactory("mivan"); - } - @Override - public Optional findById(Long id) { - final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); - Item item = entityManager.find(Item.class, id); - entityManager.close(); - return Optional.ofNullable(item); - } + @Override + public Optional findById(Long id) { + final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); + Item item = entityManager.find(Item.class, id); + entityManager.close(); + return Optional.ofNullable(item); + } - @Override - public Iterable findAll() { - final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); - List items = entityManager.createQuery("FROM Item", Item.class).getResultList(); - entityManager.close(); - return items; - } + @Override + public Iterable findAll() { + final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); + List items = entityManager.createQuery("FROM Item", Item.class).getResultList(); + entityManager.close(); + return items; + } - @Override - public void deleteItemById(Long id) { - final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); + @Override + public void deleteItemById(Long id) { + final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); try { if (!entityManager.getTransaction().isActive()) { entityManager.getTransaction().begin(); @@ -44,11 +46,11 @@ public class ItemRepositoryImpl implements ItemRepository { } catch (Exception ex) { entityManager.getTransaction().rollback(); } - } + } - @Override - public void addItem(Item item) { - final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); + @Override + public void addItem(Item item) { + final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); try { if (!entityManager.getTransaction().isActive()) { entityManager.getTransaction().begin(); @@ -56,16 +58,16 @@ public class ItemRepositoryImpl implements ItemRepository { entityManager.persist(item); entityManager.getTransaction().commit(); entityManager.close(); - + } catch (Exception ex) { entityManager.getTransaction().rollback(); } - } + } - @Override - public int getSize() { - final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); + @Override + public int getSize() { + final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); int size = 0; try { size = entityManager.createQuery("FROM Item").getResultList().size(); @@ -75,7 +77,6 @@ public class ItemRepositoryImpl implements ItemRepository { size = 0; } return size; - } + } - } \ No newline at end of file diff --git a/mivan/src/main/java/mivan/repository/LoanRepository.java b/mivan/src/main/java/mivan/repository/LoanRepository.java index 4898199..3e28591 100644 --- a/mivan/src/main/java/mivan/repository/LoanRepository.java +++ b/mivan/src/main/java/mivan/repository/LoanRepository.java @@ -1,6 +1,5 @@ package mivan.repository; - import mivan.model.Loan; public interface LoanRepository extends Repository { @@ -8,9 +7,9 @@ public interface LoanRepository extends Repository { public void deleteLoanById(Long id); public void addLoan(Loan loan); - + public void updateLoan(Long id, String stato); - + public int getSize(); - + } diff --git a/mivan/src/main/java/mivan/repository/LoanRepositoryImpl.java b/mivan/src/main/java/mivan/repository/LoanRepositoryImpl.java index 0dec829..50a7da6 100644 --- a/mivan/src/main/java/mivan/repository/LoanRepositoryImpl.java +++ b/mivan/src/main/java/mivan/repository/LoanRepositoryImpl.java @@ -9,14 +9,14 @@ import javax.persistence.Persistence; import mivan.model.Loan; -public class LoanRepositoryImpl implements LoanRepository{ +public class LoanRepositoryImpl implements LoanRepository { private EntityManagerFactory entityManagerFactory; public LoanRepositoryImpl() { this.entityManagerFactory = Persistence.createEntityManagerFactory("mivan"); } - + public Optional findById(Long id) { final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); Loan loan = entityManager.find(Loan.class, id); @@ -58,7 +58,7 @@ public class LoanRepositoryImpl implements LoanRepository{ entityManager.persist(loan); entityManager.getTransaction().commit(); entityManager.close(); - + } catch (Exception ex) { entityManager.getTransaction().rollback(); } @@ -81,7 +81,6 @@ public class LoanRepositoryImpl implements LoanRepository{ } } - @Override public int getSize() { final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); diff --git a/mivan/src/main/java/mivan/repository/Repository.java b/mivan/src/main/java/mivan/repository/Repository.java index 8c14a0f..5bb9d01 100644 --- a/mivan/src/main/java/mivan/repository/Repository.java +++ b/mivan/src/main/java/mivan/repository/Repository.java @@ -3,6 +3,7 @@ package mivan.repository; import java.util.Optional; public interface Repository { - Optional findById(ID id); - Iterable findAll(); + Optional findById(ID id); + + Iterable findAll(); } diff --git a/mivan/src/main/java/mivan/repository/StaffRepository.java b/mivan/src/main/java/mivan/repository/StaffRepository.java index 651b5c6..f0b29a3 100644 --- a/mivan/src/main/java/mivan/repository/StaffRepository.java +++ b/mivan/src/main/java/mivan/repository/StaffRepository.java @@ -3,19 +3,18 @@ package mivan.repository; import mivan.model.Location; import mivan.model.Staff; +public interface StaffRepository extends Repository { -public interface StaffRepository extends Repository{ - public void deleteStaffById(Long id); public void addStaff(Staff user); - - public void updateStaff(Long id, String firstname, String lastname, String city, String idka, String ruolo, Location location); - + + public void updateStaff(Long id, String firstname, String lastname, String city, String idka, String ruolo, + Location location); + public Staff searchStaffByidka(String idka); - - //public List searchStaffByRole(String role); - + // public List searchStaffByRole(String role); + public int getSize(); } diff --git a/mivan/src/main/java/mivan/repository/StaffRepositoryImpl.java b/mivan/src/main/java/mivan/repository/StaffRepositoryImpl.java index ae8df0c..72b0543 100644 --- a/mivan/src/main/java/mivan/repository/StaffRepositoryImpl.java +++ b/mivan/src/main/java/mivan/repository/StaffRepositoryImpl.java @@ -10,7 +10,7 @@ import javax.persistence.Persistence; import mivan.model.Location; import mivan.model.Staff; -public class StaffRepositoryImpl implements StaffRepository{ +public class StaffRepositoryImpl implements StaffRepository { private EntityManagerFactory entityManagerFactory; @@ -26,7 +26,6 @@ public class StaffRepositoryImpl implements StaffRepository{ return Optional.ofNullable(staff); } - @Override public Iterable findAll() { final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); @@ -64,8 +63,8 @@ public class StaffRepositoryImpl implements StaffRepository{ } } - - public void updateStaff(Long id, String firstname, String lastname, String city,String idka,String ruolo,Location location) { + public void updateStaff(Long id, String firstname, String lastname, String city, String idka, String ruolo, + Location location) { final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); try { if (!entityManager.getTransaction().isActive()) { @@ -74,11 +73,11 @@ public class StaffRepositoryImpl implements StaffRepository{ Staff staff = entityManager.find(Staff.class, id); staff.setFirstName(firstname); staff.setLastName(lastname); - staff.setCity(city); + staff.setCity(city); staff.setIdka(idka); staff.setRuolo(ruolo); - staff.setLocation(location); - + staff.setLocation(location); + entityManager.persist(staff); entityManager.getTransaction().commit(); entityManager.close(); @@ -101,7 +100,7 @@ public class StaffRepositoryImpl implements StaffRepository{ } return staff; - } + } public int getSize() { final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); diff --git a/mivan/src/main/java/mivan/repository/UserRepository.java b/mivan/src/main/java/mivan/repository/UserRepository.java index f040321..deb9834 100644 --- a/mivan/src/main/java/mivan/repository/UserRepository.java +++ b/mivan/src/main/java/mivan/repository/UserRepository.java @@ -2,20 +2,20 @@ package mivan.repository; import mivan.model.User; -public interface UserRepository extends Repository{ - - //public Optional findUserAddressById(Long id); +public interface UserRepository extends Repository { + + // public Optional findUserAddressById(Long id); public void deleteUserById(Long id); public void addUser(User user); - - public void updateUser(Long id, String firstname, String lastname, String city,String username, String email, String password); - + + public void updateUser(Long id, String firstname, String lastname, String city, String username, String email, + String password); + public User searchUserByName(String username); - + public User searchUserByEmail(String email); - public int getSize(); } diff --git a/mivan/src/main/java/mivan/repository/UserRepositoryImpl.java b/mivan/src/main/java/mivan/repository/UserRepositoryImpl.java index 6ce5cd8..cb6fc0c 100644 --- a/mivan/src/main/java/mivan/repository/UserRepositoryImpl.java +++ b/mivan/src/main/java/mivan/repository/UserRepositoryImpl.java @@ -7,7 +7,6 @@ import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; - import mivan.model.User; public class UserRepositoryImpl implements UserRepository { @@ -25,7 +24,7 @@ public class UserRepositoryImpl implements UserRepository { entityManager.close(); return Optional.ofNullable(user); } - + @Override public Iterable findAll() { final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); @@ -58,14 +57,14 @@ public class UserRepositoryImpl implements UserRepository { entityManager.persist(user); entityManager.getTransaction().commit(); entityManager.close(); - + } catch (Exception ex) { entityManager.getTransaction().rollback(); } } - - public void updateUser(Long id, String firstname, String lastname, String city,String username, String email, String password) { + public void updateUser(Long id, String firstname, String lastname, String city, String username, String email, + String password) { final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); try { if (!entityManager.getTransaction().isActive()) { @@ -91,7 +90,8 @@ public class UserRepositoryImpl implements UserRepository { User user = null; try { - user = (User) entityManager.createQuery("FROM User u WHERE lower(u.username) = '" + username.toLowerCase() + "'") + user = (User) entityManager + .createQuery("FROM User u WHERE lower(u.username) = '" + username.toLowerCase() + "'") .getSingleResult(); entityManager.close(); @@ -100,8 +100,8 @@ public class UserRepositoryImpl implements UserRepository { } return user; - } - + } + public User searchUserByEmail(String email) { final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); @@ -116,7 +116,7 @@ public class UserRepositoryImpl implements UserRepository { } return user; - } + } public int getSize() { final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); diff --git a/mivan/src/test/java/mivan/AuthorBookTest.java b/mivan/src/test/java/mivan/AuthorBookTest.java index 3500557..b2533dc 100644 --- a/mivan/src/test/java/mivan/AuthorBookTest.java +++ b/mivan/src/test/java/mivan/AuthorBookTest.java @@ -1,15 +1,8 @@ package mivan; -import static org.junit.jupiter.api.Assertions.assertEquals; - -import java.util.Optional; - -import org.junit.jupiter.api.Order; -import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestMethodOrder; import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; -import mivan.model.Author; import mivan.repository.AuthorRepository; import mivan.repository.AuthorRepositoryImpl; import mivan.repository.BookRepository; @@ -17,14 +10,9 @@ import mivan.repository.BookRepositoryImpl; @TestMethodOrder(OrderAnnotation.class) public class AuthorBookTest { - + private AuthorRepository authorRepository = new AuthorRepositoryImpl(); - + private BookRepository bookRepository = new BookRepositoryImpl(); - - - - - } diff --git a/mivan/src/test/java/mivan/AuthorTest.java b/mivan/src/test/java/mivan/AuthorTest.java index 59002f2..f5599d0 100644 --- a/mivan/src/test/java/mivan/AuthorTest.java +++ b/mivan/src/test/java/mivan/AuthorTest.java @@ -15,64 +15,62 @@ import mivan.repository.AuthorRepositoryImpl; @TestMethodOrder(OrderAnnotation.class) public class AuthorTest { - + private AuthorRepository authorRepository = new AuthorRepositoryImpl(); - - + @Test @Order(1) void testAddAuthor() { - - + String name = "Ivan"; - + Author author = new Author(name); - - authorRepository.addAuthor(author); - + + authorRepository.addAuthor(author); + int size = authorRepository.getSize(); - - + assertEquals(size, 1); } - + @Test @Order(2) void testUpdateAuthor() { - + String name = "Ivan"; - + Author author = new Author(name); - + authorRepository.addAuthor(author); - String updatedName ="Javoso"; - + String updatedName = "Javoso"; + authorRepository.updateAuthor(author.getId(), updatedName); Optional updated_author = authorRepository.findById(author.getId()); - updated_author.ifPresent(a -> {assertEquals(a.getName(), updatedName);}); - + updated_author.ifPresent(a -> { + assertEquals(a.getName(), updatedName); + }); + } - + @Test @Order(3) void testDeleteUser() { int size = authorRepository.getSize(); String name = "gollum"; - + Author author = new Author(name); - + authorRepository.addAuthor(author); - + Author editAuthor = authorRepository.searchAuthorByName(name); - + long ID = editAuthor.getId(); - + authorRepository.deleteAuthorById(ID); - + int end_size = authorRepository.getSize(); assertEquals(end_size, size); } - } diff --git a/mivan/src/test/java/mivan/BookTest.java b/mivan/src/test/java/mivan/BookTest.java index 9ff3abe..bbfed95 100644 --- a/mivan/src/test/java/mivan/BookTest.java +++ b/mivan/src/test/java/mivan/BookTest.java @@ -5,63 +5,55 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.Optional; -import javax.validation.constraints.AssertTrue; - import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Test; -import mivan.model.Author; import mivan.model.Book; -import mivan.model.User; import mivan.repository.BookRepository; import mivan.repository.BookRepositoryImpl; public class BookTest { - private BookRepository bookRepository = new BookRepositoryImpl(); - - + @Test @Order(1) void testAddBook() { - + long isbn = 1234; String title = "illiade"; Book prequel = null; Book book = new Book(isbn, title, prequel); - - bookRepository.addBook(book); - + + bookRepository.addBook(book); + int size = bookRepository.getSize(); - - + assertEquals(size, 1); - + } - + @Test @Order(2) - void testUpdateBook(){ + void testUpdateBook() { long isbn = 12348; String title = "odissea"; Book prequel = null; Book book = new Book(isbn, title, prequel); - - bookRepository.addBook(book); + + bookRepository.addBook(book); String update_title = "eneide"; bookRepository.updateBook(isbn, update_title, prequel); Optional opt_book2 = bookRepository.findById(isbn); - + Book book2 = opt_book2.get(); assertTrue(update_title.equals(book2.getTitle())); } - @Test @Order(3) void testDeleteBook() { @@ -71,18 +63,18 @@ public class BookTest { Book prequel = null; Book book = new Book(isbn, title, prequel); - + bookRepository.addBook(book); - + Optional opt_book2 = bookRepository.findById(isbn); - + Book book2 = opt_book2.get(); long ID = book2.getIsbn(); - + bookRepository.deleteBookById(ID); - + int size = bookRepository.getSize(); - assertEquals(start_size,size); + assertEquals(start_size, size); } - + } diff --git a/mivan/src/test/java/mivan/ItemTest.java b/mivan/src/test/java/mivan/ItemTest.java index a3e5b8b..72ac89d 100644 --- a/mivan/src/test/java/mivan/ItemTest.java +++ b/mivan/src/test/java/mivan/ItemTest.java @@ -12,47 +12,43 @@ import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; import mivan.model.Book; import mivan.model.Item; import mivan.model.Location; -import mivan.repository.BookRepository; import mivan.repository.ItemRepository; import mivan.repository.ItemRepositoryImpl; @TestMethodOrder(OrderAnnotation.class) public class ItemTest { - private ItemRepository itemRepository = new ItemRepositoryImpl(); - - - @Test - @Order(1) - void testAddItem() { - Book book = null; - Location location = null; - Item item = new Item(book, location); - itemRepository.addItem(item); - int size = itemRepository.getSize(); - assertEquals(1, size); - } - - - @Test - @Order(2) - void testRemoveItem() { - int startSize = itemRepository.getSize(); - Book book = null; - Location location = null; - Item item = new Item(book, location); - itemRepository.addItem(item); - int size = itemRepository.getSize(); - assertEquals(startSize + 1, size); - - Optional optItem2 = itemRepository.findById(item.getId()); - - Item item2 = optItem2.get(); - long ID = item2.getId(); - itemRepository.deleteItemById(ID); - - int finalSize = itemRepository.getSize(); - assertEquals(startSize, finalSize); - - - } + private ItemRepository itemRepository = new ItemRepositoryImpl(); + + @Test + @Order(1) + void testAddItem() { + Book book = null; + Location location = null; + Item item = new Item(book, location); + itemRepository.addItem(item); + int size = itemRepository.getSize(); + assertEquals(1, size); + } + + @Test + @Order(2) + void testRemoveItem() { + int startSize = itemRepository.getSize(); + Book book = null; + Location location = null; + Item item = new Item(book, location); + itemRepository.addItem(item); + int size = itemRepository.getSize(); + assertEquals(startSize + 1, size); + + Optional optItem2 = itemRepository.findById(item.getId()); + + Item item2 = optItem2.get(); + long ID = item2.getId(); + itemRepository.deleteItemById(ID); + + int finalSize = itemRepository.getSize(); + assertEquals(startSize, finalSize); + + } } \ No newline at end of file diff --git a/mivan/src/test/java/mivan/LoanTest.java b/mivan/src/test/java/mivan/LoanTest.java index af0ab4c..efa1622 100644 --- a/mivan/src/test/java/mivan/LoanTest.java +++ b/mivan/src/test/java/mivan/LoanTest.java @@ -26,7 +26,7 @@ public class LoanTest { long date_start = 1234; long date_end = 2345; String state = "loaning"; - + Item item = null; Staff staff = null; User user = null; @@ -44,11 +44,11 @@ public class LoanTest { @Test @Order(2) void testUpdateLoan() { - + long date_start = 1234; long date_end = 2345; String state = "loaning"; - + Item item = null; Staff staff = null; User user = null; @@ -56,11 +56,11 @@ public class LoanTest { Loan loan = new Loan(date_start, date_end, state, item, staff, user); loanRepository.addLoan(loan); - + String update_state = "returned"; - - loanRepository.updateLoan(0L,update_state); - + + loanRepository.updateLoan(0L, update_state); + Optional opt_loan = loanRepository.findById(0L); Loan loan2 = opt_loan.get(); @@ -71,12 +71,12 @@ public class LoanTest { @Test @Order(3) void testDeleteLoan() { - + int start_size = loanRepository.getSize(); long date_start = 1876; long date_end = 9876; String state = "loaning"; - + Item item = null; Staff staff = null; User user = null; @@ -90,11 +90,10 @@ public class LoanTest { Loan book2 = opt_loan2.get(); long ID = book2.getId(); - loanRepository.deleteLoanById(ID); int size = loanRepository.getSize(); - + assertEquals(start_size, size); } } diff --git a/mivan/src/test/java/mivan/StaffTest.java b/mivan/src/test/java/mivan/StaffTest.java index 1a4fcaa..2a3854a 100644 --- a/mivan/src/test/java/mivan/StaffTest.java +++ b/mivan/src/test/java/mivan/StaffTest.java @@ -46,8 +46,6 @@ public class StaffTest { int newsize = staffRepository.getSize(); assertEquals(newsize, size + 1); - - Iterable sad2 = staffRepository.findAll(); } @Test @@ -73,13 +71,13 @@ public class StaffTest { assertEquals(newlosize, losize + 2); // ci sono 2 locazioni aggunte - // creo un membro dello staff + // creo un membro dello staff String firstname = "test_update_staff_name"; String lastname = "test_update_staff_lastname"; String city = "test_update_staff_city"; String idka = "test_update_staff_idka"; String ruolo = "test_update_staff_role"; - + Staff newstaff = new Staff(firstname, lastname, city, idka, ruolo); int size = staffRepository.getSize(); @@ -97,7 +95,7 @@ public class StaffTest { String newlastname = "test_update_staff_newlastname"; String newcity = "test_update_staff_newcity"; String newidka = "test_update_staff_newidka"; - String newruolo = "test_update_staff_newrole"; + String newruolo = "test_update_staff_newrole"; staffRepository.updateStaff(id, newfirstname, newlastname, newcity, newidka, newruolo, newlocation);