Skip to content

Commit 73a87b8

Browse files
committed
2 parents 6da2ad5 + 7e69b12 commit 73a87b8

File tree

15 files changed

+488
-37
lines changed

15 files changed

+488
-37
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.jackson0714.common.to.es;
2+
3+
import lombok.Data;
4+
5+
/**
6+
* @Author: 公众号 | 悟空聊架构
7+
* @Date: 2021/3/31 15:16
8+
* @Site: www.passjava.cn
9+
* @Github: https://github.com/Jackson0714/PassJava-Platform
10+
*/
11+
12+
/*
13+
"properties": {
14+
"id": {
15+
"type": "long"
16+
}
17+
"title": {
18+
"type": "keyword",
19+
"analyzer": "ik_smart"
20+
},
21+
"answer": : {
22+
"type": "keyword",
23+
"analyzer": "ik_smart"
24+
},
25+
"typeName": {
26+
"type": "keyword",
27+
},
28+
}
29+
* */
30+
@Data
31+
public class QuestionEsModel {
32+
33+
private Long id;
34+
private String title;
35+
private String answer;
36+
private String typeName;
37+
38+
}

passjava-question/src/main/java/com/jackson0714/passjava/question/controller/QuestionController.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import org.springframework.web.bind.annotation.RestController;
1212

1313
import com.jackson0714.passjava.question.entity.QuestionEntity;
14-
import com.jackson0714.passjava.question.service.QuestionService;
14+
import com.jackson0714.passjava.question.service.IQuestionService;
1515
import com.jackson0714.common.utils.PageUtils;
1616
import com.jackson0714.common.utils.R;
1717

@@ -29,7 +29,7 @@
2929
@RequestMapping("question/v1/admin/question")
3030
public class QuestionController {
3131
@Autowired
32-
private QuestionService questionService;
32+
private IQuestionService IQuestionService;
3333

3434
/**
3535
* 列表
@@ -38,7 +38,7 @@ public class QuestionController {
3838
//@RequiresPermissions("question:question:list")
3939
public R list(@RequestParam Map<String, Object> params){
4040
long time = System.currentTimeMillis();
41-
PageUtils page = questionService.queryPage(params);
41+
PageUtils page = IQuestionService.queryPage(params);
4242
System.out.println("耗时:"+ (System.currentTimeMillis() - time));
4343
return R.ok().put("page", page);
4444
}
@@ -50,7 +50,7 @@ public R list(@RequestParam Map<String, Object> params){
5050
@RequestMapping("/info/{id}")
5151
//@RequiresPermissions("question:question:info")
5252
public R info(@PathVariable("id") Long id) {
53-
QuestionEntity question = questionService.getById(id);
53+
QuestionEntity question = IQuestionService.getById(id);
5454
return R.ok().put("question", question);
5555
}
5656

@@ -60,7 +60,7 @@ public R info(@PathVariable("id") Long id) {
6060
@RequestMapping("/save")
6161
//@RequiresPermissions("question:question:save")
6262
public R save(@Valid @RequestBody QuestionEntity question){
63-
questionService.save(question);
63+
IQuestionService.saveQuestion(question);
6464

6565
return R.ok();
6666
}
@@ -71,8 +71,7 @@ public R save(@Valid @RequestBody QuestionEntity question){
7171
@RequestMapping("/update")
7272
//@RequiresPermissions("question:question:update")
7373
public R update(@RequestBody QuestionEntity question){
74-
questionService.updateById(question);
75-
74+
IQuestionService.updateQuestion(question);
7675

7776
return R.ok();
7877
}
@@ -83,7 +82,7 @@ public R update(@RequestBody QuestionEntity question){
8382
@RequestMapping("/delete")
8483
//@RequiresPermissions("question:question:delete")
8584
public R delete(@RequestBody Long[] ids){
86-
questionService.removeByIds(Arrays.asList(ids));
85+
IQuestionService.removeByIds(Arrays.asList(ids));
8786

8887
return R.ok();
8988
}

passjava-question/src/main/java/com/jackson0714/passjava/question/controller/TypeController.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import org.springframework.web.bind.annotation.RestController;
1212

1313
import com.jackson0714.passjava.question.entity.TypeEntity;
14-
import com.jackson0714.passjava.question.service.TypeService;
14+
import com.jackson0714.passjava.question.service.ITypeService;
1515
import com.jackson0714.common.utils.PageUtils;
1616
import com.jackson0714.common.utils.R;
1717

@@ -28,15 +28,15 @@
2828
@RequestMapping("question/type")
2929
public class TypeController {
3030
@Autowired
31-
private TypeService typeService;
31+
private ITypeService ITypeService;
3232

3333
/**
3434
* 列表
3535
*/
3636
@RequestMapping("/list")
3737
//@RequiresPermissions("question:type:list")
3838
public R list(@RequestParam Map<String, Object> params){
39-
PageUtils page = typeService.queryPage(params);
39+
PageUtils page = ITypeService.queryPage(params);
4040

4141
return R.ok().put("page", page);
4242
}
@@ -48,7 +48,7 @@ public R list(@RequestParam Map<String, Object> params){
4848
@RequestMapping("/info/{id}")
4949
//@RequiresPermissions("question:type:info")
5050
public R info(@PathVariable("id") Long id){
51-
TypeEntity type = typeService.getById(id);
51+
TypeEntity type = ITypeService.getById(id);
5252

5353
return R.ok().put("type", type);
5454
}
@@ -59,7 +59,7 @@ public R info(@PathVariable("id") Long id){
5959
@RequestMapping("/save")
6060
//@RequiresPermissions("question:type:save")
6161
public R save(@RequestBody TypeEntity type){
62-
typeService.save(type);
62+
ITypeService.save(type);
6363

6464
return R.ok();
6565
}
@@ -70,7 +70,7 @@ public R save(@RequestBody TypeEntity type){
7070
@RequestMapping("/update")
7171
//@RequiresPermissions("question:type:update")
7272
public R update(@RequestBody TypeEntity type){
73-
typeService.updateById(type);
73+
ITypeService.updateById(type);
7474

7575
return R.ok();
7676
}
@@ -81,7 +81,7 @@ public R update(@RequestBody TypeEntity type){
8181
@RequestMapping("/delete")
8282
//@RequiresPermissions("question:type:delete")
8383
public R delete(@RequestBody Long[] ids){
84-
typeService.removeByIds(Arrays.asList(ids));
84+
ITypeService.removeByIds(Arrays.asList(ids));
8585

8686
return R.ok();
8787
}

passjava-question/src/main/java/com/jackson0714/passjava/question/exception/PassjavaExceptionControllerAdvice.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class PassjavaExceptionControllerAdvice {
2323
@ExceptionHandler(value= MethodArgumentNotValidException.class)
2424
public R handleValidException(MethodArgumentNotValidException e) {
2525
//log.error("数据校验出现问题{},异常类型:{}", e.getMessage(), e.getClass());
26+
System.out.println(e.getMessage());
2627
BindingResult bindingResult = e.getBindingResult();
2728
Map<String, String> errorMap = new HashMap<>();
2829
bindingResult.getFieldErrors().forEach((fieldError)->{
@@ -35,6 +36,7 @@ public R handleValidException(MethodArgumentNotValidException e) {
3536
@ExceptionHandler(value=Throwable.class)
3637
public R handleException(Throwable throwable) {
3738
//log.error("未知异常{},异常类型:{}", throwable.getMessage(), throwable.getClass());
39+
System.out.println(throwable.getMessage());
3840
return R.error(BizCodeEnum.UNKNOWN_EXCEPTION.getCode(), BizCodeEnum.UNKNOWN_EXCEPTION.getMsg());
3941
}
4042
}

passjava-question/src/main/java/com/jackson0714/passjava/question/service/QuestionService.java renamed to passjava-question/src/main/java/com/jackson0714/passjava/question/service/IQuestionService.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@
1313
* @email jackson0585@163.com
1414
* @date 2020-04-25 22:34:04
1515
*/
16-
public interface QuestionService extends IService<QuestionEntity> {
16+
public interface IQuestionService extends IService<QuestionEntity> {
1717

1818
PageUtils queryPage(Map<String, Object> params);
19+
20+
boolean saveQuestion(QuestionEntity question);
21+
22+
boolean updateQuestion(QuestionEntity question);
1923
}
2024

passjava-question/src/main/java/com/jackson0714/passjava/question/service/TypeService.java renamed to passjava-question/src/main/java/com/jackson0714/passjava/question/service/ITypeService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* @email jackson0585@163.com
1414
* @date 2020-04-25 22:34:04
1515
*/
16-
public interface TypeService extends IService<TypeEntity> {
16+
public interface ITypeService extends IService<TypeEntity> {
1717

1818
PageUtils queryPage(Map<String, Object> params);
1919
}

passjava-question/src/main/java/com/jackson0714/passjava/question/service/impl/QuestionServiceImpl.java

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package com.jackson0714.passjava.question.service.impl;
22

3+
import com.jackson0714.common.to.es.QuestionEsModel;
4+
import com.jackson0714.passjava.question.entity.TypeEntity;
5+
import com.jackson0714.passjava.question.service.ITypeService;
36
import org.apache.commons.lang.StringUtils;
7+
import org.springframework.beans.BeanUtils;
8+
import org.springframework.beans.factory.annotation.Autowired;
49
import org.springframework.stereotype.Service;
510
import java.util.Map;
611
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
@@ -11,11 +16,14 @@
1116

1217
import com.jackson0714.passjava.question.dao.QuestionDao;
1318
import com.jackson0714.passjava.question.entity.QuestionEntity;
14-
import com.jackson0714.passjava.question.service.QuestionService;
19+
import com.jackson0714.passjava.question.service.IQuestionService;
1520

1621

1722
@Service("questionService")
18-
public class QuestionServiceImpl extends ServiceImpl<QuestionDao, QuestionEntity> implements QuestionService {
23+
public class QuestionServiceImpl extends ServiceImpl<QuestionDao, QuestionEntity> implements IQuestionService {
24+
25+
@Autowired
26+
ITypeService ITypeService;
1927

2028
@Override
2129
public PageUtils queryPage(Map<String, Object> params) {
@@ -38,4 +46,36 @@ public PageUtils queryPage(Map<String, Object> params) {
3846
return new PageUtils(page);
3947
}
4048

49+
@Override
50+
public boolean saveQuestion(QuestionEntity question) {
51+
boolean saveResult = save(question);
52+
saveEs(question);
53+
54+
return true;
55+
}
56+
57+
@Override
58+
public boolean updateQuestion(QuestionEntity question) {
59+
updateById(question);
60+
saveEs(question);
61+
return false;
62+
}
63+
64+
private boolean saveEs(QuestionEntity question) {
65+
// 1.创建 ES model
66+
QuestionEsModel esModel = new QuestionEsModel();
67+
// 2.复制属性
68+
// 2.1 复制属性
69+
BeanUtils.copyProperties(question, esModel);
70+
// 2.2 获取“题目类型”的名称
71+
TypeEntity typeEntity = ITypeService.getById(question.getType());
72+
String typeName = typeEntity.getType();
73+
// 2.3 给 ES model 的“类型”字段赋值
74+
esModel.setTypeName(typeName);
75+
// 3.
76+
System.out.println("-----------------esModel:" + esModel);
77+
78+
return false;
79+
}
80+
4181
}

passjava-question/src/main/java/com/jackson0714/passjava/question/service/impl/TypeServiceImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010

1111
import com.jackson0714.passjava.question.dao.TypeDao;
1212
import com.jackson0714.passjava.question.entity.TypeEntity;
13-
import com.jackson0714.passjava.question.service.TypeService;
13+
import com.jackson0714.passjava.question.service.ITypeService;
1414

1515

1616
@Service("typeService")
17-
public class TypeServiceImpl extends ServiceImpl<TypeDao, TypeEntity> implements TypeService {
17+
public class TypeServiceImpl extends ServiceImpl<TypeDao, TypeEntity> implements ITypeService {
1818

1919
@Override
2020
public PageUtils queryPage(Map<String, Object> params) {

passjava-question/src/test/java/com/jackson0714/passjava/question/PassjavaQuestionApplicationTests.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,25 @@
22

33
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
44
import com.jackson0714.passjava.question.entity.TypeEntity;
5-
import com.jackson0714.passjava.question.service.TypeService;
5+
import com.jackson0714.passjava.question.service.ITypeService;
66
import org.junit.jupiter.api.Test;
77
import org.springframework.beans.factory.annotation.Autowired;
88
import org.springframework.boot.test.context.SpringBootTest;
99

10-
import java.io.ByteArrayInputStream;
1110
import java.util.List;
1211

1312
@SpringBootTest
1413
class PassjavaQuestionApplicationTests {
1514

1615
@Autowired
17-
TypeService typeService;
16+
ITypeService ITypeService;
1817

1918
// 创建题目类型
2019
@Test
2120
void testCreateType() {
2221
TypeEntity typeEntity = new TypeEntity();
2322
typeEntity.setType("javaBasic");
24-
typeService.save(typeEntity);
23+
ITypeService.save(typeEntity);
2524
System.out.println("创建成功");
2625
}
2726

@@ -31,14 +30,14 @@ void testUpdateType() {
3130
TypeEntity typeEntity = new TypeEntity();
3231
typeEntity.setId(1L);
3332
typeEntity.setType("jvm");
34-
typeService.updateById(typeEntity);
33+
ITypeService.updateById(typeEntity);
3534
System.out.println("修改成功");
3635
}
3736

3837
// 查询题目类型
3938
@Test
4039
void testSelectType() {
41-
List<TypeEntity> typeEntityList = typeService.list(new QueryWrapper<TypeEntity>().eq("id",1L));
40+
List<TypeEntity> typeEntityList = ITypeService.list(new QueryWrapper<TypeEntity>().eq("id",1L));
4241
typeEntityList.forEach((item)-> {
4342
System.out.println(item);
4443
});
@@ -48,7 +47,7 @@ void testSelectType() {
4847
// 删除题目类型记录
4948
@Test
5049
void testRemoveType() {
51-
typeService.removeById(1L);
50+
ITypeService.removeById(1L);
5251
System.out.println("删除成功");
5352
}
5453
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.jackson0714.passjava.search.config;
2+
3+
/**
4+
* @Author: 公众号 | 悟空聊架构
5+
* @Date: 2021/3/31 18:09
6+
* @Site: www.passjava.cn
7+
* @Github: https://github.com/Jackson0714/PassJava-Platform
8+
*/
9+
public class EsConstant {
10+
11+
public static final String QUESTION_INDEX = "question"; // 题目数据在 ES 中的索引
12+
}

0 commit comments

Comments
 (0)