Untitled

mail@pastecode.io avatar
unknown
java
2 years ago
1.8 kB
4
Indexable
Never
package com.dufry.localextension.demoapi;

import com.dufry.localextension.clients.mdp.MDPortalUserClient;
import com.dufry.localextension.user.UserDTO;
import com.dufry.localextension.user.UserService;
import com.dufry.localextension.user.UserServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;


@Slf4j
@RestController
@RequestMapping("/demo-users")
@ConditionalOnProperty("demo.api.enabled")
public class MDPortalFetchUserController {

  @Autowired
  private MDPortalUserClient userClient;

  @Autowired
  private UserServiceImpl service;

  @GetMapping("/get-user-by-login")
  @ConditionalOnProperty("demo.api.enabled")
  public Mono<ResponseEntity<UserDTO>> getUserFromMDPortal(@RequestParam String login) {

    return userClient
        .fetchByUsername(login)
        .map(userDTO -> {
          log.info("User with login ({}) -> {}", login, userDTO);
          return ResponseEntity.ok(userDTO);
        });

  }

  @GetMapping("/get-user-details-by-login")
  @ConditionalOnProperty("demo.api.enabled")
  public ResponseEntity<UserDetails> getUserDetailsFromMDPortal(@RequestParam String login) {

    return ResponseEntity.ok(service.loadUserByUsername(login));

  }

}