Skip to content

Commit

Permalink
#小版本先上传
Browse files Browse the repository at this point in the history
  • Loading branch information
cicicc committed Aug 26, 2018
1 parent 72c56de commit 03aeb19
Show file tree
Hide file tree
Showing 15 changed files with 333 additions and 24 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<groupId>cn.indispensable</groupId>
<artifactId>future</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<packaging>war</packaging>

<name>future</name>
<description>一个仿照知乎书写的项目--逼乎,来到这里,和世界分享你刚编的故事吧!</description>
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/cn/indispensable/future/async/EventType.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ public enum EventType {
LOGIN(2),
MAIL(3),
unFOLLOW(4),
FOLLOW(5);
FOLLOW(5),
FEED(6);

private int value;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.indispensable.future.controller;

import cn.indispensable.future.model.EntityType;
import cn.indispensable.future.model.Feed;
import cn.indispensable.future.model.HostHolder;
import cn.indispensable.future.service.FeedService;
import cn.indispensable.future.service.FollowService;
import cn.indispensable.future.service.JedisService;
import cn.indispensable.future.utils.RedisKeyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.List;

/**
* 新鲜事的controller层
* @author cicicc
* @since 0.0.1
*/
@Controller
public class FeedController {
@Autowired
private FeedService feedService;
@Autowired
private FollowService followService;
@Autowired
private JedisService jedisService;
@Autowired
private HostHolder hostHolder;

/**
* 使用推的方式获取新鲜事
* @return 新鲜事页面
*/
@RequestMapping(path = {"/pushfeeds"}, method = {RequestMethod.GET, RequestMethod.POST})
private String getPushFeeds(Model model) {
int localUserId = hostHolder.getUser() != null ? hostHolder.getUser().getId() : 0;
List<String> feedIds = jedisService.lrange(RedisKeyUtils.getTimelineKey(localUserId), 0, 10);
List<Feed> feeds = new ArrayList<>();
for (String feedId : feedIds) {
Feed feed = feedService.getFeedById(Integer.parseInt(feedId));
if (feed != null) {
feeds.add(feed);
}
}
model.addAttribute("feeds", feeds);
return "feeds";
}
/**
* 使用拉的方式获取新鲜事
* @return 新鲜事页面
*/
@RequestMapping(path = {"/pullfeeds"}, method = {RequestMethod.GET, RequestMethod.POST})
private String getPullFeeds(Model model) {
int localUserId = hostHolder.getUser() != null ? hostHolder.getUser().getId() : 0;
List<Integer> followees = new ArrayList<>();
if (localUserId != 0) {
// 关注的人
followees = followService.getFollowees(localUserId, EntityType.ENTITY_USER, Integer.MAX_VALUE);
}
List<Feed> feeds = feedService.getUserFeeds(Integer.MAX_VALUE, followees, 10);
model.addAttribute("feeds", feeds);
return "feeds";
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@

import cn.indispensable.future.async.EventModel;
import cn.indispensable.future.async.EventProducer;
import cn.indispensable.future.model.EntityType;
import cn.indispensable.future.model.HostHolder;
import cn.indispensable.future.model.User;
import cn.indispensable.future.model.ViewObject;
import cn.indispensable.future.async.EventType;
import cn.indispensable.future.model.*;
import cn.indispensable.future.service.FollowService;
import cn.indispensable.future.service.QuestionService;
import cn.indispensable.future.service.UserService;
import cn.indispensable.future.utils.JSONUtils;
import org.slf4j.Logger;
Expand All @@ -46,6 +45,8 @@ public class FellowController {
@Autowired
private FollowService followService;
@Autowired
private EventProducer eventProducer;
@Autowired
private HostHolder hostHolder;
@Autowired
private UserService userService;
Expand Down Expand Up @@ -87,11 +88,15 @@ public String toRecommendFollow(Model model) {
public String followQuestion(@RequestParam("questionId")int questionId) {
try {
User user = hostHolder.getUser();

if (user == null) {
return JSONUtils.getJSONString(999);
}else {
boolean follow = followService.follow(user.getId(), EntityType.ENTITY_QUESTION, questionId);
// new EventProducer().fireEvent(new EventModel());
//用户关注问题后将该情况加入新鲜事中
eventProducer.fireEvent(new EventModel().setActorId(hostHolder.getUser().getId())
.setEntityType(EntityType.ENTITY_QUESTION).setType(EventType.FEED)
.setEntityId(questionId));
Map<String, Object> info = new HashMap<>();
info.put("headUrl", hostHolder.getUser().getHeadUrl());
info.put("name", hostHolder.getUser().getName());
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/cn/indispensable/future/dao/FeedDAO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package cn.indispensable.future.dao;

import cn.indispensable.future.model.Comment;
import cn.indispensable.future.model.Feed;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface FeedDAO {
/**
* CREATE TABLE `feed` (
* `id` int(11) NOT NULL AUTO_INCREMENT,
* `created_date` datetime DEFAULT NULL,
* `user_id` int(11) DEFAULT NULL,
* `data` tinytext,
* `type` int(11) DEFAULT NULL,
* PRIMARY KEY (`id`),
* KEY `user_index` (`user_id`)
* ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
*/
String TABLE_NAME = "feed";
String INSERT_FIELDS = " user_id, data, type, created_date ";
String SELECT_FIELDS = "id, " + INSERT_FIELDS;

@Insert({"insert into", TABLE_NAME, "(", INSERT_FIELDS, ") values( #{userId}, #{data}, #{type}, #{createdDate})"})
int addFeed(Feed feed);

@Select({"select ",SELECT_FIELDS,"from ",TABLE_NAME, "where id =#{id} "})
Feed selectFeedById(@Param("id")int id);


List<Feed> selectUserFeeds(@Param("maxId") int maxId,
@Param("userIds") List<Integer> userIds,
@Param("count") int count);

}
26 changes: 26 additions & 0 deletions src/main/java/cn/indispensable/future/dao/FeedDAO.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="cn.indispensable.future.dao.FeedDAO">
<sql id="table">feed</sql>
<sql id="selectFields">id, created_date,user_id, data, type
</sql>
<select id="selectUserFeeds" resultType="cn.indispensable.future.model.Feed">
SELECT
<include refid="selectFields"/>
FROM
<include refid="table"/>

WHERE id &lt; #{maxId}

<if test="userIds.size() != 0">
AND user_id in
<foreach item="item" index="index" collection="userIds"
open="(" separator="," close=")">
#{item}
</foreach>
</if>
ORDER BY created_date DESC
LIMIT #{count}
</select>
</mapper>
83 changes: 83 additions & 0 deletions src/main/java/cn/indispensable/future/model/Feed.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.indispensable.future.model;

import com.alibaba.fastjson.JSONObject;

import java.util.Date;

/**
* 新鲜事对应的pojo类
* @author cicicc
* @since 0.0.1
*/
public class Feed {
private int id;
private Date createdDate;
private int userId;
private String data;
private int type;
private JSONObject dataJSON = null;

public Feed() {
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public Date getCreatedDate() {
return createdDate;
}

public void setCreatedDate(Date createdDate) {
this.createdDate = createdDate;
}

public int getUserId() {
return userId;
}

public void setUserId(int userId) {
this.userId = userId;
}

public String getData() {
return data;
}

public void setData(String data) {
this.data = data;
this.dataJSON = JSONObject.parseObject(data);
}

public int getType() {
return type;
}

public void setType(int type) {
this.type = type;
}

public String get(String key) {
return dataJSON == null ? null : dataJSON.getString(key);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,4 @@ public int addComment(Comment comment) {
return commentDAO.addComment(comment);
}

public static void main(String[] args) {
CommentService commentService = new CommentService();
List<Comment> comments = commentService.SelectLatestComment(34, 1, 0, 10);
System.out.println(comments);
}

}
47 changes: 47 additions & 0 deletions src/main/java/cn/indispensable/future/service/FeedService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.indispensable.future.service;

import cn.indispensable.future.dao.FeedDAO;
import cn.indispensable.future.model.Feed;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* 新鲜事的服务层,
* @author cicicc
* @since 0.0.1
*/
@Service
public class FeedService {
@Autowired
private FeedDAO feedDAO;


public Feed getFeedById(int id) {
return feedDAO.selectFeedById(id);
}

public List<Feed> getUserFeeds(int maxValue, List<Integer> followeeIds, int count) {
return feedDAO.selectUserFeeds(maxValue, followeeIds, count);
}

public int addFeed(Feed feed) {
return feedDAO.addFeed(feed);
}
}
20 changes: 15 additions & 5 deletions src/main/java/cn/indispensable/future/service/JedisService.java
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,20 @@ public long zrem(String key, String value) {
return 0;
}

public static void main(String[] args) {
Jedis jedis = new Jedis("redis://localhost:6379/6");
jedis.append("name", "oldchen");
System.out.println(jedis.get("name"));
jedis.close();

public List<String> lrange(String key, int start, int end) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.lrange(key, start, end);
} catch (Exception e) {
logger.error("发生异常" + e.getMessage());
} finally {
if (jedis != null) {
jedis.close();
}
}
return null;

}
}
Loading

0 comments on commit 03aeb19

Please sign in to comment.