Skip to content
This repository has been archived by the owner on Aug 19, 2023. It is now read-only.

Commit

Permalink
feat: implement /relation/list interface
Browse files Browse the repository at this point in the history
  • Loading branch information
jaxvanyang authored and Crazyokd committed Mar 11, 2023
1 parent c259b60 commit cda649d
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,10 @@ public ResponseJson add(@RequestBody Relation relation, HttpSession session) {
public ResponseJson update(@RequestBody Relation relation, HttpSession session) {
return relationService.updateRelation(relation, session);
}

@GetMapping(value = "list")
@ResponseBody
public ResponseJson list(@RequestParam int type, int status, HttpSession session) {
return relationService.listRelation(type, status, session);
}
}
7 changes: 7 additions & 0 deletions src/main/java/com/sxrekord/chatting/dao/RelationDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,11 @@ public interface RelationDao {
* @return
*/
int updateRelation(Relation relation);

/**
* 列出关系
* @param relation
* @return
*/
List<Long> listRelation(Relation relation);
}
6 changes: 6 additions & 0 deletions src/main/java/com/sxrekord/chatting/model/po/Relation.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,10 @@ public class Relation {
private Integer status;

public Relation() {}

public Relation(Long acceptId, Integer type, Integer status) {
this.acceptId = acceptId;
this.type = type;
this.status = status;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,12 @@ public interface RelationService {
* @return
*/
ResponseJson updateRelation(Relation relation, HttpSession httpSession);

/**
* 根据type和status列出所有关系
* @param type
* @param status
* @return
*/
ResponseJson listRelation(int type, int status, HttpSession session);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@

import com.sxrekord.chatting.dao.GroupDao;
import com.sxrekord.chatting.dao.RelationDao;
import com.sxrekord.chatting.dao.UserDao;
import com.sxrekord.chatting.model.po.Group;
import com.sxrekord.chatting.model.po.Relation;
import com.sxrekord.chatting.model.po.User;
import com.sxrekord.chatting.model.vo.ResponseJson;
import com.sxrekord.chatting.service.RelationService;
import com.sxrekord.chatting.util.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
* @author Rekord
Expand All @@ -23,6 +28,9 @@ public class RelationServiceImpl implements RelationService {
@Autowired
GroupDao groupDao;

@Autowired
UserDao userDao;

@Override
public ResponseJson createRelation(Relation relation, HttpSession session) {
ResponseJson responseJson = new ResponseJson();
Expand Down Expand Up @@ -51,4 +59,25 @@ public ResponseJson updateRelation(Relation relation, HttpSession session) {
return responseJson.success();
}

@Override
public ResponseJson listRelation(int type, int status, HttpSession session) {
ResponseJson responseJson = new ResponseJson();
Object acceptId = session.getAttribute(Constant.USER_TOKEN);
List<Long> ids = relationDao.listRelation(new Relation((long)acceptId, type, status));
if (type == 0) {
List<User> friends = new ArrayList<>();
for (Long id : ids) {
friends.add(this.userDao.getUserById(id));
}
responseJson.setData("friends", friends);
} else {
List<Group> groups = new ArrayList<>();
for (Long id : ids) {
groups.add(this.groupDao.getGroupById(id));
}
responseJson.setData("groups", groups);
}
return responseJson.success();
}

}
12 changes: 11 additions & 1 deletion src/main/resources/mapper/relation-mapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,14 @@
</if>
);
</update>
</mapper>

<select id="listRelation" resultType="java.lang.Long">
select request_id from relation
where type = #{type} and status = #{status} and accept_id = #{acceptId}
<if test="type == 0 and status == 1">
union
select accept_id from relation
where type = #{type} and status = #{status} and request_id = #{acceptId};
</if>
</select>
</mapper>

0 comments on commit cda649d

Please sign in to comment.