Skip to content

Commit a76dd51

Browse files
committed
查看个人/他人的个人实例作品
1 parent 54400ec commit a76dd51

File tree

6 files changed

+80
-71
lines changed

6 files changed

+80
-71
lines changed

springboot-dubbo-api/src/main/java/com/lzq/api/dto/ExampleAccount.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public class ExampleAccount implements Serializable {
7979
@JsonProperty("favorites")
8080
private String favorites;
8181

82+
8283
@TableField(exist = false)
8384
@JsonProperty("myFavorites")
8485
private Boolean myFavorites=false;

springboot-dubbo-api/src/main/java/com/lzq/api/pojo/Example.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ public class Example implements Serializable {
6565
@TableField(value = "favorites")
6666
@JsonProperty("favorites")
6767
private String favorites;
68+
69+
@TableField(exist = false)
70+
@JsonProperty("myFavorites")
71+
private Boolean myFavorites=false;
6872
/**
6973
* 用户名
7074
*/

springboot-dubbo-api/src/main/java/com/lzq/api/service/ExampleService.java

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.lzq.api.service;
22

33

4+
import com.github.pagehelper.PageInfo;
45
import com.lzq.api.pojo.Example;
56

67
import java.util.List;
@@ -13,22 +14,6 @@
1314
*/
1415
public interface ExampleService {
1516

16-
/**
17-
* 通过ID查询单条数据
18-
*
19-
* @param exampleId 主键
20-
* @return 实例对象
21-
*/
22-
Example queryById(Integer exampleId);
23-
24-
/**
25-
* 查询多条数据
26-
*
27-
* @param offset 查询起始位置
28-
* @param limit 查询条数
29-
* @return 对象列表
30-
*/
31-
List<Example> queryAllByLimit(int offset, int limit);
3217

3318
/**
3419
* 新增数据
@@ -58,17 +43,21 @@ public interface ExampleService {
5843

5944
/**
6045
* 通过用户名查询实例
61-
* @param username
46+
* @param username 用户名
47+
* @param currentPage 当前页
6248
* @return
6349
*/
64-
List<Example> queryByAccount(String username);
50+
PageInfo<Example> queryByAccount(String username, Integer currentPage);
6551

6652

6753
/**
6854
* 查询用户公开的实例
69-
* @param username
55+
* @param username 用户名
56+
* @param currentPage 当前页
7057
* @return
7158
*/
72-
List<Example> queryByPublic(String username);
59+
PageInfo<Example> queryByPublic(String username,Integer currentPage);
60+
61+
7362

7463
}

springboot-dubbo-service/src/main/java/com/lzq/dubboservice/service/ExampleServiceImpl.java

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
44
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
5+
import com.github.pagehelper.Page;
6+
import com.github.pagehelper.PageHelper;
7+
import com.github.pagehelper.PageInfo;
58
import com.lzq.api.pojo.Example;
69
import com.lzq.api.service.ExampleService;
710
import com.lzq.dubboservice.mapper.ExampleMapper;
@@ -18,15 +21,6 @@
1821
@Component
1922
@Service(interfaceClass = ExampleService.class)
2023
public class ExampleServiceImpl extends ServiceImpl<ExampleMapper, Example> implements ExampleService {
21-
@Override
22-
public Example queryById(Integer exampleId) {
23-
return null;
24-
}
25-
26-
@Override
27-
public List<Example> queryAllByLimit(int offset, int limit) {
28-
return null;
29-
}
3024

3125
@Override
3226
public Boolean insert(Example example) {
@@ -44,17 +38,25 @@ public boolean deleteById(Integer exampleId) {
4438
}
4539

4640
@Override
47-
public List<Example> queryByAccount(String username) {
41+
public PageInfo<Example> queryByAccount(String username, Integer currentPage) {
42+
Page<Example> page = new Page<>();
4843
QueryWrapper<Example> wrapper = new QueryWrapper<>();
49-
wrapper.eq("username",username);
50-
return baseMapper.selectList(wrapper);
44+
wrapper.eq("username", username);
45+
//当前页和每页条数
46+
PageHelper.startPage(currentPage, 12);
47+
List<Example> list = baseMapper.selectList(wrapper);
48+
return new PageInfo<>(list);
5149
}
5250

5351
@Override
54-
public List<Example> queryByPublic(String username) {
52+
public PageInfo<Example> queryByPublic(String username, Integer currentPage) {
53+
Page<Example> page = new Page<>();
5554
QueryWrapper<Example> wrapper = new QueryWrapper<>();
56-
wrapper.eq("username",username);
57-
wrapper.eq("ispublic",0);
58-
return baseMapper.selectList(wrapper);
55+
wrapper.eq("username", username);
56+
wrapper.eq("ispublic", 0);
57+
//当前页和每页条数
58+
PageHelper.startPage(currentPage, 12);
59+
List<Example> list = baseMapper.selectList(wrapper);
60+
return new PageInfo<>(list);
5961
}
6062
}

springboot-dubbo-web/src/main/java/com/lzq/web/controller/ExampleController.java

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -141,32 +141,6 @@ public Map<String, Object> SaveExample(Example example, Content exampleContent,
141141
}
142142
}
143143

144-
/**
145-
* 根据用户名查询全部实例
146-
* @return
147-
*/
148-
@GetMapping("/getExample")
149-
@ApiOperation("查询个人全部实例")
150-
public Map<String,Object> getExample(HttpServletRequest request){
151-
//获取用户名
152-
String username = JWTUtils.verify(request.getHeader("token"))
153-
.getClaim("username").asString();
154-
List<Example> list = exampleService.queryByAccount(username);
155-
return ResultMapUtils.ResultMap(true,0,list);
156-
}
157-
158-
/**
159-
* 获取其他用户的公开实例
160-
* @param username
161-
* @return
162-
*/
163-
@GetMapping("/getPublicExample")
164-
@ApiOperation("获取其他用户的公开实例")
165-
public Map<String,Object> getPublicExample(String username){
166-
List<Example> list = exampleService.queryByPublic(username);
167-
return ResultMapUtils.ResultMap(true,0,list);
168-
}
169-
170144
/**
171145
* 添加喜爱
172146
* @param favorites

springboot-dubbo-web/src/main/java/com/lzq/web/controller/SearchController.java renamed to springboot-dubbo-web/src/main/java/com/lzq/web/controller/QueryController.java

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
import com.github.pagehelper.PageInfo;
44
import com.lzq.api.dto.AccountResult;
55
import com.lzq.api.dto.ExampleAccount;
6+
import com.lzq.api.pojo.Account;
7+
import com.lzq.api.pojo.Example;
68
import com.lzq.api.service.AccountResultService;
79
import com.lzq.api.service.ExampleAccountService;
10+
import com.lzq.api.service.ExampleService;
811
import com.lzq.web.utils.JWTUtils;
912
import com.lzq.web.utils.ResultMapUtils;
1013
import io.swagger.annotations.Api;
@@ -15,6 +18,7 @@
1518
import org.springframework.data.redis.core.RedisTemplate;
1619
import org.springframework.web.bind.annotation.GetMapping;
1720
import org.springframework.web.bind.annotation.RequestMapping;
21+
import org.springframework.web.bind.annotation.RequestParam;
1822
import org.springframework.web.bind.annotation.RestController;
1923

2024
import javax.servlet.http.HttpServletRequest;
@@ -28,20 +32,22 @@
2832
*/
2933
@Slf4j
3034
@RestController
31-
@RequestMapping("/search")
35+
@RequestMapping("/query")
3236
@Api(value = "搜索接口", description = "搜索接口")
33-
public class SearchController {
37+
public class QueryController {
3438

3539
@Reference
3640
private AccountResultService accountResultService;
3741

3842
@Reference
3943
private ExampleAccountService exampleAccountService;
4044

45+
@Reference
46+
private ExampleService exampleService;
47+
4148
@Autowired
4249
private RedisTemplate redisTemplate;
4350

44-
4551
/**
4652
* 根据用户名查询用户信息
4753
*
@@ -55,7 +61,6 @@ public Map<String, Object> queryByUsername(String username) {
5561
return ResultMapUtils.ResultMap(true, 0, result);
5662
}
5763

58-
5964
/**
6065
* 获取关注列表
6166
*
@@ -65,7 +70,7 @@ public Map<String, Object> queryByUsername(String username) {
6570
*/
6671
@GetMapping("/getFollow")
6772
@ApiOperation("获取关注列表")
68-
public Map<String, Object> getFollowList(HttpServletRequest request, AccountResult result, Integer currentPage) {
73+
public Map<String, Object> getFollowList(HttpServletRequest request, AccountResult result, @RequestParam(defaultValue = "1") Integer currentPage) {
6974
String username = null;
7075
PageInfo<AccountResult> list = null;
7176
//判断用户是否登录
@@ -117,7 +122,7 @@ public Map<String, Object> getFollowList(HttpServletRequest request, AccountResu
117122
*/
118123
@GetMapping("/getFan")
119124
@ApiOperation("获取粉丝列表")
120-
public Map<String, Object> getFanList(HttpServletRequest request, AccountResult result, Integer currentPage) {
125+
public Map<String, Object> getFanList(HttpServletRequest request, AccountResult result, @RequestParam(defaultValue = "1") Integer currentPage) {
121126
String username = null;
122127
PageInfo<AccountResult> list = null;
123128
//判断用户是否登录
@@ -165,7 +170,6 @@ public Map<String, Object> getFanList(HttpServletRequest request, AccountResult
165170
return ResultMapUtils.ResultMap(true, 0, list);
166171
}
167172

168-
169173
/**
170174
* 根据实例名查询实例
171175
*
@@ -176,7 +180,7 @@ public Map<String, Object> getFanList(HttpServletRequest request, AccountResult
176180
*/
177181
@GetMapping("/queryByExampleName")
178182
@ApiOperation("根据实例名查询实例")
179-
public Map<String, Object> queryByExampleName(HttpServletRequest request, String exampleName, Integer currentPage) {
183+
public Map<String, Object> queryByExampleName(HttpServletRequest request, String exampleName, @RequestParam(defaultValue = "1") Integer currentPage) {
180184
String username = null;
181185
if (request.getHeader("token") != null) {
182186
//获取token中的用户名
@@ -188,8 +192,10 @@ public Map<String, Object> queryByExampleName(HttpServletRequest request, String
188192
List<ExampleAccount> list = pageInfo.getList();
189193
//当用户不登陆时不需要进行任何操作查询数据直接返回
190194
if (username != null) {
195+
//获取redis缓存中所关注的用户名列表
191196
List<String> followlist = redisTemplate.opsForList().range(username, 0, -1);
192-
List<String> favoriteslist = redisTemplate.opsForList().range(username + "fav", 0, -1);
197+
//获取redis缓存中所喜欢的实例id列表
198+
List<Integer> favoriteslist = redisTemplate.opsForList().range(username + "fav", 0, -1);
193199
//遍历修改数组
194200
for (ExampleAccount exampleAccount : list) {
195201
//当该用户被当前用户关注时设置为true,否则为false
@@ -208,4 +214,37 @@ public Map<String, Object> queryByExampleName(HttpServletRequest request, String
208214
return ResultMapUtils.ResultMap(true, 0, pageInfo);
209215
}
210216

217+
/**
218+
* 根据用户名查询全部实例
219+
*
220+
* @return
221+
*/
222+
@GetMapping("/getExample")
223+
@ApiOperation("查询个人全部实例")
224+
public Map<String, Object> getExample(HttpServletRequest request, Account account, @RequestParam(defaultValue = "1") Integer currentPage) {
225+
String username = null;
226+
PageInfo<Example> list;
227+
if (request.getHeader("token") != null) {
228+
//获取token中的用户名
229+
username = JWTUtils.verify(request.getHeader("token"))
230+
.getClaim("username").toString();
231+
}
232+
//用户名相同则查询自己的实例,不同则查询他人的公开实例
233+
if (username.equals(account.getUsername())) {
234+
list = exampleService.queryByAccount(account.getUsername(),currentPage);
235+
} else {
236+
//获取redis缓存中所喜欢的实例id列表
237+
List<Integer> favoriteslist = redisTemplate.opsForList().range(username + "fav", 0, -1);
238+
list = exampleService.queryByPublic(account.getUsername(),currentPage);
239+
//获取实例集合
240+
List<Example> exampleList = list.getList();
241+
for (Example example : exampleList) {
242+
if (favoriteslist.contains(example.getExampleId())){
243+
example.setMyFavorites(true);
244+
}
245+
}
246+
}
247+
return ResultMapUtils.ResultMap(true, 0, list);
248+
}
249+
211250
}

0 commit comments

Comments
 (0)