SlideShare a Scribd company logo
1 of 25
Download to read offline
Creating a Facebook Clone - Part XXIV
@Service
public class PostService {
private static final int DAY = 24 * 60 * 60000;
@Autowired
private UserRepository users;
@Autowired
private PostRepository posts;
@Autowired
private NewsfeedRepository news;
@Autowired
private CommentRepository comments;
@Autowired
private NotificationService notifications;
public List<PostDAO> postsOf(String authToken, String userId,
int fromPage, int pageSize) {
PostService
@Service
public class PostService {
private static final int DAY = 24 * 60 * 60000;
@Autowired
private UserRepository users;
@Autowired
private PostRepository posts;
@Autowired
private NewsfeedRepository news;
@Autowired
private CommentRepository comments;
@Autowired
private NotificationService notifications;
public List<PostDAO> postsOf(String authToken, String userId,
int fromPage, int pageSize) {
PostService
@Autowired
private NotificationService notifications;
public List<PostDAO> postsOf(String authToken, String userId,
int fromPage, int pageSize) {
User u = users.findByAuthtoken(authToken).get(0);
Page<Post> postPage;
if(u.getId().equals(userId) || u.isFriendById(userId)) {
postPage = posts.findAllPostsByUser(
userId, PageRequest.of(fromPage, pageSize,
Sort.by(Sort.Direction.DESC, "date")));
} else {
postPage = posts.findPostsByUser(
userId, VisibilityConstants.PUBLIC.asString(),
PageRequest.of(fromPage, pageSize,
Sort.by(Sort.Direction.DESC, "date")));
}
List<PostDAO> response = new ArrayList<>();
for(Post p : postPage) {
response.add(p.getDAO());
}
return response;
}
PostService
@Autowired
private NotificationService notifications;
public List<PostDAO> postsOf(String authToken, String userId,
int fromPage, int pageSize) {
User u = users.findByAuthtoken(authToken).get(0);
Page<Post> postPage;
if(u.getId().equals(userId) || u.isFriendById(userId)) {
postPage = posts.findAllPostsByUser(
userId, PageRequest.of(fromPage, pageSize,
Sort.by(Sort.Direction.DESC, "date")));
} else {
postPage = posts.findPostsByUser(
userId, VisibilityConstants.PUBLIC.asString(),
PageRequest.of(fromPage, pageSize,
Sort.by(Sort.Direction.DESC, "date")));
}
List<PostDAO> response = new ArrayList<>();
for(Post p : postPage) {
response.add(p.getDAO());
}
return response;
}
PostService
@Autowired
private NotificationService notifications;
public List<PostDAO> postsOf(String authToken, String userId,
int fromPage, int pageSize) {
User u = users.findByAuthtoken(authToken).get(0);
Page<Post> postPage;
if(u.getId().equals(userId) || u.isFriendById(userId)) {
postPage = posts.findAllPostsByUser(
userId, PageRequest.of(fromPage, pageSize,
Sort.by(Sort.Direction.DESC, "date")));
} else {
postPage = posts.findPostsByUser(
userId, VisibilityConstants.PUBLIC.asString(),
PageRequest.of(fromPage, pageSize,
Sort.by(Sort.Direction.DESC, "date")));
}
List<PostDAO> response = new ArrayList<>();
for(Post p : postPage) {
response.add(p.getDAO());
}
return response;
}
PostService
@Autowired
private NotificationService notifications;
public List<PostDAO> postsOf(String authToken, String userId,
int fromPage, int pageSize) {
User u = users.findByAuthtoken(authToken).get(0);
Page<Post> postPage;
if(u.getId().equals(userId) || u.isFriendById(userId)) {
postPage = posts.findAllPostsByUser(
userId, PageRequest.of(fromPage, pageSize,
Sort.by(Sort.Direction.DESC, "date")));
} else {
postPage = posts.findPostsByUser(
userId, VisibilityConstants.PUBLIC.asString(),
PageRequest.of(fromPage, pageSize,
Sort.by(Sort.Direction.DESC, "date")));
}
List<PostDAO> response = new ArrayList<>();
for(Post p : postPage) {
response.add(p.getDAO());
}
return response;
}
PostService
@Autowired
private NotificationService notifications;
public List<PostDAO> postsOf(String authToken, String userId,
int fromPage, int pageSize) {
User u = users.findByAuthtoken(authToken).get(0);
Page<Post> postPage;
if(u.getId().equals(userId) || u.isFriendById(userId)) {
postPage = posts.findAllPostsByUser(
userId, PageRequest.of(fromPage, pageSize,
Sort.by(Sort.Direction.DESC, "date")));
} else {
postPage = posts.findPostsByUser(
userId, VisibilityConstants.PUBLIC.asString(),
PageRequest.of(fromPage, pageSize,
Sort.by(Sort.Direction.DESC, "date")));
}
List<PostDAO> response = new ArrayList<>();
for(Post p : postPage) {
response.add(p.getDAO());
}
return response;
}
PostService
for(Post p : postPage) {
response.add(p.getDAO());
}
return response;
}
public List<PostDAO> newsfeed(String authToken, int fromPage,
int pageSize) {
Page<Newsfeed> n = news.findNewsfeedForUser(authToken,
PageRequest.of(fromPage, pageSize,
Sort.by(Sort.Order.desc("postDay"),
Sort.Order.asc("rank"),
Sort.Order.desc("postTimestamp"))));
List<PostDAO> response = new ArrayList<>();
for(Newsfeed c : n) {
response.add(c.getEntry().getDAO());
}
return response;
}
public String post(String authToken, PostDAO pd) {
User u = users.findByAuthtoken(authToken).get(0);
Post p = new Post();
p.setContent(pd.getContent());
PostService
for(Post p : postPage) {
response.add(p.getDAO());
}
return response;
}
public List<PostDAO> newsfeed(String authToken, int fromPage,
int pageSize) {
Page<Newsfeed> n = news.findNewsfeedForUser(authToken,
PageRequest.of(fromPage, pageSize,
Sort.by(Sort.Order.desc("postDay"),
Sort.Order.asc("rank"),
Sort.Order.desc("postTimestamp"))));
List<PostDAO> response = new ArrayList<>();
for(Newsfeed c : n) {
response.add(c.getEntry().getDAO());
}
return response;
}
public String post(String authToken, PostDAO pd) {
User u = users.findByAuthtoken(authToken).get(0);
Post p = new Post();
p.setContent(pd.getContent());
PostService
for(Post p : postPage) {
response.add(p.getDAO());
}
return response;
}
public List<PostDAO> newsfeed(String authToken, int fromPage,
int pageSize) {
Page<Newsfeed> n = news.findNewsfeedForUser(authToken,
PageRequest.of(fromPage, pageSize,
Sort.by(Sort.Order.desc("postDay"),
Sort.Order.asc("rank"),
Sort.Order.desc("postTimestamp"))));
List<PostDAO> response = new ArrayList<>();
for(Newsfeed c : n) {
response.add(c.getEntry().getDAO());
}
return response;
}
public String post(String authToken, PostDAO pd) {
User u = users.findByAuthtoken(authToken).get(0);
Post p = new Post();
p.setContent(pd.getContent());
PostService
response.add(c.getEntry().getDAO());
}
return response;
}
public String post(String authToken, PostDAO pd) {
User u = users.findByAuthtoken(authToken).get(0);
Post p = new Post();
p.setContent(pd.getContent());
p.setDate(System.currentTimeMillis());
p.setStyling(pd.getStyling());
p.setTitle(pd.getTitle());
p.setUser(u);
p.setVisibility(pd.getVisibility());
posts.save(p);
addPostToNewsfeed(u, p);
if(u.getFriends() != null) {
for(User c : u.getFriends()) {
addPostToNewsfeed(c, p);
}
}
return p.getId();
}
PostService
response.add(c.getEntry().getDAO());
}
return response;
}
public String post(String authToken, PostDAO pd) {
User u = users.findByAuthtoken(authToken).get(0);
Post p = new Post();
p.setContent(pd.getContent());
p.setDate(System.currentTimeMillis());
p.setStyling(pd.getStyling());
p.setTitle(pd.getTitle());
p.setUser(u);
p.setVisibility(pd.getVisibility());
posts.save(p);
addPostToNewsfeed(u, p);
if(u.getFriends() != null) {
for(User c : u.getFriends()) {
addPostToNewsfeed(c, p);
}
}
return p.getId();
}
PostService
response.add(c.getEntry().getDAO());
}
return response;
}
public String post(String authToken, PostDAO pd) {
User u = users.findByAuthtoken(authToken).get(0);
Post p = new Post();
p.setContent(pd.getContent());
p.setDate(System.currentTimeMillis());
p.setStyling(pd.getStyling());
p.setTitle(pd.getTitle());
p.setUser(u);
p.setVisibility(pd.getVisibility());
posts.save(p);
addPostToNewsfeed(u, p);
if(u.getFriends() != null) {
for(User c : u.getFriends()) {
addPostToNewsfeed(c, p);
}
}
return p.getId();
}
PostService
response.add(c.getEntry().getDAO());
}
return response;
}
public String post(String authToken, PostDAO pd) {
User u = users.findByAuthtoken(authToken).get(0);
Post p = new Post();
p.setContent(pd.getContent());
p.setDate(System.currentTimeMillis());
p.setStyling(pd.getStyling());
p.setTitle(pd.getTitle());
p.setUser(u);
p.setVisibility(pd.getVisibility());
posts.save(p);
addPostToNewsfeed(u, p);
if(u.getFriends() != null) {
for(User c : u.getFriends()) {
addPostToNewsfeed(c, p);
}
}
return p.getId();
}
PostService
response.add(c.getEntry().getDAO());
}
return response;
}
public String post(String authToken, PostDAO pd) {
User u = users.findByAuthtoken(authToken).get(0);
Post p = new Post();
p.setContent(pd.getContent());
p.setDate(System.currentTimeMillis());
p.setStyling(pd.getStyling());
p.setTitle(pd.getTitle());
p.setUser(u);
p.setVisibility(pd.getVisibility());
posts.save(p);
addPostToNewsfeed(u, p);
if(u.getFriends() != null) {
for(User c : u.getFriends()) {
addPostToNewsfeed(c, p);
}
}
return p.getId();
}
PostService
for(User c : u.getFriends()) {
addPostToNewsfeed(c, p);
}
}
return p.getId();
}
private void addPostToNewsfeed(User u, Post p) {
Newsfeed n = new Newsfeed();
n.setEntry(p);
n.setParent(u);
n.setPostDay(System.currentTimeMillis() / DAY);
n.setPostTimestamp(System.currentTimeMillis());
news.save(n);
}
public String comment(String authToken, String postId, CommentDAO cd)
throws PermissionException {
User u = users.findByAuthtoken(authToken).get(0);
Post p = posts.findById(postId).get();
if(!VisibilityConstants.isPublic(p.getVisibility())) {
if(!u.getId().equals(p.getUser().getId())) {
if(!p.getUser().isFriendById(u.getId())) {
throw new
PostService
for(User c : u.getFriends()) {
addPostToNewsfeed(c, p);
}
}
return p.getId();
}
private void addPostToNewsfeed(User u, Post p) {
Newsfeed n = new Newsfeed();
n.setEntry(p);
n.setParent(u);
n.setPostDay(System.currentTimeMillis() / DAY);
n.setPostTimestamp(System.currentTimeMillis());
news.save(n);
}
public String comment(String authToken, String postId, CommentDAO cd)
throws PermissionException {
User u = users.findByAuthtoken(authToken).get(0);
Post p = posts.findById(postId).get();
if(!VisibilityConstants.isPublic(p.getVisibility())) {
if(!u.getId().equals(p.getUser().getId())) {
if(!p.getUser().isFriendById(u.getId())) {
throw new
PostService
news.save(n);
}
public String comment(String authToken, String postId, CommentDAO cd)
throws PermissionException {
User u = users.findByAuthtoken(authToken).get(0);
Post p = posts.findById(postId).get();
if(!VisibilityConstants.isPublic(p.getVisibility())) {
if(!u.getId().equals(p.getUser().getId())) {
if(!p.getUser().isFriendById(u.getId())) {
throw new
PermissionException("This is a private post!");
}
}
}
Comment c = new Comment();
c.setDate(System.currentTimeMillis());
if(cd.getParentComment() != null) {
c.setParentComment(comments.findById(cd.getParentComment()).
get());
}
c.setText(cd.getText());
c.setUser(u);
comments.save(c);
PostService
news.save(n);
}
public String comment(String authToken, String postId, CommentDAO cd)
throws PermissionException {
User u = users.findByAuthtoken(authToken).get(0);
Post p = posts.findById(postId).get();
if(!VisibilityConstants.isPublic(p.getVisibility())) {
if(!u.getId().equals(p.getUser().getId())) {
if(!p.getUser().isFriendById(u.getId())) {
throw new
PermissionException("This is a private post!");
}
}
}
Comment c = new Comment();
c.setDate(System.currentTimeMillis());
if(cd.getParentComment() != null) {
c.setParentComment(comments.findById(cd.getParentComment()).
get());
}
c.setText(cd.getText());
c.setUser(u);
comments.save(c);
PostService
if(!p.getUser().isFriendById(u.getId())) {
throw new
PermissionException("This is a private post!");
}
}
}
Comment c = new Comment();
c.setDate(System.currentTimeMillis());
if(cd.getParentComment() != null) {
c.setParentComment(comments.findById(cd.getParentComment()).
get());
}
c.setText(cd.getText());
c.setUser(u);
comments.save(c);
p.getComments().add(c);
posts.save(p);
notifications.sendNotification(u,
new NotificationDAO(null, u.getDAO(),
"commented on your post", "uE0B7",
0x00A400, 0, false,
postId, cd.getId()));
return c.getId();
}
PostService
if(!p.getUser().isFriendById(u.getId())) {
throw new
PermissionException("This is a private post!");
}
}
}
Comment c = new Comment();
c.setDate(System.currentTimeMillis());
if(cd.getParentComment() != null) {
c.setParentComment(comments.findById(cd.getParentComment()).
get());
}
c.setText(cd.getText());
c.setUser(u);
comments.save(c);
p.getComments().add(c);
posts.save(p);
notifications.sendNotification(u,
new NotificationDAO(null, u.getDAO(),
"commented on your post", "uE0B7",
0x00A400, 0, false,
postId, cd.getId()));
return c.getId();
}
PostService
p.getComments().add(c);
posts.save(p);
notifications.sendNotification(u,
new NotificationDAO(null, u.getDAO(),
"commented on your post", "uE0B7",
0x00A400, 0, false,
postId, cd.getId()));
return c.getId();
}
public void like(String authToken, String postId) {
User u = users.findByAuthtoken(authToken).get(0);
Post p = posts.findById(postId).get();
if(!p.getLikes().contains(u)) {
p.getLikes().add(u);
posts.save(p);
notifications.sendNotification(u,
new NotificationDAO(null, u.getDAO(),
"liked your post", "uE8DC",
0x587EBE, 0, false,
postId, null));
}
}
}
PostService
p.getComments().add(c);
posts.save(p);
notifications.sendNotification(u,
new NotificationDAO(null, u.getDAO(),
"commented on your post", "uE0B7",
0x00A400, 0, false,
postId, cd.getId()));
return c.getId();
}
public void like(String authToken, String postId) {
User u = users.findByAuthtoken(authToken).get(0);
Post p = posts.findById(postId).get();
if(!p.getLikes().contains(u)) {
p.getLikes().add(u);
posts.save(p);
notifications.sendNotification(u,
new NotificationDAO(null, u.getDAO(),
"liked your post", "uE8DC",
0x587EBE, 0, false,
postId, null));
}
}
}
PostService
p.getComments().add(c);
posts.save(p);
notifications.sendNotification(u,
new NotificationDAO(null, u.getDAO(),
"commented on your post", "uE0B7",
0x00A400, 0, false,
postId, cd.getId()));
return c.getId();
}
public void like(String authToken, String postId) {
User u = users.findByAuthtoken(authToken).get(0);
Post p = posts.findById(postId).get();
if(!p.getLikes().contains(u)) {
p.getLikes().add(u);
posts.save(p);
notifications.sendNotification(u,
new NotificationDAO(null, u.getDAO(),
"liked your post", "uE8DC",
0x587EBE, 0, false,
postId, null));
}
}
}
PostService

More Related Content

Similar to Creating a Facebook Clone - Part XXIV.pdf

Testdrevet javautvikling på objektorienterte skinner
Testdrevet javautvikling på objektorienterte skinnerTestdrevet javautvikling på objektorienterte skinner
Testdrevet javautvikling på objektorienterte skinner
Truls Jørgensen
 
Include- Modularity using design patterns- Fault tolerance and Compone.pdf
Include- Modularity using design patterns- Fault tolerance and Compone.pdfInclude- Modularity using design patterns- Fault tolerance and Compone.pdf
Include- Modularity using design patterns- Fault tolerance and Compone.pdf
RyanF2PLeev
 
Golang slidesaudrey
Golang slidesaudreyGolang slidesaudrey
Golang slidesaudrey
Audrey Lim
 
Spca2014 hillier build your_own_rest_service
Spca2014 hillier build your_own_rest_serviceSpca2014 hillier build your_own_rest_service
Spca2014 hillier build your_own_rest_service
NCCOMMS
 

Similar to Creating a Facebook Clone - Part XXIV.pdf (20)

Creating a Facebook Clone - Part XX - Transcript.pdf
Creating a Facebook Clone - Part XX - Transcript.pdfCreating a Facebook Clone - Part XX - Transcript.pdf
Creating a Facebook Clone - Part XX - Transcript.pdf
 
Creating a Whatsapp Clone - Part XIII.pdf
Creating a Whatsapp Clone - Part XIII.pdfCreating a Whatsapp Clone - Part XIII.pdf
Creating a Whatsapp Clone - Part XIII.pdf
 
Creating a Facebook Clone - Part XXIX - Transcript.pdf
Creating a Facebook Clone - Part XXIX - Transcript.pdfCreating a Facebook Clone - Part XXIX - Transcript.pdf
Creating a Facebook Clone - Part XXIX - Transcript.pdf
 
Creating a Facebook Clone - Part XIX.pdf
Creating a Facebook Clone - Part XIX.pdfCreating a Facebook Clone - Part XIX.pdf
Creating a Facebook Clone - Part XIX.pdf
 
Creating a Facebook Clone - Part XXXI.pdf
Creating a Facebook Clone - Part XXXI.pdfCreating a Facebook Clone - Part XXXI.pdf
Creating a Facebook Clone - Part XXXI.pdf
 
Creating a Facebook Clone - Part XX.pdf
Creating a Facebook Clone - Part XX.pdfCreating a Facebook Clone - Part XX.pdf
Creating a Facebook Clone - Part XX.pdf
 
Creating a Facebook Clone - Part XXXVIII - Transcript.pdf
Creating a Facebook Clone - Part XXXVIII - Transcript.pdfCreating a Facebook Clone - Part XXXVIII - Transcript.pdf
Creating a Facebook Clone - Part XXXVIII - Transcript.pdf
 
Testdrevet javautvikling på objektorienterte skinner
Testdrevet javautvikling på objektorienterte skinnerTestdrevet javautvikling på objektorienterte skinner
Testdrevet javautvikling på objektorienterte skinner
 
Creating a Whatsapp Clone - Part II.pdf
Creating a Whatsapp Clone - Part II.pdfCreating a Whatsapp Clone - Part II.pdf
Creating a Whatsapp Clone - Part II.pdf
 
Include- Modularity using design patterns- Fault tolerance and Compone.pdf
Include- Modularity using design patterns- Fault tolerance and Compone.pdfInclude- Modularity using design patterns- Fault tolerance and Compone.pdf
Include- Modularity using design patterns- Fault tolerance and Compone.pdf
 
Creating a Facebook Clone - Part XXXIII.pdf
Creating a Facebook Clone - Part XXXIII.pdfCreating a Facebook Clone - Part XXXIII.pdf
Creating a Facebook Clone - Part XXXIII.pdf
 
Code Smells y Refactoring o haciendo que nuestro codigo huela (y se vea) mejo...
Code Smells y Refactoring o haciendo que nuestro codigo huela (y se vea) mejo...Code Smells y Refactoring o haciendo que nuestro codigo huela (y se vea) mejo...
Code Smells y Refactoring o haciendo que nuestro codigo huela (y se vea) mejo...
 
Creating a Facebook Clone - Part XXVIII.pdf
Creating a Facebook Clone - Part XXVIII.pdfCreating a Facebook Clone - Part XXVIII.pdf
Creating a Facebook Clone - Part XXVIII.pdf
 
Creating a Whatsapp Clone - Part XI.pdf
Creating a Whatsapp Clone - Part XI.pdfCreating a Whatsapp Clone - Part XI.pdf
Creating a Whatsapp Clone - Part XI.pdf
 
Creating an Uber Clone - Part XXXX.pdf
Creating an Uber Clone - Part XXXX.pdfCreating an Uber Clone - Part XXXX.pdf
Creating an Uber Clone - Part XXXX.pdf
 
Server Side Swift with Swag
Server Side Swift with SwagServer Side Swift with Swag
Server Side Swift with Swag
 
Golang slidesaudrey
Golang slidesaudreyGolang slidesaudrey
Golang slidesaudrey
 
Spca2014 hillier build your_own_rest_service
Spca2014 hillier build your_own_rest_serviceSpca2014 hillier build your_own_rest_service
Spca2014 hillier build your_own_rest_service
 
Creating a Facebook Clone - Part XXXI - Transcript.pdf
Creating a Facebook Clone - Part XXXI - Transcript.pdfCreating a Facebook Clone - Part XXXI - Transcript.pdf
Creating a Facebook Clone - Part XXXI - Transcript.pdf
 
Entity Framework Core: tips and tricks
Entity Framework Core: tips and tricksEntity Framework Core: tips and tricks
Entity Framework Core: tips and tricks
 

More from ShaiAlmog1

More from ShaiAlmog1 (20)

The Duck Teaches Learn to debug from the masters. Local to production- kill ...
The Duck Teaches  Learn to debug from the masters. Local to production- kill ...The Duck Teaches  Learn to debug from the masters. Local to production- kill ...
The Duck Teaches Learn to debug from the masters. Local to production- kill ...
 
create-netflix-clone-06-client-ui.pdf
create-netflix-clone-06-client-ui.pdfcreate-netflix-clone-06-client-ui.pdf
create-netflix-clone-06-client-ui.pdf
 
create-netflix-clone-01-introduction_transcript.pdf
create-netflix-clone-01-introduction_transcript.pdfcreate-netflix-clone-01-introduction_transcript.pdf
create-netflix-clone-01-introduction_transcript.pdf
 
create-netflix-clone-02-server_transcript.pdf
create-netflix-clone-02-server_transcript.pdfcreate-netflix-clone-02-server_transcript.pdf
create-netflix-clone-02-server_transcript.pdf
 
create-netflix-clone-04-server-continued_transcript.pdf
create-netflix-clone-04-server-continued_transcript.pdfcreate-netflix-clone-04-server-continued_transcript.pdf
create-netflix-clone-04-server-continued_transcript.pdf
 
create-netflix-clone-01-introduction.pdf
create-netflix-clone-01-introduction.pdfcreate-netflix-clone-01-introduction.pdf
create-netflix-clone-01-introduction.pdf
 
create-netflix-clone-06-client-ui_transcript.pdf
create-netflix-clone-06-client-ui_transcript.pdfcreate-netflix-clone-06-client-ui_transcript.pdf
create-netflix-clone-06-client-ui_transcript.pdf
 
create-netflix-clone-03-server.pdf
create-netflix-clone-03-server.pdfcreate-netflix-clone-03-server.pdf
create-netflix-clone-03-server.pdf
 
create-netflix-clone-04-server-continued.pdf
create-netflix-clone-04-server-continued.pdfcreate-netflix-clone-04-server-continued.pdf
create-netflix-clone-04-server-continued.pdf
 
create-netflix-clone-05-client-model_transcript.pdf
create-netflix-clone-05-client-model_transcript.pdfcreate-netflix-clone-05-client-model_transcript.pdf
create-netflix-clone-05-client-model_transcript.pdf
 
create-netflix-clone-03-server_transcript.pdf
create-netflix-clone-03-server_transcript.pdfcreate-netflix-clone-03-server_transcript.pdf
create-netflix-clone-03-server_transcript.pdf
 
create-netflix-clone-02-server.pdf
create-netflix-clone-02-server.pdfcreate-netflix-clone-02-server.pdf
create-netflix-clone-02-server.pdf
 
create-netflix-clone-05-client-model.pdf
create-netflix-clone-05-client-model.pdfcreate-netflix-clone-05-client-model.pdf
create-netflix-clone-05-client-model.pdf
 
Creating a Whatsapp Clone - Part IX - Transcript.pdf
Creating a Whatsapp Clone - Part IX - Transcript.pdfCreating a Whatsapp Clone - Part IX - Transcript.pdf
Creating a Whatsapp Clone - Part IX - Transcript.pdf
 
Creating a Whatsapp Clone - Part II - Transcript.pdf
Creating a Whatsapp Clone - Part II - Transcript.pdfCreating a Whatsapp Clone - Part II - Transcript.pdf
Creating a Whatsapp Clone - Part II - Transcript.pdf
 
Creating a Whatsapp Clone - Part V - Transcript.pdf
Creating a Whatsapp Clone - Part V - Transcript.pdfCreating a Whatsapp Clone - Part V - Transcript.pdf
Creating a Whatsapp Clone - Part V - Transcript.pdf
 
Creating a Whatsapp Clone - Part IV - Transcript.pdf
Creating a Whatsapp Clone - Part IV - Transcript.pdfCreating a Whatsapp Clone - Part IV - Transcript.pdf
Creating a Whatsapp Clone - Part IV - Transcript.pdf
 
Creating a Whatsapp Clone - Part IV.pdf
Creating a Whatsapp Clone - Part IV.pdfCreating a Whatsapp Clone - Part IV.pdf
Creating a Whatsapp Clone - Part IV.pdf
 
Creating a Whatsapp Clone - Part I - Transcript.pdf
Creating a Whatsapp Clone - Part I - Transcript.pdfCreating a Whatsapp Clone - Part I - Transcript.pdf
Creating a Whatsapp Clone - Part I - Transcript.pdf
 
Creating a Whatsapp Clone - Part IX.pdf
Creating a Whatsapp Clone - Part IX.pdfCreating a Whatsapp Clone - Part IX.pdf
Creating a Whatsapp Clone - Part IX.pdf
 

Recently uploaded

Structuring Teams and Portfolios for Success
Structuring Teams and Portfolios for SuccessStructuring Teams and Portfolios for Success
Structuring Teams and Portfolios for Success
UXDXConf
 

Recently uploaded (20)

Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
 
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptxUnpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
 
Structuring Teams and Portfolios for Success
Structuring Teams and Portfolios for SuccessStructuring Teams and Portfolios for Success
Structuring Teams and Portfolios for Success
 
Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdf
Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdfLinux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdf
Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdf
 
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdfThe Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
 
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
 
Intro in Product Management - Коротко про професію продакт менеджера
Intro in Product Management - Коротко про професію продакт менеджераIntro in Product Management - Коротко про професію продакт менеджера
Intro in Product Management - Коротко про професію продакт менеджера
 
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdfSimplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
 
Top 10 Symfony Development Companies 2024
Top 10 Symfony Development Companies 2024Top 10 Symfony Development Companies 2024
Top 10 Symfony Development Companies 2024
 
What's New in Teams Calling, Meetings and Devices April 2024
What's New in Teams Calling, Meetings and Devices April 2024What's New in Teams Calling, Meetings and Devices April 2024
What's New in Teams Calling, Meetings and Devices April 2024
 
Demystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John StaveleyDemystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John Staveley
 
Where to Learn More About FDO _ Richard at FIDO Alliance.pdf
Where to Learn More About FDO _ Richard at FIDO Alliance.pdfWhere to Learn More About FDO _ Richard at FIDO Alliance.pdf
Where to Learn More About FDO _ Richard at FIDO Alliance.pdf
 
AI presentation and introduction - Retrieval Augmented Generation RAG 101
AI presentation and introduction - Retrieval Augmented Generation RAG 101AI presentation and introduction - Retrieval Augmented Generation RAG 101
AI presentation and introduction - Retrieval Augmented Generation RAG 101
 
Syngulon - Selection technology May 2024.pdf
Syngulon - Selection technology May 2024.pdfSyngulon - Selection technology May 2024.pdf
Syngulon - Selection technology May 2024.pdf
 
Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdf
Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdfIntroduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdf
Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdf
 
Strategic AI Integration in Engineering Teams
Strategic AI Integration in Engineering TeamsStrategic AI Integration in Engineering Teams
Strategic AI Integration in Engineering Teams
 
Optimizing NoSQL Performance Through Observability
Optimizing NoSQL Performance Through ObservabilityOptimizing NoSQL Performance Through Observability
Optimizing NoSQL Performance Through Observability
 
IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024
 
The Metaverse: Are We There Yet?
The  Metaverse:    Are   We  There  Yet?The  Metaverse:    Are   We  There  Yet?
The Metaverse: Are We There Yet?
 
WebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM PerformanceWebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM Performance
 

Creating a Facebook Clone - Part XXIV.pdf

  • 1. Creating a Facebook Clone - Part XXIV
  • 2. @Service public class PostService { private static final int DAY = 24 * 60 * 60000; @Autowired private UserRepository users; @Autowired private PostRepository posts; @Autowired private NewsfeedRepository news; @Autowired private CommentRepository comments; @Autowired private NotificationService notifications; public List<PostDAO> postsOf(String authToken, String userId, int fromPage, int pageSize) { PostService
  • 3. @Service public class PostService { private static final int DAY = 24 * 60 * 60000; @Autowired private UserRepository users; @Autowired private PostRepository posts; @Autowired private NewsfeedRepository news; @Autowired private CommentRepository comments; @Autowired private NotificationService notifications; public List<PostDAO> postsOf(String authToken, String userId, int fromPage, int pageSize) { PostService
  • 4. @Autowired private NotificationService notifications; public List<PostDAO> postsOf(String authToken, String userId, int fromPage, int pageSize) { User u = users.findByAuthtoken(authToken).get(0); Page<Post> postPage; if(u.getId().equals(userId) || u.isFriendById(userId)) { postPage = posts.findAllPostsByUser( userId, PageRequest.of(fromPage, pageSize, Sort.by(Sort.Direction.DESC, "date"))); } else { postPage = posts.findPostsByUser( userId, VisibilityConstants.PUBLIC.asString(), PageRequest.of(fromPage, pageSize, Sort.by(Sort.Direction.DESC, "date"))); } List<PostDAO> response = new ArrayList<>(); for(Post p : postPage) { response.add(p.getDAO()); } return response; } PostService
  • 5. @Autowired private NotificationService notifications; public List<PostDAO> postsOf(String authToken, String userId, int fromPage, int pageSize) { User u = users.findByAuthtoken(authToken).get(0); Page<Post> postPage; if(u.getId().equals(userId) || u.isFriendById(userId)) { postPage = posts.findAllPostsByUser( userId, PageRequest.of(fromPage, pageSize, Sort.by(Sort.Direction.DESC, "date"))); } else { postPage = posts.findPostsByUser( userId, VisibilityConstants.PUBLIC.asString(), PageRequest.of(fromPage, pageSize, Sort.by(Sort.Direction.DESC, "date"))); } List<PostDAO> response = new ArrayList<>(); for(Post p : postPage) { response.add(p.getDAO()); } return response; } PostService
  • 6. @Autowired private NotificationService notifications; public List<PostDAO> postsOf(String authToken, String userId, int fromPage, int pageSize) { User u = users.findByAuthtoken(authToken).get(0); Page<Post> postPage; if(u.getId().equals(userId) || u.isFriendById(userId)) { postPage = posts.findAllPostsByUser( userId, PageRequest.of(fromPage, pageSize, Sort.by(Sort.Direction.DESC, "date"))); } else { postPage = posts.findPostsByUser( userId, VisibilityConstants.PUBLIC.asString(), PageRequest.of(fromPage, pageSize, Sort.by(Sort.Direction.DESC, "date"))); } List<PostDAO> response = new ArrayList<>(); for(Post p : postPage) { response.add(p.getDAO()); } return response; } PostService
  • 7. @Autowired private NotificationService notifications; public List<PostDAO> postsOf(String authToken, String userId, int fromPage, int pageSize) { User u = users.findByAuthtoken(authToken).get(0); Page<Post> postPage; if(u.getId().equals(userId) || u.isFriendById(userId)) { postPage = posts.findAllPostsByUser( userId, PageRequest.of(fromPage, pageSize, Sort.by(Sort.Direction.DESC, "date"))); } else { postPage = posts.findPostsByUser( userId, VisibilityConstants.PUBLIC.asString(), PageRequest.of(fromPage, pageSize, Sort.by(Sort.Direction.DESC, "date"))); } List<PostDAO> response = new ArrayList<>(); for(Post p : postPage) { response.add(p.getDAO()); } return response; } PostService
  • 8. @Autowired private NotificationService notifications; public List<PostDAO> postsOf(String authToken, String userId, int fromPage, int pageSize) { User u = users.findByAuthtoken(authToken).get(0); Page<Post> postPage; if(u.getId().equals(userId) || u.isFriendById(userId)) { postPage = posts.findAllPostsByUser( userId, PageRequest.of(fromPage, pageSize, Sort.by(Sort.Direction.DESC, "date"))); } else { postPage = posts.findPostsByUser( userId, VisibilityConstants.PUBLIC.asString(), PageRequest.of(fromPage, pageSize, Sort.by(Sort.Direction.DESC, "date"))); } List<PostDAO> response = new ArrayList<>(); for(Post p : postPage) { response.add(p.getDAO()); } return response; } PostService
  • 9. for(Post p : postPage) { response.add(p.getDAO()); } return response; } public List<PostDAO> newsfeed(String authToken, int fromPage, int pageSize) { Page<Newsfeed> n = news.findNewsfeedForUser(authToken, PageRequest.of(fromPage, pageSize, Sort.by(Sort.Order.desc("postDay"), Sort.Order.asc("rank"), Sort.Order.desc("postTimestamp")))); List<PostDAO> response = new ArrayList<>(); for(Newsfeed c : n) { response.add(c.getEntry().getDAO()); } return response; } public String post(String authToken, PostDAO pd) { User u = users.findByAuthtoken(authToken).get(0); Post p = new Post(); p.setContent(pd.getContent()); PostService
  • 10. for(Post p : postPage) { response.add(p.getDAO()); } return response; } public List<PostDAO> newsfeed(String authToken, int fromPage, int pageSize) { Page<Newsfeed> n = news.findNewsfeedForUser(authToken, PageRequest.of(fromPage, pageSize, Sort.by(Sort.Order.desc("postDay"), Sort.Order.asc("rank"), Sort.Order.desc("postTimestamp")))); List<PostDAO> response = new ArrayList<>(); for(Newsfeed c : n) { response.add(c.getEntry().getDAO()); } return response; } public String post(String authToken, PostDAO pd) { User u = users.findByAuthtoken(authToken).get(0); Post p = new Post(); p.setContent(pd.getContent()); PostService
  • 11. for(Post p : postPage) { response.add(p.getDAO()); } return response; } public List<PostDAO> newsfeed(String authToken, int fromPage, int pageSize) { Page<Newsfeed> n = news.findNewsfeedForUser(authToken, PageRequest.of(fromPage, pageSize, Sort.by(Sort.Order.desc("postDay"), Sort.Order.asc("rank"), Sort.Order.desc("postTimestamp")))); List<PostDAO> response = new ArrayList<>(); for(Newsfeed c : n) { response.add(c.getEntry().getDAO()); } return response; } public String post(String authToken, PostDAO pd) { User u = users.findByAuthtoken(authToken).get(0); Post p = new Post(); p.setContent(pd.getContent()); PostService
  • 12. response.add(c.getEntry().getDAO()); } return response; } public String post(String authToken, PostDAO pd) { User u = users.findByAuthtoken(authToken).get(0); Post p = new Post(); p.setContent(pd.getContent()); p.setDate(System.currentTimeMillis()); p.setStyling(pd.getStyling()); p.setTitle(pd.getTitle()); p.setUser(u); p.setVisibility(pd.getVisibility()); posts.save(p); addPostToNewsfeed(u, p); if(u.getFriends() != null) { for(User c : u.getFriends()) { addPostToNewsfeed(c, p); } } return p.getId(); } PostService
  • 13. response.add(c.getEntry().getDAO()); } return response; } public String post(String authToken, PostDAO pd) { User u = users.findByAuthtoken(authToken).get(0); Post p = new Post(); p.setContent(pd.getContent()); p.setDate(System.currentTimeMillis()); p.setStyling(pd.getStyling()); p.setTitle(pd.getTitle()); p.setUser(u); p.setVisibility(pd.getVisibility()); posts.save(p); addPostToNewsfeed(u, p); if(u.getFriends() != null) { for(User c : u.getFriends()) { addPostToNewsfeed(c, p); } } return p.getId(); } PostService
  • 14. response.add(c.getEntry().getDAO()); } return response; } public String post(String authToken, PostDAO pd) { User u = users.findByAuthtoken(authToken).get(0); Post p = new Post(); p.setContent(pd.getContent()); p.setDate(System.currentTimeMillis()); p.setStyling(pd.getStyling()); p.setTitle(pd.getTitle()); p.setUser(u); p.setVisibility(pd.getVisibility()); posts.save(p); addPostToNewsfeed(u, p); if(u.getFriends() != null) { for(User c : u.getFriends()) { addPostToNewsfeed(c, p); } } return p.getId(); } PostService
  • 15. response.add(c.getEntry().getDAO()); } return response; } public String post(String authToken, PostDAO pd) { User u = users.findByAuthtoken(authToken).get(0); Post p = new Post(); p.setContent(pd.getContent()); p.setDate(System.currentTimeMillis()); p.setStyling(pd.getStyling()); p.setTitle(pd.getTitle()); p.setUser(u); p.setVisibility(pd.getVisibility()); posts.save(p); addPostToNewsfeed(u, p); if(u.getFriends() != null) { for(User c : u.getFriends()) { addPostToNewsfeed(c, p); } } return p.getId(); } PostService
  • 16. response.add(c.getEntry().getDAO()); } return response; } public String post(String authToken, PostDAO pd) { User u = users.findByAuthtoken(authToken).get(0); Post p = new Post(); p.setContent(pd.getContent()); p.setDate(System.currentTimeMillis()); p.setStyling(pd.getStyling()); p.setTitle(pd.getTitle()); p.setUser(u); p.setVisibility(pd.getVisibility()); posts.save(p); addPostToNewsfeed(u, p); if(u.getFriends() != null) { for(User c : u.getFriends()) { addPostToNewsfeed(c, p); } } return p.getId(); } PostService
  • 17. for(User c : u.getFriends()) { addPostToNewsfeed(c, p); } } return p.getId(); } private void addPostToNewsfeed(User u, Post p) { Newsfeed n = new Newsfeed(); n.setEntry(p); n.setParent(u); n.setPostDay(System.currentTimeMillis() / DAY); n.setPostTimestamp(System.currentTimeMillis()); news.save(n); } public String comment(String authToken, String postId, CommentDAO cd) throws PermissionException { User u = users.findByAuthtoken(authToken).get(0); Post p = posts.findById(postId).get(); if(!VisibilityConstants.isPublic(p.getVisibility())) { if(!u.getId().equals(p.getUser().getId())) { if(!p.getUser().isFriendById(u.getId())) { throw new PostService
  • 18. for(User c : u.getFriends()) { addPostToNewsfeed(c, p); } } return p.getId(); } private void addPostToNewsfeed(User u, Post p) { Newsfeed n = new Newsfeed(); n.setEntry(p); n.setParent(u); n.setPostDay(System.currentTimeMillis() / DAY); n.setPostTimestamp(System.currentTimeMillis()); news.save(n); } public String comment(String authToken, String postId, CommentDAO cd) throws PermissionException { User u = users.findByAuthtoken(authToken).get(0); Post p = posts.findById(postId).get(); if(!VisibilityConstants.isPublic(p.getVisibility())) { if(!u.getId().equals(p.getUser().getId())) { if(!p.getUser().isFriendById(u.getId())) { throw new PostService
  • 19. news.save(n); } public String comment(String authToken, String postId, CommentDAO cd) throws PermissionException { User u = users.findByAuthtoken(authToken).get(0); Post p = posts.findById(postId).get(); if(!VisibilityConstants.isPublic(p.getVisibility())) { if(!u.getId().equals(p.getUser().getId())) { if(!p.getUser().isFriendById(u.getId())) { throw new PermissionException("This is a private post!"); } } } Comment c = new Comment(); c.setDate(System.currentTimeMillis()); if(cd.getParentComment() != null) { c.setParentComment(comments.findById(cd.getParentComment()). get()); } c.setText(cd.getText()); c.setUser(u); comments.save(c); PostService
  • 20. news.save(n); } public String comment(String authToken, String postId, CommentDAO cd) throws PermissionException { User u = users.findByAuthtoken(authToken).get(0); Post p = posts.findById(postId).get(); if(!VisibilityConstants.isPublic(p.getVisibility())) { if(!u.getId().equals(p.getUser().getId())) { if(!p.getUser().isFriendById(u.getId())) { throw new PermissionException("This is a private post!"); } } } Comment c = new Comment(); c.setDate(System.currentTimeMillis()); if(cd.getParentComment() != null) { c.setParentComment(comments.findById(cd.getParentComment()). get()); } c.setText(cd.getText()); c.setUser(u); comments.save(c); PostService
  • 21. if(!p.getUser().isFriendById(u.getId())) { throw new PermissionException("This is a private post!"); } } } Comment c = new Comment(); c.setDate(System.currentTimeMillis()); if(cd.getParentComment() != null) { c.setParentComment(comments.findById(cd.getParentComment()). get()); } c.setText(cd.getText()); c.setUser(u); comments.save(c); p.getComments().add(c); posts.save(p); notifications.sendNotification(u, new NotificationDAO(null, u.getDAO(), "commented on your post", "uE0B7", 0x00A400, 0, false, postId, cd.getId())); return c.getId(); } PostService
  • 22. if(!p.getUser().isFriendById(u.getId())) { throw new PermissionException("This is a private post!"); } } } Comment c = new Comment(); c.setDate(System.currentTimeMillis()); if(cd.getParentComment() != null) { c.setParentComment(comments.findById(cd.getParentComment()). get()); } c.setText(cd.getText()); c.setUser(u); comments.save(c); p.getComments().add(c); posts.save(p); notifications.sendNotification(u, new NotificationDAO(null, u.getDAO(), "commented on your post", "uE0B7", 0x00A400, 0, false, postId, cd.getId())); return c.getId(); } PostService
  • 23. p.getComments().add(c); posts.save(p); notifications.sendNotification(u, new NotificationDAO(null, u.getDAO(), "commented on your post", "uE0B7", 0x00A400, 0, false, postId, cd.getId())); return c.getId(); } public void like(String authToken, String postId) { User u = users.findByAuthtoken(authToken).get(0); Post p = posts.findById(postId).get(); if(!p.getLikes().contains(u)) { p.getLikes().add(u); posts.save(p); notifications.sendNotification(u, new NotificationDAO(null, u.getDAO(), "liked your post", "uE8DC", 0x587EBE, 0, false, postId, null)); } } } PostService
  • 24. p.getComments().add(c); posts.save(p); notifications.sendNotification(u, new NotificationDAO(null, u.getDAO(), "commented on your post", "uE0B7", 0x00A400, 0, false, postId, cd.getId())); return c.getId(); } public void like(String authToken, String postId) { User u = users.findByAuthtoken(authToken).get(0); Post p = posts.findById(postId).get(); if(!p.getLikes().contains(u)) { p.getLikes().add(u); posts.save(p); notifications.sendNotification(u, new NotificationDAO(null, u.getDAO(), "liked your post", "uE8DC", 0x587EBE, 0, false, postId, null)); } } } PostService
  • 25. p.getComments().add(c); posts.save(p); notifications.sendNotification(u, new NotificationDAO(null, u.getDAO(), "commented on your post", "uE0B7", 0x00A400, 0, false, postId, cd.getId())); return c.getId(); } public void like(String authToken, String postId) { User u = users.findByAuthtoken(authToken).get(0); Post p = posts.findById(postId).get(); if(!p.getLikes().contains(u)) { p.getLikes().add(u); posts.save(p); notifications.sendNotification(u, new NotificationDAO(null, u.getDAO(), "liked your post", "uE8DC", 0x587EBE, 0, false, postId, null)); } } } PostService