diff -r 1574965c7dc7 -r 57cfb94ab99f src/main/java/de/uapcore/lightpit/dao/postgres/PGProjectDao.java --- a/src/main/java/de/uapcore/lightpit/dao/postgres/PGProjectDao.java Wed May 13 21:46:26 2020 +0200 +++ b/src/main/java/de/uapcore/lightpit/dao/postgres/PGProjectDao.java Thu May 14 22:48:01 2020 +0200 @@ -28,7 +28,7 @@ */ package de.uapcore.lightpit.dao.postgres; -import de.uapcore.lightpit.dao.AbstractDao; +import de.uapcore.lightpit.dao.GenericDao; import de.uapcore.lightpit.dao.ProjectDao; import de.uapcore.lightpit.entities.Project; import de.uapcore.lightpit.entities.User; @@ -37,18 +37,31 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; import java.util.Objects; -public final class PGProjectDao extends AbstractDao implements ProjectDao { +import static de.uapcore.lightpit.dao.Functions.setForeignKeyOrNull; +import static de.uapcore.lightpit.dao.Functions.setStringOrNull; - private final PGUserDao userDao; +public final class PGProjectDao implements ProjectDao, GenericDao { + + private final PreparedStatement insert, update, list, find; - private final PreparedStatement insert; - private final PreparedStatement update; + public PGProjectDao(Connection connection) throws SQLException { + list = connection.prepareStatement( + "select id, name, description, repourl, " + + "userid, username, lastname, givenname, mail " + + "from lpit_project " + + "left join lpit_user owner on lpit_project.owner = owner.userid " + + "order by name"); - public PGProjectDao(Connection connection, PGUserDao userDao) throws SQLException { - super(connection.prepareStatement( - "select * from lpit_project join lpit_user owner on lpit_project.owner = owner.userid")); + find = connection.prepareStatement( + "select id, name, description, repourl, " + + "userid, username, lastname, givenname, mail " + + "from lpit_project " + + "left join lpit_user owner on lpit_project.owner = owner.userid " + + "where id = ?"); insert = connection.prepareStatement( "insert into lpit_project (name, description, repourl, owner) values (?, ?, ?, ?)" @@ -56,17 +69,24 @@ update = connection.prepareStatement( "update lpit_project set name = ?, description = ?, repourl = ?, owner = ? where id = ?" ); - - this.userDao = userDao; } - @Override - public Project mapColumns(ResultSet result, String q) throws SQLException { - final var proj = new Project(result.getInt(qual(q, "id"))); - proj.setName(result.getString(qual(q, "name"))); - proj.setDescription(result.getString(qual(q, "description"))); - proj.setRepoUrl(result.getString(qual(q, "repourl"))); - proj.setOwner(userDao.mapColumns(result, "owner")); + public Project mapColumns(ResultSet result) throws SQLException { + final var proj = new Project(result.getInt("id")); + proj.setName(result.getString("name")); + proj.setDescription(result.getString("description")); + proj.setRepoUrl(result.getString("repourl")); + + final int id = result.getInt("userid"); + if (id != 0) { + final var user = new User(id); + user.setUsername(result.getString("username")); + user.setGivenname(result.getString("givenname")); + user.setLastname(result.getString("lastname")); + user.setMail(result.getString("mail")); + proj.setOwner(user); + } + return proj; } @@ -87,6 +107,30 @@ setStringOrNull(update, 2, instance.getDescription()); setStringOrNull(update, 3, instance.getRepoUrl()); setForeignKeyOrNull(update, 4, instance.getOwner(), User::getUserID); + update.setInt(5, instance.getId()); return update.executeUpdate() > 0; } + + @Override + public List list() throws SQLException { + List projects = new ArrayList<>(); + try (var result = list.executeQuery()) { + while (result.next()) { + projects.add(mapColumns(result)); + } + } + return projects; + } + + @Override + public Project find(int id) throws SQLException { + find.setInt(1, id); + try (var result = find.executeQuery()) { + if (result.next()) { + return mapColumns(result); + } else { + return null; + } + } + } }