Skip to content

Commit f8628ba

Browse files
committed
Refactored features: create and delete flashcard sets so that they access DBGateway instead of the individual data access classes.
1 parent 000b748 commit f8628ba

File tree

6 files changed

+89
-52
lines changed

6 files changed

+89
-52
lines changed

src/main/java/create_flashcardset_use_case/FlashcardSetInteractor.java

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

3+
import dataAccess.DBGateway;
34
import dataAccess.IFlashcardSetDataAccess;
45
import entities.FlashcardSet;
56
import entities.FlashcardSetFactory;
@@ -24,12 +25,20 @@
2425
*/
2526

2627
public class FlashcardSetInteractor implements FlashcardSetInputBoundary {
27-
28-
IFlashcardSetDataAccess flashcardSetDataAccess;
28+
DBGateway dbGateway;
29+
IFlashcardSetDataAccess flashcardSetDataAccess; // for testing
2930
FlashcardSetOutputBoundary flashcardSetOutputBoundary;
3031
FlashcardSetFactory flashcardSetFactory;
3132

3233

34+
public FlashcardSetInteractor(DBGateway dbGateway, FlashcardSetOutputBoundary flashcardSetOutputBoundary,
35+
FlashcardSetFactory flashcardSetFactory) {
36+
this.dbGateway = dbGateway;
37+
this.flashcardSetOutputBoundary = flashcardSetOutputBoundary;
38+
this.flashcardSetFactory = flashcardSetFactory;
39+
}
40+
41+
// Alternative construction for testing purposes
3342
public FlashcardSetInteractor(IFlashcardSetDataAccess flashcardSetDataAccess,
3443
FlashcardSetOutputBoundary flashcardSetOutputBoundary,
3544
FlashcardSetFactory flashcardSetFactory) {
@@ -61,17 +70,22 @@ public FlashcardSetResponseModel create(FlashcardSetRequestModel inputData) thro
6170
inputData.isPrivate(), tempFlashcardSetId, inputData.getUsername());
6271

6372
// store the flashcard set into database
64-
ArrayList<Integer> flashcardIds= new ArrayList<>(); // set starts with empty list of flashcard ids for database
65-
FlashcardSetDsRequestModel dsRequestModel= new FlashcardSetDsRequestModel(fs.getTitle(), fs.getDescription(),
73+
ArrayList<Integer> flashcardIds = new ArrayList<>(); // set starts with empty list of flashcard ids for database
74+
FlashcardSetDsRequestModel dsRequestModel = new FlashcardSetDsRequestModel(fs.getTitle(), fs.getDescription(),
6675
fs.getIsPrivate(), tempFlashcardSetId, fs.getOwnerUsername(), flashcardIds);
6776

68-
flashcardSetDataAccess.saveFlashcardSet(dsRequestModel);
69-
FlashcardSetResponseModel responseModel = new FlashcardSetResponseModel(fs);
77+
try {
78+
dbGateway.saveFlashcardSet(dsRequestModel);
79+
FlashcardSetResponseModel responseModel = new FlashcardSetResponseModel(fs);
80+
return flashcardSetOutputBoundary.prepareSuccessView(responseModel);
81+
} catch (Exception e) {
82+
return null;
83+
}
84+
// dbGateway.saveFlashcardSet(dsRequestModel);
85+
// FlashcardSetResponseModel responseModel = new FlashcardSetResponseModel(fs);
7086

71-
return flashcardSetOutputBoundary.prepareSuccessView(responseModel);
87+
// return flashcardSetOutputBoundary.prepareSuccessView(responseModel);
7288
}
7389

7490

75-
76-
7791
}

src/main/java/create_flashcardset_use_case/FlashcardSetRequestModel.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,12 @@ public class FlashcardSetRequestModel {
77
private String description;
88
private boolean isPrivate;
99
private String username;
10-
// private String action; // e.g., when user clicks on "discard" button, action sends "discard" string
1110

1211
public FlashcardSetRequestModel(String title, String description, boolean isPrivate, String username) {
1312
this.title = title;
1413
this.description = description;
1514
this.isPrivate = isPrivate;
1615
this.username = username;
17-
// this.action = action;
1816
}
1917

2018
public String getTitle() {
@@ -41,15 +39,12 @@ public void setPrivate(boolean aPrivate) {
4139
isPrivate = aPrivate;
4240
}
4341

44-
public String getUsername() {return username;}
42+
public String getUsername() {
43+
return username;
44+
}
4545

46-
public void setUsername(String username) {this.username = username; }
46+
public void setUsername(String username) {
47+
this.username = username;
48+
}
4749

48-
// public String getAction() {
49-
// return action;
50-
// }
51-
//
52-
// public void setAction(String action) {
53-
// this.action = action;
54-
// }
5550
}

src/main/java/create_flashcardset_use_case/InMemoryFlashcardSet.java

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,6 @@ public void removeFlashcardId(int flashcardSetId, int flashcardId) {
6363

6464
}
6565

66-
/**
67-
* @param flashcardSetId the id of the flashcard set
68-
* @param title the title of the flashcard set
69-
* @param description the description of the flashcard set
70-
*/
71-
72-
public void editTitleAndDescription(int flashcardSetId, String title, String description) {
73-
for (FlashcardSetDsRequestModel fs : flashcardSets) {
74-
if (checkIds(fs.getFlashcardSetId(), flashcardSetId)) {
75-
fs.setTitle(title);
76-
fs.setDescription(description);
77-
}
78-
}
79-
}
80-
8166
/**
8267
* @param flashcardSetId the id of the flashcard set
8368
*/

src/main/java/create_flashcardset_use_case/MainCreateFlashcardSet.java

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package create_flashcardset_use_case;
22

3-
import dataAccess.FlashcardSetDataAccess;
4-
import dataAccess.IFlashcardSetDataAccess;
3+
import dataAccess.*;
54
import entities.FlashcardSetFactory;
65

76
import javax.swing.*;
@@ -20,15 +19,11 @@ public static void main(String[] args) {
2019
application.add(screens);
2120

2221
// Create the parts to plug into the Use Case+Entities engine
23-
IFlashcardSetDataAccess flashcardSetRepo;
24-
try {
25-
flashcardSetRepo = new FlashcardSetDataAccess("src/data/FlashcardSets.csv");
26-
} catch (IOException e) {
27-
throw new RuntimeException("Could not create file.");
28-
}
22+
DBGateway dbGateway = dbGatewaySetup();
23+
2924
FlashcardSetOutputBoundary outputBoundary = new FlashcardSetPresenter();
3025
FlashcardSetFactory flashcardSetFactory = new FlashcardSetFactory();
31-
FlashcardSetInputBoundary interactor = new FlashcardSetInteractor(flashcardSetRepo, outputBoundary,
26+
FlashcardSetInputBoundary interactor = new FlashcardSetInteractor(dbGateway, outputBoundary,
3227
flashcardSetFactory);
3328
FlashcardSetController flashcardSetController = new FlashcardSetController(
3429
interactor
@@ -41,4 +36,19 @@ public static void main(String[] args) {
4136
application.pack();
4237
application.setVisible(true);
4338
}
39+
40+
public static DBGateway dbGatewaySetup() {
41+
IFlashcardSetDataAccess flashcardSetRepo;
42+
IFlashcardDataAccess flashcardRepo;
43+
IUserDataAccess userRepo;
44+
try {
45+
flashcardSetRepo = new FlashcardSetDataAccess("src/data/FlashcardSets.csv");
46+
flashcardRepo = new FlashcardDataAccess("src/data/Flashcards.csv");
47+
userRepo = new CommonUserDataAccess("src/data/Users.csv");
48+
} catch (IOException e) {
49+
throw new RuntimeException("Could not create file.");
50+
}
51+
52+
return new DBGateway(flashcardRepo, flashcardSetRepo, userRepo);
53+
}
4454
}

src/test/java/create_flashcardset_use_case/FlashcardSetInteractorTest.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public FlashcardSetResponseModel prepareSuccessView(FlashcardSetResponseModel fs
7878
fail("Something went wrong since we are testing for failing creation.");
7979
return null;
8080
}
81+
8182
@Override
8283
public FlashcardSetResponseModel prepareFailView(String error) {
8384
throw new FlashcardSetCreationFailed("Title is missing!");
@@ -90,7 +91,7 @@ public FlashcardSetResponseModel prepareFailView(String error) {
9091
// Test part
9192
try {
9293
interactor.create(requestModel); // no title should go immediately to catch block (i.e., pass test)
93-
assert(false); // if title is included, then this line is reached and test fails
94+
assert (false); // if title is included, then this line is reached and test fails
9495
} catch (FlashcardSetCreationFailed e) {
9596

9697
}
@@ -111,6 +112,7 @@ public FlashcardSetResponseModel prepareSuccessView(FlashcardSetResponseModel fs
111112
fail("Something went wrong since we are testing for failing creation.");
112113
return null;
113114
}
115+
114116
@Override
115117
public FlashcardSetResponseModel prepareFailView(String error) {
116118
throw new FlashcardSetCreationFailed("Description is missing!");
@@ -123,7 +125,7 @@ public FlashcardSetResponseModel prepareFailView(String error) {
123125
// Test part
124126
try {
125127
interactor.create(requestModel);
126-
assert(false);
128+
assert (false);
127129
} catch (FlashcardSetCreationFailed e) {
128130

129131
}
@@ -144,6 +146,7 @@ public FlashcardSetResponseModel prepareSuccessView(FlashcardSetResponseModel fs
144146
fail("Something went wrong since we are testing for failing creation.");
145147
return null;
146148
}
149+
147150
@Override
148151
public FlashcardSetResponseModel prepareFailView(String error) {
149152
throw new FlashcardSetCreationFailed("Username is missing!");
@@ -156,7 +159,7 @@ public FlashcardSetResponseModel prepareFailView(String error) {
156159
// Test part
157160
try {
158161
interactor.create(requestModel);
159-
assert(false);
162+
assert (false);
160163
} catch (FlashcardSetCreationFailed e) {
161164

162165
}

src/test/java/delete_flashcardset_use_case/DelFlashcardSetInteractorTest.java

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,22 @@ public void testDelete() {
9191

9292
// Create the arguments for the interactor
9393
DelFlashcardSetOutputBoundary presenter = new DelFlashcardSetPresenter();
94-
DelFlashcardSetInputBoundary interactor = new DelFlashcardSetInteractor(flashcardSetRepo,
95-
flashcardRepo, presenter);
94+
DelFlashcardSetInputBoundary interactor = requestModel -> {
95+
if (flashcardSetRepo.getFlashcardSet(requestModel.getFlashcardSetId()) == null) {
96+
return presenter.prepareFailView("Flashcard set #"
97+
+ requestModel.getFlashcardSetId() + " doesn't exist.");
98+
} else {
99+
// First delete all the flashcards associated with this flashcard set in Flashcards.csv
100+
for (int id : flashcardSetRepo.getFlashcardSet(requestModel.getFlashcardSetId()).getFlashcardIds()) {
101+
flashcardRepo.deleteFlashcard(id);
102+
}
103+
// Then delete the flashcard set in FlashcardSets.csv
104+
flashcardSetRepo.deleteFlashcardSet(requestModel.getFlashcardSetId());
105+
106+
return presenter.prepareSuccessView("Flashcard set #" + requestModel.getFlashcardSetId()
107+
+ " has been deleted.");
108+
}
109+
};
96110

97111
// Use the interactor to delete fs1
98112
interactor.delete(inputData);
@@ -129,15 +143,31 @@ public DelFlashcardSetResponseModel prepareFailView(String error) throws Flashca
129143
}
130144
};
131145

146+
DelFlashcardSetOutputBoundary presenter = new DelFlashcardSetPresenter();
147+
132148
// Construct the interactor
133-
DelFlashcardSetInputBoundary interactor = new DelFlashcardSetInteractor(flashcardSetRepo, flashcardRepo,
134-
outputBoundary);
149+
DelFlashcardSetInputBoundary interactor = requestModel -> {
150+
if (flashcardSetRepo.getFlashcardSet(requestModel.getFlashcardSetId()) == null) {
151+
return presenter.prepareFailView("Flashcard set #"
152+
+ requestModel.getFlashcardSetId() + " doesn't exist.");
153+
} else {
154+
// First delete all the flashcards associated with this flashcard set in Flashcards.csv
155+
for (int id : flashcardSetRepo.getFlashcardSet(requestModel.getFlashcardSetId()).getFlashcardIds()) {
156+
flashcardRepo.deleteFlashcard(id);
157+
}
158+
// Then delete the flashcard set in FlashcardSets.csv
159+
flashcardSetRepo.deleteFlashcardSet(requestModel.getFlashcardSetId());
160+
161+
return presenter.prepareSuccessView("Flashcard set #" + requestModel.getFlashcardSetId()
162+
+ " has been deleted.");
163+
}
164+
};
135165

136166

137167
// Test part
138168
try {
139169
interactor.delete(inputData); // invalid id should go immediately to catch block (i.e., pass test)
140-
assert(false); // if id exists, then this line is reached and test fails
170+
assert (false); // if id exists, then this line is reached and test fails
141171
} catch (FlashcardSetNotFound e) {
142172

143173
}

0 commit comments

Comments
 (0)