Skip to content

Commit 37f6ac9

Browse files
committed
Resolve the situation of creating duplicate flashcards with the same term.
1 parent daec1a6 commit 37f6ac9

File tree

5 files changed

+99
-44
lines changed

5 files changed

+99
-44
lines changed

src/main/java/create_flashcard_use_case/CreateFlashcardInputBoundary.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88

99
public interface CreateFlashcardInputBoundary {
1010
CreateFlashcardResponseModel create(CreateFlashcardRequestModel requestModel);
11+
CreateFlashcardResponseModel create(CreateFlashcardRequestModel requestModel, int flashcardId);
1112

1213
}
Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package create_flashcard_use_case;
2-
32
import frameworks_and_drivers.database.DBGateway;
4-
import data_access_use_case.IFlashcardDataAccess;
5-
import data_access_use_case.IFlashcardSetDataAccess;
63
import data_access_use_case.entity_request_models.FlashcardDsRequestModel;
74

85
import java.time.LocalDateTime;
@@ -13,8 +10,7 @@
1310
*/
1411
public class CreateFlashcardInteractor implements CreateFlashcardInputBoundary {
1512
CreateFlashcardOutputBoundary presenter;
16-
IFlashcardDataAccess fcDataAccess;
17-
IFlashcardSetDataAccess fcsDataAccess;
13+
DBGateway gateway;
1814

1915
/**
2016
* Create FcCInteractor
@@ -23,8 +19,7 @@ public class CreateFlashcardInteractor implements CreateFlashcardInputBoundary {
2319
*/
2420
public CreateFlashcardInteractor(DBGateway gateway, CreateFlashcardOutputBoundary presenter){
2521
this.presenter = presenter;
26-
this.fcDataAccess = gateway.getFlashcardGateway();
27-
this.fcsDataAccess = gateway.getFlashcardSetGateway();
22+
this.gateway = gateway;
2823
}
2924

3025
/**
@@ -34,19 +29,36 @@ public CreateFlashcardInteractor(DBGateway gateway, CreateFlashcardOutputBoundar
3429
*/
3530
@Override
3631
public CreateFlashcardResponseModel create(CreateFlashcardRequestModel requestModel) {
37-
if(requestModel.getDefinition().isEmpty()||requestModel.getTerm().isEmpty()){
32+
String term = requestModel.getTerm().replace("\n", " ").trim();
33+
String definition = requestModel.getDefinition().replace("\n", " ").trim();
34+
if(term.isEmpty() || definition.isEmpty()){
3835
return presenter.prepareFailView("Term or definition is empty.");
39-
} else if (fcsDataAccess.getFlashcardSet(requestModel.getFlashcardSetId()) == null) {
36+
}
37+
else if (gateway.getFlashcardSet(requestModel.getFlashcardSetId()) == null) {
4038
return presenter.prepareFailView("Flashcard set does not exist.");
4139
}
40+
for (int flashcardId : gateway.getFlashcardSet(requestModel.getFlashcardSetId()).getFlashcardIds()){
41+
if (gateway.getFlashcard(flashcardId).getTerm().equals(term)){
42+
return new CreateFlashcardResponseModel(LocalDateTime.now(), requestModel.getTerm(),
43+
gateway.getFlashcard(flashcardId).getDefinition(), flashcardId);
44+
}
45+
}
46+
LocalDateTime creationDate = LocalDateTime.now();
47+
gateway.saveFlashcard(new FlashcardDsRequestModel(term, definition, creationDate, -1,
48+
requestModel.getFlashcardSetId()));
49+
return new CreateFlashcardResponseModel(creationDate, term, definition, -1);
50+
51+
}
52+
53+
public CreateFlashcardResponseModel create(CreateFlashcardRequestModel requestModel, int flashcardId){
54+
String term = requestModel.getTerm().replace("\n", " ").trim();
55+
String definition = requestModel.getDefinition().replace("\n", " ").trim();
4256
LocalDateTime creationDate = LocalDateTime.now();
43-
FlashcardDsRequestModel flashcard = new FlashcardDsRequestModel(
44-
requestModel.getTerm().replace("\n", " "),
45-
requestModel.getDefinition().replace("\n", " "), creationDate,-1, requestModel.getFlashcardSetId());
46-
int flashcardId = fcDataAccess.saveFlashcard(flashcard);
47-
fcsDataAccess.saveFlashcardID(requestModel.getFlashcardSetId(), flashcardId);
48-
49-
return presenter.prepareSuccessView(new CreateFlashcardResponseModel(creationDate, requestModel.getTerm(),
50-
requestModel.getDefinition()));
57+
gateway.editFlashcard(new FlashcardDsRequestModel(term, definition, creationDate, flashcardId,
58+
requestModel.getFlashcardSetId()));
59+
return new CreateFlashcardResponseModel(creationDate, term, definition, -1);
5160
}
61+
62+
63+
5264
}

src/main/java/create_flashcard_use_case/CreateFlashcardResponseModel.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,20 @@
99
public class CreateFlashcardResponseModel {
1010
private final LocalDateTime creationDate;
1111
private String term, definition;
12+
private int flashcardId;
1213

1314
/**
1415
* Create FcCResponseModel
1516
* @param creationDate date of creation of the flashcard
1617
* @param term term of the flashcard
1718
* @param definition definition of the flashcard
1819
*/
19-
public CreateFlashcardResponseModel(LocalDateTime creationDate, String term, String definition) {
20+
public CreateFlashcardResponseModel(LocalDateTime creationDate, String term, String definition, int flashcardId) {
2021
this.creationDate = creationDate;
2122
this.term = term;
2223
this.definition = definition;
24+
this.flashcardId = flashcardId;
25+
2326
}
2427

2528
public void setDefinition(String definition) {
@@ -30,6 +33,10 @@ public void setTerm(String term) {
3033
this.term = term;
3134
}
3235

36+
public void setFlashcardId(int flashcardId) {
37+
this.flashcardId = flashcardId;
38+
}
39+
3340
public String getDefinition() {
3441
return definition;
3542
}
@@ -41,4 +48,8 @@ public String getTerm() {
4148
public LocalDateTime getCreationDate() {
4249
return creationDate;
4350
}
51+
52+
public int getFlashcardId() {
53+
return flashcardId;
54+
}
4455
}

src/main/java/frameworks_and_drivers/components/CreateFlashcardPanel.java

Lines changed: 51 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,18 @@
1414
*/
1515
public class CreateFlashcardPanel extends JPanel implements ActionListener {
1616
CreateFlashcardController controller;
17-
JTextArea term_text;
18-
JTextArea definition_text;
17+
JTextArea term_text, definition_text;
18+
JLabel existing_term, existing_definition;
1919
JFrame fcCMain;
20-
public CreateFlashcardPanel(CreateFlashcardController controller, JFrame fcCMain){
20+
21+
public CreateFlashcardPanel(CreateFlashcardController controller, JFrame fcCMain) {
2122
//Initiating all the sub panels.
2223
this.fcCMain = fcCMain;
2324
this.controller = controller;
2425
this.setLayout(new BorderLayout());
2526
JPanel buttonPanel = new JPanel(new FlowLayout());
26-
JPanel labelPanel = new JPanel(new GridLayout(1,2));
27-
JPanel textPanel = new JPanel(new GridLayout(1,2));
27+
JPanel labelPanel = new JPanel(new GridLayout(1, 2));
28+
JPanel textPanel = new JPanel(new GridLayout(2, 2));
2829

2930
//Creating the button panel.
3031
JButton confirm = new JButton("confirm");
@@ -33,7 +34,6 @@ public CreateFlashcardPanel(CreateFlashcardController controller, JFrame fcCMain
3334
cancel.addActionListener(e -> this.fcCMain.dispose());
3435
buttonPanel.add(confirm);
3536
buttonPanel.add(cancel);
36-
3737
JLabel term_label = new JLabel("term");
3838
JLabel definition_label = new JLabel("definition");
3939
labelPanel.add(term_label);
@@ -44,48 +44,75 @@ public CreateFlashcardPanel(CreateFlashcardController controller, JFrame fcCMain
4444
definition_text = new JTextArea();
4545
term_text.setLineWrap(true);
4646
definition_text.setLineWrap(true);
47+
existing_term = new JLabel("");
48+
existing_definition = new JLabel("");
4749
textPanel.add(term_text);
4850
textPanel.add(definition_text);
51+
textPanel.add(existing_term);
52+
textPanel.add(existing_definition);
4953

5054
//Creating the label panel.
5155
this.add(labelPanel, BorderLayout.NORTH);
5256
this.add(textPanel);
5357
this.add(buttonPanel, BorderLayout.SOUTH);
54-
this.setSize(1000,500);
58+
this.setSize(1000, 500);
5559
this.setVisible(true);
5660

5761

58-
5962
}
6063

6164
/**
6265
* Response based on creation success or failure.
66+
*
6367
* @param e the event to be processed
6468
*/
6569
@Override
6670
public void actionPerformed(ActionEvent e) {
67-
try{
71+
try {
6872
//Success view.
69-
CreateFlashcardResponseModel responseModel = controller.create(term_text.getText().replace("\n", " "),
70-
definition_text.getText().replace("\n", " "));
71-
int action = JOptionPane.showConfirmDialog(this,
72-
"Card created:\n"+responseModel.getTerm()+ "\n" + responseModel.getDefinition()
73-
+"\ncreate another card?");
74-
if(action == JOptionPane.YES_OPTION){
75-
term_text.setText("");
76-
definition_text.setText("");
77-
}else {
78-
fcCMain.dispose();
73+
CreateFlashcardResponseModel responseModel = controller.create(term_text.getText(),
74+
definition_text.getText(), -1);
75+
if (responseModel.getFlashcardId() == -1) {
76+
successView(responseModel);
77+
} else {
78+
Object[] options = {"re-edit current card", "overwrite", "back to flashcard set"};
79+
int action = JOptionPane.showOptionDialog(this,
80+
"Term already exist:\n" + responseModel.getTerm() + "\n" + responseModel.getDefinition()
81+
+ "\ncurrent card:\n"+ term_text.getText() + "\n" + definition_text.getText(),
82+
"conflict", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE,
83+
null, options, options[0]);
84+
if (action == JOptionPane.YES_OPTION) {
85+
existing_definition.setText("<html>existing definition :<br>" + responseModel.getDefinition() +
86+
"</html>");
87+
} else if (action == JOptionPane.NO_OPTION) {
88+
responseModel = controller.create(term_text.getText().replace("\n", " "),
89+
definition_text.getText().replace("\n", " "), responseModel.getFlashcardId());
90+
successView(responseModel);
91+
} else {
92+
fcCMain.dispose();
93+
}
7994
}
80-
}catch (RuntimeException error){
95+
} catch (RuntimeException error) {
8196
//Failure view.
8297
int action = JOptionPane.showConfirmDialog(this,
83-
error + "\nRecreate?");
84-
if(action == JOptionPane.YES_OPTION){
85-
term_text.setText("");
86-
}else {
98+
error + "\nre-edit?");
99+
if (action == JOptionPane.NO_OPTION) {
87100
fcCMain.dispose();
88101
}
89102
}
90103
}
104+
105+
private void successView(CreateFlashcardResponseModel responseModel){
106+
Object[] options = {"create another card", "back to flashcard set"};
107+
int action = JOptionPane.showOptionDialog(this,
108+
"Card created:\n" + responseModel.getTerm() + "\n" + responseModel.getDefinition(),
109+
"Success", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,
110+
null, options, options[0]);
111+
if (action == JOptionPane.YES_OPTION) {
112+
term_text.setText("");
113+
definition_text.setText("");
114+
} else {
115+
fcCMain.dispose();
116+
}
117+
}
91118
}

src/main/java/interface_adapters/controllers/CreateFlashcardController.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ public CreateFlashcardController(CreateFlashcardInputBoundary inputBoundary, int
2323
this.inputBoundary = inputBoundary;
2424
this.flashcardSetId = flashcardSetId;
2525
}
26-
public CreateFlashcardResponseModel create(String term, String definition){
27-
return inputBoundary.create(new CreateFlashcardRequestModel(flashcardSetId, term, definition));
26+
27+
public CreateFlashcardResponseModel create(String term, String definition, int flashcardId){
28+
if (flashcardId == -1){
29+
return inputBoundary.create(new CreateFlashcardRequestModel(flashcardSetId, term, definition));
30+
}
31+
return inputBoundary.create(new CreateFlashcardRequestModel(flashcardSetId, term, definition), flashcardId);
2832
}
2933
}

0 commit comments

Comments
 (0)