NaisTeamController.java

  1. package no.nav.data.team.naisteam;

  2. import io.swagger.v3.oas.annotations.Operation;
  3. import io.swagger.v3.oas.annotations.responses.ApiResponse;
  4. import io.swagger.v3.oas.annotations.tags.Tag;
  5. import lombok.extern.slf4j.Slf4j;
  6. import no.nav.data.common.exceptions.NotFoundException;
  7. import no.nav.data.common.exceptions.ValidationException;
  8. import no.nav.data.common.rest.RestResponsePage;
  9. import org.springframework.http.HttpStatus;
  10. import org.springframework.http.ResponseEntity;
  11. import org.springframework.web.bind.annotation.GetMapping;
  12. import org.springframework.web.bind.annotation.PathVariable;
  13. import org.springframework.web.bind.annotation.RequestMapping;
  14. import org.springframework.web.bind.annotation.RestController;

  15. import java.util.Optional;

  16. @Slf4j
  17. @RestController
  18. @RequestMapping("/naisteam")
  19. @Tag(name = "Team", description = "REST API for nais teams")
  20. public class NaisTeamController {

  21.     private final NaisConsoleClient naisTeamService;

  22.     public NaisTeamController(NaisConsoleClient naisTeamService) {
  23.         this.naisTeamService = naisTeamService;
  24.     }

  25.     @Operation(summary = "Get all teams")
  26.     @ApiResponse(description = "NaisTeams fetched")
  27.     @GetMapping
  28.     public RestResponsePage<NaisTeam> findAll() {
  29.         log.info("Received a request for all teams");
  30.         return new RestResponsePage<>(naisTeamService.getAllNaisTeams());
  31.     }

  32.     @Operation(summary = "Get team")
  33.     @ApiResponse(description = "NaisTeams fetched")
  34.     @GetMapping("/{teamId}")
  35.     public ResponseEntity<NaisTeam> getTeamByName(@PathVariable String teamId) {
  36.         log.info("Received request for Team with id {}", teamId);
  37.         Optional<NaisTeam> team = naisTeamService.getNaisteam(teamId);
  38.         if (team.isEmpty()) {
  39.             throw new NotFoundException("Couldn't find team " + teamId);
  40.         }
  41.         return new ResponseEntity<>(team.get(), HttpStatus.OK);
  42.     }

  43.     @Operation(summary = "Search teams")
  44.     @ApiResponse(description = "NaisTeams fetched")
  45.     @GetMapping("/search/{name}")
  46.     public ResponseEntity<RestResponsePage<NaisTeam>> searchTeamByName(@PathVariable String name) {
  47.         log.info("Received request for Team with the name like {}", name);
  48.         if (name.length() < 3) {
  49.             throw new ValidationException("Search teams must be at least 3 characters");
  50.         }
  51.         var teams = naisTeamService.searchForNaisTeams(name);
  52.         log.info("Returned {} teams", teams.size());
  53.         return new ResponseEntity<>(new RestResponsePage<>(teams), HttpStatus.OK);
  54.     }

  55.     static class TeamPage extends RestResponsePage<NaisTeam> {

  56.     }
  57. }