@Test public void testDeleteProject() throws Exception { User manager = _dbInjecter.injectUser("manager"); TreeType treeType = _dbInjecter.injectTreeType("wood", "wood desc", 0.5); Project project = _dbInjecter.injectProject("project", manager, "desc", true, 1.0f, 1.0f); _dbInjecter.injectProjectArticle(treeType, project, 10, 1.0, 1.0); _dbInjecter.injectProjectImage("title", "desc", 1L, 1000000L); assertThat(_projectRepository.count()).isEqualTo(1L); assertThat(_projectArticleRepository.count()).isEqualTo(1L); assertThat(_projectImageRepository.count()).isEqualTo(1L); String userToken = _tokenAuthenticationService.getTokenFromUser(_userRepository.findById(1L).orElse(null)); mockMvc.perform(delete(Uris.PROJECT_DELETE).header("X-AUTH-TOKEN", userToken).contentType(TestUtil.APPLICATION_JSON_UTF8).param("id", "1")).andExpect(status().isOk()); assertThat(_projectRepository.count()).isEqualTo(0); assertThat(_projectArticleRepository.count()).isEqualTo(0); assertThat(_projectImageRepository.count()).isEqualTo(0); }
@RequestMapping(value = Uris.PROJECT_DELETE, method = RequestMethod.DELETE) public ResponseEntity<?> deleteProject(@RequestParam Long id) { try { if (projectRepository.existsById(id)) { Project project = projectRepository.findById(id).orElse(null); List<ProjectArticle> articles = projectArticleRepository.findByProject(project); List<ProjectImage> images = projectImageRepository.findProjectImagesToProjectByProjectId(id); projectImageRepository.deleteAll(images); projectArticleRepository.deleteAll(articles); projectRepository.deleteById(id); return new ResponseEntity<>(HttpStatus.OK); } else { return new ResponseEntity<>(HttpStatus.BAD_REQUEST); } } catch (Exception e) { return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } }
ProjectController { @RequestMapping(value = Uris.PROJECT_DELETE, method = RequestMethod.DELETE) public ResponseEntity<?> deleteProject(@RequestParam Long id) { try { if (projectRepository.existsById(id)) { Project project = projectRepository.findById(id).orElse(null); List<ProjectArticle> articles = projectArticleRepository.findByProject(project); List<ProjectImage> images = projectImageRepository.findProjectImagesToProjectByProjectId(id); projectImageRepository.deleteAll(images); projectArticleRepository.deleteAll(articles); projectRepository.deleteById(id); return new ResponseEntity<>(HttpStatus.OK); } else { return new ResponseEntity<>(HttpStatus.BAD_REQUEST); } } catch (Exception e) { return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } } }
ProjectController { @RequestMapping(value = Uris.PROJECT_DELETE, method = RequestMethod.DELETE) public ResponseEntity<?> deleteProject(@RequestParam Long id) { try { if (projectRepository.existsById(id)) { Project project = projectRepository.findById(id).orElse(null); List<ProjectArticle> articles = projectArticleRepository.findByProject(project); List<ProjectImage> images = projectImageRepository.findProjectImagesToProjectByProjectId(id); projectImageRepository.deleteAll(images); projectArticleRepository.deleteAll(articles); projectRepository.deleteById(id); return new ResponseEntity<>(HttpStatus.OK); } else { return new ResponseEntity<>(HttpStatus.BAD_REQUEST); } } catch (Exception e) { return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } }  }
ProjectController { @RequestMapping(value = Uris.PROJECT_DELETE, method = RequestMethod.DELETE) public ResponseEntity<?> deleteProject(@RequestParam Long id) { try { if (projectRepository.existsById(id)) { Project project = projectRepository.findById(id).orElse(null); List<ProjectArticle> articles = projectArticleRepository.findByProject(project); List<ProjectImage> images = projectImageRepository.findProjectImagesToProjectByProjectId(id); projectImageRepository.deleteAll(images); projectArticleRepository.deleteAll(articles); projectRepository.deleteById(id); return new ResponseEntity<>(HttpStatus.OK); } else { return new ResponseEntity<>(HttpStatus.BAD_REQUEST); } } catch (Exception e) { return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } }  @RequestMapping(value = Uris.PROJECTS, method = RequestMethod.GET) @JsonView(Views.ProjectNameAndId.class) ResponseEntity<?> getAllProjects(); @RequestMapping(value = Uris.PROJECT, method = RequestMethod.GET) @JsonView(Views.ProjectData.class) ResponseEntity<?> getProject(@RequestParam long projectId); @RequestMapping(value = Uris.ARTICLES, method = RequestMethod.GET) @JsonView(Views.ProjectArticle.class) ResponseEntity<?> getArticlesForProject(@RequestParam long projectId); @RequestMapping(value = Uris.IMAGES, method = RequestMethod.GET) @JsonView(Views.ProjectImage.class) ResponseEntity<?> getImagesForProject(@RequestParam long projectId); @RequestMapping(value = Uris.PROJECT_EDIT, method = RequestMethod.POST) ResponseEntity<?> editProject(@RequestBody Project project); @RequestMapping(value = Uris.PROJECT_CREATE, method = RequestMethod.POST) ResponseEntity<?> createProject(@RequestBody Project project); @RequestMapping(value = Uris.PROJECT_DELETE, method = RequestMethod.DELETE) ResponseEntity<?> deleteProject(@RequestParam Long id); @RequestMapping(value = Uris.PROJECT_REMOVE_ARTICLE, method = RequestMethod.POST) ResponseEntity<?> removeArticle(@RequestParam Long articleId); @RequestMapping(value = Uris.PROJECT_MAIN_IMAGE, method = RequestMethod.POST) @Transactional ResponseEntity<?> addMainImage(@RequestParam Long projectId, @RequestParam("file") MultipartFile file); @RequestMapping(value = Uris.PROJECT_IMAGE_CREATE_EDIT, method = RequestMethod.POST) @Transactional ResponseEntity<?> createEditProjectImageData(@RequestBody ProjectImageRequest projectImageRequest); @RequestMapping(value = Uris.PROJECT_IMAGE_UPLOAD, method = RequestMethod.POST) @Transactional ResponseEntity<?> uploadUserImage(@RequestParam Long imageId, @RequestParam("file") MultipartFile file); @RequestMapping(value = Uris.PROJECT_IMAGE_DELETE, method = RequestMethod.POST) ResponseEntity<?> removeProjectImage(@RequestParam Long projectImageId, @RequestParam String imageFileName); }
ProjectController { @RequestMapping(value = Uris.PROJECT_DELETE, method = RequestMethod.DELETE) public ResponseEntity<?> deleteProject(@RequestParam Long id) { try { if (projectRepository.existsById(id)) { Project project = projectRepository.findById(id).orElse(null); List<ProjectArticle> articles = projectArticleRepository.findByProject(project); List<ProjectImage> images = projectImageRepository.findProjectImagesToProjectByProjectId(id); projectImageRepository.deleteAll(images); projectArticleRepository.deleteAll(articles); projectRepository.deleteById(id); return new ResponseEntity<>(HttpStatus.OK); } else { return new ResponseEntity<>(HttpStatus.BAD_REQUEST); } } catch (Exception e) { return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } }  @RequestMapping(value = Uris.PROJECTS, method = RequestMethod.GET) @JsonView(Views.ProjectNameAndId.class) ResponseEntity<?> getAllProjects(); @RequestMapping(value = Uris.PROJECT, method = RequestMethod.GET) @JsonView(Views.ProjectData.class) ResponseEntity<?> getProject(@RequestParam long projectId); @RequestMapping(value = Uris.ARTICLES, method = RequestMethod.GET) @JsonView(Views.ProjectArticle.class) ResponseEntity<?> getArticlesForProject(@RequestParam long projectId); @RequestMapping(value = Uris.IMAGES, method = RequestMethod.GET) @JsonView(Views.ProjectImage.class) ResponseEntity<?> getImagesForProject(@RequestParam long projectId); @RequestMapping(value = Uris.PROJECT_EDIT, method = RequestMethod.POST) ResponseEntity<?> editProject(@RequestBody Project project); @RequestMapping(value = Uris.PROJECT_CREATE, method = RequestMethod.POST) ResponseEntity<?> createProject(@RequestBody Project project); @RequestMapping(value = Uris.PROJECT_DELETE, method = RequestMethod.DELETE) ResponseEntity<?> deleteProject(@RequestParam Long id); @RequestMapping(value = Uris.PROJECT_REMOVE_ARTICLE, method = RequestMethod.POST) ResponseEntity<?> removeArticle(@RequestParam Long articleId); @RequestMapping(value = Uris.PROJECT_MAIN_IMAGE, method = RequestMethod.POST) @Transactional ResponseEntity<?> addMainImage(@RequestParam Long projectId, @RequestParam("file") MultipartFile file); @RequestMapping(value = Uris.PROJECT_IMAGE_CREATE_EDIT, method = RequestMethod.POST) @Transactional ResponseEntity<?> createEditProjectImageData(@RequestBody ProjectImageRequest projectImageRequest); @RequestMapping(value = Uris.PROJECT_IMAGE_UPLOAD, method = RequestMethod.POST) @Transactional ResponseEntity<?> uploadUserImage(@RequestParam Long imageId, @RequestParam("file") MultipartFile file); @RequestMapping(value = Uris.PROJECT_IMAGE_DELETE, method = RequestMethod.POST) ResponseEntity<?> removeProjectImage(@RequestParam Long projectImageId, @RequestParam String imageFileName);  }