From cdb49fdb70f233c6bb5c9e73e78ef9a7e3a5706a Mon Sep 17 00:00:00 2001 From: Anastasia Diseth Date: Mon, 30 Nov 2020 14:56:43 +0200 Subject: [PATCH 1/2] DatabaseService has now methods for timestamps --- .../dao/DatabaseRecommendationDao.java | 36 +++++++++++++++++++ .../dao/InMemoryRecommendationDao.java | 21 +++++++++++ .../dao/RecommendationDao.java | 7 ++++ .../domain/DatabaseService.java | 21 +++++++++++ .../{Timestamp.java => TimeMemory.java} | 4 +-- .../domain/VideoRecommendation.java | 4 +-- 6 files changed, 89 insertions(+), 4 deletions(-) rename src/main/java/recommendation_library/domain/{Timestamp.java => TimeMemory.java} (87%) diff --git a/src/main/java/recommendation_library/dao/DatabaseRecommendationDao.java b/src/main/java/recommendation_library/dao/DatabaseRecommendationDao.java index 8ac09a0..ce34739 100644 --- a/src/main/java/recommendation_library/dao/DatabaseRecommendationDao.java +++ b/src/main/java/recommendation_library/dao/DatabaseRecommendationDao.java @@ -23,6 +23,8 @@ public DatabaseRecommendationDao(String filename) { this.fileName = filename; connect(); createBookTable(); + createVideoTable(); + createTimeStampTable(); } private Connection connect() { @@ -154,6 +156,7 @@ public void createVideoRecommendation(String url, String title, String descripti * @param timestamp * @param comment */ + @Override public void addTimeStampToVideo(int videoId, String timestamp, String comment) { String sql = "INSERT INTO timestamps(timestamp, comment, video_id) " + "VALUES(?,?,?)"; @@ -211,6 +214,24 @@ public List getAllVideoRecommendations() { return videos; } + @Override + public List getAllTimestampsForVideo(int videId) { + ArrayList timestamps = new ArrayList<>(); + String sql = "SELECT * FROM timestamps WHERE video_id = ?"; + try { + Connection connection = this.connect(); + PreparedStatement pstatement = connection.prepareStatement(sql); + pstatement.setInt(1, videId); + ResultSet result = pstatement.executeQuery(); + while (result.next()) { + timestamps.add(new TimeMemory(result.getString("timestamp"), + result.getString("comment"), result.getInt("video_id"))); + } + } catch (SQLException e) { + System.out.println(e.getMessage()); + } + return timestamps; + } @Override public void editBookRecommendation(String title, String fieldToBeEdited, String newValue) { String sql = "UPDATE books SET " + fieldToBeEdited + " = ? WHERE title = ?"; @@ -224,7 +245,22 @@ public void editBookRecommendation(String title, String fieldToBeEdited, String System.out.println(e.getMessage()); } } + + @Override + public void editVideoRecommendation(String title, String fieldToBeEdited, String newValue) { + String sql = "UPDATE videos SET " + fieldToBeEdited + " = ? WHERE title = ?"; + try { + Connection conn = this.connect(); + PreparedStatement preparedstmt = conn.prepareStatement(sql); + preparedstmt.setString(1, newValue); + preparedstmt.setString(2, title); + preparedstmt.executeUpdate(); + } catch (SQLException e) { + System.out.println(e.getMessage()); + } + } + @Override public int searchViedoByTitle(String title) { String sql = "SELECT id FROM videos WHERE title = ?"; int id = 0; diff --git a/src/main/java/recommendation_library/dao/InMemoryRecommendationDao.java b/src/main/java/recommendation_library/dao/InMemoryRecommendationDao.java index 4b94100..c99bb44 100644 --- a/src/main/java/recommendation_library/dao/InMemoryRecommendationDao.java +++ b/src/main/java/recommendation_library/dao/InMemoryRecommendationDao.java @@ -9,6 +9,7 @@ import java.util.ArrayList; import java.util.Date; import java.util.List; +import recommendation_library.domain.TimeMemory; import recommendation_library.domain.Type; import recommendation_library.domain.VideoRecommendation; @@ -66,6 +67,26 @@ public List getAllVideoRecommendations() { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } + @Override + public void editVideoRecommendation(String title, String fieldToBeEdited, String newValue) { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public int searchViedoByTitle(String title) { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public void addTimeStampToVideo(int videoId, String timestamp, String comment) { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public List getAllTimestampsForVideo(int videId) { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + } diff --git a/src/main/java/recommendation_library/dao/RecommendationDao.java b/src/main/java/recommendation_library/dao/RecommendationDao.java index 42cdd7e..32b7541 100644 --- a/src/main/java/recommendation_library/dao/RecommendationDao.java +++ b/src/main/java/recommendation_library/dao/RecommendationDao.java @@ -7,6 +7,7 @@ import recommendation_library.domain.BookRecommendation; import java.util.List; +import recommendation_library.domain.TimeMemory; import recommendation_library.domain.Type; import recommendation_library.domain.VideoRecommendation; @@ -18,10 +19,16 @@ public interface RecommendationDao { void createBookRecommendation(String author, String title, String description, String isbn, int pageCount); void createVideoRecommendation(String url, String title, String description); + void addTimeStampToVideo(int videoId, String timestamp, String comment); List getAllBookRecommendations(); List getAllVideoRecommendations(); + List getAllTimestampsForVideo(int videId); void editBookRecommendation(String title, String fieldToBeEdited, String newValue); + void editVideoRecommendation(String title, String fieldToBeEdited, String newValue); + void deleteBookByTitle(String title); + + int searchViedoByTitle(String title); } diff --git a/src/main/java/recommendation_library/domain/DatabaseService.java b/src/main/java/recommendation_library/domain/DatabaseService.java index 8d67f68..c9bc4ec 100644 --- a/src/main/java/recommendation_library/domain/DatabaseService.java +++ b/src/main/java/recommendation_library/domain/DatabaseService.java @@ -88,4 +88,25 @@ public boolean deleteRecommendation(String title) { } return false; } + + public boolean addTimeStampToVideo(String timestamp, String comment, String videoTitle) { + int id = dao.searchViedoByTitle(videoTitle); + if (id == 0) { + return false; + } + dao.addTimeStampToVideo(id, timestamp, comment); + return true; + } + + public List getTimetampsForVideo(String videoTitle) { + List timestamps = new ArrayList<>(); + int id = dao.searchViedoByTitle(videoTitle); + if (id == 0) { + return timestamps; + } + for (TimeMemory timestamp : dao.getAllTimestampsForVideo(id)) { + timestamps.add(timestamp); + } + return timestamps; + } } diff --git a/src/main/java/recommendation_library/domain/Timestamp.java b/src/main/java/recommendation_library/domain/TimeMemory.java similarity index 87% rename from src/main/java/recommendation_library/domain/Timestamp.java rename to src/main/java/recommendation_library/domain/TimeMemory.java index 491c7d4..7315cf1 100644 --- a/src/main/java/recommendation_library/domain/Timestamp.java +++ b/src/main/java/recommendation_library/domain/TimeMemory.java @@ -9,13 +9,13 @@ * * @author anadis */ -public class Timestamp { +public class TimeMemory { private String timestamp; private String comment; private int videoId; - public Timestamp(String timestamp, String comment, int videoId) { + public TimeMemory(String timestamp, String comment, int videoId) { this.comment = comment; this.timestamp = timestamp; this.videoId = videoId; diff --git a/src/main/java/recommendation_library/domain/VideoRecommendation.java b/src/main/java/recommendation_library/domain/VideoRecommendation.java index 126561c..45cf094 100644 --- a/src/main/java/recommendation_library/domain/VideoRecommendation.java +++ b/src/main/java/recommendation_library/domain/VideoRecommendation.java @@ -7,7 +7,7 @@ public class VideoRecommendation extends Recommendation{ String url; - List timestamps = new ArrayList<>(); + List timestamps = new ArrayList<>(); public VideoRecommendation(String url, String title, String description, String addDate) { @@ -15,7 +15,7 @@ public VideoRecommendation(String url, String title, String description, String this.url = url; } - public void addTimestamp(Timestamp timestamp) { + public void addTimestamp(TimeMemory timestamp) { this.timestamps.add(timestamp); } From dcdc8093eed9df5c74dffa8ef14184cf9e554993 Mon Sep 17 00:00:00 2001 From: Anastasia Diseth Date: Mon, 30 Nov 2020 15:23:05 +0200 Subject: [PATCH 2/2] refaktoroi DatabaseDaoTest class to not depend on ui or io --- .../dao/DatabasaRecommendationDaoTest.java | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/src/test/java/recommendation_library/dao/DatabasaRecommendationDaoTest.java b/src/test/java/recommendation_library/dao/DatabasaRecommendationDaoTest.java index 70c545b..c1e1b27 100644 --- a/src/test/java/recommendation_library/dao/DatabasaRecommendationDaoTest.java +++ b/src/test/java/recommendation_library/dao/DatabasaRecommendationDaoTest.java @@ -21,8 +21,6 @@ */ public class DatabasaRecommendationDaoTest { - IO io; - UserInterface ui; RecommendationDao db_dao; DatabaseService service; @@ -33,8 +31,6 @@ public DatabasaRecommendationDaoTest() { public void setUp() { db_dao = new DatabaseRecommendationDao("src/test/resources/test.db"); service = new DatabaseService(db_dao); - io = mock(IO.class); - ui = new UserInterface(io, db_dao); List books = db_dao.getAllBookRecommendations(); for (BookRecommendation b : books) { @@ -44,15 +40,7 @@ public void setUp() { @Test public void createRecommendationAddsToTheDatabase() { - when(io.nextLine()) - .thenReturn("Jane") - .thenReturn("Hobitti") - .thenReturn("Sci-fi thriller") - .thenReturn("1234-ABCD") - .thenReturn("10"); - - ui.addBook(); - verify(io, times(5)).nextLine(); + service.addBook("Jane", "Hobitti", "Sci-fi thriller", "1234-ABCD", 10); assertFalse(service.getAllBookRecommendations().isEmpty());