@ModelAttribute(ApplicationConstants.SESSION_SIGNIN_USER) User signInUser,
Model model, HttpServletRequest request, HttpSession session){
if(!ajaxUtil.isAjaxRequest(request)){
throw new ResourceNotFoundException();
}
Spot target = spotRepository.findOne(targetId);
if(target==null){
throw new RuntimeException("Invalid spot id : " + targetId);
}
TrackShip ts = trackShipRepository.getByTargetAndTracked(targetId, signInUser.getId());
boolean tracked = false;
if(ts!=null && ts.getStatus() == ApplicationConstants.TRACKSHIP_DISABLED){
tracked = true;
ts.setStatus(ApplicationConstants.TRACKSHIP_NORMAL);
ts.setUpdatedAt(new Date());
userRepository.inc(signInUser.getId(), "trackCount", 1);
spotRepository.inc(target.getId(), "trackedCount", 1);
trackShipRepository.save(ts);
}else if(ts==null){
tracked = true;
ts = new TrackShip();
ts.setCreatedAt(new Date());
ts.setUpdatedAt(ts.getCreatedAt());
ts.setTarget(target);
ts.setTracked(signInUser);
ts.setStatus(ApplicationConstants.FOLLOWSHIP_NORMAL);
userRepository.inc(signInUser.getId(), "trackCount", 1);
spotRepository.inc(target.getId(), "trackedCount", 1);
trackShipRepository.save(ts);
}
if(tracked){
trackShipRepository.save(ts);
// save activity
Activity activity = new Activity();
activity.setOwner(signInUser.getId());
activity.setCreatedAt(new Date());
activity.setTargetSpot(target.getId());
activity.setType(ActivityType.TRACK);
activity.setBy(sessionUtil.getBy(session));
activityRepository.save(activity);
}
return new AjaxResult(AjaxResultCode.SUCCESS);