添加删除公告接口

master
panqihua 5 years ago
parent 5514f029e8
commit 09b9be24d8
  1. 38
      src/main/java/com/community/pocket/api/android/ForumController.java
  2. 56
      src/main/java/com/community/pocket/entity/vo/NoticeVo.java
  3. 12
      src/main/java/com/community/pocket/entity/vo/android/ForumNoticeResponse.java
  4. 37
      src/main/java/com/community/pocket/repository/android/NoticeDao.java

@ -2,23 +2,22 @@ package com.community.pocket.api.android;
import com.community.pocket.entity.po.Forum; import com.community.pocket.entity.po.Forum;
import com.community.pocket.entity.po.ForumContent; import com.community.pocket.entity.po.ForumContent;
import com.community.pocket.entity.po.Notice;
import com.community.pocket.entity.po.android.Active; import com.community.pocket.entity.po.android.Active;
import com.community.pocket.entity.po.android.Complain; import com.community.pocket.entity.po.android.Complain;
import com.community.pocket.entity.po.android.Token; import com.community.pocket.entity.po.android.Token;
import com.community.pocket.entity.vo.NoticeVo;
import com.community.pocket.entity.vo.Result; import com.community.pocket.entity.vo.Result;
import com.community.pocket.entity.vo.android.*; import com.community.pocket.entity.vo.android.*;
import com.community.pocket.repository.android.*; import com.community.pocket.repository.android.*;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List; import java.util.List;
/** /**
* 论坛接口 * 论坛接口
*/ */
//客户端跨域测试
@RestController @RestController
public class ForumController { public class ForumController {
@ -44,9 +43,10 @@ public class ForumController {
private ComplainDao complainDao; private ComplainDao complainDao;
//加载公告接口 //加载公告接口
@GetMapping("/forum/notice") @CrossOrigin("http://localhost:4200")
@GetMapping({"/api/forum/notices", "/forum/notice"})
public ForumNoticeResponse loadNotices() { public ForumNoticeResponse loadNotices() {
List<Notice> noticeList = noticeDao.loadNotices(); List<NoticeVo> noticeList = noticeDao.loadNotices();
if (noticeList != null) { if (noticeList != null) {
ForumNoticeResponse response = new ForumNoticeResponse(Result.OK, ForumNoticeResponse.Msg.ok); ForumNoticeResponse response = new ForumNoticeResponse(Result.OK, ForumNoticeResponse.Msg.ok);
response.setNoticeList(noticeList); response.setNoticeList(noticeList);
@ -56,6 +56,32 @@ public class ForumController {
} }
} }
/**
* 添加公告
*/
@CrossOrigin("http://localhost:4200")
@PostMapping("/api/forum/notice/add")
public ForumNoticeResponse addNotices(@RequestBody NoticeVo noticeForm) {
if (noticeDao.save(noticeForm) != null) {
return new ForumNoticeResponse(Result.OK, ForumNoticeResponse.Msg.ok);
} else {
return new ForumNoticeResponse(Result.FAIL, ForumNoticeResponse.Msg.fail);
}
}
/**
* 删除公告
*/
@CrossOrigin("http://localhost:4200")
@PostMapping("/api/forum/notice/delete")
public ForumNoticeResponse deleteNotices(@RequestBody NoticeVo noticeVo) {
if (noticeDao.deleteNotice(noticeVo)) {
return new ForumNoticeResponse(Result.OK, ForumNoticeResponse.Msg.delete_ok);
} else {
return new ForumNoticeResponse(Result.FAIL, ForumNoticeResponse.Msg.delete_fail);
}
}
//加载热榜接口 //加载热榜接口
@GetMapping("/forum/hot") @GetMapping("/forum/hot")
public ForumHotResponse loadHot(Integer num) { public ForumHotResponse loadHot(Integer num) {

@ -0,0 +1,56 @@
package com.community.pocket.entity.vo;
/**
* 公告
*/
public class NoticeVo {
private String id;
//公告标题
private String title;
//公告内容
private String content;
//公告人
private String author;
//公告时间
private Long time;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Long getTime() {
return time;
}
public void setTime(Long time) {
this.time = time;
}
}

@ -1,6 +1,6 @@
package com.community.pocket.entity.vo.android; package com.community.pocket.entity.vo.android;
import com.community.pocket.entity.po.Notice; import com.community.pocket.entity.vo.NoticeVo;
import com.community.pocket.entity.vo.Response; import com.community.pocket.entity.vo.Response;
import com.community.pocket.entity.vo.Result; import com.community.pocket.entity.vo.Result;
@ -11,13 +11,13 @@ import java.util.List;
*/ */
public class ForumNoticeResponse extends Response<ForumNoticeResponse.Msg> { public class ForumNoticeResponse extends Response<ForumNoticeResponse.Msg> {
private List<Notice> noticeList; private List<NoticeVo> noticeList;
public List<Notice> getNoticeList() { public List<NoticeVo> getNoticeList() {
return noticeList; return noticeList;
} }
public void setNoticeList(List<Notice> noticeList) { public void setNoticeList(List<NoticeVo> noticeList) {
this.noticeList = noticeList; this.noticeList = noticeList;
} }
@ -27,6 +27,8 @@ public class ForumNoticeResponse extends Response<ForumNoticeResponse.Msg> {
public enum Msg implements CustomMessage { public enum Msg implements CustomMessage {
ok, ok,
fail fail,
delete_ok,
delete_fail
} }
} }

@ -1,11 +1,15 @@
package com.community.pocket.repository.android; package com.community.pocket.repository.android;
import com.community.pocket.entity.po.Notice; import com.community.pocket.entity.po.Notice;
import com.community.pocket.entity.vo.NoticeVo;
import com.community.pocket.repository.BaseDao; import com.community.pocket.repository.BaseDao;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; 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 org.springframework.stereotype.Repository;
import java.util.ArrayList;
import java.util.List; import java.util.List;
/** /**
@ -16,6 +20,18 @@ public class NoticeDao extends BaseDao<Notice> {
private static final Logger LOG = LoggerFactory.getLogger(NoticeDao.class); 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 @Override
public Notice save(Notice notice) { public Notice save(Notice notice) {
try { try {
@ -28,9 +44,20 @@ public class NoticeDao extends BaseDao<Notice> {
} }
//加载所有公告 //加载所有公告
public List<Notice> loadNotices(){ public List<NoticeVo> loadNotices() {
List<NoticeVo> noticeVos = new ArrayList<>();
try { try {
return mongoTemplate.findAll(Notice.class); 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) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
LOG.error(e.toString()); LOG.error(e.toString());
@ -38,6 +65,12 @@ public class NoticeDao extends BaseDao<Notice> {
return null; return null;
} }
//删除公告
public boolean deleteNotice(NoticeVo noticeVo) {
return mongoTemplate.remove(new Query(Criteria.where("id").is(noticeVo.getId())), entityClass()).wasAcknowledged();
}
@Override @Override
public Class<Notice> entityClass() { public Class<Notice> entityClass() {
return Notice.class; return Notice.class;

Loading…
Cancel
Save