save follow
This commit is contained in:
parent
13571f2f5e
commit
daaf2070c7
@ -3,12 +3,15 @@ package io.spring.api;
|
|||||||
import io.spring.api.exception.ResourceNotFoundException;
|
import io.spring.api.exception.ResourceNotFoundException;
|
||||||
import io.spring.application.profile.ProfileData;
|
import io.spring.application.profile.ProfileData;
|
||||||
import io.spring.application.profile.ProfileQueryService;
|
import io.spring.application.profile.ProfileQueryService;
|
||||||
|
import io.spring.core.user.FollowRelation;
|
||||||
import io.spring.core.user.User;
|
import io.spring.core.user.User;
|
||||||
|
import io.spring.core.user.UserRepository;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
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.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
@ -18,10 +21,12 @@ import java.util.HashMap;
|
|||||||
@RequestMapping(path = "profiles/{username}")
|
@RequestMapping(path = "profiles/{username}")
|
||||||
public class ProfileApi {
|
public class ProfileApi {
|
||||||
private ProfileQueryService profileQueryService;
|
private ProfileQueryService profileQueryService;
|
||||||
|
private UserRepository userRepository;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public ProfileApi(ProfileQueryService profileQueryService) {
|
public ProfileApi(ProfileQueryService profileQueryService, UserRepository userRepository) {
|
||||||
this.profileQueryService = profileQueryService;
|
this.profileQueryService = profileQueryService;
|
||||||
|
this.userRepository = userRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping
|
@GetMapping
|
||||||
@ -32,6 +37,16 @@ public class ProfileApi {
|
|||||||
.orElseThrow(ResourceNotFoundException::new);
|
.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) {
|
private ResponseEntity profileResponse(ProfileData profile) {
|
||||||
return ResponseEntity.ok(new HashMap<String, Object>() {{
|
return ResponseEntity.ok(new HashMap<String, Object>() {{
|
||||||
put("profile", profile);
|
put("profile", profile);
|
||||||
|
17
src/main/java/io/spring/core/user/FollowRelation.java
Normal file
17
src/main/java/io/spring/core/user/FollowRelation.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
@ -13,4 +13,8 @@ public interface UserRepository {
|
|||||||
Optional<User> findByUsername(String username);
|
Optional<User> findByUsername(String username);
|
||||||
|
|
||||||
Optional<User> findByEmail(String email);
|
Optional<User> findByEmail(String email);
|
||||||
|
|
||||||
|
void saveRelation(FollowRelation followRelation);
|
||||||
|
|
||||||
|
Optional<FollowRelation> findRelation(String userId, String targetId);
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package io.spring.infrastructure.user;
|
package io.spring.infrastructure.user;
|
||||||
|
|
||||||
|
import io.spring.core.user.FollowRelation;
|
||||||
import io.spring.core.user.User;
|
import io.spring.core.user.User;
|
||||||
import io.spring.core.user.UserRepository;
|
import io.spring.core.user.UserRepository;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@ -39,4 +40,14 @@ public class MyBatisUserRepository implements UserRepository {
|
|||||||
public Optional<User> findByEmail(String email) {
|
public Optional<User> findByEmail(String email) {
|
||||||
return Optional.ofNullable(userMapper.findByEmail(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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package io.spring.infrastructure.user;
|
package io.spring.infrastructure.user;
|
||||||
|
|
||||||
|
import io.spring.core.user.FollowRelation;
|
||||||
import io.spring.core.user.User;
|
import io.spring.core.user.User;
|
||||||
import org.apache.ibatis.annotations.Mapper;
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
import org.apache.ibatis.annotations.Param;
|
import org.apache.ibatis.annotations.Param;
|
||||||
@ -16,4 +17,8 @@ public interface UserMapper {
|
|||||||
User findById(@Param("id") String id);
|
User findById(@Param("id") String id);
|
||||||
|
|
||||||
void update(@Param("user") User user);
|
void update(@Param("user") User user);
|
||||||
|
|
||||||
|
FollowRelation findRelation(@Param("userId") String userId, @Param("targetId") String targetId);
|
||||||
|
|
||||||
|
void saveRelation(@Param("followRelation") FollowRelation followRelation);
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,9 @@
|
|||||||
#{user.image}
|
#{user.image}
|
||||||
)
|
)
|
||||||
</insert>
|
</insert>
|
||||||
|
<insert id="saveRelation">
|
||||||
|
insert into follows(user_id, follow_id) values (#{followRelation.userId}, #{followRelation.targetId})
|
||||||
|
</insert>
|
||||||
<update id="update">
|
<update id="update">
|
||||||
update users
|
update users
|
||||||
<set>
|
<set>
|
||||||
@ -31,6 +34,18 @@
|
|||||||
<select id="findById" resultMap="user">
|
<select id="findById" resultMap="user">
|
||||||
select id, username, email, password, bio, image from users where id = #{id}
|
select id, username, email, password, bio, image from users where id = #{id}
|
||||||
</select>
|
</select>
|
||||||
|
<select id="findRelation" resultMap="follow">
|
||||||
|
SELECT
|
||||||
|
F.user_id followUserId,
|
||||||
|
F.follow_id followTargetId
|
||||||
|
from follows F
|
||||||
|
where F.user_id = #{userId} and F.follow_id = #{targetId}
|
||||||
|
</select>
|
||||||
|
|
||||||
|
<resultMap id="follow" type="io.spring.core.user.FollowRelation">
|
||||||
|
<result column="followUserId" property="userId"/>
|
||||||
|
<result column="followTargetId" property="targetId"/>
|
||||||
|
</resultMap>
|
||||||
|
|
||||||
<resultMap id="user" type="io.spring.core.user.User" >
|
<resultMap id="user" type="io.spring.core.user.User" >
|
||||||
<id column="id" property="id"/>
|
<id column="id" property="id"/>
|
||||||
|
@ -5,6 +5,7 @@ import io.spring.application.profile.ProfileData;
|
|||||||
import io.spring.application.profile.ProfileQueryService;
|
import io.spring.application.profile.ProfileQueryService;
|
||||||
import io.spring.core.article.Article;
|
import io.spring.core.article.Article;
|
||||||
import io.spring.core.user.User;
|
import io.spring.core.user.User;
|
||||||
|
import io.spring.core.user.UserRepository;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
@ -15,6 +16,7 @@ import org.springframework.test.context.junit4.SpringRunner;
|
|||||||
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import static io.restassured.RestAssured.given;
|
||||||
import static org.hamcrest.core.IsEqual.equalTo;
|
import static org.hamcrest.core.IsEqual.equalTo;
|
||||||
import static org.mockito.Matchers.eq;
|
import static org.mockito.Matchers.eq;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
@ -29,13 +31,15 @@ public class ProfileApiTest extends TestWithCurrentUser {
|
|||||||
|
|
||||||
@MockBean
|
@MockBean
|
||||||
private ProfileQueryService profileQueryService;
|
private ProfileQueryService profileQueryService;
|
||||||
|
|
||||||
private ProfileData profileData;
|
private ProfileData profileData;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
RestAssured.port = port;
|
RestAssured.port = port;
|
||||||
userFixture();
|
userFixture();
|
||||||
profileData = new ProfileData("id", "username", "bio", "img", false);
|
anotherUser = new User("username@test.com", "username", "123", "", "");
|
||||||
|
profileData = new ProfileData(anotherUser.getId(), anotherUser.getUsername(), anotherUser.getBio(), anotherUser.getImage(), false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -49,4 +53,18 @@ public class ProfileApiTest extends TestWithCurrentUser {
|
|||||||
.statusCode(200)
|
.statusCode(200)
|
||||||
.body("profile.username", equalTo(profileData.getUsername()));
|
.body("profile.username", equalTo(profileData.getUsername()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void should_follow_user_success() throws Exception {
|
||||||
|
when(userRepository.findByUsername(eq(anotherUser.getUsername()))).thenReturn(Optional.of(anotherUser));
|
||||||
|
when(profileQueryService.findByUsername(eq(profileData.getUsername()), eq(user))).thenReturn(Optional.of(profileData));
|
||||||
|
given()
|
||||||
|
.header("Authorization", "Token " + token)
|
||||||
|
.when()
|
||||||
|
.post("/profiles/{username}/follow", anotherUser.getUsername())
|
||||||
|
.prettyPeek()
|
||||||
|
.then()
|
||||||
|
.statusCode(200);
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,5 +1,6 @@
|
|||||||
package io.spring.infrastructure.user;
|
package io.spring.infrastructure.user;
|
||||||
|
|
||||||
|
import io.spring.core.user.FollowRelation;
|
||||||
import io.spring.core.user.User;
|
import io.spring.core.user.User;
|
||||||
import io.spring.core.user.UserRepository;
|
import io.spring.core.user.UserRepository;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
@ -54,4 +55,14 @@ public class MyBatisUserRepositoryTest {
|
|||||||
assertThat(optional.get().getUsername(), is(newUsername));
|
assertThat(optional.get().getUsername(), is(newUsername));
|
||||||
assertThat(optional.get().getImage(), is(user.getImage()));
|
assertThat(optional.get().getImage(), is(user.getImage()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void should_create_new_user_follow_success() throws Exception {
|
||||||
|
User other = new User("other@example.com", "other", "123", "", "");
|
||||||
|
userRepository.save(other);
|
||||||
|
|
||||||
|
FollowRelation followRelation = new FollowRelation(user.getId(), other.getId());
|
||||||
|
userRepository.saveRelation(followRelation);
|
||||||
|
assertThat(userRepository.findRelation(user.getId(), other.getId()).isPresent(), is(true));
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user