remove follow
This commit is contained in:
@@ -9,6 +9,7 @@ import io.spring.core.user.UserRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
@@ -16,6 +17,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Optional;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(path = "profiles/{username}")
|
||||
@@ -47,6 +49,22 @@ public class ProfileApi {
|
||||
}).orElseThrow(ResourceNotFoundException::new);
|
||||
}
|
||||
|
||||
@DeleteMapping(path = "follow")
|
||||
public ResponseEntity unfollow(@PathVariable("username") String username,
|
||||
@AuthenticationPrincipal User user) {
|
||||
Optional<User> userOptional = userRepository.findByUsername(username);
|
||||
if (userOptional.isPresent()) {
|
||||
User target = userOptional.get();
|
||||
return userRepository.findRelation(user.getId(), target.getId())
|
||||
.map(relation -> {
|
||||
userRepository.removeRelation(relation);
|
||||
return profileResponse(profileQueryService.findByUsername(username, user).get());
|
||||
}).orElseThrow(ResourceNotFoundException::new);
|
||||
} else {
|
||||
throw new ResourceNotFoundException();
|
||||
}
|
||||
}
|
||||
|
||||
private ResponseEntity profileResponse(ProfileData profile) {
|
||||
return ResponseEntity.ok(new HashMap<String, Object>() {{
|
||||
put("profile", profile);
|
||||
|
||||
@@ -17,4 +17,6 @@ public interface UserRepository {
|
||||
void saveRelation(FollowRelation followRelation);
|
||||
|
||||
Optional<FollowRelation> findRelation(String userId, String targetId);
|
||||
|
||||
void removeRelation(FollowRelation followRelation);
|
||||
}
|
||||
|
||||
@@ -43,11 +43,18 @@ public class MyBatisUserRepository implements UserRepository {
|
||||
|
||||
@Override
|
||||
public void saveRelation(FollowRelation followRelation) {
|
||||
userMapper.saveRelation(followRelation);
|
||||
if (!findRelation(followRelation.getUserId(), followRelation.getTargetId()).isPresent()) {
|
||||
userMapper.saveRelation(followRelation);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<FollowRelation> findRelation(String userId, String targetId) {
|
||||
return Optional.ofNullable(userMapper.findRelation(userId, targetId));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeRelation(FollowRelation followRelation) {
|
||||
userMapper.deleteRelation(followRelation);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,4 +21,6 @@ public interface UserMapper {
|
||||
FollowRelation findRelation(@Param("userId") String userId, @Param("targetId") String targetId);
|
||||
|
||||
void saveRelation(@Param("followRelation") FollowRelation followRelation);
|
||||
|
||||
void deleteRelation(@Param("followRelation") FollowRelation followRelation);
|
||||
}
|
||||
|
||||
@@ -25,6 +25,9 @@
|
||||
</set>
|
||||
where id = #{user.id}
|
||||
</update>
|
||||
<delete id="deleteRelation">
|
||||
delete from follows where user_id = #{followRelation.userId} and follow_id = #{followRelation.targetId}
|
||||
</delete>
|
||||
<select id="findByUsername" resultMap="user">
|
||||
select * from users where username = #{username}
|
||||
</select>
|
||||
|
||||
Reference in New Issue
Block a user