autdant commited on
Commit
dbb6648
·
1 Parent(s): 9cdbf06

cập nhật đêm số người theo dõi

Browse files
connexa-parent/connexa-web/API_DOCS.md CHANGED
@@ -335,7 +335,9 @@ Response `200 OK`:
335
  "gender": "MALE",
336
  "birthday": "20/10/2001",
337
  "avatarUrl": "https://...",
338
- "coverUrl": "https://..."
 
 
339
  }
340
  ```
341
 
@@ -358,6 +360,24 @@ Authorization: Bearer <accessToken>
358
 
359
  Response `200 OK`: giong `GET /api/users/me`.
360
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
361
  Response loi `404 Not Found`:
362
 
363
  ```json
 
335
  "gender": "MALE",
336
  "birthday": "20/10/2001",
337
  "avatarUrl": "https://...",
338
+ "coverUrl": "https://...",
339
+ "followersCount": 12,
340
+ "followingsCount": 8
341
  }
342
  ```
343
 
 
360
 
361
  Response `200 OK`: giong `GET /api/users/me`.
362
 
363
+ Vi du response:
364
+
365
+ ```json
366
+ {
367
+ "id": 2,
368
+ "email": "user2@example.com",
369
+ "name": "Tran Thi B",
370
+ "username": "tranthib",
371
+ "phoneNumber": "0909999999",
372
+ "gender": "FEMALE",
373
+ "birthday": "10/11/2002",
374
+ "avatarUrl": "https://...",
375
+ "coverUrl": "https://...",
376
+ "followersCount": 20,
377
+ "followingsCount": 15
378
+ }
379
+ ```
380
+
381
  Response loi `404 Not Found`:
382
 
383
  ```json
connexa-parent/connexa-web/src/main/java/org/connexa/connexaweb/follow/FollowRepository.java CHANGED
@@ -14,6 +14,10 @@ import java.util.Optional;
14
  public interface FollowRepository extends JpaRepository<Follow, Integer> {
15
  Optional<Follow> findByFollowerIdAndFollowingId(Integer id, Integer targetId);
16
 
 
 
 
 
17
  @Query("""
18
  SELECT uf FROM Follow uf
19
  JOIN FETCH uf.follower f
 
14
  public interface FollowRepository extends JpaRepository<Follow, Integer> {
15
  Optional<Follow> findByFollowerIdAndFollowingId(Integer id, Integer targetId);
16
 
17
+ long countByFollowingIdAndFollowerDeletedFalse(Integer id);
18
+
19
+ long countByFollowerIdAndFollowingDeletedFalse(Integer id);
20
+
21
  @Query("""
22
  SELECT uf FROM Follow uf
23
  JOIN FETCH uf.follower f
connexa-parent/connexa-web/src/main/java/org/connexa/connexaweb/user/UserMapper.java CHANGED
@@ -3,6 +3,7 @@ package org.connexa.connexaweb.user;
3
  import org.connexa.connexacommon.entity.UserProfile;
4
  import org.mapstruct.BeanMapping;
5
  import org.mapstruct.Mapper;
 
6
  import org.mapstruct.MappingTarget;
7
  import org.mapstruct.NullValuePropertyMappingStrategy;
8
  import org.mapstruct.factory.Mappers;
@@ -10,6 +11,9 @@ import org.mapstruct.factory.Mappers;
10
  @Mapper(componentModel = "spring")
11
  public interface UserMapper {
12
  UserMapper INSTANCE = Mappers.getMapper(UserMapper.class);
 
 
 
13
  UserResponseDTO toDto(UserProfile userProfile);
14
 
15
  @BeanMapping(nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE)
 
3
  import org.connexa.connexacommon.entity.UserProfile;
4
  import org.mapstruct.BeanMapping;
5
  import org.mapstruct.Mapper;
6
+ import org.mapstruct.Mapping;
7
  import org.mapstruct.MappingTarget;
8
  import org.mapstruct.NullValuePropertyMappingStrategy;
9
  import org.mapstruct.factory.Mappers;
 
11
  @Mapper(componentModel = "spring")
12
  public interface UserMapper {
13
  UserMapper INSTANCE = Mappers.getMapper(UserMapper.class);
14
+
15
+ @Mapping(target = "followersCount", ignore = true)
16
+ @Mapping(target = "followingsCount", ignore = true)
17
  UserResponseDTO toDto(UserProfile userProfile);
18
 
19
  @BeanMapping(nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE)
connexa-parent/connexa-web/src/main/java/org/connexa/connexaweb/user/UserResponseDTO.java CHANGED
@@ -31,4 +31,8 @@ public class UserResponseDTO {
31
  private String avatarUrl;
32
 
33
  private String coverUrl;
 
 
 
 
34
  }
 
31
  private String avatarUrl;
32
 
33
  private String coverUrl;
34
+
35
+ private long followersCount;
36
+
37
+ private long followingsCount;
38
  }
connexa-parent/connexa-web/src/main/java/org/connexa/connexaweb/user/impl/UserServiceImpl.java CHANGED
@@ -9,6 +9,7 @@ import org.connexa.connexaweb.auth.OtpVerifyRequestDTO;
9
  import org.connexa.connexaweb.auth.ResetPasswordRequestDTO;
10
  import org.connexa.connexaweb.auth.UserRegisterDTO;
11
  import org.connexa.connexaweb.email.EmailService;
 
12
  import org.connexa.connexaweb.imagekit.ImageKitService;
13
  import org.connexa.connexaweb.otp.OtpService;
14
  import org.connexa.connexaweb.user.*;
@@ -50,6 +51,9 @@ public class UserServiceImpl implements UserService {
50
  @Autowired
51
  private ImageKitService imageKitService;
52
 
 
 
 
53
  @Transactional
54
  @Override
55
  public Map<String, String> createUser(UserRegisterDTO dto) {
@@ -131,7 +135,7 @@ public class UserServiceImpl implements UserService {
131
  UserProfile user = userRepository.findByEmail(email).orElseThrow(
132
  () -> new UserNotFoundException("Không tìm thấy người dùng hiện tại")
133
  );
134
- return userMapper.toDto(user);
135
  }
136
 
137
  @Override
@@ -139,7 +143,7 @@ public class UserServiceImpl implements UserService {
139
  UserProfile user = userRepository.findByUsernameAndDeletedFalse(username).orElseThrow(
140
  () -> new UserNotFoundException("Không tìm thấy tài khoản này: " + username)
141
  );
142
- return userMapper.toDto(user);
143
  }
144
 
145
  @Override
@@ -188,4 +192,10 @@ public class UserServiceImpl implements UserService {
188
  Otp otp = otpService.generateOtp(user, OtpType.REGISTER);
189
  emailService.sendOtpVerifyEmail(user.getEmail(), otp.getCode());
190
  }
 
 
 
 
 
 
191
  }
 
9
  import org.connexa.connexaweb.auth.ResetPasswordRequestDTO;
10
  import org.connexa.connexaweb.auth.UserRegisterDTO;
11
  import org.connexa.connexaweb.email.EmailService;
12
+ import org.connexa.connexaweb.follow.FollowRepository;
13
  import org.connexa.connexaweb.imagekit.ImageKitService;
14
  import org.connexa.connexaweb.otp.OtpService;
15
  import org.connexa.connexaweb.user.*;
 
51
  @Autowired
52
  private ImageKitService imageKitService;
53
 
54
+ @Autowired
55
+ private FollowRepository followRepository;
56
+
57
  @Transactional
58
  @Override
59
  public Map<String, String> createUser(UserRegisterDTO dto) {
 
135
  UserProfile user = userRepository.findByEmail(email).orElseThrow(
136
  () -> new UserNotFoundException("Không tìm thấy người dùng hiện tại")
137
  );
138
+ return enrichFollowCounts(userMapper.toDto(user), user.getId());
139
  }
140
 
141
  @Override
 
143
  UserProfile user = userRepository.findByUsernameAndDeletedFalse(username).orElseThrow(
144
  () -> new UserNotFoundException("Không tìm thấy tài khoản này: " + username)
145
  );
146
+ return enrichFollowCounts(userMapper.toDto(user), user.getId());
147
  }
148
 
149
  @Override
 
192
  Otp otp = otpService.generateOtp(user, OtpType.REGISTER);
193
  emailService.sendOtpVerifyEmail(user.getEmail(), otp.getCode());
194
  }
195
+
196
+ private UserResponseDTO enrichFollowCounts(UserResponseDTO dto, Integer userId) {
197
+ dto.setFollowersCount(followRepository.countByFollowingIdAndFollowerDeletedFalse(userId));
198
+ dto.setFollowingsCount(followRepository.countByFollowerIdAndFollowingDeletedFalse(userId));
199
+ return dto;
200
+ }
201
  }