Skip to content

Instantly share code, notes, and snippets.

@arahansa
Created August 22, 2016 10:16
Show Gist options
  • Save arahansa/56a316702f02f7246a84ee26958726b8 to your computer and use it in GitHub Desktop.
Save arahansa/56a316702f02f7246a84ee26958726b8 to your computer and use it in GitHub Desktop.
public class RestCommonDeleteController {
@Autowired
private AdminProductService productService;
@Autowired
private BlogArticleService blogArticleService;
@Autowired
private SCInfoService scInfoService;
@Autowired
private IntroService introService;
@Autowired
private UserService userService;
@Autowired
private BlogCategoryInfoRepository blogCategoryInfoRepository;
@Autowired
private BlogArticleRepository blogArticleRepository;
@Autowired
private LabItemService labItemService;
// Delete -> List
@RequestMapping(value="/{deleteType}/delete/{id}")
public ResponseEntity<String> delete(@PathVariable String deleteType, @PathVariable Long id){
log.debug("delete Item : {} ", id);
switch(deleteType){
case "product" : productService.deleteProduct(id); break;
case "blog" : blogArticleService.delete(id); break;
case "scinfo": scInfoService.delete(id); break;
case "faq" : introService.deleteFaq(id); break;
case "user" : userService.delete(id); break;
case "blogCategory" : blogArticleService.update4Delete(id); break;
case "labItem": labItemService.delete(id); break;
}
String result="{'msg':'success'}";
return new ResponseEntity(result, HttpStatus.OK);
}
public class RestCommonDeleteController {
private static final ResponseEntity notFound = new ResponseEntity(HttpStatus.NOT_FOUND);
private static final ResponseEntity ok = new ResponseEntity(HttpStatus.OK);
private Map<String, CommonDeleteService> commonDelServiceMap = new HashMap<>();
@Autowired
public void setCommonDelServiceMap(List<CommonDeleteService> commonDelServiceList){
commonDelServiceList.forEach(n -> commonDelServiceMap.put(n.getReqPath(), n));
}
@RequestMapping(value="/{deleteType}/delete/{id}")
public ResponseEntity delete(@PathVariable String deleteType, @PathVariable Long id){
log.debug("Type : {}, delete Item : {} ", deleteType, id);
CommonDeleteService commonDeleteService = commonDelServiceMap.get(deleteType);
if(commonDeleteService==null)
return notFound;
commonDeleteService.delete(id);
return ok;
}
}
public class RestCommonDeleteControllerTest {
@Test
public void testDelete() throws Exception {
RestCommonDeleteController rcdc = new RestCommonDeleteController();
MockDeleteService mockDelService1 = new MockDeleteService("test1");
MockDeleteService mockDelService2 = new MockDeleteService("test2");
rcdc.setCommonDelServiceMap(Arrays.asList(mockDelService1, mockDelService2));
ResponseEntity succcess1 = rcdc.delete("test1", 1L);
ResponseEntity succcess2 = rcdc.delete("test2", 2L);
ResponseEntity fail = rcdc.delete("fail", 1L);
assertEquals(1L, mockDelService1.getId(),1);
assertEquals(2L, mockDelService2.getId(),1);
assertEquals(HttpStatus.OK, succcess1.getStatusCode());
assertEquals(HttpStatus.OK, succcess2.getStatusCode());
assertEquals(HttpStatus.NOT_FOUND, fail.getStatusCode());
}
}
class MockDeleteService implements CommonDeleteService{
private Long id;
@Override
public String getReqPath() {
return reqPath;
}
@Override
public void delete(Long id) {
this.id = id;
}
private String reqPath;
public MockDeleteService(String reqPath){
this.reqPath = reqPath;
}
public Long getId(){
return id;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment