Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trending service and chat corrections #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Spring-Bloogt-Main</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
</buildSpec>
<natures>
</natures>
</projectDescription>
11 changes: 10 additions & 1 deletion SpringBloogtRestApiServer/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,17 @@
<version>0.11.2</version>
<scope>runtime</scope>
</dependency>




<!-- JAVA FAKER -->
<dependency>
<groupId>com.github.javafaker</groupId>
<artifactId>javafaker</artifactId>
<version>0.14</version>
</dependency>

<!-- -->
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public static void main(String[] args) {

@Override
public void run(String... args) throws Exception {
userCreation.createBot();
userCreation.createRandomPostsForBot();



}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import com.blog.project.app.entities.Hashtag;
import com.blog.project.app.entities.Hashtag.PostsOfHashtag;
import com.blog.project.app.entities.Post;
import com.blog.project.app.entities.Post.PostDetails;
import com.blog.project.app.entities.Post.PostDetailsCommentsSortByDateAsc;
import com.blog.project.app.entities.Post.PostDetailsCommentsSortByDateDesc;
import com.blog.project.app.entities.Post.showPosts;
Expand Down Expand Up @@ -97,7 +96,7 @@ public String listPostsById(Model model, @PathVariable(value = "id") int id) {



PostDetails returningJSON;
showPosts returningJSON;
switch (orderType) {
case "asc":
returningJSON = (PostDetailsCommentsSortByDateDesc) postService.findPostByIdAndSortByCreatedDateDesc(id);
Expand Down Expand Up @@ -167,7 +166,7 @@ public String listPostsFromHashtag(Model model, @PathVariable(value = "hashtag")

PostsOfHashtag hash = hashtagService.findPostOfHashtagByName(hashtag);

List<PostDetails> returningPostJSON = hash.getPosts();
List<showPosts> returningPostJSON = hash.getPosts();

model.addAttribute("titulo", "#" + hashtag);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import org.springframework.beans.factory.annotation.Value;

import com.blog.project.app.entities.Post.PostDetails;
import com.blog.project.app.entities.Post.showPosts;

@Entity
public class Hashtag implements Serializable {
Expand Down Expand Up @@ -98,6 +98,6 @@ public interface PostsOfHashtag {
String getName();
@Value("#{target.getPosts().size()}")
int getPostCount();
List<PostDetails> getPosts();
List<showPosts> getPosts();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -255,23 +255,20 @@ public interface showPosts {

}

public interface PostDetails extends showPosts{

}

// It will load PostDetails and then add the sorted comments
public interface PostDetailsCommentsSortByDateAsc extends PostDetails {
public interface PostDetailsCommentsSortByDateAsc extends showPosts {
@Value("#{target.getCommentsSortDateAsc()}")
List<ShowComments> getComments();
}

// It will load PostDetails and then add the sorted comments
public interface PostDetailsCommentsSortByDateDesc extends PostDetails {
public interface PostDetailsCommentsSortByDateDesc extends showPosts {
@Value("#{target.getCommentsSortDateDesc()}")
List<ShowComments> getComments();
}

public interface PostDetailsCommentsSortByThumbsUp extends PostDetails {
public interface PostDetailsCommentsSortByThumbsUp extends showPosts {
// TODO Thumbs up are not implemented !
}
public interface PostBy{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ public class User implements Serializable {



@OneToMany
@OneToMany(cascade = CascadeType.ALL, orphanRemoval=true)
@JoinColumn(name = "user_id", referencedColumnName = "id")
private List<Comments> comments;

@OneToMany
@OneToMany(cascade = CascadeType.ALL, orphanRemoval=true)
@JoinColumn(name = "user_id", referencedColumnName = "id")
private List<Post> posts;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,15 @@ public Date getLastMessageDate() {
return dateMessages.get(0);
}



public Message getLastMessage() {
List<Message> messages = this.getMessages();
Collections.sort(messages);
if(messages == null)
return null;

return messages.get(0);
}
public String toString() {
return "Id of chat : " + this.getId();
}
Expand All @@ -191,7 +199,9 @@ public interface ListChatsOfUser {

@Value("#{target.getMessages().size()}")
int getMessageCount();


@Value("#{target.getLastMessage()}")
MessageData getLastMessage();

int getUnreadMessages();
Map<String,Integer> getUnreadMessagesEachUser();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@

import org.springframework.format.annotation.DateTimeFormat;

import com.blog.project.app.entities.Comments;
import com.blog.project.app.entities.User;
import com.blog.project.app.entities.User.OnlyUsername;

@Entity
public class Message implements Serializable{
public class Message implements Serializable, Comparable<Message>{

private static final long serialVersionUID = 1L;

Expand Down Expand Up @@ -87,6 +88,16 @@ public void setRead(boolean isRead) {
this.isRead = isRead;
}


@Override
public int compareTo(Message m) {
if (this.getCreatedAt() == null || m.getCreatedAt() == null) {
return 0;
}
return getCreatedAt().compareTo(m.getCreatedAt());
}


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////// Projections
///////////
Expand All @@ -95,9 +106,10 @@ public void setRead(boolean isRead) {

public interface MessageData {
int getId();
String message();
String getMessage();
OnlyUsername getAuthor();
Date getCreatedAt();
boolean isRead();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
@Entity
public class CommentReaction extends Reaction {

@ManyToOne(cascade = CascadeType.ALL)
@ManyToOne()
// @JoinColumn(name = "reaction_id", referencedColumnName = "id")
private Comments comment;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public abstract class Reaction {

private boolean reaction;

@ManyToOne(cascade = CascadeType.ALL)
@ManyToOne()
@JoinColumn(name = "user_id", referencedColumnName = "id")
private User reactedBy;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.blog.project.app.entities.Category;
import com.blog.project.app.entities.Comments;
import com.blog.project.app.entities.Post;
import com.blog.project.app.entities.User;
import com.blog.project.app.entities.Comments.ShowAllComments;
import com.blog.project.app.entities.Comments.ShowComments;
import com.blog.project.app.entities.Category.CategoryDetails;
Expand All @@ -20,6 +21,7 @@ public interface IComments extends BaseRepository <Comments, Long> {
List<ShowComments> findByPost(Post post);
List<ShowAllComments> findAllProjectedBy();

List<Comments> findByCreatedBy(User user);
public Comments findCommentById(int id);

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,16 @@

import java.util.List;

import org.springframework.data.jpa.repository.Query;

import com.blog.project.app.entities.Category;
import com.blog.project.app.entities.Post;
import com.blog.project.app.entities.Post.PostDetails;
import com.blog.project.app.entities.Post.PostDetailsCommentsSortByDateAsc;
import com.blog.project.app.entities.Post.PostDetailsCommentsSortByDateDesc;
import com.blog.project.app.entities.Post.showPosts;
import com.blog.project.app.entities.User;

public interface IPost extends BaseRepository<Post, Long> {

PostDetails findById(int id);
showPosts findById(int id);
Post findPostById(int id);

PostDetailsCommentsSortByDateAsc findByIdOrderByCreatedAtAsc(int id);
Expand All @@ -36,6 +33,7 @@ public interface IPost extends BaseRepository<Post, Long> {



List<Post> findByCreatedBy(User user);
List<showPosts> findByCreatedByIn(List<User> listUser);
List<showPosts> findByCreatedByInAndCategoryOrderByCreatedAtDesc(List<User> listUser, Category category);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.blog.project.app.models.dao;

import java.util.List;

import org.springframework.data.jpa.repository.Query;

import com.blog.project.app.entities.Post.showPosts;
import com.blog.project.app.entities.Role;

public interface ITrending extends BaseRepository <Role, Long> {

@Query(value = "SELECT comment_id FROM reaction\r\n" +
"where reaction.dtype = 'CommentReaction' \r\n" +
"and reaction.reaction = true\r\n" +
"and reaction.created_at >= DATE_SUB(NOW(),INTERVAL 1 HOUR) \r\n" +
"group by reaction.comment_id\r\n" +
"order by count(*) desc\r\n" +
"limit ?1,?2", nativeQuery = true)
List<Integer> getMoreLikedCommentsLastHour(int startAt, int limit);

@Query(value = " SELECT post_id FROM reaction, post, category where reaction.dtype = 'PostReaction' and reaction.reaction = true and reaction.created_at >= DATE_SUB(NOW(),INTERVAL 1 HOUR) and post.id = reaction.post_id and post.category_id = category.id and category.name = ?1 group by reaction.post_id order by count(*) desc limit ?2, ?3", nativeQuery = true)
List<Integer> getMoreLikedPostsLastHourOfCategory(String categoryName, int startAt, int limit);

@Query(value = "SELECT post.id FROM post,category where post.category_id = category.id and category.name not like ?1 order by post.created_at desc limit ?2, ?3", nativeQuery = true)
List<Integer> getLastPostsExceptCategory(String categoryName, int startAt, int limit);



}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@

public interface IUser extends BaseRepository <User, Long> {

@Query("select count(*) from User")
int countAll();




@Query(value = "SELECT user.* FROM post, user where post.user_id = user.id and user.username like 'BOT-%' group by user.username", nativeQuery = true)
List<User> getAllBotsThatHavePosts();

User findUserForLoginByUsername(String username);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public interface IChatMessageService {
public Chat findChatById(int chatId);
public void newMessageToChat(User fromUser, String message, int chatId);

public int getUnreadMessages();
public int getUnreadMessagesLoggedSession();
public int getUnreadMessages(User user);

public void allMessagesToRead(Chat chat);
public void changeMessageToRead(Message message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import com.blog.project.app.entities.Category;
import com.blog.project.app.entities.Post;
import com.blog.project.app.entities.Post.PostDetails;
import com.blog.project.app.entities.Post.showPosts;
import com.blog.project.app.entities.User;

Expand All @@ -16,7 +15,7 @@ public interface IPostService {

/// Custom

public PostDetails findPostById(int id);
public showPosts findPostById(int id);
public List<showPosts> findAllPostsProjection();
public List<showPosts> findAllPostsProjectionByCategory(Category category);
public List<showPosts> findAllPostsProjectionByCategoryNot(Category category);
Expand All @@ -27,8 +26,8 @@ public interface IPostService {
public Post findReturnPostById(int id);
void deletePostById(int id);

PostDetails findPostByIdAndSortByCreatedDateDesc(int id);
PostDetails findPostByIdAndSortByCreatedDateAsc(int id);
showPosts findPostByIdAndSortByCreatedDateDesc(int id);
showPosts findPostByIdAndSortByCreatedDateAsc(int id);

public void addVisit(int id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.blog.project.app.models.service;

import java.util.List;

import com.blog.project.app.entities.Post.showPosts;

public interface ITrendingService {
List<showPosts> getMoreVotedPostsLastHour(String category, int start, int end);
List<showPosts> getLastPostsExceptCategory(String category, int start, int end);
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,22 @@ public void newMessageToChat(User fromUser, String message, int chatId) {
}

@Override
public int getUnreadMessages() {
public int getUnreadMessagesLoggedSession() {
try {
User fromUser = userService.getLoggedUser();
return chatDao.getUnreadMessages(fromUser.getId());
return this.getUnreadMessages(fromUser);
} catch (Exception e) {
return 0;
}
}
@Override
public int getUnreadMessages(User user) {
try {
return chatDao.getUnreadMessages(user.getId());
} catch (Exception e) {
return 0;
}
}

@Override
public void changeMessageToRead(Message message) {
message.setRead(true);
Expand Down
Loading