41 lines
1.5 KiB
Java
41 lines
1.5 KiB
Java
|
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);
|
||
|
}});
|
||
|
}
|
||
|
}
|