1
+ package com .backend .usecases .managers ;
2
+
3
+ import com .backend .entities .IDs .AccountID ;
4
+ import com .backend .entities .Task ;
5
+ import com .backend .entities .TaskActive ;
6
+ import com .backend .entities .TaskCompletionRecord ;
7
+ import com .backend .repositories .TaskActiveRepo ;
8
+ import com .backend .repositories .TaskCompletionRepo ;
9
+ import com .backend .repositories .TaskRepo ;
10
+ import org .springframework .beans .factory .annotation .Autowired ;
11
+ import org .springframework .beans .factory .annotation .Configurable ;
12
+ import org .springframework .stereotype .Service ;
13
+
14
+ import java .sql .Timestamp ;
15
+ import java .util .ArrayList ;
16
+ import java .util .Date ;
17
+ import java .util .List ;
18
+ import java .util .Random ;
19
+
20
+ /**
21
+ * Task related managers
22
+ */
23
+ @ Service
24
+ @ Configurable
25
+ public class TaskManager {
26
+ private final TaskRepo taskRepo ;
27
+ private final TaskActiveRepo activeRepo ;
28
+ private final TaskCompletionRepo completeRepo ;
29
+
30
+ /**
31
+ * database connection, Spring Boot dependency injection
32
+ * @param taskRepo of type TaskRepo, establishes connection to the task repository
33
+ * @param activeRepo of type TaskActiveRepo, establishes connection to the task active repository
34
+ * @param completeRepo of type TaskCompletionRepo, establishes connection to the task completion repository
35
+ */
36
+ @ Autowired
37
+ @ SuppressWarnings ("unused" )
38
+ public TaskManager (TaskRepo taskRepo , TaskActiveRepo activeRepo , TaskCompletionRepo completeRepo ){
39
+ this .taskRepo = taskRepo ;
40
+ this .activeRepo = activeRepo ;
41
+ this .completeRepo = completeRepo ;
42
+ }
43
+
44
+ /**
45
+ * Post request to create a TaskCompletionRecord in the database
46
+ * @param account of type AccountID, references the account
47
+ * @param name of type String, the name of the task
48
+ * @param image of type String, the URL link of the image
49
+ */
50
+ public TaskCompletionRecord completeTask (AccountID account , String name , String image ){
51
+ return this .completeRepo .save (new TaskCompletionRecord (
52
+ account .getID (), new Timestamp (System .currentTimeMillis ()), name , image ));
53
+ }
54
+
55
+ /**
56
+ * Verifies whether the task name and task reward are valid
57
+ * @param name of type String, the name of the task
58
+ * @param reward of type double, the reward from the task
59
+ * @return true if the task name and reward are valid, false otherwise
60
+ */
61
+ public boolean verifyTask (String name , String image , double reward ) {
62
+ if (image .equals ("" )){
63
+ return false ;
64
+ }
65
+
66
+ //check if the task is part of active tasks and reward is correct
67
+ List <TaskActive > active = this .activeRepo .findAll ();
68
+ TaskActive current = null ;
69
+ for (TaskActive task : active ) {
70
+ if (task .getName ().equals (name )){
71
+ current = task ;
72
+ }
73
+ }
74
+
75
+ if (current == null ) {
76
+ return false ;
77
+ }
78
+ return current .getReward () == reward ;
79
+ }
80
+
81
+ /**
82
+ * Delete all correlated TaskCompletionRecords when a user deletes their account
83
+ * @param accountID of type AccountID, accountID references associated account
84
+ */
85
+ public void deleteAllCorrelatedCompletions (AccountID accountID ) {
86
+ //get all the records completed by account and delete them
87
+ List <TaskCompletionRecord > complete = getTaskCompletionRecords (accountID );
88
+ for (TaskCompletionRecord task : complete ) {
89
+ this .completeRepo .deleteById (task .getID ());
90
+ }
91
+ }
92
+
93
+ /**
94
+ * Gets all completed tasks completed by accountID
95
+ * @param account of type AccountID, references the account
96
+ * @return a list of all tasks completed by accountID
97
+ */
98
+ public List <TaskCompletionRecord > getTaskCompletionRecords (AccountID account ) {
99
+ return this .completeRepo .findAllByAccountID (account .getID ());
100
+ }
101
+
102
+ /**
103
+ * Checks if the task has already been completed today
104
+ * @param name of type String, the name of the task
105
+ * @param account of AccountID, references the account
106
+ * @return true is the task has already been completed, false otherwise
107
+ */
108
+ public boolean checkCompleted (String name , AccountID account ) {
109
+ List <TaskCompletionRecord > taskCompletionRecords = getTaskCompletionRecords (account );
110
+ String today = new Date (System .currentTimeMillis ()).toString ().substring (0 ,10 );
111
+
112
+ //check if the task has been completed today
113
+ for (TaskCompletionRecord current : taskCompletionRecords ) {
114
+ String date = current .getDate ();
115
+ if (date .equals (today ) && current .getName ().equals (name )) {
116
+ return true ;
117
+ }
118
+ }
119
+ return false ;
120
+ }
121
+
122
+ /**
123
+ * Gets a list of active tasks for the user
124
+ * @param account of type AccountID, references the account
125
+ * @return a list of active tasks for the account
126
+ */
127
+ public List <TaskActive > activeTasks (AccountID account ){
128
+ List <TaskActive > taskActives = this .activeRepo .findAll ();
129
+ String today = new Date (System .currentTimeMillis ()).toString ().substring (0 , 10 );
130
+ String recent = taskActives .get (0 ).getDate ();
131
+
132
+ if (taskActives .isEmpty () || recent .equals (today )){
133
+ return updateActiveTasks (account , taskActives );
134
+ }
135
+ else {
136
+ return newActiveTasks ();
137
+ }
138
+ }
139
+
140
+ /** activeTasks() helper method
141
+ * Creates a new set of active tasks and saves it to the database
142
+ */
143
+ public List <TaskActive > newActiveTasks () {
144
+ //empty active tasks in order to replace them
145
+ List <Task > tasks = this .taskRepo .findAll ();
146
+ List <Integer > prev = new ArrayList <>();
147
+ Random rand = new Random ();
148
+
149
+ this .activeRepo .deleteAll ();
150
+
151
+ //choosing random tasks, changing them into active tasks, and adding them to the array
152
+ while (prev .size () < 3 ) {
153
+ int num = rand .nextInt (tasks .size ());
154
+ if (!prev .contains (num )) {
155
+ Task task = tasks .get (num );
156
+ prev .add (num );
157
+
158
+ this .activeRepo .save (new TaskActive (task .getName (), task .getReward () ,
159
+ new Date (System .currentTimeMillis ())));
160
+ }
161
+ }
162
+ return this .activeRepo .findAll ();
163
+ }
164
+
165
+ /** activeTasks() helper method
166
+ * Updates the active tasks to reflect the tasks that have not yet been completed today
167
+ * @param account of type AccountID, references the account
168
+ * @param taskActives of type List of TaskActive, references the current active tasks
169
+ */
170
+ public List <TaskActive > updateActiveTasks (AccountID account , List <TaskActive > taskActives ) {
171
+ //find completed tasks in order to remove them from active tasks
172
+ String today = new Date (System .currentTimeMillis ()).toString ().substring (0 ,10 );
173
+ List <String > names = new ArrayList <>();
174
+ List <TaskCompletionRecord > complete = getTaskCompletionRecords (account );
175
+
176
+ //look through the completed tasks to see which task names have been completed today
177
+ for (TaskCompletionRecord current : complete ) {
178
+ String date = current .getDate ();
179
+ if (date .equals (today )) {
180
+ names .add (current .getName ());
181
+ }
182
+ }
183
+
184
+ //remove the tasks with the same names as those found in today's completed tasks
185
+ for (String name : names ) {
186
+ for (int x = 0 ; x < taskActives .size (); x ++) {
187
+ if (name .equals (taskActives .get (x ).getName ())) {
188
+ taskActives .remove (x );
189
+ break ;
190
+ }
191
+ }
192
+ }
193
+ return taskActives ;
194
+ }
195
+
196
+ /**
197
+ * Gets a list of all known TaskCompletionRecords from the DB
198
+ * @return a list of TaskCompletionRecords
199
+ */
200
+ public List <TaskCompletionRecord > getAllTaskCompletionRecords () {
201
+ return this .completeRepo .findAll ();
202
+ }
203
+ }
0 commit comments