Update Mivan.java, Author.java, and 22 more files...

develop
@Territory91 5 years ago
parent 0850c9f730
commit ba2f9e2b0c
  1. 23
      mivan/src/main/java/mivan/Mivan.java
  2. 5
      mivan/src/main/java/mivan/model/Author.java
  3. 5
      mivan/src/main/java/mivan/model/Book.java
  4. 5
      mivan/src/main/java/mivan/model/Item.java
  5. 8
      mivan/src/main/java/mivan/model/Person.java
  6. 5
      mivan/src/main/java/mivan/repository/AuthorRepositoryImpl.java
  7. 1
      mivan/src/main/java/mivan/repository/BookRepository.java
  8. 4
      mivan/src/main/java/mivan/repository/BookRepositoryImpl.java
  9. 3
      mivan/src/main/java/mivan/repository/ItemRepositoryImpl.java
  10. 1
      mivan/src/main/java/mivan/repository/LoanRepository.java
  11. 1
      mivan/src/main/java/mivan/repository/LoanRepositoryImpl.java
  12. 1
      mivan/src/main/java/mivan/repository/Repository.java
  13. 5
      mivan/src/main/java/mivan/repository/StaffRepository.java
  14. 5
      mivan/src/main/java/mivan/repository/StaffRepositoryImpl.java
  15. 4
      mivan/src/main/java/mivan/repository/UserRepository.java
  16. 8
      mivan/src/main/java/mivan/repository/UserRepositoryImpl.java
  17. 12
      mivan/src/test/java/mivan/AuthorBookTest.java
  18. 8
      mivan/src/test/java/mivan/AuthorTest.java
  19. 8
      mivan/src/test/java/mivan/BookTest.java
  20. 4
      mivan/src/test/java/mivan/ItemTest.java
  21. 1
      mivan/src/test/java/mivan/LoanTest.java
  22. 2
      mivan/src/test/java/mivan/StaffTest.java

@ -1,36 +1,13 @@
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);
}

@ -30,7 +30,9 @@ public class Author implements Serializable {
@ManyToMany(mappedBy = "authors")
private List<Book> books;
public Author() {}
public Author() {
}
public Author(String name) {
super();
this.name = name;
@ -70,5 +72,4 @@ public class Author implements Serializable {
book.removeAuthor(this);
}
}

@ -30,10 +30,7 @@ public class Book implements Serializable {
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<Author> authors;
public Book() {

@ -30,7 +30,9 @@ public class Item implements Serializable{
@JoinColumn(name = "location")
private Location location;
public Item () {}
public Item() {
}
public Item(Book book, Location location) {
super();
this.book = book;
@ -57,5 +59,4 @@ public class Item implements Serializable{
this.location = location;
}
}

@ -7,10 +7,8 @@ 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)
@ -25,8 +23,8 @@ public class Person implements Serializable {
@Column(name = "city")
private String city;
public Person() {}
public Person() {
}
public Person(String firstName, String lastName, String city) {
super();
@ -72,6 +70,4 @@ public class Person implements Serializable {
return "Person [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", city=" + city + "]";
}
}

@ -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) {
@ -152,5 +152,4 @@ public class AuthorRepositoryImpl implements AuthorRepository {
}
}
}

@ -20,4 +20,3 @@ public interface BookRepository extends Repository<Book, Long>{
public int getSize();
}

@ -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();
@ -157,6 +155,4 @@ public class BookRepositoryImpl implements BookRepository {
}
}
}

@ -1,4 +1,5 @@
package mivan.repository;
import java.util.List;
import java.util.Optional;
@ -14,6 +15,7 @@ public class ItemRepositoryImpl implements ItemRepository {
public ItemRepositoryImpl() {
this.entityManagerFactory = Persistence.createEntityManagerFactory("mivan");
}
@Override
public Optional<Item> findById(Long id) {
final EntityManager entityManager = this.entityManagerFactory.createEntityManager();
@ -77,5 +79,4 @@ public class ItemRepositoryImpl implements ItemRepository {
return size;
}
}

@ -1,6 +1,5 @@
package mivan.repository;
import mivan.model.Loan;
public interface LoanRepository extends Repository<Loan, Long> {

@ -81,7 +81,6 @@ public class LoanRepositoryImpl implements LoanRepository{
}
}
@Override
public int getSize() {
final EntityManager entityManager = this.entityManagerFactory.createEntityManager();

@ -4,5 +4,6 @@ import java.util.Optional;
public interface Repository<T, ID> {
Optional<T> findById(ID id);
Iterable<T> findAll();
}

@ -3,19 +3,18 @@ package mivan.repository;
import mivan.model.Location;
import mivan.model.Staff;
public interface StaffRepository extends Repository<Staff, Long> {
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<Staff> searchStaffByRole(String role);
public int getSize();
}

@ -26,7 +26,6 @@ public class StaffRepositoryImpl implements StaffRepository{
return Optional.ofNullable(staff);
}
@Override
public Iterable<Staff> 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()) {

@ -10,12 +10,12 @@ public interface UserRepository extends Repository<User, Long>{
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();
}

@ -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 {
@ -64,8 +63,8 @@ public class UserRepositoryImpl implements UserRepository {
}
}
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();

@ -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;
@ -22,9 +15,4 @@ public class AuthorBookTest {
private BookRepository bookRepository = new BookRepositoryImpl();
}

@ -18,12 +18,10 @@ public class AuthorTest {
private AuthorRepository authorRepository = new AuthorRepositoryImpl();
@Test
@Order(1)
void testAddAuthor() {
String name = "Ivan";
Author author = new Author(name);
@ -32,7 +30,6 @@ public class AuthorTest {
int size = authorRepository.getSize();
assertEquals(size, 1);
}
@ -50,7 +47,9 @@ public class AuthorTest {
authorRepository.updateAuthor(author.getId(), updatedName);
Optional<Author> updated_author = authorRepository.findById(author.getId());
updated_author.ifPresent(a -> {assertEquals(a.getName(), updatedName);});
updated_author.ifPresent(a -> {
assertEquals(a.getName(), updatedName);
});
}
@ -74,5 +73,4 @@ public class AuthorTest {
assertEquals(end_size, size);
}
}

@ -5,23 +5,17 @@ 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() {
@ -36,7 +30,6 @@ public class BookTest {
int size = bookRepository.getSize();
assertEquals(size, 1);
}
@ -61,7 +54,6 @@ public class BookTest {
}
@Test
@Order(3)
void testDeleteBook() {

@ -12,7 +12,6 @@ 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;
@ -20,7 +19,6 @@ import mivan.repository.ItemRepositoryImpl;
public class ItemTest {
private ItemRepository itemRepository = new ItemRepositoryImpl();
@Test
@Order(1)
void testAddItem() {
@ -32,7 +30,6 @@ public class ItemTest {
assertEquals(1, size);
}
@Test
@Order(2)
void testRemoveItem() {
@ -53,6 +50,5 @@ public class ItemTest {
int finalSize = itemRepository.getSize();
assertEquals(startSize, finalSize);
}
}

@ -90,7 +90,6 @@ public class LoanTest {
Loan book2 = opt_loan2.get();
long ID = book2.getId();
loanRepository.deleteLoanById(ID);
int size = loanRepository.getSize();

@ -46,8 +46,6 @@ public class StaffTest {
int newsize = staffRepository.getSize();
assertEquals(newsize, size + 1);
Iterable<Staff> sad2 = staffRepository.findAll();
}
@Test

Loading…
Cancel
Save