You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.3 KiB

package com.community.pocket.repository.android;
import com.community.pocket.entity.po.Notice;
import com.community.pocket.entity.vo.NoticeVo;
import com.community.pocket.repository.BaseDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;
import java.util.ArrayList;
import java.util.List;
/**
* 公告信息数据接口
*/
@Repository
public class NoticeDao extends BaseDao<Notice> {
private static final Logger LOG = LoggerFactory.getLogger(NoticeDao.class);
//保存公告
public Notice save(NoticeVo noticeVo) {
Notice notice = new Notice();
notice.setAuthor(noticeVo.getAuthor());
notice.setContent(noticeVo.getContent());
notice.setTime(System.currentTimeMillis());
notice.setTitle(noticeVo.getTitle());
return save(notice);
}
@Override
public Notice save(Notice notice) {
try {
return mongoTemplate.save(notice);
} catch (Exception e) {
e.printStackTrace();
LOG.error(e.toString());
}
return null;
}
//加载所有公告
public List<NoticeVo> loadNotices() {
List<NoticeVo> noticeVos = new ArrayList<>();
try {
List<Notice> notices = mongoTemplate.findAll(Notice.class);
for (Notice notice : notices) {
NoticeVo noticeVo = new NoticeVo();
noticeVo.setId(notice.getId().toString());
noticeVo.setAuthor(notice.getAuthor());
noticeVo.setContent(notice.getContent());
noticeVo.setTime(notice.getTime());
noticeVo.setTitle(notice.getTitle());
noticeVos.add(noticeVo);
}
return noticeVos;
} catch (Exception e) {
e.printStackTrace();
LOG.error(e.toString());
}
return null;
}
//删除公告
public boolean deleteNotice(NoticeVo noticeVo) {
return mongoTemplate.remove(new Query(Criteria.where("id").is(noticeVo.getId())), entityClass()).wasAcknowledged();
}
@Override
public Class<Notice> entityClass() {
return Notice.class;
}
}