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