@RequestMapping(value = "api/{idString}/exprs", method = RequestMethod.GET) public @ResponseBody List<ExprDTO> getAllExprs(@PathVariable("idString") String idString) { // Get all collections List<Collection> collections = collectionRepository.findAllByOwner(idString); // Get all exprs List<Long> exprIds = new ArrayList<Long>(); for (Collection collection : collections) { exprIds.add(collection.getExprId()); } List<Expr> exprs = exprRepository.findAllByIdIn(exprIds); // Put collections into exprs List<ExprDTO> exprDTOs = new ArrayList<ExprDTO>(); Map<Long, ExprDTO> exprDTOMap = new HashMap<Long, ExprDTO>(); for (Expr expr : exprs) { ExprDTO exprDTO = new ExprDTO(expr); exprDTOs.add(exprDTO); exprDTOMap.put(exprDTO.getId(), exprDTO); } for (Collection collection : collections) { if (collection.getContent().length() > 0) { exprDTOMap.get(collection.getExprId()).tags.add(collection.getContent()); } } return exprDTOs; }
@RequestMapping(value = "api/{idString}/exprs/my", method = RequestMethod.GET) public @ResponseBody List<Expr> getMyExprsBycollection( @PathVariable("idString") String idString, @RequestParam(value = "tag") String content, HttpServletRequest request) { Visit visit = new Visit(); visit.setCreated(new Date()); visit.setUserId(idString); visit.setVisitUrl(request.getRequestURL().toString()); visit.setClientIp(request.getRemoteAddr()); visitRepository.save(visit); List<Collection> collections = collectionRepository.findAllByOwnerAndContent(idString, content); List<Long> exprIds = new ArrayList<Long>(); for (Collection collection : collections) { exprIds.add(collection.getExprId()); } List<Expr> exprs = exprRepository.findAllByIdIn(exprIds); for (Expr expr : exprs) { expr.eraseDangerousInfo(); } return exprs; }
@RequestMapping(value = "api/{idString}/expr/new", method = RequestMethod.POST) public @ResponseBody Expr createExpr( @PathVariable("idString") String idString, @RequestParam("expr") MultipartFile exprFile, @RequestParam("tag") String content, HttpServletRequest request) throws IOException { Visit visit = new Visit(); visit.setCreated(new Date()); visit.setUserId(idString); visit.setVisitUrl(request.getRequestURL().toString()); visit.setClientIp(request.getRemoteAddr()); visitRepository.save(visit); String md5 = StringUtil.MD5(exprFile.getBytes()); List<Expr> exprs = exprRepository.findAllByMd5(md5); if (exprs.isEmpty()) { String extension = FileUtil.getExtention(exprFile); if (FileUtil.saveExpr(md5 + extension, exprFile.getBytes())) { Expr expr = new Expr(); expr.setMd5(md5); expr.setCreator(idString); expr.setExtension(extension); exprRepository.save(expr); Collection collection = new Collection(); collection.setExprId(expr.getId()); collection.setOwner(idString); collection.setContent(""); collectionRepository.save(collection); if (content.length() > 0) { collection = new Collection(); collection.setExprId(expr.getId()); collection.setOwner(idString); collection.setContent(content); collectionRepository.save(collection); } return expr.eraseDangerousInfo(); } else { return null; } } else { List<Collection> collections = collectionRepository.findAllByOwnerAndContentAndExprId( idString, "", exprs.get(0).getId()); if (collections.isEmpty()) { Collection collection = new Collection(); collection.setExprId(exprs.get(0).getId()); collection.setOwner(idString); collection.setContent(""); collectionRepository.save(collection); } collections = collectionRepository.findAllByOwnerAndContentAndExprId( idString, content, exprs.get(0).getId()); if (collections.isEmpty()) { Collection collection = new Collection(); collection.setExprId(exprs.get(0).getId()); collection.setOwner(idString); collection.setContent(content); collectionRepository.save(collection); } return exprs.get(0).eraseDangerousInfo(); } }