Skip to content

Commit 3c6f978

Browse files
authored
Merge pull request #67 from CSC207-2022F-UofT/63-search-edit-delete
Implemented edit and delete for admins on search results
2 parents 418c89e + 7939753 commit 3c6f978

File tree

12 files changed

+172
-55
lines changed

12 files changed

+172
-55
lines changed

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/java/MainPage/FlashcardSetDataPanel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public FlashcardSetDataPanel(String title, String description,
5454
DelFlashcardSetOutputBoundary presenter = new DelFlashcardSetPresenter();
5555
DelFlashcardSetInputBoundary interactor = new DelFlashcardSetInteractor(gateway, presenter);
5656
DelFlashcardSetController controller = new DelFlashcardSetController(interactor);
57-
new DeletionScreen(flashcardSetId, controller, user);
57+
new DeletionScreen(flashcardSetId, controller, user, gateway);
5858
});
5959

6060
this.add(buttons);

src/main/java/delete_flashcardset_use_case/DeletionScreen.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package delete_flashcardset_use_case;
22

33

4+
import dataAccess.DBGateway;
5+
import entityRequestModels.CommonUserDsRequestModel;
46
import login_and_signup_use_case.UserLoginResponseModel;
57

68
import javax.swing.*;
79
import java.awt.*;
810
import java.awt.event.ActionEvent;
911
import java.awt.event.ActionListener;
12+
import java.util.ArrayList;
13+
import java.util.Collection;
1014
import java.util.Objects;
1115

1216
// Frameworks/Drivers (Blue) layer
@@ -23,6 +27,7 @@ public class DeletionScreen extends JFrame implements ActionListener {
2327
* The controller
2428
*/
2529
DelFlashcardSetController controller;
30+
DBGateway gateway;
2631

2732
// /**
2833
// * The application
@@ -32,10 +37,12 @@ public class DeletionScreen extends JFrame implements ActionListener {
3237
/**
3338
* A window with a title and a JButton.
3439
*/
35-
public DeletionScreen(int flashcardSetID, DelFlashcardSetController controller, UserLoginResponseModel user) {
40+
public DeletionScreen(int flashcardSetID, DelFlashcardSetController controller, UserLoginResponseModel user,
41+
DBGateway gateway) {
3642
this.user=user;
3743
this.flashcardSetID = flashcardSetID;
3844
this.controller = controller;
45+
this.gateway = gateway;
3946

4047
JLabel name = new JLabel("Flashcard Set Deletion Screen");
4148
name.setAlignmentX(Component.CENTER_ALIGNMENT);
@@ -80,7 +87,16 @@ public void actionPerformed(ActionEvent evt) {
8087
// int id = Integer.parseInt(flashcardSetID.getText()); // check input is an integer
8188

8289
// Ask for confirmation
83-
String title = user.getFlashcardSets().get(flashcardSetID)[0];
90+
String title;
91+
// Admin deletion
92+
if (user.getFlashcardSets().get(flashcardSetID) == null){
93+
title = gateway.getFlashcardSet(flashcardSetID).getTitle();
94+
}
95+
else{
96+
// User deletion
97+
title = user.getFlashcardSets().get(flashcardSetID)[0];
98+
}
99+
84100

85101
int confirmation = JOptionPane.showConfirmDialog(this,
86102
"Delete " + title + "?",

src/main/java/searchTestMain.java

Lines changed: 0 additions & 36 deletions
This file was deleted.

src/main/java/search_use_case/ResultsScreen.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package search_use_case;
22

33
import dataAccess.DBGateway;
4+
import delete_flashcardset_use_case.*;
5+
import editor_main_page.EditorMainPage;
6+
import login_and_signup_use_case.UserLoginResponseModel;
47
import quiz_use_case.*;
58
import quiz_use_case.screens.QuizSettingsScreen;
69
import studyMode.*;
@@ -26,7 +29,7 @@ public class ResultsScreen extends JFrame implements ActionListener {
2629
* @param responseModel contains results from search
2730
* @param gateway to access information for study and quiz options
2831
*/
29-
public ResultsScreen(SearchResponseModel responseModel, DBGateway gateway){
32+
public ResultsScreen(SearchResponseModel responseModel, DBGateway gateway, UserLoginResponseModel user){
3033
super("Search Results");
3134

3235
// store results in a Box layout
@@ -76,15 +79,34 @@ public ResultsScreen(SearchResponseModel responseModel, DBGateway gateway){
7679
JOptionPane.showMessageDialog(this, s.getMessage());
7780
}
7881
});
82+
JButton edit = new JButton("Edit");
83+
edit.addActionListener((e) -> new EditorMainPage(responseModel.getResult_set().get(tempX)
84+
.getFlashcardSetId()));
85+
JButton delete = new JButton("Delete");
86+
delete.addActionListener(e -> {
87+
DelFlashcardSetOutputBoundary presenter = new DelFlashcardSetPresenter();
88+
DelFlashcardSetInputBoundary interactor = new DelFlashcardSetInteractor(gateway, presenter);
89+
DelFlashcardSetController controller = new DelFlashcardSetController(interactor);
90+
new DeletionScreen(responseModel.getResult_set().get(tempX)
91+
.getFlashcardSetId(), controller, user, gateway);
92+
this.dispose();
93+
});
7994

8095
// add elements to the GridLayout
96+
panel.setLayout(new GridLayout(6, 1, 20, 20));
8197
panel.add(title);
8298
panel.add(description);
8399
panel.add(owner);
84100
panel.add(study);
85101
panel.add(test);
102+
if (user.getIsAdmin()) {
103+
panel.setLayout(new GridLayout(8, 1, 20, 20));
104+
panel.add(edit);
105+
panel.add(delete);
106+
}
86107
result_panel.add(panel);
87108
}
109+
88110
// add the panel containing all the results
89111
add(scrPane);
90112
setSize(400, 500);

src/main/java/search_use_case/SearchController.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,10 @@ public SearchController(SearchInputBoundary userInput){
2323

2424
/**
2525
* Creates a Response model to return a result set of FlashcardSets given user input and tags.
26-
* @param search_input the user search input
27-
* @param tags the tags the user selected
28-
* @param user the current user
26+
* @param requestModel the request model with search input, tags, and current user
2927
* @return a response model with the result set
3028
*/
31-
public SearchResponseModel create(String search_input, ArrayList<String> tags, UserLoginResponseModel user) throws IOException {
32-
SearchRequestModel requestModel = new SearchRequestModel(search_input, tags, user);
33-
29+
public SearchResponseModel create(SearchRequestModel requestModel) throws IOException {
3430
return userInput.create(requestModel);
3531
}
3632
}

src/main/java/search_use_case/SearchInteractor.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,15 @@ public SearchResponseModel create(SearchRequestModel requestModel) throws IOExce
9090
SearchResponseModel searchResponseModel = new SearchResponseModel(result_set);
9191
return presenter.prepareSuccessView(searchResponseModel);
9292
}
93+
if (tags.size() > 0 && !input.equals("")){
94+
return presenter.prepareFailView("There are currently no FlashcardSets in the database.");
95+
}
9396
// User chooses no tags
9497
if (tags.size() == 0){
9598
return presenter.prepareFailView("Please select at least 1 tag.");
9699
}
97100
// User provides no input
98-
if (input.equals("")){
99-
return presenter.prepareFailView("Please enter a keyword to search.");
100-
}
101+
return presenter.prepareFailView("Please enter a keyword to search.");
101102
// User search unsuccessful
102-
return presenter.prepareFailView("No Flashcard Sets matched your search criteria, please try again.");
103103
}
104104
}

src/main/java/search_use_case/SearchScreen.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,10 @@ public SearchScreen(SearchController search_controller, DBGateway gateway, UserL
6161
}
6262
// navigate to results screen if results are found
6363
try {
64-
new ResultsScreen(search_controller.create(s1, selected_tags, curr_user),
65-
gateway);
64+
SearchRequestModel requestModel = new SearchRequestModel(s1, selected_tags, curr_user);
65+
new ResultsScreen(search_controller.create(requestModel),
66+
gateway, curr_user);
67+
this.dispose();
6668
}
6769
catch (Exception x){
6870
JOptionPane.showMessageDialog(this, x.getMessage());
@@ -75,8 +77,11 @@ public SearchScreen(SearchController search_controller, DBGateway gateway, UserL
7577
search_all.setBounds(130, 400, 100, 40);
7678
search_all.addActionListener( e -> {
7779
try {
78-
new ResultsScreen(search_controller.create("GET_ALL", selected_tags, curr_user),
79-
gateway);
80+
SearchRequestModel requestModel = new SearchRequestModel("GET_ALL",
81+
selected_tags, curr_user);
82+
new ResultsScreen(search_controller.create(requestModel),
83+
gateway, curr_user);
84+
this.dispose();
8085
}
8186
catch (Exception x){
8287
JOptionPane.showMessageDialog(this, x.getMessage());
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package searchUseCase;
2+
3+
import dataAccess.*;
4+
import login_and_signup_use_case.UserLoginResponseModel;
5+
import search_use_case.*;
6+
import org.junit.jupiter.api.Test;
7+
8+
import java.io.IOException;
9+
import java.util.ArrayList;
10+
import java.util.HashMap;
11+
12+
import static org.junit.jupiter.api.Assertions.assertEquals;
13+
14+
public class SearchUseCaseTest {
15+
SearchController controller;
16+
UserLoginResponseModel common_user = new UserLoginResponseModel("common_user",
17+
false, new HashMap<>());
18+
UserLoginResponseModel admin = new UserLoginResponseModel("admin",
19+
true, new HashMap<>());
20+
21+
SearchRequestModel setup(String search_input, ArrayList<String> tags,
22+
UserLoginResponseModel user) throws IOException {
23+
IFlashcardDataAccess flashcardGateway = new FlashcardDataAccess(
24+
"src/test/java/searchUseCase/testData/Flashcards.csv");
25+
IFlashcardSetDataAccess flashcardSetGateway = new FlashcardSetDataAccess(
26+
"src/test/java/searchUseCase/testData/FlashcardSets.csv");
27+
IUserDataAccess userGateway = new CommonUserDataAccess(
28+
"src/test/java/searchUseCase/testData/Users.csv");
29+
DBGateway gateway = new DBGateway(flashcardGateway, flashcardSetGateway, userGateway);
30+
31+
SearchOutputBoundary search_presenter = new SearchPresenter();
32+
SearchInputBoundary search_interactor = new SearchInteractor(search_presenter, gateway);
33+
this.controller = new SearchController(search_interactor);
34+
35+
return new SearchRequestModel(search_input, tags, user);
36+
}
37+
38+
@Test
39+
void testSearchAll() throws IOException {
40+
SearchRequestModel searchRequestModel = this.setup("GET_ALL", new ArrayList<>(), common_user);
41+
SearchResponseModel responseModel = controller.create(searchRequestModel);
42+
43+
assertEquals(4, responseModel.getResult_set().size());
44+
assertEquals("test4", responseModel.getResult_set().get(0).getTitle());
45+
assertEquals("test1", responseModel.getResult_set().get(3).getTitle());
46+
47+
}
48+
// Test Search results on all 3 tags
49+
50+
@Test
51+
void testSearchTitle() throws IOException{
52+
ArrayList<String> tags = new ArrayList<>();
53+
tags.add("Title");
54+
55+
SearchRequestModel searchRequestModel = this.setup("test1", tags, common_user);
56+
SearchResponseModel responseModel = controller.create(searchRequestModel);
57+
58+
assertEquals(1, responseModel.getResult_set().size());
59+
assertEquals("test1", responseModel.getResult_set().get(0).getTitle());
60+
}
61+
62+
@Test
63+
void testSearchDescription() throws IOException{
64+
ArrayList<String> tags = new ArrayList<>();
65+
tags.add("Description");
66+
67+
SearchRequestModel searchRequestModel = this.setup("d2", tags, common_user);
68+
SearchResponseModel responseModel = controller.create(searchRequestModel);
69+
70+
assertEquals(1, responseModel.getResult_set().size());
71+
assertEquals("d2", responseModel.getResult_set().get(0).getDescription());
72+
}
73+
74+
@Test
75+
void testSearchOwner() throws IOException{
76+
ArrayList<String> tags = new ArrayList<>();
77+
tags.add("Owner");
78+
79+
SearchRequestModel searchRequestModel = this.setup("testUser3", tags, common_user);
80+
SearchResponseModel responseModel = controller.create(searchRequestModel);
81+
82+
assertEquals(1, responseModel.getResult_set().size());
83+
assertEquals("testUser3", responseModel.getResult_set().get(0).getOwnerUsername());
84+
}
85+
86+
87+
// Test Admin search ignores privates
88+
@Test
89+
void testAdminSearch() throws IOException{
90+
SearchRequestModel searchRequestModel = this.setup("GET_ALL", new ArrayList<>(), admin);
91+
SearchResponseModel responseModel = controller.create(searchRequestModel);
92+
93+
assertEquals(6, responseModel.getResult_set().size());
94+
assertEquals("test6", responseModel.getResult_set().get(2).getTitle());
95+
assertEquals("testUser1", responseModel.getResult_set().get(5).getOwnerUsername());
96+
}
97+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
title, description, privacy, id, owner, flashcardsIds
2+
test1,d1,false,0,testUser1,0
3+
test2,d2,false,1,testUser2,0
4+
test3,d3,false,2,testUser3,0
5+
test4,d4,false,3,testUser4,0
6+
test5,d5,true,4,testUser4,0
7+
test6,d6,true,5,testUser4,0

0 commit comments

Comments
 (0)