Skip to content

Commit

Permalink
follower Autocreate Hotfix (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
samuellvo authored Jul 20, 2023
1 parent 9b1072e commit 0100a87
Showing 1 changed file with 16 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
import com.fasterxml.jackson.databind.JsonNode;
import edu.sjsu.moth.server.db.Followers;
import edu.sjsu.moth.server.db.FollowersRepository;
import edu.sjsu.moth.server.db.AccountRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.ArrayList;

import java.util.Collections;
import java.util.List;

Expand All @@ -21,6 +23,8 @@
public class InboxController {
@Autowired
FollowersRepository followersRepository;
@Autowired
AccountRepository accountRepository;


//required to map payload from JSON to a Java Object for data access
Expand All @@ -42,11 +46,19 @@ public Mono<String> usersInbox(@PathVariable String id, @RequestBody JsonNode in
public Mono<String> followerHandler(String id, JsonNode inboxNode, String requestType) {
String follower = inboxNode.get("actor").asText();
if (requestType.equals("Follow")) {
// check id
if (accountRepository.findItemByAcct(id)==null) {
return Mono.error(new RuntimeException("Error: Account to follow doesn't exist."));
}
// find id, grab arraylist, append
return followersRepository.findItemById(id).flatMap(followedUser -> {
followedUser.getFollowers().add(follower);
return followersRepository.save(followedUser).thenReturn("done");
});
else {
return followersRepository.findItemById(id)
.switchIfEmpty(Mono.just(new Followers(id, new ArrayList<>())))
.flatMap(followedUser -> {
followedUser.getFollowers().add(follower);
return followersRepository.save(followedUser).thenReturn("done");
});
}
}
if (requestType.equals("Undo")) {
// find id, grab arraylist, remove
Expand Down

0 comments on commit 0100a87

Please sign in to comment.