save follow

This commit is contained in:
aisensiy
2017-08-16 16:25:08 +08:00
parent 13571f2f5e
commit daaf2070c7
8 changed files with 98 additions and 2 deletions

View File

@@ -3,12 +3,15 @@ package io.spring.api;
import io.spring.api.exception.ResourceNotFoundException;
import io.spring.application.profile.ProfileData;
import io.spring.application.profile.ProfileQueryService;
import io.spring.core.user.FollowRelation;
import io.spring.core.user.User;
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.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@@ -18,10 +21,12 @@ import java.util.HashMap;
@RequestMapping(path = "profiles/{username}")
public class ProfileApi {
private ProfileQueryService profileQueryService;
private UserRepository userRepository;
@Autowired
public ProfileApi(ProfileQueryService profileQueryService) {
public ProfileApi(ProfileQueryService profileQueryService, UserRepository userRepository) {
this.profileQueryService = profileQueryService;
this.userRepository = userRepository;
}
@GetMapping
@@ -32,6 +37,16 @@ public class ProfileApi {
.orElseThrow(ResourceNotFoundException::new);
}
@PostMapping(path = "follow")
public ResponseEntity follow(@PathVariable("username") String username,
@AuthenticationPrincipal User user) {
return userRepository.findByUsername(username).map(target -> {
FollowRelation followRelation = new FollowRelation(user.getId(), target.getId());
userRepository.saveRelation(followRelation);
return profileResponse(profileQueryService.findByUsername(username, user).get());
}).orElseThrow(ResourceNotFoundException::new);
}
private ResponseEntity profileResponse(ProfileData profile) {
return ResponseEntity.ok(new HashMap<String, Object>() {{
put("profile", profile);

View File

@@ -0,0 +1,17 @@
package io.spring.core.user;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
public class FollowRelation {
private String userId;
private String targetId;
public FollowRelation(String userId, String targetId) {
this.userId = userId;
this.targetId = targetId;
}
}

View File

@@ -13,4 +13,8 @@ public interface UserRepository {
Optional<User> findByUsername(String username);
Optional<User> findByEmail(String email);
void saveRelation(FollowRelation followRelation);
Optional<FollowRelation> findRelation(String userId, String targetId);
}

View File

@@ -1,5 +1,6 @@
package io.spring.infrastructure.user;
import io.spring.core.user.FollowRelation;
import io.spring.core.user.User;
import io.spring.core.user.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
@@ -39,4 +40,14 @@ public class MyBatisUserRepository implements UserRepository {
public Optional<User> findByEmail(String email) {
return Optional.ofNullable(userMapper.findByEmail(email));
}
@Override
public void saveRelation(FollowRelation followRelation) {
userMapper.saveRelation(followRelation);
}
@Override
public Optional<FollowRelation> findRelation(String userId, String targetId) {
return Optional.ofNullable(userMapper.findRelation(userId, targetId));
}
}

View File

@@ -1,5 +1,6 @@
package io.spring.infrastructure.user;
import io.spring.core.user.FollowRelation;
import io.spring.core.user.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@@ -16,4 +17,8 @@ public interface UserMapper {
User findById(@Param("id") String id);
void update(@Param("user") User user);
FollowRelation findRelation(@Param("userId") String userId, @Param("targetId") String targetId);
void saveRelation(@Param("followRelation") FollowRelation followRelation);
}