/*
 * Copyright 2017 Banco Bilbao Vizcaya Argentaria, S.A..
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bbva.arq.devops.ae.mirrorgate.api;

import com.bbva.arq.devops.ae.mirrorgate.core.dto.CommitDTO;
import com.bbva.arq.devops.ae.mirrorgate.service.CommitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.PUT;


/**
 * Commit controller.
 */
@RestController
public class CommitController {

    private final CommitService commitService;

    @Autowired
    public CommitController(CommitService commitService) {
        this.commitService = commitService;

    }

    @RequestMapping(value = "/api/commits", method = PUT,
            consumes = APPLICATION_JSON_VALUE,
            produces = APPLICATION_JSON_VALUE)
    public ResponseEntity<?> saveCommits(@Valid @RequestBody Iterable<CommitDTO> request) {
        return ResponseEntity.status(HttpStatus.CREATED).body(commitService.saveCommits(request));
    }

    @RequestMapping(value = "/api/commits/lastcommit", method = GET,
        produces = APPLICATION_JSON_VALUE)
    public ResponseEntity<?> getLastCommitOfBranch(
        @RequestParam(value = "repo", required = true) String repo,
        @RequestParam(value = "branch", required = true) String branch
        ) {
        return ResponseEntity.status(HttpStatus.CREATED).body(commitService.getLastCommitOfBranch(repo, branch));
    }

}
@@ -789,16 +789,83 @@
 ervice;%0A
+import com.bbva.arq.devops.ae.mirrorgate.service.DashboardService;%0A
 import o
@@ -1094,16 +1094,76 @@
 Valid;%0A%0A
+import java.util.List;%0Aimport java.util.stream.Collectors;%0A%0A
 import s
@@ -1496,24 +1496,77 @@
 mmitService;
+%0A    private final DashboardService dashboardService;
 %0A%0A    @Autow
@@ -1625,16 +1625,51 @@
 tService
+, DashboardService dashboardService
 ) %7B%0A    
@@ -1704,24 +1704,629 @@
 mitService;%0A
+        this.dashboardService = dashboardService;%0A    %7D%0A%0A    @RequestMapping(value = %22/api/repositories%22, method = GET,%0A        produces = APPLICATION_JSON_VALUE)%0A    public List%3CString%3E getRepositories() %7B%0A        return dashboardService.getActiveDashboards()%0A            .stream()%0A            .filter(d -%3E d.getBitbucketRepos() != null)%0A            .filter(d -%3E !d.getBitbucketRepos().isEmpty())%0A            .flatMap(d -%3E d.getBitbucketRepos().stream()%0A                .filter(r -%3E r != null)%0A                .filter(r -%3E !r.isEmpty()))%0A            .distinct()%0A            .collect(Collectors.toList());
 %0A    %7D%0A%0A    

add new endpoint to get repositories