Skip to content

Commit eeee9b3

Browse files
committed
implemented feed system
1 parent cd73714 commit eeee9b3

File tree

3 files changed

+313
-0
lines changed

3 files changed

+313
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.backend.controller;
2+
3+
import com.backend.usecases.facades.FeedSystemFacade;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.http.ResponseEntity;
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.RequestParam;
8+
import org.springframework.web.bind.annotation.RestController;
9+
10+
@RestController
11+
public class FeedController {
12+
private final FeedSystemFacade feedSystemFacade;
13+
14+
/**
15+
* Spring Boot Dependency Injection of the accountSystemFacade
16+
* @param feedSystemFacade the dependency to be injected
17+
*/
18+
@SuppressWarnings("unused")
19+
@Autowired
20+
public FeedController(FeedSystemFacade feedSystemFacade) {
21+
this.feedSystemFacade = feedSystemFacade;
22+
}
23+
24+
/**
25+
* Get a list of feed data representing information regarding recent posts made by friends
26+
* @param sessionID of type String, password to reference associated account
27+
* @return a response entity detailing successful completion (with a newly generated SessionID) or any associated error
28+
*/
29+
@GetMapping("/feed" )
30+
public ResponseEntity<Object> getFeed(@RequestParam String sessionID){
31+
// Run AccountManager service
32+
return this.feedSystemFacade.getFeed(sessionID);
33+
}
34+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.backend.usecases.facades;
2+
3+
import com.backend.entities.FeedItem;
4+
import com.backend.entities.IDs.AccountID;
5+
import com.backend.entities.IDs.SessionID;
6+
import com.backend.entities.TaskCompletionRecord;
7+
import com.backend.error.exceptions.SessionException;
8+
import com.backend.usecases.IErrorHandler;
9+
import com.backend.usecases.managers.AccountManager;
10+
import com.backend.usecases.managers.FriendsManager;
11+
import com.backend.usecases.managers.PetManager;
12+
import com.backend.usecases.managers.TaskManager;
13+
import org.springframework.beans.factory.annotation.Autowired;
14+
import org.springframework.http.HttpStatus;
15+
import org.springframework.http.ResponseEntity;
16+
import org.springframework.stereotype.Service;
17+
18+
import java.util.ArrayList;
19+
import java.util.Collections;
20+
import java.util.List;
21+
22+
@Service
23+
public class FeedSystemFacade {
24+
private final AccountManager accountManager;
25+
private final TaskManager taskManager;
26+
private final PetManager petManager;
27+
private final FriendsManager friendsManager;
28+
private final IErrorHandler errorHandler;
29+
30+
/**
31+
* Spring Boot Dependency Injection
32+
*
33+
* @param accountManager the dependency to be injected
34+
* @param taskManager the dependency to be injected
35+
* @param petManager the dependency to be injected
36+
* @param errorHandler the dependency to be injected
37+
*/
38+
@SuppressWarnings("unused")
39+
@Autowired
40+
public FeedSystemFacade(AccountManager accountManager, TaskManager taskManager, PetManager petManager, FriendsManager friendsManager, IErrorHandler errorHandler) {
41+
this.accountManager = accountManager;
42+
this.taskManager = taskManager;
43+
this.petManager = petManager;
44+
this.friendsManager = friendsManager;
45+
this.errorHandler = errorHandler;
46+
}
47+
48+
/**
49+
* Get a list of feed data representing information regarding recent posts made by friends
50+
* @param sessionID of type String, password to reference associated account
51+
* @return a response entity detailing successful completion (with a newly generated SessionID) or any associated error
52+
*/
53+
public ResponseEntity<Object> getFeed(String sessionID) {
54+
// verify session
55+
AccountID accountID = this.accountManager.verifySession(new SessionID(sessionID));
56+
if (accountID == null) {
57+
return this.errorHandler.logError(new SessionException("Invalid Session"), HttpStatus.BAD_REQUEST);
58+
}
59+
60+
// get list of friends
61+
List<String> friends = this.friendsManager.getFriends(accountID.getID());
62+
63+
// wrap FeedItem Objects
64+
List<FeedItem> feedItems = new ArrayList<>();
65+
List<TaskCompletionRecord> recordList = this.taskManager.getAllTaskCompletionRecords();
66+
Collections.reverse(recordList);
67+
68+
for (TaskCompletionRecord record : recordList.subList(0, 9)) {
69+
if (friends.contains(record.getAccountID())) {
70+
feedItems.add(new FeedItem(this.accountManager.getAccountInfo(record.getAccountIDObject()), record, this.petManager.getPet(record.getAccountID())));
71+
}
72+
}
73+
74+
return new ResponseEntity<>(feedItems, HttpStatus.OK);
75+
}
76+
}
Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
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

Comments
 (0)