Spaces:
Runtime error
Runtime error
triển khai chặn người dùng
Browse files- connexa-common/src/main/java/org/connexa/connexacommon/entity/BlockAction.java +5 -0
- connexa-common/src/main/java/org/connexa/connexacommon/entity/UserBlock.java +39 -0
- connexa-common/src/main/java/org/connexa/connexacommon/entity/UserBlockHistory.java +40 -0
- connexa-parent/connexa-web/src/main/java/org/connexa/connexaweb/block/UserBlockHistoryRepository.java +9 -0
- connexa-parent/connexa-web/src/main/java/org/connexa/connexaweb/block/UserBlockNotFoundException.java +7 -0
- connexa-parent/connexa-web/src/main/java/org/connexa/connexaweb/block/UserBlockRepository.java +14 -0
- connexa-parent/connexa-web/src/main/java/org/connexa/connexaweb/block/UserBlockService.java +7 -0
- connexa-parent/connexa-web/src/main/java/org/connexa/connexaweb/block/impl/UserBlockServiceImpl.java +84 -0
- connexa-parent/connexa-web/src/main/java/org/connexa/connexaweb/user/UserController.java +17 -0
connexa-common/src/main/java/org/connexa/connexacommon/entity/BlockAction.java
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package org.connexa.connexacommon.entity;
|
| 2 |
+
|
| 3 |
+
public enum BlockAction {
|
| 4 |
+
BLOCK, UNBLOCK
|
| 5 |
+
}
|
connexa-common/src/main/java/org/connexa/connexacommon/entity/UserBlock.java
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package org.connexa.connexacommon.entity;
|
| 2 |
+
|
| 3 |
+
import jakarta.persistence.*;
|
| 4 |
+
import lombok.*;
|
| 5 |
+
|
| 6 |
+
import java.util.Date;
|
| 7 |
+
|
| 8 |
+
@Entity
|
| 9 |
+
@Table(
|
| 10 |
+
name = "user_blocks",
|
| 11 |
+
uniqueConstraints = {
|
| 12 |
+
@UniqueConstraint(name = "uq_user_block", columnNames = {"blocker_id", "blocked_id"})
|
| 13 |
+
},
|
| 14 |
+
indexes = {
|
| 15 |
+
@Index(name = "idx_blocker_id", columnList = "blocker_id"),
|
| 16 |
+
@Index(name = "idx_blocked_id", columnList = "blocked_id")
|
| 17 |
+
}
|
| 18 |
+
)
|
| 19 |
+
@Getter
|
| 20 |
+
@Setter
|
| 21 |
+
@NoArgsConstructor
|
| 22 |
+
@AllArgsConstructor
|
| 23 |
+
@Builder
|
| 24 |
+
public class UserBlock {
|
| 25 |
+
@Id
|
| 26 |
+
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
| 27 |
+
private Integer id;
|
| 28 |
+
|
| 29 |
+
@ManyToOne(fetch = FetchType.LAZY, optional = false)
|
| 30 |
+
@JoinColumn(name = "blocker_id", nullable = false)
|
| 31 |
+
private UserProfile blocker;
|
| 32 |
+
|
| 33 |
+
@ManyToOne(fetch = FetchType.LAZY, optional = false)
|
| 34 |
+
@JoinColumn(name = "blocked_id", nullable = false)
|
| 35 |
+
private UserProfile blocked;
|
| 36 |
+
|
| 37 |
+
@Column(name = "created_at", nullable = false, updatable = false)
|
| 38 |
+
private Date createdAt;
|
| 39 |
+
}
|
connexa-common/src/main/java/org/connexa/connexacommon/entity/UserBlockHistory.java
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package org.connexa.connexacommon.entity;
|
| 2 |
+
|
| 3 |
+
import jakarta.persistence.*;
|
| 4 |
+
import lombok.*;
|
| 5 |
+
|
| 6 |
+
import java.util.Date;
|
| 7 |
+
|
| 8 |
+
@Entity
|
| 9 |
+
@Table(name = "user_block_histories",
|
| 10 |
+
indexes = {
|
| 11 |
+
@Index(name = "idx_block_history_blocker", columnList = "blocker_id"),
|
| 12 |
+
@Index(name = "idx_block_history_blocked", columnList = "blocked_id")
|
| 13 |
+
})
|
| 14 |
+
@Getter
|
| 15 |
+
@Setter
|
| 16 |
+
@NoArgsConstructor
|
| 17 |
+
@AllArgsConstructor
|
| 18 |
+
@Builder
|
| 19 |
+
public class UserBlockHistory {
|
| 20 |
+
@Id
|
| 21 |
+
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
| 22 |
+
private Integer id;
|
| 23 |
+
|
| 24 |
+
@ManyToOne(fetch = FetchType.LAZY, optional = false)
|
| 25 |
+
@JoinColumn(name = "blocker_id", nullable = false,
|
| 26 |
+
foreignKey = @ForeignKey(name = "fk_block_history_blocker"))
|
| 27 |
+
private UserProfile blocker;
|
| 28 |
+
|
| 29 |
+
@ManyToOne(fetch = FetchType.LAZY, optional = false)
|
| 30 |
+
@JoinColumn(name = "blocked_id", nullable = false,
|
| 31 |
+
foreignKey = @ForeignKey(name = "fk_block_history_blocked"))
|
| 32 |
+
private UserProfile blocked;
|
| 33 |
+
|
| 34 |
+
@Enumerated(EnumType.STRING)
|
| 35 |
+
@Column(name = "action", nullable = false, length = 20)
|
| 36 |
+
private BlockAction action;
|
| 37 |
+
|
| 38 |
+
@Column(name = "created_at", nullable = false, updatable = false)
|
| 39 |
+
private Date createdAt;
|
| 40 |
+
}
|
connexa-parent/connexa-web/src/main/java/org/connexa/connexaweb/block/UserBlockHistoryRepository.java
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package org.connexa.connexaweb.block;
|
| 2 |
+
|
| 3 |
+
import org.connexa.connexacommon.entity.UserBlockHistory;
|
| 4 |
+
import org.springframework.data.jpa.repository.JpaRepository;
|
| 5 |
+
import org.springframework.stereotype.Repository;
|
| 6 |
+
|
| 7 |
+
@Repository
|
| 8 |
+
public interface UserBlockHistoryRepository extends JpaRepository<UserBlockHistory, Integer> {
|
| 9 |
+
}
|
connexa-parent/connexa-web/src/main/java/org/connexa/connexaweb/block/UserBlockNotFoundException.java
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package org.connexa.connexaweb.block;
|
| 2 |
+
|
| 3 |
+
public class UserBlockNotFoundException extends RuntimeException {
|
| 4 |
+
public UserBlockNotFoundException(String s) {
|
| 5 |
+
super(s);
|
| 6 |
+
}
|
| 7 |
+
}
|
connexa-parent/connexa-web/src/main/java/org/connexa/connexaweb/block/UserBlockRepository.java
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package org.connexa.connexaweb.block;
|
| 2 |
+
|
| 3 |
+
import org.connexa.connexacommon.entity.UserBlock;
|
| 4 |
+
import org.springframework.data.jpa.repository.JpaRepository;
|
| 5 |
+
import org.springframework.data.jpa.repository.Modifying;
|
| 6 |
+
import org.springframework.stereotype.Repository;
|
| 7 |
+
|
| 8 |
+
@Repository
|
| 9 |
+
public interface UserBlockRepository extends JpaRepository<UserBlock, Integer> {
|
| 10 |
+
boolean existsByBlockerIdAndBlockedId(Integer userBlockerId, Integer userBlockedId);
|
| 11 |
+
|
| 12 |
+
@Modifying
|
| 13 |
+
void deleteByBlockerIdAndBlockedId(Integer blockerId, Integer blockedId);
|
| 14 |
+
}
|
connexa-parent/connexa-web/src/main/java/org/connexa/connexaweb/block/UserBlockService.java
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package org.connexa.connexaweb.block;
|
| 2 |
+
|
| 3 |
+
public interface UserBlockService {
|
| 4 |
+
void blockUser(String email,Integer userBlockedId);
|
| 5 |
+
|
| 6 |
+
void unblockUser(String email, Integer userUnblockedId);
|
| 7 |
+
}
|
connexa-parent/connexa-web/src/main/java/org/connexa/connexaweb/block/impl/UserBlockServiceImpl.java
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package org.connexa.connexaweb.block.impl;
|
| 2 |
+
|
| 3 |
+
import jakarta.transaction.Transactional;
|
| 4 |
+
import org.connexa.connexacommon.entity.BlockAction;
|
| 5 |
+
import org.connexa.connexacommon.entity.UserBlock;
|
| 6 |
+
import org.connexa.connexacommon.entity.UserBlockHistory;
|
| 7 |
+
import org.connexa.connexacommon.entity.UserProfile;
|
| 8 |
+
import org.connexa.connexaweb.block.UserBlockHistoryRepository;
|
| 9 |
+
import org.connexa.connexaweb.block.UserBlockNotFoundException;
|
| 10 |
+
import org.connexa.connexaweb.block.UserBlockRepository;
|
| 11 |
+
import org.connexa.connexaweb.block.UserBlockService;
|
| 12 |
+
import org.connexa.connexaweb.user.UserNotFoundException;
|
| 13 |
+
import org.connexa.connexaweb.user.UserRepository;
|
| 14 |
+
import org.springframework.beans.factory.annotation.Autowired;
|
| 15 |
+
import org.springframework.stereotype.Service;
|
| 16 |
+
|
| 17 |
+
import java.util.Date;
|
| 18 |
+
|
| 19 |
+
@Service
|
| 20 |
+
public class UserBlockServiceImpl implements UserBlockService {
|
| 21 |
+
@Autowired
|
| 22 |
+
private UserRepository userRepository;
|
| 23 |
+
@Autowired
|
| 24 |
+
private UserBlockRepository userBlockRepository;
|
| 25 |
+
|
| 26 |
+
@Autowired
|
| 27 |
+
private UserBlockHistoryRepository userBlockHistoryRepository;
|
| 28 |
+
|
| 29 |
+
@Transactional
|
| 30 |
+
@Override
|
| 31 |
+
public void blockUser(String email, Integer userBlockedId) {
|
| 32 |
+
UserProfile userBlocker = userRepository.findByEmail(email).orElseThrow(
|
| 33 |
+
() -> new UserNotFoundException("Không tìm thấy người dùng hiện tại")
|
| 34 |
+
);
|
| 35 |
+
|
| 36 |
+
UserProfile userBlocked = userRepository.findById(userBlockedId).orElseThrow(
|
| 37 |
+
() -> new UserNotFoundException("Không tìm thấy người dùng này: " + userBlockedId)
|
| 38 |
+
);
|
| 39 |
+
if(userBlocker.getId().equals(userBlockedId)){
|
| 40 |
+
throw new IllegalArgumentException("Không thể thực hiện hành động này");
|
| 41 |
+
}
|
| 42 |
+
if(userBlockRepository.existsByBlockerIdAndBlockedId(userBlocker.getId(), userBlocked.getId())){
|
| 43 |
+
return;
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
UserBlock userBlock = new UserBlock();
|
| 47 |
+
userBlock.setBlocker(userBlocker);
|
| 48 |
+
userBlock.setBlocked(userBlocked);
|
| 49 |
+
Date timeBlock = new Date();
|
| 50 |
+
userBlock.setCreatedAt(timeBlock);
|
| 51 |
+
|
| 52 |
+
userBlockRepository.save(userBlock);
|
| 53 |
+
|
| 54 |
+
createBlockHistory(userBlock.getBlocker(), userBlock.getBlocked(), BlockAction.BLOCK, timeBlock);
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
@Transactional
|
| 58 |
+
@Override
|
| 59 |
+
public void unblockUser(String email, Integer userUnblockedId) {
|
| 60 |
+
UserProfile userUnblocker = userRepository.findByEmail(email).orElseThrow(
|
| 61 |
+
() -> new UserNotFoundException("Không tìm thấy người dùng hiện tại")
|
| 62 |
+
);
|
| 63 |
+
|
| 64 |
+
UserProfile userUnlocked = userRepository.findById(userUnblockedId).orElseThrow(
|
| 65 |
+
() -> new UserNotFoundException("Không tìm thấy người dùng này: " + userUnblockedId)
|
| 66 |
+
);
|
| 67 |
+
|
| 68 |
+
if(!userBlockRepository.existsByBlockerIdAndBlockedId(userUnblocker.getId(), userUnlocked.getId())){
|
| 69 |
+
throw new UserBlockNotFoundException("Không tìm thấy lịch sử chặn người dùng này");
|
| 70 |
+
}
|
| 71 |
+
userBlockRepository.deleteByBlockerIdAndBlockedId(userUnblocker.getId(), userUnlocked.getId());
|
| 72 |
+
|
| 73 |
+
createBlockHistory(userUnblocker, userUnlocked, BlockAction.UNBLOCK, new Date());
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
private void createBlockHistory(UserProfile actor, UserProfile target, BlockAction action, Date timeBlock) {
|
| 77 |
+
UserBlockHistory history = new UserBlockHistory();
|
| 78 |
+
history.setBlocker(actor);
|
| 79 |
+
history.setBlocked(target);
|
| 80 |
+
history.setAction( action);
|
| 81 |
+
history.setCreatedAt(timeBlock);
|
| 82 |
+
userBlockHistoryRepository.save(history);
|
| 83 |
+
}
|
| 84 |
+
}
|
connexa-parent/connexa-web/src/main/java/org/connexa/connexaweb/user/UserController.java
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
package org.connexa.connexaweb.user;
|
| 2 |
|
| 3 |
import jakarta.validation.Valid;
|
|
|
|
| 4 |
import org.springframework.beans.factory.annotation.Autowired;
|
| 5 |
import org.springframework.http.HttpStatus;
|
| 6 |
import org.springframework.http.ResponseEntity;
|
|
@@ -17,6 +18,8 @@ import org.springframework.web.multipart.MultipartFile;
|
|
| 17 |
public class UserController {
|
| 18 |
@Autowired
|
| 19 |
private UserService userService;
|
|
|
|
|
|
|
| 20 |
|
| 21 |
@GetMapping("/me")
|
| 22 |
public ResponseEntity<?> getProfile(){
|
|
@@ -56,4 +59,18 @@ public class UserController {
|
|
| 56 |
|
| 57 |
return new ResponseEntity<>(HttpStatus.OK);
|
| 58 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
}
|
|
|
|
| 1 |
package org.connexa.connexaweb.user;
|
| 2 |
|
| 3 |
import jakarta.validation.Valid;
|
| 4 |
+
import org.connexa.connexaweb.block.UserBlockService;
|
| 5 |
import org.springframework.beans.factory.annotation.Autowired;
|
| 6 |
import org.springframework.http.HttpStatus;
|
| 7 |
import org.springframework.http.ResponseEntity;
|
|
|
|
| 18 |
public class UserController {
|
| 19 |
@Autowired
|
| 20 |
private UserService userService;
|
| 21 |
+
@Autowired
|
| 22 |
+
private UserBlockService userBlockService;
|
| 23 |
|
| 24 |
@GetMapping("/me")
|
| 25 |
public ResponseEntity<?> getProfile(){
|
|
|
|
| 59 |
|
| 60 |
return new ResponseEntity<>(HttpStatus.OK);
|
| 61 |
}
|
| 62 |
+
|
| 63 |
+
@PostMapping("/block/{id}")
|
| 64 |
+
public ResponseEntity<?> blockUser(@PathVariable("id") Integer userBlockedId){
|
| 65 |
+
String email = SecurityContextHolder.getContext().getAuthentication().getName();
|
| 66 |
+
userBlockService.blockUser(email, userBlockedId);
|
| 67 |
+
return new ResponseEntity<>(HttpStatus.OK);
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
@PostMapping("/unblock/{id}")
|
| 71 |
+
public ResponseEntity<?> unblockUser(@PathVariable("id") Integer userUnblockedId){
|
| 72 |
+
String email = SecurityContextHolder.getContext().getAuthentication().getName();
|
| 73 |
+
userBlockService.unblockUser(email, userUnblockedId);
|
| 74 |
+
return new ResponseEntity<>(HttpStatus.OK);
|
| 75 |
+
}
|
| 76 |
}
|