1111import org .springframework .http .HttpStatus ;
1212import org .springframework .stereotype .Service ;
1313import org .springframework .web .server .ResponseStatusException ;
14+ import reactor .core .publisher .Flux ;
1415import reactor .core .publisher .Mono ;
1516
1617import java .time .LocalDateTime ;
1718
1819@ Service ("userServiceV2" )
1920@ RequiredArgsConstructor
2021public class UserService {
22+
2123 private final CryptographyService cryptographyService ;
22- private final UserRepository repository ;
24+ private final UserRepository userRepository ;
2325 private final GetAuthenticatedUserId getAuthenticatedUserId ;
2426
2527 public Mono <User > create (BrukerDTO bruker ) {
@@ -35,7 +37,7 @@ public Mono<User> create(BrukerDTO bruker) {
3537 entity .setNew (true );
3638 return entity ;
3739 })
38- .flatMap (repository ::save )
40+ .flatMap (userRepository ::save )
3941 .map (User ::new );
4042
4143 }
@@ -47,17 +49,22 @@ public Mono<User> getUserFromOrganisasjonsnummer(String organisasjonsnummer) {
4749 .flatMap (this ::getUser );
4850 }
4951
52+ public Flux <User > getAllUsers () {
53+
54+ return userRepository .findAll ()
55+ .map (User ::new );
56+ }
5057
5158 public Mono <User > getUser (String id ) {
5259 return getUser (id , false );
5360 }
5461
5562 public Mono <User > getUser (String id , boolean loggedIn ) {
56- return repository .findById (id ).flatMap (entity -> {
63+ return userRepository .findById (id ).flatMap (entity -> {
5764 if (loggedIn ) {
5865 entity .setNew (false );
5966 entity .setSistInnlogget (LocalDateTime .now ());
60- return repository .save (entity );
67+ return userRepository .save (entity );
6168 }
6269 return Mono .just (entity );
6370 }).map (User ::new );
@@ -67,26 +74,26 @@ public Mono<User> updateUser(BrukerDTO bruker) {
6774 return getAuthenticatedUserId
6875 .call ()
6976 .map (userId -> cryptographyService .createId (userId , bruker .organisasjonsnummer ()))
70- .flatMap (repository ::findById )
77+ .flatMap (userRepository ::findById )
7178 .switchIfEmpty (Mono .error (new ResponseStatusException (HttpStatus .NOT_FOUND , "Bruker ikke funnet." )))
7279 .flatMap (entity -> {
7380 entity .setEpost (bruker .epost ());
74- return repository .save (entity );
81+ return userRepository .save (entity );
7582 })
7683 .map (User ::new );
7784 }
7885
7986 public Mono <User > getUserByBrukernavn (String username ) {
80- return repository .findByBrukernavn (username ).map (User ::new );
87+ return userRepository .findByBrukernavn (username ).map (User ::new );
8188 }
8289
8390 public Mono <Void > delete (String id ) {
84- return repository .deleteById (id );
91+ return userRepository .deleteById (id );
8592 }
8693
8794 private Mono <Void > validateCreateUser (String userId , String representing ) {
8895 var id = cryptographyService .createId (userId , representing );
89- return repository .existsById (id )
96+ return userRepository .existsById (id )
9097 .doOnNext (exists -> {
9198 if (Boolean .TRUE .equals (exists )) {
9299 throw new UserAlreadyExistsException (id );
0 commit comments