diff --git a/src/main/java/recommendation_library/RecommendationApp.java b/src/main/java/recommendation_library/RecommendationApp.java index a550091..67571c6 100644 --- a/src/main/java/recommendation_library/RecommendationApp.java +++ b/src/main/java/recommendation_library/RecommendationApp.java @@ -101,6 +101,7 @@ public boolean addBlog(String title, String author, String description, String u public boolean addTagToBook(String title, String tagText) { try { + System.err.println(title); return service.addTagToBook(title, tagText); } catch (Exception e) { this.io.print(e.getMessage()); diff --git a/src/main/java/recommendation_library/dao/InMemoryRecommendationDao.java b/src/main/java/recommendation_library/dao/InMemoryRecommendationDao.java index 994891a..1c674fa 100644 --- a/src/main/java/recommendation_library/dao/InMemoryRecommendationDao.java +++ b/src/main/java/recommendation_library/dao/InMemoryRecommendationDao.java @@ -21,6 +21,10 @@ public class InMemoryRecommendationDao implements RecommendationDao { private List blogRecommendations; private List podcastRecommendations; private List tags; + private Map> tagsBooks; + private Map> tagsVideos; + private Map> tagsBlogs; + private Map> tagsPodcasts; public InMemoryRecommendationDao() { this.bookRecommendations = new ArrayList<>(); @@ -29,6 +33,10 @@ public InMemoryRecommendationDao() { this.blogRecommendations = new ArrayList<>(); this.podcastRecommendations = new ArrayList<>(); this.tags = new ArrayList<>(); + tagsBooks = new HashMap<>(); + tagsBlogs = new HashMap<>(); + tagsVideos = new HashMap<>(); + tagsPodcasts = new HashMap<>(); } @Override @@ -105,7 +113,6 @@ private void editVideoMatchingField(VideoRecommendation v, String fieldToBeEdite map.put("title", v::setTitle); map.put("url", v::setUrl); map.put("description", v::setDescription); - System.err.println(fieldToBeEdited); map.get(fieldToBeEdited).apply(newValue); } @@ -118,7 +125,7 @@ public int getVideoIdByTitle(String title) { } return 0; } - + @Override public int getBookIdByTitle(String title) { for (BookRecommendation b : bookRecommendations) { @@ -128,7 +135,7 @@ public int getBookIdByTitle(String title) { } return 0; } - + @Override public int getBlogIdByTitle(String title) { for (BlogRecommendation b : blogRecommendations) { @@ -138,7 +145,7 @@ public int getBlogIdByTitle(String title) { } return 0; } - + @Override public int getPodcastIdByTitle(String title) { for (PodcastRecommendation p : podcastRecommendations) { @@ -260,7 +267,6 @@ private void editBlogMatchingField(BlogRecommendation b, String fieldToBeEdited, map.put("author", b::setAuthor); map.put("url", b::setUrl); map.put("description", b::setDescription); - System.err.println(fieldToBeEdited); map.get(fieldToBeEdited).apply(newValue); } @@ -280,7 +286,6 @@ private void editPodcastMatchingField(PodcastRecommendation p, String fieldToBeE map.put("author", p::setAuthor); map.put("podcastname", p::setPodcastName); map.put("description", p::setDescription); - System.err.println(fieldToBeEdited); map.get(fieldToBeEdited).apply(newValue); } @@ -358,44 +363,63 @@ public List getAllTagsForPodcast(int podcastId) { @Override public void addTagToBook(int bookId, String tagText) { - loop: + Tag tag = getTag(tagText); for (BookRecommendation b : bookRecommendations) { + if (b.getId() == bookId) { + if (tagsBooks.get(tag) == null) { + tagsBooks.put(tag, new ArrayList<>()); + } + tagsBooks.get(tag).add(b); + b.addTag(getTag(tagText)); - break loop; + break; } } } @Override public void addTagToVideo(int videoId, String tagText) { - loop: + Tag tag = getTag(tagText); for (VideoRecommendation v : videoRecommendations) { if (v.getId() == videoId) { + if (tagsVideos.get(tag) == null) { + tagsVideos.put(tag, new ArrayList<>()); + } + tagsVideos.get(tag).add(v); + v.addTag(getTag(tagText)); - break loop; + break; } - } + } } @Override public void addTagToBlog(int blogId, String tagText) { - loop: + Tag tag = getTag(tagText); for (BlogRecommendation b : blogRecommendations) { if (b.getId() == blogId) { + if (tagsBlogs.get(tag) == null) { + tagsBlogs.put(tag, new ArrayList<>()); + } + tagsBlogs.get(tag).add(b); b.addTag(getTag(tagText)); - break loop; + break; } } } @Override public void addTagToPodcast(int podcastId, String tagText) { - loop: + Tag tag = getTag(tagText); for (PodcastRecommendation p : podcastRecommendations) { if (p.getId() == podcastId) { + if (tagsPodcasts.get(tag) == null) { + tagsPodcasts.put(tag, new ArrayList<>()); + } + tagsPodcasts.get(tag).add(p); p.addTag(getTag(tagText)); - break loop; + break; } } } @@ -410,30 +434,34 @@ public int getTagId(String tagText) { return 0; } - // not implemented yet @Override public List getRecommendationsWithTag(String tag) { - return null; + List x = new ArrayList<>(); + x.addAll(getBooksWithTag(tag)); + x.addAll(getBlogsWithTag(tag)); + x.addAll(getVideosWithTag(tag)); + x.addAll(getPodcastsWithTag(tag)); + return x; } @Override public List getBooksWithTag(String tag) { - return null; + return tagsBooks.getOrDefault(getTag(tag), new ArrayList<>()); } @Override public List getVideosWithTag(String tag) { - return null; + return tagsVideos.getOrDefault(getTag(tag), new ArrayList<>()); } @Override public List getPodcastsWithTag(String tag) { - return null; + return tagsPodcasts.getOrDefault(getTag(tag), new ArrayList<>()); } @Override public List getBlogsWithTag(String tag) { - return null; + return tagsBlogs.getOrDefault(getTag(tag), new ArrayList<>()); } @Override @@ -446,14 +474,15 @@ public void createTag(String tagText) { Tag newTag = new Tag(this.tags.size() + 1, tagText); this.tags.add(newTag); } - + private Tag getTag(String tagText) { for (Tag t : tags) { if (t.getTagText().equals(tagText)) { return t; } } - return null; + createTag(tagText); + return getTag(tagText); } } diff --git a/src/main/java/recommendation_library/domain/DatabaseService.java b/src/main/java/recommendation_library/domain/DatabaseService.java index 494ea32..f36f803 100644 --- a/src/main/java/recommendation_library/domain/DatabaseService.java +++ b/src/main/java/recommendation_library/domain/DatabaseService.java @@ -320,6 +320,8 @@ public boolean checkIdForZero(int id) { public boolean addTagToBook(String bookTitle, String tagText) { int bookId = dao.getBookIdByTitle(bookTitle); + System.err.println(bookId); + dao.addTagToBook(bookId, tagText); return !checkIdForZero(bookId); } diff --git a/src/test/java/recommendation_library/Stepdefs.java b/src/test/java/recommendation_library/Stepdefs.java index 891a0c4..f5e2057 100644 --- a/src/test/java/recommendation_library/Stepdefs.java +++ b/src/test/java/recommendation_library/Stepdefs.java @@ -208,25 +208,17 @@ public void systemWillRespondWith(String expectedOutput) { dao = new InMemoryRecommendationDao(); ui = new UserInterface(io, dao); ui.run(); + assertTrue(io.getPrints().contains(expectedOutput)); } @Then("app lists a recommendation with author {string}, title {string}, description {string}, isbn {string}, and page count {string}") public void listingAddedBookRecommendation(String author, String title, String description, String isbn, String pageCount) { - System.err.println(author); - System.err.println(title); - System.err.println(description); - System.err.println(isbn); - System.err.println(pageCount); - System.err.println(String.join("\n", inputLines)); io = new StubIO(inputLines); - System.err.println(io); dao = new InMemoryRecommendationDao(); - System.err.println(dao); ui = new UserInterface(io, dao); - System.err.println(ui); ui.run(); @@ -279,6 +271,8 @@ public void listingAddedVideoRecommendationWithTimestamp(String title, String ti @Then("app lists a recommendation with title {string}, author {string}, description {string} and url {string}") public void listingAddedBlogRecommendation(String title, String author, String description, String url) { + System.err.println(String.join("\n", inputLines)); + io = new StubIO(inputLines); dao = new InMemoryRecommendationDao(); ui = new UserInterface(io, dao); @@ -296,6 +290,7 @@ public void listingAddedBlogRecommendation(String title, String author, String d @Then("app lists a podcast recommendation with title {string}, name {string}, author {string} and description {string}") public void listingAddedPodcastRecommendation(String title, String name, String author, String description) { + io = new StubIO(inputLines); dao = new InMemoryRecommendationDao(); ui = new UserInterface(io, dao); diff --git a/src/test/resources/RecommendationLibrary/searchByTags.feature b/src/test/resources/RecommendationLibrary/searchByTags.feature index 568aaf9..01983be 100644 --- a/src/test/resources/RecommendationLibrary/searchByTags.feature +++ b/src/test/resources/RecommendationLibrary/searchByTags.feature @@ -27,7 +27,7 @@ Feature: As a user I can search a recommendation by tag And field to edit "eka" is entered And command list videos is selected Then app lists a recommendation with title "TitleTest", description "DescriptionTest" and url "urlTest" - + Scenario: user can list added blog recommendations Given command add is selected When command blog is selected