Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
tire95 committed Nov 30, 2020
2 parents 9a2aa64 + dcdc809 commit 4afedf7
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public DatabaseRecommendationDao(String filename) {
this.fileName = filename;
connect();
createBookTable();
createVideoTable();
createTimeStampTable();
}

private Connection connect() {
Expand Down Expand Up @@ -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(?,?,?)";
Expand Down Expand Up @@ -211,6 +214,24 @@ public List<VideoRecommendation> getAllVideoRecommendations() {
return videos;
}

@Override
public List<TimeMemory> getAllTimestampsForVideo(int videId) {
ArrayList<TimeMemory> 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 = ?";
Expand All @@ -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 searchVideoByTitle(String title) {
String sql = "SELECT id FROM videos WHERE title = ?";
int id = 0;
Expand Down Expand Up @@ -255,20 +291,6 @@ public void deleteBookByTitle(String title) {
}
}

@Override
public void editVideoRecommendation(String title, String fieldToBeEdited, String newValue) {
String sql = "UPDATE videos SET " + fieldToBeEdited + " = ? WHERE title = ?";
try {
Connection conn = this.connect();
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, newValue);
pstmt.setString(2, title);
pstmt.executeUpdate();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}

@Override
public void deleteVideoByTitle(String title) {
int videoId = searchVideoByTitle(title);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -71,6 +72,21 @@ public void editVideoRecommendation(String title, String fieldToBeEdited, String
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public int searchVideoByTitle(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<TimeMemory> getAllTimestampsForVideo(int videId) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public void deleteVideoByTitle(String title) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -18,13 +19,17 @@ 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<BookRecommendation> getAllBookRecommendations();
List<VideoRecommendation> getAllVideoRecommendations();
List<TimeMemory> getAllTimestampsForVideo(int videId);

void editBookRecommendation(String title, String fieldToBeEdited, String newValue);
void editVideoRecommendation(String title, String fieldToBeEdited, String newValue);

void deleteBookByTitle(String title);

void editVideoRecommendation(String title, String fieldToBeEdited, String newValue);
void deleteVideoByTitle(String title);
int searchVideoByTitle(String title);
}
21 changes: 21 additions & 0 deletions src/main/java/recommendation_library/domain/DatabaseService.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,25 @@ public boolean deleteRecommendation(String title) {
}
return false;
}

public boolean addTimeStampToVideo(String timestamp, String comment, String videoTitle) {
int id = dao.searchVideoByTitle(videoTitle);
if (id == 0) {
return false;
}
dao.addTimeStampToVideo(id, timestamp, comment);
return true;
}

public List<TimeMemory> getTimetampsForVideo(String videoTitle) {
List<TimeMemory> timestamps = new ArrayList<>();
int id = dao.searchVideoByTitle(videoTitle);
if (id == 0) {
return timestamps;
}
for (TimeMemory timestamp : dao.getAllTimestampsForVideo(id)) {
timestamps.add(timestamp);
}
return timestamps;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
public class VideoRecommendation extends Recommendation{

String url;
List<Timestamp> timestamps = new ArrayList<>();
List<TimeMemory> timestamps = new ArrayList<>();


public VideoRecommendation(String url, String title, String description, String addDate) {
super(title, Type.VIDEO, description, addDate);
this.url = url;
}

public void addTimestamp(Timestamp timestamp) {
public void addTimestamp(TimeMemory timestamp) {
this.timestamps.add(timestamp);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
*/
public class DatabasaRecommendationDaoTest {

IO io;
UserInterface ui;
RecommendationDao db_dao;
DatabaseService service;

Expand All @@ -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<BookRecommendation> books = db_dao.getAllBookRecommendations();
for (BookRecommendation b : books) {
Expand All @@ -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());

Expand Down

0 comments on commit 4afedf7

Please sign in to comment.