Browse Source

完成controller

blumonilr 5 years ago
parent
commit
4c4020c776

+ 16 - 5
src/main/java/com/nju/edu/gitlab/controller/GitlabGroupController.java

@@ -1,21 +1,32 @@
 package com.nju.edu.gitlab.controller;
 
+import com.nju.edu.gitlab.service.api.GroupService;
 import com.nju.edu.gitlab.web.dto.GroupMemberDTO;
 import com.nju.edu.gitlab.web.response.EmptyResponse;
+import com.nju.edu.gitlab.web.response.ResourceResponse;
 import com.nju.edu.gitlab.web.response.Response;
+import org.gitlab4j.api.GitLabApiException;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 
 @RestController
 @RequestMapping("/api/group")
 public class GitlabGroupController {
-    
+
+    private final GroupService groupService;
+
+    @Autowired
+    public GitlabGroupController(GroupService groupService) {
+        this.groupService = groupService;
+    }
+
     @PostMapping("/")
-    public Response createGitlabGroup(@RequestParam String groupName){
-        return new EmptyResponse();
+    public Response createGitlabGroup(@RequestParam String groupName) throws GitLabApiException {
+        return new ResourceResponse(groupService.createGroup(groupName));
     }
 
     @PostMapping("/addMember")
-    public Response addGroupMember(@RequestBody GroupMemberDTO groupMemberDTO){
-        return new EmptyResponse();
+    public Response addGroupMember(@RequestBody GroupMemberDTO groupMemberDTO) throws GitLabApiException {
+        return new ResourceResponse(groupService.addMember(groupMemberDTO));
     }
 }

+ 34 - 23
src/main/java/com/nju/edu/gitlab/controller/GitlabProjectController.java

@@ -1,46 +1,57 @@
 package com.nju.edu.gitlab.controller;
 
+import com.nju.edu.gitlab.service.api.ProjectService;
 import com.nju.edu.gitlab.web.dto.*;
 import com.nju.edu.gitlab.web.response.EmptyResponse;
+import com.nju.edu.gitlab.web.response.ResourceResponse;
 import com.nju.edu.gitlab.web.response.Response;
+import org.gitlab4j.api.GitLabApiException;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 
-import javax.ws.rs.Path;
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
 
 @RestController
 @RequestMapping("/api/project")
 public class GitlabProjectController {
+    private final ProjectService projectService;
+
+    @Autowired
+    public GitlabProjectController(ProjectService projectService) {
+        this.projectService = projectService;
+    }
 
     @PostMapping("/")
-    public Response createProject(@RequestBody ProjectDTO projectDTO){
-        return new EmptyResponse();
+    public Response createProject(@RequestBody ProjectDTO projectDTO) throws GitLabApiException {
+        return new ResourceResponse(projectService.createProject(projectDTO));
     }
 
     @PostMapping("/userFork")
-    public Response forkProject(@RequestBody UserForkDTO userForkDTO){
-        return new EmptyResponse();
+    public Response forkProject(@RequestBody UserForkDTO userForkDTO) throws GitLabApiException {
+        return new ResourceResponse(projectService.forkProject(userForkDTO));
     }
 
     @PostMapping("/groupFork")
-    public Response groupForkProject(@RequestBody GroupForkDTO groupForkDTO){
-        return new EmptyResponse();
+    public Response groupForkProject(@RequestBody GroupForkDTO groupForkDTO) throws GitLabApiException {
+        return new ResourceResponse(projectService.forkGroupProject(groupForkDTO));
     }
 
     @GetMapping("/compare")
     public Response compareProject(@RequestParam Integer projectId,
                                    @RequestParam String fromBash,
-                                   @RequestParam String toBash){
-        return new EmptyResponse();
+                                   @RequestParam String toBash) throws GitLabApiException {
+        return new ResourceResponse(projectService.compare(projectId,fromBash,toBash));
     }
 
     @PostMapping("/webhook")
-    public Response addWebHook(@RequestBody WebHookDTO webHookDTO){
-        return new EmptyResponse();
+    public Response addWebHook(@RequestBody WebHookDTO webHookDTO) throws GitLabApiException {
+        return new ResourceResponse(projectService.addWebHook(webHookDTO));
     }
 
     @PostMapping("/archive")
-    public Response archiveProject(@RequestParam Integer projectId){
-        return new EmptyResponse();
+    public Response archiveProject(@RequestParam Integer projectId) throws GitLabApiException {
+        return new ResourceResponse(projectService.archiveProject(projectId));
     }
 
     @DeleteMapping("/{projectId}")
@@ -50,30 +61,30 @@ public class GitlabProjectController {
 
     @GetMapping("/{projectId}/getFile")
     public Response getProjectFile(@PathVariable(name = "projectId") Integer projectId,
-                                   @RequestBody ProjectFileDTO projectFileDTO){
-        return new EmptyResponse();
+                                   @RequestBody ProjectFileDTO projectFileDTO) throws UnsupportedEncodingException, GitLabApiException {
+        return new ResourceResponse(projectService.getProjectFile(projectId,projectFileDTO));
     }
 
     @GetMapping("/{projectId}/getRawFile")
     public Response getProjectRawFile(@PathVariable(name = "projectId") Integer projectId,
-                                      @RequestBody ProjectRawFileDTO projectRawFileDTO){
-        return new EmptyResponse();
+                                      @RequestBody ProjectRawFileDTO projectRawFileDTO) throws GitLabApiException, IOException {
+        return new ResourceResponse(projectService.getProjectRawFile(projectId,projectRawFileDTO));
     }
 
     @PostMapping("/{projectId}/commit")
     public Response createCommit(@PathVariable(name = "projectId") Integer projectId,
-                                 @RequestBody CommitDTO commitDTO){
-        return new EmptyResponse();
+                                 @RequestBody CommitDTO commitDTO) throws GitLabApiException {
+        return new ResourceResponse(projectService.createCommit(projectId,commitDTO));
     }
 
     @GetMapping("{projectId}/commit/{commitHash}")
     public Response getAllCommitMessage(@PathVariable(name = "projectId") Integer projectId,
-                                        @PathVariable(name = "commitHash") String commitHash){
-        return new EmptyResponse();
+                                        @PathVariable(name = "commitHash") String commitHash) throws GitLabApiException {
+        return new ResourceResponse(projectService.getAllCommitMessage(projectId,commitHash));
     }
 
     @GetMapping("/{projectId}/branches")
-    public Response getAllBranches(@PathVariable(name = "projectId") Integer projectId){
-        return new EmptyResponse();
+    public Response getAllBranches(@PathVariable(name = "projectId") Integer projectId) throws GitLabApiException {
+        return new ResourceResponse(projectService.getProjectBranches(projectId));
     }
 }

+ 17 - 6
src/main/java/com/nju/edu/gitlab/controller/GitlabUserController.java

@@ -1,27 +1,38 @@
 package com.nju.edu.gitlab.controller;
 
+import com.nju.edu.gitlab.service.api.UserService;
 import com.nju.edu.gitlab.web.dto.ChangePasswordDTO;
 import com.nju.edu.gitlab.web.dto.GitlabUserDTO;
 import com.nju.edu.gitlab.web.response.EmptyResponse;
+import com.nju.edu.gitlab.web.response.ResourceResponse;
 import com.nju.edu.gitlab.web.response.Response;
+import org.gitlab4j.api.GitLabApiException;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 
 @RestController
 @RequestMapping("/api/user")
 public class GitlabUserController {
 
+    private final UserService userService;
+
+    @Autowired
+    public GitlabUserController(UserService userService) {
+        this.userService = userService;
+    }
+
     @PostMapping("/")
-    public Response createGitlabUser(@RequestBody GitlabUserDTO gitlabUserDTO){
-        return new EmptyResponse();
+    public Response createGitlabUser(@RequestBody GitlabUserDTO gitlabUserDTO) throws GitLabApiException {
+        return new ResourceResponse(userService.createGitlabUser(gitlabUserDTO));
     }
 
     @PostMapping("/changePassword")
-    public Response changePassword(@RequestBody ChangePasswordDTO changePasswordDTO){
-        return new EmptyResponse();
+    public Response changePassword(@RequestBody ChangePasswordDTO changePasswordDTO) throws GitLabApiException {
+        return new ResourceResponse(userService.changePassword(changePasswordDTO));
     }
 
     @DeleteMapping("/{userId}")
-    public Response deleteUser(@PathVariable(name = "userId") Integer userId){
-        return new EmptyResponse();
+    public Response deleteUser(@PathVariable(name = "userId") Integer userId) throws GitLabApiException {
+        return new ResourceResponse(userService.deleteUser(userId));
     }
 }

+ 22 - 0
src/main/java/com/nju/edu/gitlab/web/handler/ApiExceptionHandler.java

@@ -0,0 +1,22 @@
+package com.nju.edu.gitlab.web.handler;
+
+import com.nju.edu.gitlab.web.response.ErrorResponse;
+import com.nju.edu.gitlab.web.response.Response;
+import org.apache.http.HttpStatus;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.bind.annotation.ResponseBody;
+import org.springframework.web.bind.annotation.RestControllerAdvice;
+
+import javax.servlet.http.HttpServletResponse;
+
+@RestControllerAdvice("{com.nju.edu.gitlab.controller}")
+public class ApiExceptionHandler {
+    
+    @ExceptionHandler(Exception.class)
+    @ResponseBody
+    public Response handle(HttpServletResponse response, Exception exception) {
+        int error = HttpStatus.SC_INTERNAL_SERVER_ERROR;
+        response.setStatus(error);
+        return new ErrorResponse(error, exception.getMessage());
+    }
+}