get profile

This commit is contained in:
aisensiy
2017-08-16 15:58:51 +08:00
parent 7c685942ea
commit 13571f2f5e
16 changed files with 191 additions and 14 deletions

View File

@@ -0,0 +1,40 @@
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.User;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
@RestController
@RequestMapping(path = "profiles/{username}")
public class ProfileApi {
private ProfileQueryService profileQueryService;
@Autowired
public ProfileApi(ProfileQueryService profileQueryService) {
this.profileQueryService = profileQueryService;
}
@GetMapping
public ResponseEntity getProfile(@PathVariable("username") String username,
@AuthenticationPrincipal User user) {
return profileQueryService.findByUsername(username, user)
.map(this::profileResponse)
.orElseThrow(ResourceNotFoundException::new);
}
private ResponseEntity profileResponse(ProfileData profile) {
return ResponseEntity.ok(new HashMap<String, Object>() {{
put("profile", profile);
}});
}
}

View File

@@ -26,7 +26,7 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
.authorizeRequests()
.antMatchers(HttpMethod.POST, "/users", "/users/login").permitAll()
.antMatchers(HttpMethod.GET, "/articles/**").permitAll()
.antMatchers(HttpMethod.GET, "/articles/**", "/profiles/**").permitAll()
.anyRequest().authenticated();
http.addFilterBefore(jwtTokenFilter(), UsernamePasswordAuthenticationFilter.class);

View File

@@ -0,0 +1,36 @@
package io.spring.application.profile;
import io.spring.application.user.UserData;
import io.spring.application.user.UserReadService;
import io.spring.core.user.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Optional;
@Component
public class ProfileQueryService {
private UserReadService userReadService;
private UserRelationshipQueryService userRelationshipQueryService;
@Autowired
public ProfileQueryService(UserReadService userReadService, UserRelationshipQueryService userRelationshipQueryService) {
this.userReadService = userReadService;
this.userRelationshipQueryService = userRelationshipQueryService;
}
public Optional<ProfileData> findByUsername(String username, User currentUser) {
UserData userData = userReadService.findByUsername(username);
if (userData == null) {
return Optional.empty();
} else {
ProfileData profileData = new ProfileData(
userData.getId(),
userData.getUsername(),
userData.getBio(),
userData.getImage(),
userRelationshipQueryService.isUserFollowing(currentUser.getId(), userData.getId()));
return Optional.of(profileData);
}
}
}

View File

@@ -1,5 +1,6 @@
package io.spring.application.user;
import io.spring.application.profile.ProfileData;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Component;

View File

@@ -18,7 +18,7 @@ public class MyBatisArticleFavoriteRepository implements ArticleFavoriteReposito
@Override
public void save(ArticleFavorite articleFavorite) {
if (mapper.find(articleFavorite.getArticleId(), articleFavorite.getUserId()) != null) {
if (mapper.find(articleFavorite.getArticleId(), articleFavorite.getUserId()) == null) {
mapper.insert(articleFavorite);
}
}

View File

@@ -3,4 +3,5 @@ image.default=https://static.productionready.io/images/smiley-cyrus.jpg
jwt.secret=nRvyYC4soFxBdZ-F-5Nnzz5USXstR1YylsTd-mA0aKtI9HUlriGrtkf-TiuDapkLiUCogO3JOK7kwZisrHp6wA
jwt.sessionTime=86400
mybatis.config-location=classpath:mybatis-config.xml
mybatis.mapper-locations=mapper/*.xml
mybatis.mapper-locations=mapper/*.xml
logging.level.io.spring.application.article.ArticleReadService=DEBUG

View File

@@ -16,12 +16,12 @@
A.body articleBody,
A.created_at articleCreatedAt,
A.updated_at articleUpdatedAt,
T.name as tagName,
T.name tagName,
<include refid="profileColumns"/>
from
articles A
left join article_tags AT on A.id = AT.article_id
left join tags T on T.id = AT.article_id
left join tags T on T.id = AT.tag_id
left join users U on U.id = A.user_id
</sql>
@@ -43,7 +43,9 @@
<result column="articleCreatedAt" property="createdAt"/>
<result column="articleUpdatedAt" property="updatedAt"/>
<association property="profileData" resultMap="profileData"/>
<collection property="tagList" ofType="arraylist" column="tagName" javaType="string"/>
<collection property="tagList" javaType="list" ofType="string">
<result column="tagName"/>
</collection>
</resultMap>
<resultMap id="profileData" type="io.spring.application.profile.ProfileData">