update
This commit is contained in:
@@ -10,54 +10,49 @@ import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.transaction.Transactional;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@Transactional
|
||||
public class DiscussionsServiceImpl implements IDiscussionsService {
|
||||
|
||||
@Autowired
|
||||
private DiscussionsRepository discussionsRepository;
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
@Override
|
||||
public List<Discussion> getAllList() {
|
||||
return discussionsRepository.findAll();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@Override
|
||||
public Discussion insert(Discussion discussion) {
|
||||
|
||||
return discussionsRepository.save(discussion);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
@Override
|
||||
public Discussion findOne(Integer id) {
|
||||
Discussion discussion = new Discussion();
|
||||
discussion.setId(id);
|
||||
return discussionsRepository.getOne(id);
|
||||
// getReferenceById replaces removed JpaRepository.getOne()
|
||||
return discussionsRepository.getReferenceById(id);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
@Override
|
||||
public Page<Discussion> queryAllDiscussionsList(Discussion discussion, int pageNum, int pageSize, String tag, String type) {
|
||||
Sort sort = new Sort(Sort.Direction.DESC, "id");
|
||||
if (type.equals("hot")) {
|
||||
sort = new Sort(Sort.Direction.DESC, "likeCount");
|
||||
} else if (type.equals("new")) {
|
||||
sort = new Sort(Sort.Direction.DESC, "startTime");
|
||||
}
|
||||
@SuppressWarnings("deprecation")
|
||||
Pageable pageable = new PageRequest(pageNum, pageSize, sort);
|
||||
//将匹配对象封装成Example对象
|
||||
Sort sort = switch (type) {
|
||||
case "hot" -> Sort.by(Sort.Direction.DESC, "likeCount");
|
||||
case "new" -> Sort.by(Sort.Direction.DESC, "startTime");
|
||||
default -> Sort.by(Sort.Direction.DESC, "id");
|
||||
};
|
||||
Pageable pageable = PageRequest.of(pageNum, pageSize, sort);
|
||||
Example<Discussion> example = Example.of(discussion);
|
||||
if (null != tag && !"all".equals(tag)) {
|
||||
Page<Discussion> pageList = discussionsRepository.findByQuery(tag, pageable);
|
||||
return pageList;
|
||||
if (tag != null && !"all".equals(tag)) {
|
||||
return discussionsRepository.findByQuery(tag, pageable);
|
||||
} else {
|
||||
Page<Discussion> pageList = discussionsRepository.findAll(example, pageable);
|
||||
return pageList;
|
||||
return discussionsRepository.findAll(example, pageable);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user