Skip to content

Commit a96c093

Browse files
committed
production+ Query handling and caching+ new docker
1 parent 9866a45 commit a96c093

File tree

4 files changed

+36
-30
lines changed

4 files changed

+36
-30
lines changed

Dockerfile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
FROM maven:3.8.4-openjdk-17 as build
22
WORKDIR /SocialNetwork
3-
COPY . .
3+
COPY pom.xml ./
4+
RUN mvn dependency:go-offline -B
5+
COPY src ./src
46
RUN mvn clean package -DskipTests
57

68

79
FROM openjdk:17-jdk-alpine
810
VOLUME /tmp
911
COPY --from=build /SocialNetwork/target/SocialNetwork.jar SocialNetwork.jar
10-
ENTRYPOINT ["java", "-jar", "/SocialNetwork.jar"]
12+
ENTRYPOINT ["java", "-jar", "/SocialNetwork.jar"]
Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,33 @@
11
package org.server.socialnetworkserver.dtos;
22

3-
import lombok.Getter;
3+
44
import lombok.Setter;
55

6-
@Getter
6+
77
@Setter
88
public class ProfileStatsDto {
9-
private int followersCount;
10-
private int followingCount;
11-
private boolean isFollowing;
12-
13-
public ProfileStatsDto(int followersCount, int followingCount, boolean isFollowing) {
14-
this.followersCount = followersCount;
15-
this.followingCount = followingCount;
16-
this.isFollowing = isFollowing;
9+
private Long followersCount;
10+
private Long followingCount;
11+
private Boolean isFollowing;
12+
13+
public ProfileStatsDto(Long followersCount, Long followingCount, Boolean isFollowing) {
14+
this.followersCount = (followersCount != null) ? followersCount : 0;
15+
this.followingCount = (followingCount != null) ? followingCount : 0;
16+
this.isFollowing = (isFollowing != null) ? isFollowing : false;
17+
}
18+
19+
public ProfileStatsDto() {}
20+
21+
public Long getFollowersCount() {
22+
return followersCount;
1723
}
1824

19-
public ProfileStatsDto(){
25+
public Long getFollowingCount() {
26+
return followingCount;
27+
}
2028

29+
public Boolean isFollowing() {
30+
return isFollowing;
2131
}
32+
2233
}

src/main/java/org/server/socialnetworkserver/repositoris/FollowRepository.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,17 @@ boolean isFollowing(
6565

6666
@Query("""
6767
SELECT new org.server.socialnetworkserver.dtos.ProfileStatsDto(
68-
(SELECT COUNT(f) FROM Follow f WHERE f.following.username = :username),
69-
(SELECT COUNT(f) FROM Follow f WHERE f.follower.username = :username),
68+
(SELECT COUNT(f) FROM Follow f WHERE f.following.id = u.id),
69+
(SELECT COUNT(f) FROM Follow f WHERE f.follower.id = u.id),
7070
CASE
71-
WHEN EXISTS (SELECT 1 FROM Follow f WHERE f.follower.username = :currentUsername AND f.following.username = :username)
71+
WHEN EXISTS (SELECT 1 FROM Follow f WHERE f.follower.id = :currentUserId AND f.following.id = u.id)
7272
THEN true ELSE false
7373
END
7474
)
7575
FROM User u
7676
WHERE u.username = :username
7777
""")
78-
Optional<ProfileStatsDto> getProfileStats(@Param("username") String username, @Param("currentUsername") String currentUsername);
78+
Optional<ProfileStatsDto> getProfileStats(@Param("username") String username, @Param("currentUserId") Long currentUserId);
7979

8080

8181
@Modifying

src/main/java/org/server/socialnetworkserver/services/FollowService.java

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public ProfileResponse getAllDetailsOfProfileSearch(@PathVariable String current
100100
return new ProfileResponse(true,"Profile response success.",profileDto);
101101
}
102102
*/
103-
@Cacheable(value = "profileCache", key = "#username")
103+
@Cacheable(value = "profileCache", key = "#currentUsername + '_' + #username")
104104
public ProfileResponse getAllDetailsOfProfileSearch(String currentUsername, String username){
105105
User currentUser = userRepository.findByUsername(currentUsername);
106106
User searchUser = userRepository.findByUsername(username);
@@ -109,16 +109,9 @@ public ProfileResponse getAllDetailsOfProfileSearch(String currentUsername, Stri
109109
return new ProfileResponse(false, "No find user.", null);
110110
}
111111

112-
Optional<ProfileStatsDto> profileStatsOptional = followRepository.getProfileStats(username, currentUsername);
112+
ProfileStatsDto profileStats = followRepository.getProfileStats(username, currentUser.getId())
113+
.orElse(new ProfileStatsDto(0L, 0L, false));
113114

114-
if (profileStatsOptional.isEmpty()) {
115-
return new ProfileResponse(false, "Profile stats not found.", null);
116-
}
117-
118-
ProfileStatsDto profileStats = profileStatsOptional.get();
119-
int followers = profileStats.getFollowersCount();
120-
int following = profileStats.getFollowingCount();
121-
boolean isFollowing = profileStats.isFollowing();
122115

123116
List<Object[]> results = postRepository.findProfilePosts(username, currentUser.getId());
124117
List<PostDto> postDtos = results.stream()
@@ -138,9 +131,9 @@ public ProfileResponse getAllDetailsOfProfileSearch(String currentUsername, Stri
138131
ProfileDto profileDto = new ProfileDto(
139132
searchUser.getUsername(),
140133
searchUser.getProfilePicture(),
141-
followers,
142-
following,
143-
isFollowing,
134+
profileStats.getFollowersCount().intValue(),
135+
profileStats.getFollowingCount().intValue(),
136+
profileStats.isFollowing(),
144137
postDtos
145138
);
146139

0 commit comments

Comments
 (0)