2017-08-16 14:58:51 +07:00
|
|
|
package io.spring.api;
|
|
|
|
|
|
|
|
import io.spring.api.exception.ResourceNotFoundException;
|
|
|
|
import io.spring.application.profile.ProfileData;
|
|
|
|
import io.spring.application.profile.ProfileQueryService;
|
2017-08-16 15:25:08 +07:00
|
|
|
import io.spring.core.user.FollowRelation;
|
2017-08-16 14:58:51 +07:00
|
|
|
import io.spring.core.user.User;
|
2017-08-16 15:25:08 +07:00
|
|
|
import io.spring.core.user.UserRepository;
|
2017-08-16 14:58:51 +07:00
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
|
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
2017-08-16 16:13:24 +07:00
|
|
|
import org.springframework.web.bind.annotation.DeleteMapping;
|
2017-08-16 14:58:51 +07:00
|
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
|
|
import org.springframework.web.bind.annotation.PathVariable;
|
2017-08-16 15:25:08 +07:00
|
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
2017-08-16 14:58:51 +07:00
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
2017-08-16 16:13:24 +07:00
|
|
|
import java.util.Optional;
|
2017-08-16 14:58:51 +07:00
|
|
|
|
|
|
|
@RestController
|
|
|
|
@RequestMapping(path = "profiles/{username}")
|
|
|
|
public class ProfileApi {
|
|
|
|
private ProfileQueryService profileQueryService;
|
2017-08-16 15:25:08 +07:00
|
|
|
private UserRepository userRepository;
|
2017-08-16 14:58:51 +07:00
|
|
|
|
|
|
|
@Autowired
|
2017-08-16 15:25:08 +07:00
|
|
|
public ProfileApi(ProfileQueryService profileQueryService, UserRepository userRepository) {
|
2017-08-16 14:58:51 +07:00
|
|
|
this.profileQueryService = profileQueryService;
|
2017-08-16 15:25:08 +07:00
|
|
|
this.userRepository = userRepository;
|
2017-08-16 14:58:51 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
@GetMapping
|
|
|
|
public ResponseEntity getProfile(@PathVariable("username") String username,
|
|
|
|
@AuthenticationPrincipal User user) {
|
|
|
|
return profileQueryService.findByUsername(username, user)
|
|
|
|
.map(this::profileResponse)
|
|
|
|
.orElseThrow(ResourceNotFoundException::new);
|
|
|
|
}
|
|
|
|
|
2017-08-16 15:25:08 +07:00
|
|
|
@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);
|
|
|
|
}
|
|
|
|
|
2017-08-16 16:13:24 +07:00
|
|
|
@DeleteMapping(path = "follow")
|
|
|
|
public ResponseEntity unfollow(@PathVariable("username") String username,
|
|
|
|
@AuthenticationPrincipal User user) {
|
|
|
|
Optional<User> userOptional = userRepository.findByUsername(username);
|
|
|
|
if (userOptional.isPresent()) {
|
|
|
|
User target = userOptional.get();
|
|
|
|
return userRepository.findRelation(user.getId(), target.getId())
|
|
|
|
.map(relation -> {
|
|
|
|
userRepository.removeRelation(relation);
|
|
|
|
return profileResponse(profileQueryService.findByUsername(username, user).get());
|
|
|
|
}).orElseThrow(ResourceNotFoundException::new);
|
|
|
|
} else {
|
|
|
|
throw new ResourceNotFoundException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-16 14:58:51 +07:00
|
|
|
private ResponseEntity profileResponse(ProfileData profile) {
|
|
|
|
return ResponseEntity.ok(new HashMap<String, Object>() {{
|
|
|
|
put("profile", profile);
|
|
|
|
}});
|
|
|
|
}
|
|
|
|
}
|