Huge refactoring

develop
Meliurwen 5 years ago
parent e08e2f5444
commit e9d766a54b
  1. 4
      .gitignore
  2. 14
      mivan/.editorconfig
  3. 0
      mivan/.gitignore
  4. 4
      mivan/.mvn/wrapper/MavenWrapperDownloader.java
  5. BIN
      mivan/.mvn/wrapper/maven-wrapper.jar
  6. 4
      mivan/.mvn/wrapper/maven-wrapper.properties
  7. 6
      mivan/mvnw
  8. 8
      mivan/mvnw.cmd
  9. 36
      mivan/pom.xml
  10. 37
      mivan/src/main/java/mivan/Mivan.java
  11. 74
      mivan/src/main/java/mivan/model/Author.java
  12. 93
      mivan/src/main/java/mivan/model/Book.java
  13. 83
      mivan/src/main/java/mivan/model/Location.java
  14. 17
      mivan/src/main/java/mivan/model/Person.java
  15. 71
      mivan/src/main/java/mivan/model/Staff.java
  16. 90
      mivan/src/main/java/mivan/model/User.java
  17. 20
      mivan/src/main/java/mivan/repository/AuthorRepository.java
  18. 156
      mivan/src/main/java/mivan/repository/AuthorRepositoryImpl.java
  19. 24
      mivan/src/main/java/mivan/repository/BookRepository.java
  20. 157
      mivan/src/main/java/mivan/repository/BookRepositoryImpl.java
  21. 20
      mivan/src/main/java/mivan/repository/LocationRepository.java
  22. 114
      mivan/src/main/java/mivan/repository/LocationRepositoryImpl.java
  23. 8
      mivan/src/main/java/mivan/repository/Repository.java
  24. 21
      mivan/src/main/java/mivan/repository/StaffRepository.java
  25. 118
      mivan/src/main/java/mivan/repository/StaffRepositoryImpl.java
  26. 21
      mivan/src/main/java/mivan/repository/UserRepository.java
  27. 133
      mivan/src/main/java/mivan/repository/UserRepositoryImpl.java
  28. 27
      mivan/src/main/resources/META-INF/persistence.xml
  29. 2
      mivan/src/main/resources/application.properties
  30. 30
      mivan/src/test/java/mivan/AuthorBookTest.java
  31. 77
      mivan/src/test/java/mivan/AuthorTest.java
  32. 41
      mivan/src/test/java/mivan/BookTest.java
  33. 116
      mivan/src/test/java/mivan/LocationTest.java
  34. 146
      mivan/src/test/java/mivan/StaffTest.java
  35. 109
      mivan/src/test/java/mivan/UserTest.java
  36. BIN
      multibook/database.mv.db
  37. 13
      multibook/src/main/java/com/unimib/disco/mivan/multibook/MultibookApplication.java
  38. 24
      multibook/src/main/java/com/unimib/disco/mivan/multibook/controller/UserController.java
  39. 55
      multibook/src/main/java/com/unimib/disco/mivan/multibook/model/Author.java
  40. 68
      multibook/src/main/java/com/unimib/disco/mivan/multibook/model/Book.java
  41. 65
      multibook/src/main/java/com/unimib/disco/mivan/multibook/model/Item.java
  42. 101
      multibook/src/main/java/com/unimib/disco/mivan/multibook/model/Loan.java
  43. 36
      multibook/src/main/java/com/unimib/disco/mivan/multibook/model/Location.java
  44. 26
      multibook/src/main/java/com/unimib/disco/mivan/multibook/model/Staff.java
  45. 67
      multibook/src/main/java/com/unimib/disco/mivan/multibook/model/User.java
  46. 17
      multibook/src/main/java/com/unimib/disco/mivan/multibook/repository/PersonRepository.java
  47. 19
      multibook/src/main/java/com/unimib/disco/mivan/multibook/repository/UserRepository.java
  48. 37
      multibook/src/main/java/com/unimib/disco/mivan/multibook/service/UserService.java
  49. 23
      multibook/src/main/resources/application.properties
  50. 13
      multibook/src/test/java/com/unimib/disco/mivan/multibook/MultibookApplicationTests.java
  51. 33
      multibook/src/test/java/com/unimib/disco/mivan/multibook/UserTests.java

4
.gitignore vendored

@ -13,3 +13,7 @@
*.bib~
*.pdf
mivan/target
mivan/bin

@ -0,0 +1,14 @@
# http://editorconfig.org
root = true
[*]
indent_style = space
indent_size = 2
insert_final_newline = true
trim_trailing_whitespace = true
end_of_line = lf
charset = utf-8
[*.{yml,yaml,json}]
insert_final_newline = false

@ -1,5 +1,5 @@
/*
* Copyright 2007-present the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -20,7 +20,7 @@ import java.util.Properties;
public class MavenWrapperDownloader {
private static final String WRAPPER_VERSION = "0.5.6";
private static final String WRAPPER_VERSION = "0.5.5";
/**
* Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided.
*/

@ -1,2 +1,2 @@
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.3/apache-maven-3.6.3-bin.zip
wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.2/apache-maven-3.6.2-bin.zip
wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.5/maven-wrapper-0.5.5.jar

@ -19,7 +19,7 @@
# ----------------------------------------------------------------------------
# ----------------------------------------------------------------------------
# Maven Start Up Batch script
# Maven2 Start Up Batch script
#
# Required ENV vars:
# ------------------
@ -212,9 +212,9 @@ else
echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..."
fi
if [ -n "$MVNW_REPOURL" ]; then
jarUrl="$MVNW_REPOURL/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar"
jarUrl="$MVNW_REPOURL/io/takari/maven-wrapper/0.5.5/maven-wrapper-0.5.5.jar"
else
jarUrl="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar"
jarUrl="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.5/maven-wrapper-0.5.5.jar"
fi
while IFS="=" read key value; do
case "$key" in (wrapperUrl) jarUrl="$value"; break ;;

@ -18,7 +18,7 @@
@REM ----------------------------------------------------------------------------
@REM ----------------------------------------------------------------------------
@REM Maven Start Up Batch script
@REM Maven2 Start Up Batch script
@REM
@REM Required ENV vars:
@REM JAVA_HOME - location of a JDK home dir
@ -26,7 +26,7 @@
@REM Optional ENV vars
@REM M2_HOME - location of maven2's installed home dir
@REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands
@REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a keystroke before ending
@REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a key stroke before ending
@REM MAVEN_OPTS - parameters passed to the Java VM when running Maven
@REM e.g. to debug Maven itself, use
@REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000
@ -120,7 +120,7 @@ SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe"
set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar"
set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain
set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar"
set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.5/maven-wrapper-0.5.5.jar"
FOR /F "tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO (
IF "%%A"=="wrapperUrl" SET DOWNLOAD_URL=%%B
@ -134,7 +134,7 @@ if exist %WRAPPER_JAR% (
)
) else (
if not "%MVNW_REPOURL%" == "" (
SET DOWNLOAD_URL="%MVNW_REPOURL%/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar"
SET DOWNLOAD_URL="%MVNW_REPOURL%/io/takari/maven-wrapper/0.5.5/maven-wrapper-0.5.5.jar"
)
if "%MVNW_VERBOSE%" == "true" (
echo Couldn't find %WRAPPER_JAR%, downloading it ...

@ -1,35 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
<version>2.2.1.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.unimib.disco.mivan</groupId>
<artifactId>multibook</artifactId>
<artifactId>mivan</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>multibook</name>
<description>Project for book loan</description>
<name>mivan</name>
<description>Spring MVC and Hibernate project</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
@ -53,6 +61,10 @@
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
<finalName>${project.artifactId}</finalName>
</configuration>
</plugin>
</plugins>
</build>

@ -0,0 +1,37 @@
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);
}
}

@ -0,0 +1,74 @@
package mivan.model;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
@Entity
@Table(name = "author")
public class Author implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true)
private long id;
@Column(name = "name", nullable=false)
private String name;
@ManyToMany(mappedBy="authors")
private List<Book> books;
public Author() {}
public Author(String name) {
super();
this.name = name;
this.books = new ArrayList <Book>();
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Book> 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);
}
}

@ -0,0 +1,93 @@
package mivan.model;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity
@Table(name = "book")
public class Book implements Serializable {
private static final long serialVersionUID = -5124436115031696628L;
@Id
@Column(name = "isbn", nullable=false)
private int isbn;
@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") )
private List<Author> authors;
public Book() {
}
public Book(int isbn, String title, Book prequel) {
this.isbn = isbn;
this.title = title;
this.prequel = prequel;
this.authors = new ArrayList<Author>();
}
public int getIsbn() {
return isbn;
}
public String getTitolo() {
return title;
}
public Book getPrequel() {
return prequel;
}
public void setIsbn(int isbn) {
this.isbn = isbn;
}
public void setTitle(String title) {
this.title = title;
}
public void setPrequel(Book prequel) {
this.prequel = prequel;
}
public List<Author> getAuthors() {
return this.authors;
}
public void addAuthor(Author author) {
if (!this.authors.contains(author)) {
this.authors.add(author);
author.addBook(this);
}
}
public void removeAuthor(Author author) {
if (this.authors.contains(author)) {
this.authors.remove(author);
author.removeBook(this);
}
}
}

@ -0,0 +1,83 @@
package mivan.model;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name = "location")
public class Location implements Serializable {
private static final long serialVersionUID = -5124436115031696628L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true)
private Long id;
@Column(name = "name", nullable = false, unique = true)
private String name;
@Column(name = "adress", nullable = false)
private String adress;
@OneToMany(mappedBy = "location")
private List<Staff> staffs;
protected Location() {
}
public Location(String name, String adress) {
this.name = name;
this.adress = adress;
this.staffs = new ArrayList<Staff>();
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
public String getAdress() {
return adress;
}
public List<Staff> getStaffs() {
return staffs;
}
public void setId(Long id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setAdress(String adress) {
this.adress = adress;
}
public void setStaffs(List<Staff> staffs) {
this.staffs = staffs;
}
public void addStaff(Staff staff) {
this.staffs.add(staff);
if (staff.getLocation() != this) {
staff.setLocation(this);
}
}
}

@ -1,15 +1,19 @@
package com.unimib.disco.mivan.multibook.model;
package mivan.model;
import java.io.Serializable;
import javax.persistence.*;
@MappedSuperclass
public class Person {
public class Person implements Serializable {
private static final long serialVersionUID = -5124436115031696628L;
@Id
@Column(name = "id", nullable=false)
@GeneratedValue(strategy = GenerationType.AUTO)
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true)
private long id;
@Column(name = "firstName", nullable=false)
@ -63,6 +67,11 @@ public class Person {
this.city = city;
}
@Override
public String toString() {
return "Person [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", city=" + city + "]";
}
}

@ -0,0 +1,71 @@
package mivan.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name = "staff")
public class Staff extends Person {
private static final long serialVersionUID = -5124436115031696628L;
@Column(name = "idka", nullable = false, unique = true)
private String idka;
@Column(name = "ruolo", nullable = false)
private String ruolo;
@ManyToOne
@JoinColumn(name = "location_id", referencedColumnName = "id")
private Location location;
public Staff() {
super();
// TODO Auto-generated constructor stub
}
public Staff(String firstName, String lastName, String city, String idka, String ruolo, Location location) {
super(firstName, lastName, city);
this.idka = idka;
this.ruolo = ruolo;
setLocation(location);
}
public Staff(String firstName, String lastName, String city, String idka, String ruolo) {
super(firstName, lastName, city);
this.idka = idka;
this.ruolo = ruolo;
}
public String getIdka() {
return idka;
}
public String getRuolo() {
return ruolo;
}
public Location getLocation() {
return location;
}
public void setIdka(String idka) {
this.idka = idka;
}
public void setRuolo(String ruolo) {
this.ruolo = ruolo;
}
public void setLocation(Location location) {
this.location = location;
if (!location.getStaffs().contains(this)) {
location.getStaffs().add(this);
}
}
}

@ -0,0 +1,90 @@
package mivan.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
@Entity
@Table(name = "user")
public class User extends Person {
private static final long serialVersionUID = -5124436115031696628L;
@Column(name = "username", nullable=false,unique=true)
private String username;
@Column(name = "email", nullable=false,unique=true)
private String email;
@Column(name = "password", nullable=false)
private String password;
protected User() {
}
public User(String firstName, String lastName, String city,String username, String email, String password) {
super(firstName,lastName,city);
this.username = username;
this.email = email;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
@Override
public String toString() {
return "User [username=" + username + ", email=" + email + ", password=" + password + "]";
}
}

@ -0,0 +1,20 @@
package mivan.repository;
import mivan.model.Author;
public interface AuthorRepository extends Repository<Author, Long> {
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(int isbn, long id);
public void removeBook(int isbn, long id);
public int getSize();
}

@ -0,0 +1,156 @@
package mivan.repository;
import java.util.List;
import java.util.Optional;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import mivan.model.Author;
import mivan.model.Book;
public class AuthorRepositoryImpl implements AuthorRepository {
private EntityManagerFactory entityManagerFactory;
public AuthorRepositoryImpl() {
this.entityManagerFactory = Persistence.createEntityManagerFactory("mivan");
}
@Override
public Optional<Author> findById(Long id) {
final EntityManager entityManager = this.entityManagerFactory.createEntityManager();
Author author = entityManager.find(Author.class, id);
entityManager.close();
return Optional.ofNullable(author);
}
@Override
public Iterable<Author> findAll() {
final EntityManager entityManager = this.entityManagerFactory.createEntityManager();
List<Author> author = entityManager.createQuery("FROM Author", Author.class).getResultList();
entityManager.close();
return author;
}
@Override
public void deleteAuthorById(Long id) {
final EntityManager entityManager = this.entityManagerFactory.createEntityManager();
try {
if (!entityManager.getTransaction().isActive()) {
entityManager.getTransaction().begin();
}
Author author = entityManager.find(Author.class, id);
entityManager.remove(author);
entityManager.getTransaction().commit();
entityManager.close();
} catch (Exception ex) {
entityManager.getTransaction().rollback();
}
}
@Override
public void addAuthor(Author author) {
final EntityManager entityManager = this.entityManagerFactory.createEntityManager();
try {
if (!entityManager.getTransaction().isActive()) {
entityManager.getTransaction().begin();
}
entityManager.persist(author);
entityManager.getTransaction().commit();
entityManager.close();
} catch (Exception ex) {
entityManager.getTransaction().rollback();
}
}
@Override
public void updateAuthor(Long id, String name) {
final EntityManager entityManager = this.entityManagerFactory.createEntityManager();
try {
if (!entityManager.getTransaction().isActive()) {
entityManager.getTransaction().begin();
}
Author author = entityManager.find(Author.class, id);
author.setName(name);
entityManager.persist(author);
entityManager.getTransaction().commit();
entityManager.close();
} catch (Exception ex) {
entityManager.getTransaction().rollback();
}
}
@Override
public Author searchAuthorByName(String name) {
final EntityManager entityManager = this.entityManagerFactory.createEntityManager();
Author author = null;
try {
author = (Author) entityManager.createQuery("FROM Author u WHERE lower(u.name) = '" + name.toLowerCase() + "'")
.getSingleResult();
entityManager.close();
} catch (Exception ex) {
author = null;
}
return author;
}
@Override
public int getSize() {
final EntityManager entityManager = this.entityManagerFactory.createEntityManager();
int size = 0;
try {
size = entityManager.createQuery("FROM Author").getResultList().size();
entityManager.close();
} catch (Exception ex) {
size = 0;
}
return size;
}
@Override
public void addBook(int isbn, long id) {
final EntityManager entityManager = this.entityManagerFactory.createEntityManager();
try {
if (!entityManager.getTransaction().isActive()) {
entityManager.getTransaction().begin();
}
Book book = entityManager.find(Book.class, isbn);
Author author = entityManager.find(Author.class, id);
book.addAuthor(author);
entityManager.persist(book);
entityManager.persist(author);
entityManager.getTransaction().commit();
entityManager.close();
} catch (Exception ex) {
entityManager.getTransaction().rollback();
}
}
@Override
public void removeBook(int isbn, long id) {
final EntityManager entityManager = this.entityManagerFactory.createEntityManager();
try {
if (!entityManager.getTransaction().isActive()) {
entityManager.getTransaction().begin();
}
Book book = entityManager.find(Book.class, isbn);
Author author = entityManager.find(Author.class, id);
book.removeAuthor(author);
entityManager.persist(book);
entityManager.persist(author);
entityManager.getTransaction().commit();
entityManager.close();
} catch (Exception ex) {
entityManager.getTransaction().rollback();
}
}
}

@ -0,0 +1,24 @@
package mivan.repository;
import mivan.model.Author;
import mivan.model.Book;
public interface BookRepository extends Repository<Book, Long>{
//public Optional<AddressUser> findUserAddressById(Long id);
public void deleteBookById(int isbn);
public void addBook(Book book);
public void updateBook(int isbn, String title, Book prequel);
public Book searchBookByTitle(String title);
public void addAuthor(int isbn, long id);
public void removeAuthor(int isbn, long id);
public int getSize();
}

@ -0,0 +1,157 @@
package mivan.repository;
import java.util.List;
import java.util.Optional;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import mivan.model.Author;
import mivan.model.Book;
public class BookRepositoryImpl implements BookRepository {
private EntityManagerFactory entityManagerFactory;
@Override
public Optional<Book> findById(Long id) {
final EntityManager entityManager = this.entityManagerFactory.createEntityManager();
Book book = entityManager.find(Book.class, id);
entityManager.close();
return Optional.ofNullable(book);
}
@Override
public Iterable<Book> findAll() {
final EntityManager entityManager = this.entityManagerFactory.createEntityManager();
List<Book> book = entityManager.createQuery("FROM Book", Book.class).getResultList();
entityManager.close();
return book;
}
@Override
public void deleteBookById(int isbn) {
final EntityManager entityManager = this.entityManagerFactory.createEntityManager();
try {
if (!entityManager.getTransaction().isActive()) {
entityManager.getTransaction().begin();
}
Book book = entityManager.find(Book.class, isbn);
entityManager.remove(book);
entityManager.getTransaction().commit();
entityManager.close();
} catch (Exception ex) {
entityManager.getTransaction().rollback();
}
}
@Override
public void addBook(Book book) {
final EntityManager entityManager = this.entityManagerFactory.createEntityManager();
try {
if (!entityManager.getTransaction().isActive()) {
entityManager.getTransaction().begin();
}
entityManager.persist(book);
entityManager.getTransaction().commit();
entityManager.close();
} catch (Exception ex) {
entityManager.getTransaction().rollback();
}
}
@Override
public void updateBook(int isbn, String title, Book prequel) {
final EntityManager entityManager = this.entityManagerFactory.createEntityManager();
try {
if (!entityManager.getTransaction().isActive()) {
entityManager.getTransaction().begin();
}
Book book = entityManager.find(Book.class, isbn);
book.setTitle(title);
entityManager.persist(book);
if (prequel != null) {
book.setPrequel(prequel);
}
entityManager.getTransaction().commit();
entityManager.close();
} catch (Exception ex) {
entityManager.getTransaction().rollback();
}
}
@Override
public Book searchBookByTitle(String title) {
final EntityManager entityManager = this.entityManagerFactory.createEntityManager();
Book book = null;
try {
book = (Book) entityManager.createQuery("FROM Book u WHERE lower(u.title) = '" + title.toLowerCase() + "'")
.getSingleResult();
entityManager.close();
} catch (Exception ex) {
book = null;
}
return book;
}
@Override
public int getSize() {
final EntityManager entityManager = this.entityManagerFactory.createEntityManager();
int size = 0;
try {
size = entityManager.createQuery("FROM Book").getResultList().size();
entityManager.close();
} catch (Exception ex) {
size = 0;
}
return size;
}
@Override
public void addAuthor(int isbn, long id) {
final EntityManager entityManager = this.entityManagerFactory.createEntityManager();
try {
if (!entityManager.getTransaction().isActive()) {
entityManager.getTransaction().begin();
}
Book book = entityManager.find(Book.class, isbn);
Author author = entityManager.find(Author.class, id);
book.addAuthor(author);
entityManager.persist(book);
entityManager.persist(author);
entityManager.getTransaction().commit();
entityManager.close();
} catch (Exception ex) {
entityManager.getTransaction().rollback();
}
}
@Override
public void removeAuthor(int isbn, long id) {
final EntityManager entityManager = this.entityManagerFactory.createEntityManager();
try {
if (!entityManager.getTransaction().isActive()) {
entityManager.getTransaction().begin();
}
Book book = entityManager.find(Book.class, isbn);
Author author = entityManager.find(Author.class, id);
book.removeAuthor(author);
entityManager.persist(book);
entityManager.persist(author);
entityManager.getTransaction().commit();
entityManager.close();
} catch (Exception ex) {
entityManager.getTransaction().rollback();
}
}
}

@ -0,0 +1,20 @@
package mivan.repository;
import java.util.List;
import mivan.model.Location;
import mivan.model.Staff;
public interface LocationRepository extends Repository<Location, Long> {
public void deleteLocationById(Long id);
public void addLocation(Location user);
public void updateLocation(Long id, String name, String adress, List<Staff> staffs);
public Location searchLocationByName(String name);
public int getSize();
}

@ -0,0 +1,114 @@
package mivan.repository;
import java.util.List;
import java.util.Optional;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import mivan.model.Location;
import mivan.model.Staff;
public class LocationRepositoryImpl implements LocationRepository {
private EntityManagerFactory entityManagerFactory;
public LocationRepositoryImpl() {
this.entityManagerFactory = Persistence.createEntityManagerFactory("mivan");
}
@Override
public Optional<Location> findById(Long id) {
final EntityManager entityManager = this.entityManagerFactory.createEntityManager();
Location location = entityManager.find(Location.class, id);
entityManager.close();
return Optional.ofNullable(location);
}
@Override
public Iterable<Location> findAll() {
final EntityManager entityManager = this.entityManagerFactory.createEntityManager();
List<Location> location = entityManager.createQuery("FROM Location", Location.class).getResultList();
entityManager.close();
return location;
}
public void deleteLocationById(Long id) {
final EntityManager entityManager = this.entityManagerFactory.createEntityManager();
try {
if (!entityManager.getTransaction().isActive()) {
entityManager.getTransaction().begin();
}
Location location = entityManager.find(Location.class, id);
entityManager.remove(location);
entityManager.getTransaction().commit();
entityManager.close();
} catch (Exception ex) {
entityManager.getTransaction().rollback();
}
}
public void addLocation(Location location) {
final EntityManager entityManager = this.entityManagerFactory.createEntityManager();
try {
if (!entityManager.getTransaction().isActive()) {
entityManager.getTransaction().begin();
}
entityManager.persist(location);
entityManager.getTransaction().commit();
entityManager.close();
} catch (Exception ex) {
entityManager.getTransaction().rollback();
}
}
public void updateLocation(Long id, String name, String adress, List<Staff> staffs) {
final EntityManager entityManager = this.entityManagerFactory.createEntityManager();
try {
if (!entityManager.getTransaction().isActive()) {
entityManager.getTransaction().begin();
}
Location location = entityManager.find(Location.class, id);
location.setName(name);
location.setAdress(adress);
location.setStaffs(staffs);
entityManager.persist(location);
entityManager.getTransaction().commit();
entityManager.close();
} catch (Exception ex) {
entityManager.getTransaction().rollback();
}
}
public Location searchLocationByName(String name) {
final EntityManager entityManager = this.entityManagerFactory.createEntityManager();
Location location = null;
try {
location = (Location) entityManager
.createQuery("FROM Location u WHERE lower(u.name) = '" + name.toLowerCase() + "'")
.getSingleResult();
entityManager.close();
} catch (Exception ex) {
location = null;
}
return location;
}
public int getSize() {
final EntityManager entityManager = this.entityManagerFactory.createEntityManager();
int size = 0;
try {
size = entityManager.createQuery("FROM Location").getResultList().size();
entityManager.close();
} catch (Exception ex) {
size = 0;
}
return size;
}
}

@ -0,0 +1,8 @@
package mivan.repository;
import java.util.Optional;
public interface Repository<T, ID> {
Optional<T> findById(ID id);
Iterable<T> findAll();
}

@ -0,0 +1,21 @@
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 Staff searchStaffByidka(String idka);
//public List<Staff> searchStaffByRole(String role);
public int getSize();
}

@ -0,0 +1,118 @@
package mivan.repository;
import java.util.List;
import java.util.Optional;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import mivan.model.Location;
import mivan.model.Staff;
public class StaffRepositoryImpl implements StaffRepository{
private EntityManagerFactory entityManagerFactory;
public StaffRepositoryImpl() {
this.entityManagerFactory = Persistence.createEntityManagerFactory("mivan");
}
@Override
public Optional<Staff> findById(Long id) {
final EntityManager entityManager = this.entityManagerFactory.createEntityManager();
Staff staff = entityManager.find(Staff.class, id);
entityManager.close();
return Optional.ofNullable(staff);
}
@Override
public Iterable<Staff> findAll() {
final EntityManager entityManager = this.entityManagerFactory.createEntityManager();
List<Staff> staffs = entityManager.createQuery("FROM Staff", Staff.class).getResultList();
entityManager.close();
return staffs;
}
public void deleteStaffById(Long id) {
final EntityManager entityManager = this.entityManagerFactory.createEntityManager();
try {
if (!entityManager.getTransaction().isActive()) {
entityManager.getTransaction().begin();
}
Staff staff = entityManager.find(Staff.class, id);
entityManager.remove(staff);
entityManager.getTransaction().commit();
entityManager.close();
} catch (Exception ex) {
entityManager.getTransaction().rollback();
}
}
public void addStaff(Staff staff) {
final EntityManager entityManager = this.entityManagerFactory.createEntityManager();
try {
if (!entityManager.getTransaction().isActive()) {
entityManager.getTransaction().begin();
}
entityManager.persist(staff);
entityManager.getTransaction().commit();
entityManager.close();
} catch (Exception ex) {
entityManager.getTransaction().rollback();
}
}
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()) {
entityManager.getTransaction().begin();
}
Staff staff = entityManager.find(Staff.class, id);
staff.setFirstName(firstname);
staff.setLastName(lastname);
staff.setCity(city);
staff.setIdka(idka);
staff.setRuolo(ruolo);
staff.setLocation(location);
entityManager.persist(staff);
entityManager.getTransaction().commit();
entityManager.close();
} catch (Exception ex) {
entityManager.getTransaction().rollback();
}
}
public Staff searchStaffByidka(String idka) {
final EntityManager entityManager = this.entityManagerFactory.createEntityManager();
Staff staff = null;
try {
staff = (Staff) entityManager.createQuery("FROM Staff u WHERE lower(u.idka) = '" + idka.toLowerCase() + "'")
.getSingleResult();
entityManager.close();
} catch (Exception ex) {
staff = null;
}
return staff;
}
public int getSize() {
final EntityManager entityManager = this.entityManagerFactory.createEntityManager();
int size = 0;
try {
size = entityManager.createQuery("FROM Staff").getResultList().size();
entityManager.close();
} catch (Exception ex) {
size = 0;
}
return size;
}
}

@ -0,0 +1,21 @@
package mivan.repository;
import mivan.model.User;
public interface UserRepository extends Repository<User, Long>{
//public Optional<AddressUser> 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 User searchUserByName(String username);
public User searchUserByEmail(String email);
public int getSize();
}

@ -0,0 +1,133 @@
package mivan.repository;
import java.util.List;
import java.util.Optional;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import mivan.model.User;
public class UserRepositoryImpl implements UserRepository {
private EntityManagerFactory entityManagerFactory;
public UserRepositoryImpl() {
this.entityManagerFactory = Persistence.createEntityManagerFactory("mivan");
}
@Override
public Optional<User> findById(Long id) {
final EntityManager entityManager = this.entityManagerFactory.createEntityManager();
User user = entityManager.find(User.class, id);
entityManager.close();
return Optional.ofNullable(user);
}
@Override
public Iterable<User> findAll() {
final EntityManager entityManager = this.entityManagerFactory.createEntityManager();
List<User> users = entityManager.createQuery("FROM User", User.class).getResultList();
entityManager.close();
return users;
}
public void deleteUserById(Long id) {
final EntityManager entityManager = this.entityManagerFactory.createEntityManager();
try {
if (!entityManager.getTransaction().isActive()) {
entityManager.getTransaction().begin();
}
User user = entityManager.find(User.class, id);
entityManager.remove(user);
entityManager.getTransaction().commit();
entityManager.close();
} catch (Exception ex) {
entityManager.getTransaction().rollback();
}
}
public void addUser(User user) {
final EntityManager entityManager = this.entityManagerFactory.createEntityManager();
try {
if (!entityManager.getTransaction().isActive()) {
entityManager.getTransaction().begin();
}
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) {
final EntityManager entityManager = this.entityManagerFactory.createEntityManager();
try {
if (!entityManager.getTransaction().isActive()) {
entityManager.getTransaction().begin();
}
User user = entityManager.find(User.class, id);
user.setFirstName(firstname);
user.setLastName(lastname);
user.setCity(city);
user.setUsername(username);
user.setEmail(email);
user.setPassword(password);
entityManager.persist(user);
entityManager.getTransaction().commit();
entityManager.close();
} catch (Exception ex) {
entityManager.getTransaction().rollback();
}
}
public User searchUserByName(String username) {
final EntityManager entityManager = this.entityManagerFactory.createEntityManager();
User user = null;
try {
user = (User) entityManager.createQuery("FROM User u WHERE lower(u.username) = '" + username.toLowerCase() + "'")
.getSingleResult();
entityManager.close();
} catch (Exception ex) {
user = null;
}
return user;
}
public User searchUserByEmail(String email) {
final EntityManager entityManager = this.entityManagerFactory.createEntityManager();
User user = null;
try {
user = (User) entityManager.createQuery("FROM User u WHERE lower(u.email) = '" + email.toLowerCase() + "'")
.getSingleResult();
entityManager.close();
} catch (Exception ex) {
user = null;
}
return user;
}
public int getSize() {
final EntityManager entityManager = this.entityManagerFactory.createEntityManager();
int size = 0;
try {
size = entityManager.createQuery("FROM User").getResultList().size();
entityManager.close();
} catch (Exception ex) {
size = 0;
}
return size;
}
}

@ -0,0 +1,27 @@
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit
name="mivan">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="javax.persistence.jdbc.driver"
value="org.h2.Driver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:h2:mem:mivanDB" />
<property name="javax.persistence.jdbc.user"
value="password" />
<property name="javax.persistence.jdbc.password"
value="password" />
<property name="hibernate.dialect"
value="org.hibernate.dialect.H2Dialect" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
</properties>
</persistence-unit>
</persistence>

@ -0,0 +1,2 @@
spring.application.name=Mivan
spring.datasource.url=jdbc:h2:file:./data/demo

@ -0,0 +1,30 @@
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;
import mivan.repository.BookRepositoryImpl;
@TestMethodOrder(OrderAnnotation.class)
public class AuthorBookTest {
private AuthorRepository authorRepository = new AuthorRepositoryImpl();
private BookRepository bookRepository = new BookRepositoryImpl();
}

@ -0,0 +1,77 @@
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;
@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);
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";
authorRepository.updateAuthor(author.getId(), updatedName);
Optional<Author> updated_author = authorRepository.findById(author.getId());
updated_author.ifPresent(a -> {assertEquals(a.getName(), updatedName);});
}
@Test
@Order(3)
void testDeleteUser() {
String name = "ivan";
Author author = new Author(name);
authorRepository.addAuthor(author);
Author editAuthor = authorRepository.searchAuthorByName(name);
long ID = editAuthor.getId();
authorRepository.deleteAuthorById(ID);
int size = authorRepository.getSize();
assertEquals(size, 0);
}
}

@ -0,0 +1,41 @@
package mivan;
import static org.junit.jupiter.api.Assertions.assertEquals;
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() {
int isbn = 1234;
String title = "illiade";
Book prequel = null;
Book book = new Book(isbn, title, prequel);
bookRepository.addBook(book);
int size = bookRepository.getSize();
assertEquals(size, 1);
}
}

@ -0,0 +1,116 @@
package mivan;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.ArrayList;
import java.util.List;
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.Location;
import mivan.model.Staff;
import mivan.repository.LocationRepository;
import mivan.repository.LocationRepositoryImpl;
import mivan.repository.StaffRepository;
import mivan.repository.StaffRepositoryImpl;
@TestMethodOrder(OrderAnnotation.class)
public class LocationTest {
private StaffRepository staffRepository = new StaffRepositoryImpl();
private LocationRepository locationRepository = new LocationRepositoryImpl();
@Test
@Order(1)
void testAddLocation() {
String lname = "Library bria 2";
String ladress = "via chissa dove2";
Location location = new Location(lname, ladress);
int size = locationRepository.getSize();
locationRepository.addLocation(location);
int newsize = locationRepository.getSize();
assertEquals(newsize, size + 1);
}
@Test
@Order(2)
void testUpdateLocation() {
// creo nuovo menìmbro dello staff
String firstname = "Ivan";
String lastname = "Donati";
String city = "Milano";
String idka = "lihwelhqluchwlir8";
String ruolo = "frontoffice";
Staff newstaff1 = new Staff(firstname, lastname, city, idka, ruolo);
String firstname2 = "Rossi";
String lastname2 = "Donati";
String city2 = "Milano";
String idka2 = "lihwelhsfgdghhwlir8";
String ruolo2 = "manager";
Staff newstaff2 = new Staff(firstname2, lastname2, city2, idka2, ruolo2);
staffRepository.addStaff(newstaff1);
staffRepository.addStaff(newstaff2);
List<Staff> staffs = new ArrayList<Staff>();
staffs.add(newstaff1);
staffs.add(newstaff2);
// creo una locazione
String lname = "Library cosa 2";
String ladress = "via chissa dove";
Location newlocation = new Location(lname, ladress);
locationRepository.addLocation(newlocation);
Location editLocation = locationRepository.searchLocationByName(lname);
long id = editLocation.getId();
// nuovi dati per modificare i vecchi
String newlName = "Library bria 5";
String newlAdress = "via prossima stella";
// aggungo i membri dello staff / modifico i dati della location
locationRepository.updateLocation(id, newlName, newlAdress, staffs);
Optional<Location> location = locationRepository.findById(id);
location.ifPresent(a -> {
assertEquals(a.getName(), newlName);
assertEquals(a.getAdress(), newlAdress);
assertEquals(a.getStaffs(), staffs);
});
}
@Test
@Order(3)
void testDeleteLocation() {
String lname = "Library bria 9";
String ladress = "via chissa dove9";
Location location = new Location(lname, ladress);
locationRepository.addLocation(location);
Location removeLocation = locationRepository.searchLocationByName(lname);
long ID = removeLocation.getId();
int size = locationRepository.getSize();
locationRepository.deleteLocationById(ID);
int newsize = locationRepository.getSize();
assertEquals(newsize, size -1);
}
}

@ -0,0 +1,146 @@
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.Location;
import mivan.model.Staff;
import mivan.repository.LocationRepository;
import mivan.repository.LocationRepositoryImpl;
import mivan.repository.StaffRepository;
import mivan.repository.StaffRepositoryImpl;
@TestMethodOrder(OrderAnnotation.class)
public class StaffTest {
private StaffRepository staffRepository = new StaffRepositoryImpl();
private LocationRepository locationRepository = new LocationRepositoryImpl();
@Test
@Order(1)
void testAddStaff() {
String firstname = "Ivan";
String lastname = "Donati";
String city = "Milano";
String idka = "lihwelhqluchwlir8";
String ruolo = "frontoffice";
String lname = "Library bria 2";
String ladress = "via chissa dove";
Location location = new Location(lname, ladress);
locationRepository.addLocation(location);
Staff newstaff = new Staff(firstname, lastname, city, idka, ruolo, location);
int size = staffRepository.getSize();
staffRepository.addStaff(newstaff);
int newsize = staffRepository.getSize();
assertEquals(newsize, size + 1);
Iterable<Staff> sad2 = staffRepository.findAll();
}
@Test
@Order(2)
void testUpdateStaff() {
// creo una locazione
String lname = "Library cosa 2";
String ladress = "via chissa dove";
Location location = new Location(lname, ladress);
// creo una seconda locazione
String newlName = "Library destra 5";
String newlAdress = "via prossima stella";
Location newlocation = new Location(newlName, newlAdress);
int losize = locationRepository.getSize();
locationRepository.addLocation(location);
locationRepository.addLocation(newlocation);
int newlosize = locationRepository.getSize();
assertEquals(newlosize, losize + 2); // ci sono 2 locazioni aggunte
// creo un membro dello staff
String firstname = "Maccio";
String lastname = "Donati";
String city = "Milano";
String idka = "lihwelhqluchwlir8";
String ruolo = "frontoffice";
Staff newstaff = new Staff(firstname, lastname, city, idka, ruolo);
int size = staffRepository.getSize();
staffRepository.addStaff(newstaff);
int newsize = staffRepository.getSize();
assertEquals(newsize, size + 1); // c'è 1 nuovo membro dello staff
Staff editStaff = staffRepository.searchStaffByidka(idka);
long id = editStaff.getId();
String newfirstname = "Ivans";
String newlastname = "Donatis";
String newcity = "Milanos";
String newidka = "lihwelhkluchwlir5";
String newruolo = "spazzino";
staffRepository.updateStaff(id, newfirstname, newlastname, newcity, newidka, newruolo, newlocation);
Optional<Staff> staff = staffRepository.findById(id);
staff.ifPresent(a -> {
assertEquals(a.getFirstName(), newfirstname);
assertEquals(a.getLastName(), newlastname);
assertEquals(a.getCity(), newcity);
assertEquals(a.getIdka(), newidka);
assertEquals(a.getRuolo(), newruolo);
assertEquals(a.getLocation().getName(), newlName);
assertEquals(a.getLocation().getAdress(), newlAdress);
});
}
@Test
@Order(3)
void testDeleteStaff() {
String firstname = "John";
String lastname = "Donati";
String city = "Milano";
String idka = "lihwelhsfghhwlir8";
String ruolo = "manager";
String lname = "Library siniz 2";
String ladress = "via sassa dove";
Location location = new Location(lname, ladress);
locationRepository.addLocation(location);
Staff newstaff = new Staff(firstname, lastname, city, idka, ruolo, location);
staffRepository.addStaff(newstaff);
Staff editStaff = staffRepository.searchStaffByidka(idka);
long ID = editStaff.getId();
staffRepository.deleteStaffById(ID);
;
int size = staffRepository.getSize();
assertEquals(size, 0);
}
}

@ -0,0 +1,109 @@
package mivan;
import static org.junit.jupiter.api.Assertions.*;
import java.util.Optional;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import mivan.model.User;
import mivan.repository.UserRepository;
import mivan.repository.UserRepositoryImpl;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
@TestMethodOrder(OrderAnnotation.class)
class UserTest {
private UserRepository userRepository = new UserRepositoryImpl();
@Test
@Order(1)
void testAddUser() {
String firstname = "Ivan";
String lastname = "Donati";
String city = "Milano";
String username = "p.donati";
String email = "p.donati@campus.unimib.it";
String password = "pdonatipassword";
User user = new User(firstname, lastname, city, username, email, password);
int size = userRepository.getSize();
userRepository.addUser(user);
int newsize = userRepository.getSize();
assertEquals(newsize, size + 1);
}
@Test
@Order(2)
void testUpdateCompany() {
String firstname = "Simone";
String lastname = "Donati";
String city = "Milano";
String username = "i.donati";
String email = "i.donati@campus.unimib.it";
String password = "passwordsicura";
User newuser = new User(firstname, lastname, city, username, email, password);
userRepository.addUser(newuser);
User editUser = userRepository.searchUserByName(username);
long id = editUser.getId();
String newfirstname = "Ivans";
String newlastname = "Donatis";
String newcity = "Milanos";
String newusername = "asd.donati1s";
String newemail = "asd.donati@campus.unimib.com";
String newpassword = "newpdonatipassword";
userRepository.updateUser(id, newfirstname, newlastname, newcity, newusername, newemail, newpassword);
Optional<User> user = userRepository.findById(id);
user.ifPresent(a -> {
assertEquals(a.getFirstName(), newfirstname);
assertEquals(a.getLastName(), newlastname);
assertEquals(a.getCity(), newcity);
assertEquals(a.getUsername(), newusername);
assertEquals(a.getEmail(), newemail);
assertEquals(a.getPassword(), newpassword);
});
}
@Test
@Order(3)
void testDeleteUser() {
String firstname = "Rossi";
String lastname = "Donati";
String city = "Milano";
String username = "r.donati";
String email = "r.donati@campus.unimib.it";
String password = "rdonatipassword";
User newuser = new User(firstname, lastname, city, username, email, password);
userRepository.addUser(newuser);
User editUser = userRepository.searchUserByName(username);
long ID = editUser.getId();
int size = userRepository.getSize();
userRepository.deleteUserById(ID);
int newsize = userRepository.getSize();
assertEquals(newsize, size - 1);
}
}

Binary file not shown.

@ -1,13 +0,0 @@
package com.unimib.disco.mivan.multibook;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MultibookApplication {
public static void main(String[] args) {
SpringApplication.run(MultibookApplication.class, args);
}
}

@ -1,24 +0,0 @@
package com.unimib.disco.mivan.multibook.controller;
import org.springframework.beans.factory.annotation.Autowired;
import com.unimib.disco.mivan.multibook.model.User;
import com.unimib.disco.mivan.multibook.repository.UserRepository;
public class UserController {
@Autowired
UserRepository userRepository;
public void createUser(String firstName, String lastName, String city,String username, String email, String password) {
User user = new User(firstName, lastName, city,username,email,password);
userRepository.save(user);
}
}

@ -1,55 +0,0 @@
/*package com.unimib.disco.mivan.multibook.model;
import java.util.List;
import javax.persistence.*;
public class Author {
@Id
@Column(name = "id", nullable=false)
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column(name = "firstName", nullable=false)
private String artName;
@ManyToMany
List<Book> publications;
public Author() {
// TODO Auto-generated constructor stub
}
public Author(String artName, List<Book> publications) {
super();
this.artName = artName;
this.publications = publications;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getArtName() {
return artName;
}
public void setArtName(String artName) {
this.artName = artName;
}
public List<Book> getPublications() {
return publications;
}
public void setPublications(List<Book> publications) {
this.publications = publications;
}
}*/

@ -1,68 +0,0 @@
package com.unimib.disco.mivan.multibook.model;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.OneToOne;
@Entity
public class Book {
@Id
@Column(name = "isbn", nullable=false)
private int isbn;
@Column(name = "titolo", nullable=false)
private String titolo;
@OneToOne
private Book prequel;
/*
@ManyToMany
List<Author> authors;
*/
public Book() {
// TODO Auto-generated constructor stub
}
public Book(int isbn, String titolo, Book prequel/*, List<Author> authors*/) {
super();
this.isbn = isbn;
this.titolo = titolo;
this.prequel = prequel;
//this.authors = authors;
}
public int getIsbn() {
return isbn;
}
public void setIsbn(int isbn) {
this.isbn = isbn;
}
public String getTitolo() {
return titolo;
}
public void setTitolo(String titolo) {
this.titolo = titolo;
}
public Book getPrequel() {
return prequel;
}
public void setPrequel(Book prequel) {
this.prequel = prequel;
}
}

@ -1,65 +0,0 @@
package com.unimib.disco.mivan.multibook.model;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
public class Item{
@Id
@Column(name = "id", nullable=false)
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@ManyToOne
private Location location;
@ManyToOne
private Book libro;
public Item() {
// TODO Auto-generated constructor stub
}
public Item(Book libro,Location location) {
this.libro = libro;
this.location=location;
// TODO Auto-generated constructor stub
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Location getLocation() {
return location;
}
public void setLocation(Location location) {
this.location = location;
}
public Book getLibro() {
return libro;
}
public void setLibro(Book libro) {
this.libro = libro;
}
}

@ -1,101 +0,0 @@
package com.unimib.disco.mivan.multibook.model;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
public class Loan {
@Id
@Column(name = "id", nullable=false)
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@ManyToOne
private Item item;
@ManyToOne
private Staff staff;
@ManyToOne
private User user;
@Column(name = "start")
@Temporal(TemporalType.TIMESTAMP)
Date start;
@Column(name = "end")
@Temporal(TemporalType.TIMESTAMP)
Date end;
public Loan() {
// TODO Auto-generated constructor stub
}
public Loan(int id, Item item, Staff staff, User user, Date start, Date end) {
super();
this.item = item;
this.staff = staff;
this.user = user;
this.start = start;
this.end = end;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Item getItem() {
return item;
}
public void setItem(Item item) {
this.item = item;
}
public Staff getStaff() {
return staff;
}
public void setStaff(Staff staff) {
this.staff = staff;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Date getStart() {
return start;
}
public void setStart(Date start) {
this.start = start;
}
public Date getEnd() {
return end;
}
public void setEnd(Date end) {
this.end = end;
}
}

@ -1,36 +0,0 @@
package com.unimib.disco.mivan.multibook.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Location {
@Id
@Column(name = "id", nullable=false)
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column(name = "name", nullable=false)
private String name;
@Column(name = "address", nullable=false)
private String address;
@Column(name = "city", nullable=false)
private String city;
public Location() {
// TODO Auto-generated constructor stub
}
public Location(String name, String address, String city) {
super();
this.name = name;
this.address = address;
this.city = city;
}
}

@ -1,26 +0,0 @@
package com.unimib.disco.mivan.multibook.model;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToOne;
@Entity
public class Staff extends User {
@ManyToOne(fetch = FetchType.LAZY)
private Location library;
public Staff() {
// TODO Auto-generated constructor stub
}
public Staff(String firstName, String lastName, String city, String username, String email,
String password, Location library) {
super(firstName, lastName, city, username, email, password);
this.library=library;
// TODO Auto-generated constructor stub
}
}

@ -1,67 +0,0 @@
package com.unimib.disco.mivan.multibook.model;
import javax.persistence.*;
@Entity
public class User extends Person {
@Column(name = "username", nullable=false,unique=true)
private String username;
@Column(name = "email", nullable=false,unique=true)
private String email;
@Column(name = "password", nullable=false)
private String password;
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(String firstName, String lastName, String city, String username, String email, String password) {
super(firstName, lastName, city);
this.username = username;
this.email = email;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}

@ -1,17 +0,0 @@
package com.unimib.disco.mivan.multibook.repository;
import java.util.List;
import org.springframework.data.repository.CrudRepository;
import com.unimib.disco.mivan.multibook.model.Person;
public interface PersonRepository <T extends Person> extends CrudRepository<T, Long> {
List<T> findByFirstNameAllIgnoreCase(String firstName);
List<T> findByLastNameAllIgnoreCase(String lastName);
List<T> findByirstNameAndLastNameAllIgnoreCase(String firstName, String lastName);
}

@ -1,19 +0,0 @@
package com.unimib.disco.mivan.multibook.repository;
import com.unimib.disco.mivan.multibook.model.User;
import java.util.Optional;
public interface UserRepository extends PersonRepository<User> {
public Optional<User> findById(Long id);
public User findByEmail(String email);
public User findByUsername(String username);
}

@ -1,37 +0,0 @@
package com.unimib.disco.mivan.multibook.service;
import java.util.List;
import java.util.Optional;
import com.unimib.disco.mivan.multibook.model.User;
import com.unimib.disco.mivan.multibook.repository.UserRepository;
public class UserService {
private UserRepository userRepository;
public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}
/*
public List<User> getClients(){
return Lists.newArrayList(userRepository.findAll());
}*/
public Optional<User> getClient(Long id) {
return userRepository.findById(id);
}
public List<User> getClientsByNameAndSurname(String name, String surname) {
return userRepository.findByirstNameAndLastNameAllIgnoreCase(name, surname);
}
public void deleteClient(User client) {
userRepository.delete(client);
}
public User insertOrUpdateClient(User client) {
return userRepository.save(client);
}
}

@ -1,23 +0,0 @@
spring.datasource.url=jdbc:h2:./database;
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=librarymenagment
spring.datasource.password=password123!
spring.datasource.initialization-mode=always
spring.h2.console.enabled=true
spring.h2.console.path=/h2
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
# Used for initialization of db.
# !!! Remove comment for initialize db first time. !!!
# spring.datasource.initialization-mode=always

@ -1,13 +0,0 @@
package com.unimib.disco.mivan.multibook;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class MultibookApplicationTests {
@Test
void contextLoads() {
}
}

@ -1,33 +0,0 @@
package com.unimib.disco.mivan.multibook;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.springframework.boot.test.context.SpringBootTest;
import com.unimib.disco.mivan.multibook.model.User;
import com.unimib.disco.mivan.multibook.service.UserService;
@SpringBootTest
public class UserTests {
//private UserRepository userRepository = new UserRepository();
private UserService userRepository;
@Test
void userCreation() {
User asd = new User("Ivan", "Donati", "Novara", "territory", "ciao.iva@gmail.com", "sonounapassword");
userRepository.insertOrUpdateClient(asd);
//userRepository.insertOrUpdateClient("Ivan", "Donati", "Novara", "territory", "ciao.iva@gmail.com", "sonounapassword");
}
}
Loading…
Cancel
Save