get profile
This commit is contained in:
40
src/main/java/io/spring/api/ProfileApi.java
Normal file
40
src/main/java/io/spring/api/ProfileApi.java
Normal 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);
|
||||
}});
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
@@ -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">
|
||||
|
||||
Reference in New Issue
Block a user