Add petDAO

parent cf4e4de2
...@@ -35,11 +35,11 @@ INSERT INTO `daaexample`.`people` (`id`,`name`,`surname`) VALUES (0,'Alba','Fern ...@@ -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'); INSERT INTO `daaexample`.`people` (`id`,`name`,`surname`) VALUES (0,'Asunción','Jiménez');
-- Add inserts to pets -- Add inserts to pets
INSERT INTO `daaexample`.`pet` (`idPet`,`name`,`idMaster`) VALUES (0,'Jimminy',0); INSERT INTO `daaexample`.`pet` (`idPet`,`name`,`idMaster`) VALUES (0,'Jimminy',1);
INSERT INTO `daaexample`.`pet` (`idPet`,`name`,`idMaster`) VALUES (0,'Cisco',0); INSERT INTO `daaexample`.`pet` (`idPet`,`name`,`idMaster`) VALUES (0,'Cisco',2);
INSERT INTO `daaexample`.`pet` (`idPet`,`name`,`idMaster`) VALUES (0,'Phoenix',1); INSERT INTO `daaexample`.`pet` (`idPet`,`name`,`idMaster`) VALUES (0,'Phoenix',3);
INSERT INTO `daaexample`.`pet` (`idPet`,`name`,`idMaster`) VALUES (0,'Rocky',1); INSERT INTO `daaexample`.`pet` (`idPet`,`name`,`idMaster`) VALUES (0,'Rocky',3);
INSERT INTO `daaexample`.`pet` (`idPet`,`name`,`idMaster`) VALUES (0,'Tallulah',2); INSERT INTO `daaexample`.`pet` (`idPet`,`name`,`idMaster`) VALUES (0,'Tallulah',4);
......
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<Pet> 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<Pet> 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<Pet> 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<Pet> 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
...@@ -6,10 +6,12 @@ import org.junit.runners.Suite.SuiteClasses; ...@@ -6,10 +6,12 @@ import org.junit.runners.Suite.SuiteClasses;
import es.uvigo.esei.daa.dao.PeopleDAOUnitTest; import es.uvigo.esei.daa.dao.PeopleDAOUnitTest;
import es.uvigo.esei.daa.entities.PersonUnitTest; import es.uvigo.esei.daa.entities.PersonUnitTest;
import es.uvigo.esei.daa.entities.PetUnitTest;
import es.uvigo.esei.daa.rest.PeopleResourceUnitTest; import es.uvigo.esei.daa.rest.PeopleResourceUnitTest;
@SuiteClasses({ @SuiteClasses({
PersonUnitTest.class, PersonUnitTest.class,
PetUnitTest.class,
PeopleDAOUnitTest.class, PeopleDAOUnitTest.class,
PeopleResourceUnitTest.class PeopleResourceUnitTest.class
}) })
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment