Merge branch 'master' into bael-16656
This commit is contained in:
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.tx;
|
||||
package com.baeldung.model;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.baeldung.model;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.Set;
|
||||
|
||||
@Entity
|
||||
@Table(name = "users")
|
||||
public class User {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
|
||||
private String username;
|
||||
|
||||
@ElementCollection
|
||||
private Set<String> permissions;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public Set<String> getPermissions() {
|
||||
return permissions;
|
||||
}
|
||||
|
||||
public void setPermissions(Set<String> permissions) {
|
||||
this.permissions = permissions;
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.repository;
|
||||
|
||||
import com.baeldung.model.User;
|
||||
import org.springframework.data.jpa.repository.EntityGraph;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface UserRepository extends JpaRepository<User, Long> {
|
||||
|
||||
@EntityGraph(attributePaths = "permissions")
|
||||
Optional<User> findDetailedByUsername(String username);
|
||||
|
||||
Optional<User> findSummaryByUsername(String username);
|
||||
|
||||
Optional<User> findByUsername(String username);
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.service;
|
||||
|
||||
import com.baeldung.model.User;
|
||||
import com.baeldung.repository.UserRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public class SimpleUserService implements UserService {
|
||||
|
||||
private final UserRepository userRepository;
|
||||
|
||||
public SimpleUserService(UserRepository userRepository) {
|
||||
this.userRepository = userRepository;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
public Optional<User> findOne(String username) {
|
||||
return userRepository.findDetailedByUsername(username);
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.service;
|
||||
|
||||
import com.baeldung.model.User;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface UserService {
|
||||
Optional<User> findOne(String username);
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
package com.baeldung.web;
|
||||
|
||||
import com.baeldung.model.User;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
public class DetailedUserDto {
|
||||
|
||||
private Long id;
|
||||
private String username;
|
||||
private Set<String> permissions;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public Set<String> getPermissions() {
|
||||
return permissions;
|
||||
}
|
||||
|
||||
public void setPermissions(Set<String> permissions) {
|
||||
this.permissions = permissions;
|
||||
}
|
||||
|
||||
public static DetailedUserDto fromEntity(User user) {
|
||||
DetailedUserDto detailed = new DetailedUserDto();
|
||||
detailed.setId(user.getId());
|
||||
detailed.setUsername(user.getUsername());
|
||||
detailed.setPermissions(user.getPermissions());
|
||||
|
||||
return detailed;
|
||||
}
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.web;
|
||||
|
||||
import com.baeldung.service.UserService;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
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;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/users")
|
||||
public class UserController {
|
||||
|
||||
private final UserService userService;
|
||||
|
||||
public UserController(UserService userService) {
|
||||
this.userService = userService;
|
||||
}
|
||||
|
||||
@GetMapping("/{username}")
|
||||
public ResponseEntity<?> findOne(@PathVariable String username) {
|
||||
return userService.findOne(username)
|
||||
.map(DetailedUserDto::fromEntity)
|
||||
.map(ResponseEntity::ok)
|
||||
.orElse(ResponseEntity.notFound().build());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user