remove follow

This commit is contained in:
aisensiy
2017-08-16 17:13:24 +08:00
parent daaf2070c7
commit 0c12253ef0
7 changed files with 67 additions and 2 deletions

View File

@@ -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);

View File

@@ -17,4 +17,6 @@ public interface UserRepository {
void saveRelation(FollowRelation followRelation);
Optional<FollowRelation> findRelation(String userId, String targetId);
void removeRelation(FollowRelation followRelation);
}

View File

@@ -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);
}
}

View File

@@ -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);
}