diff --git a/db/mysql-with-inserts.sql b/db/mysql-with-inserts.sql index 29e70f4b34e217f37c9636561bc1f9f58e36e396..b9b6c59e8bcdf9d30a705d07f86fb17ea0d8f80c 100644 --- a/db/mysql-with-inserts.sql +++ b/db/mysql-with-inserts.sql @@ -35,11 +35,11 @@ INSERT INTO `daaexample`.`people` (`id`,`name`,`surname`) VALUES (0,'Alba','Fern INSERT INTO `daaexample`.`people` (`id`,`name`,`surname`) VALUES (0,'Asunción','Jiménez'); -- Add inserts to pets -INSERT INTO `daaexample`.`pet` (`idPet`,`name`,`idMaster`) VALUES (0,'Jimminy',0); -INSERT INTO `daaexample`.`pet` (`idPet`,`name`,`idMaster`) VALUES (0,'Cisco',0); -INSERT INTO `daaexample`.`pet` (`idPet`,`name`,`idMaster`) VALUES (0,'Phoenix',1); -INSERT INTO `daaexample`.`pet` (`idPet`,`name`,`idMaster`) VALUES (0,'Rocky',1); -INSERT INTO `daaexample`.`pet` (`idPet`,`name`,`idMaster`) VALUES (0,'Tallulah',2); +INSERT INTO `daaexample`.`pet` (`idPet`,`name`,`idMaster`) VALUES (0,'Jimminy',1); +INSERT INTO `daaexample`.`pet` (`idPet`,`name`,`idMaster`) VALUES (0,'Cisco',2); +INSERT INTO `daaexample`.`pet` (`idPet`,`name`,`idMaster`) VALUES (0,'Phoenix',3); +INSERT INTO `daaexample`.`pet` (`idPet`,`name`,`idMaster`) VALUES (0,'Rocky',3); +INSERT INTO `daaexample`.`pet` (`idPet`,`name`,`idMaster`) VALUES (0,'Tallulah',4); diff --git a/src/main/java/es/uvigo/esei/daa/dao/PetDAO.java b/src/main/java/es/uvigo/esei/daa/dao/PetDAO.java new file mode 100644 index 0000000000000000000000000000000000000000..c9394734a5f580be13e06004c5ae697f856df1f3 --- /dev/null +++ b/src/main/java/es/uvigo/esei/daa/dao/PetDAO.java @@ -0,0 +1,190 @@ +package es.uvigo.esei.daa.dao; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.LinkedList; +import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; + + +import es.uvigo.esei.daa.entities.Pet; + + +public class PetDAO extends DAO { + private final static Logger LOG = Logger.getLogger(PeopleDAO.class.getName()); + + + public Pet get(int idPet) + throws DAOException, IllegalArgumentException { + try (final Connection conn = this.getConnection()) { + final String query = "SELECT * FROM pet WHERE idPet=?"; + + try (final PreparedStatement statement = conn.prepareStatement(query)) { + statement.setInt(1, idPet); + + try (final ResultSet result = statement.executeQuery()) { + if (result.next()) { + return rowToEntity(result); + } else { + throw new IllegalArgumentException("Invalid idPet"); + } + } + } + } catch (SQLException e) { + LOG.log(Level.SEVERE, "Error getting a pet", e); + throw new DAOException(e); + } + } + + + public List list() throws DAOException { + try (final Connection conn = this.getConnection()) { + final String query = "SELECT * FROM pet"; + + try (final PreparedStatement statement = conn.prepareStatement(query)) { + try (final ResultSet result = statement.executeQuery()) { + final List pets = new LinkedList<>(); + + while (result.next()) { + pets.add(rowToEntity(result)); + } + + return pets; + } + } + } catch (SQLException e) { + LOG.log(Level.SEVERE, "Error listing pets", e); + throw new DAOException(e); + } + } + + + + + + + public List listAllPetsOfPerson(int idMaster) throws DAOException { + try (final Connection conn = this.getConnection()) { + final String query = "SELECT * FROM pet WHERE idMaster=?"; + + try (final PreparedStatement statement = conn.prepareStatement(query)) { + + statement.setInt(1, idMaster); + + try (final ResultSet result = statement.executeQuery()) { + final List pets = new LinkedList<>(); + + while (result.next()) { + pets.add(rowToEntity(result)); + } + + return pets; + } + } + } catch (SQLException e) { + LOG.log(Level.SEVERE, "Error listing pets", e); + throw new DAOException(e); + } + } + + + + + + + public Pet add(String name, int idMaster) + throws DAOException, IllegalArgumentException { + if (name == null ) { + throw new IllegalArgumentException("name can't be null"); + } + + try (Connection conn = this.getConnection()) { + final String query = "INSERT INTO pet VALUES(null, ?, ?)"; + + try (PreparedStatement statement = conn.prepareStatement(query, Statement.RETURN_GENERATED_KEYS)) { + statement.setString(1, name); + statement.setInt(2, idMaster); + + if (statement.executeUpdate() == 1) { + try (ResultSet resultKeys = statement.getGeneratedKeys()) { + if (resultKeys.next()) { + return new Pet(resultKeys.getInt(1), name, idMaster); + } else { + LOG.log(Level.SEVERE, "Error retrieving inserted id"); + throw new SQLException("Error retrieving inserted id"); + } + } + } else { + LOG.log(Level.SEVERE, "Error inserting value"); + throw new SQLException("Error inserting value"); + } + } + } catch (SQLException e) { + LOG.log(Level.SEVERE, "Error adding a pet", e); + throw new DAOException(e); + } + } + + + + + + public void modify(Pet pet) + throws DAOException, IllegalArgumentException { + if (pet == null) { + throw new IllegalArgumentException("pet can't be null"); + } + + try (Connection conn = this.getConnection()) { + final String query = "UPDATE pet SET name=?, idMaster=? WHERE idPet=?"; + + try (PreparedStatement statement = conn.prepareStatement(query)) { + statement.setString(1, pet.getName()); + statement.setInt(2, pet.getIdMaster()); + statement.setInt(3, pet.getIdPet()); + + if (statement.executeUpdate() != 1) { + throw new IllegalArgumentException("name can't be null"); + } + } + } catch (SQLException e) { + LOG.log(Level.SEVERE, "Error modifying a pet", e); + throw new DAOException(); + } + } + + + + public void delete(int idPet) + throws DAOException, IllegalArgumentException { + try (final Connection conn = this.getConnection()) { + final String query = "DELETE FROM pet WHERE idPet=?"; + + try (final PreparedStatement statement = conn.prepareStatement(query)) { + statement.setInt(1, idPet); + + if (statement.executeUpdate() != 1) { + throw new IllegalArgumentException("Invalid idPet"); + } + } + } catch (SQLException e) { + LOG.log(Level.SEVERE, "Error deleting a pet", e); + throw new DAOException(e); + } + } + + + + + private Pet rowToEntity(ResultSet row) throws SQLException { + return new Pet( + row.getInt("idPet"), + row.getString("name"), + row.getInt("idMaster") + ); + } +} \ No newline at end of file diff --git a/src/test/java/es/uvigo/esei/daa/suites/UnitTestSuite.java b/src/test/java/es/uvigo/esei/daa/suites/UnitTestSuite.java index a21ca3663b624f6cf2ea326a347de55ae8f96ca9..7b311954312c9a760a01e0c744e4017b98e6db38 100644 --- a/src/test/java/es/uvigo/esei/daa/suites/UnitTestSuite.java +++ b/src/test/java/es/uvigo/esei/daa/suites/UnitTestSuite.java @@ -6,10 +6,12 @@ import org.junit.runners.Suite.SuiteClasses; import es.uvigo.esei.daa.dao.PeopleDAOUnitTest; import es.uvigo.esei.daa.entities.PersonUnitTest; +import es.uvigo.esei.daa.entities.PetUnitTest; import es.uvigo.esei.daa.rest.PeopleResourceUnitTest; @SuiteClasses({ PersonUnitTest.class, + PetUnitTest.class, PeopleDAOUnitTest.class, PeopleResourceUnitTest.class })