fix:诗词的后端

上级 c103bd9e
package com.kwan.springbootkwan.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kwan.springbootkwan.entity.AphorismPoetry;
import com.kwan.springbootkwan.entity.Result;
import com.kwan.springbootkwan.entity.dto.AphorismPoetryDTO;
import com.kwan.springbootkwan.entity.query.AphorismPoetryQuery;
import com.kwan.springbootkwan.service.AphorismPoetryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* 名言警句(AphorismPoetry)表控制层
*
* @author makejava
* @since 2023-10-09 11:13:12
*/
@RestController
@RequestMapping("aphorismPoetry")
public class AphorismPoetryController {
/**
* 服务对象
*/
@Resource
private AphorismPoetryService aphorismPoetryService;
/**
* 分页查询所有数据
*
* @return 所有数据
*/
@GetMapping("/page")
public Result selectAll(@RequestParam Integer page
, @RequestParam Integer pageSize
, @RequestParam String poetryText) {
Page<AphorismPoetry> pageParm = new Page<>();
pageParm.setCurrent(page);
pageParm.setSize(pageSize);
QueryWrapper<AphorismPoetry> wrapper = new QueryWrapper<>();
wrapper.orderByDesc("id");
wrapper.eq("is_delete", 0);
if (StringUtils.isNotEmpty(poetryText)) {
wrapper.like("poetry_text", poetryText);
}
return Result.ok(AphorismPoetryDTO.Converter.INSTANCE.from(this.aphorismPoetryService.page(pageParm, wrapper)));
}
/**
* 随机一题
*/
@GetMapping("/random")
public Result random() {
QueryWrapper<AphorismPoetry> wrapper = new QueryWrapper<>();
wrapper.eq("is_delete", 0);
wrapper.orderByAsc("rand()").last("limit 1");
return Result.ok(AphorismPoetryDTO.Converter.INSTANCE.from(this.aphorismPoetryService.getOne(wrapper)));
}
/**
* 新增问题
*
* @return 所有数据
*/
@PostMapping("/add")
public Result add(@RequestBody AphorismPoetryQuery poetryQuery) {
final Integer addType = poetryQuery.getAddType();
final String poetryText = poetryQuery.getPoetryText();
if (StringUtils.isEmpty(poetryText)) {
return Result.error("内容不能为空");
}
//批量添加
if (addType == 1) {
final String[] split = poetryText.split("\n");
for (String str : split) {
str = str.trim().replace("- ", "");
if (StringUtils.isEmpty(str)) {
continue;
}
AphorismPoetry algorithmicProblem = new AphorismPoetry();
QueryWrapper<AphorismPoetry> wrapper = new QueryWrapper<>();
wrapper.eq("poetry_text", str);
wrapper.eq("is_delete", 0);
final AphorismPoetry one = this.aphorismPoetryService.getOne(wrapper);
if (one == null) {
BeanUtils.copyProperties(poetryQuery, algorithmicProblem);
algorithmicProblem.setPoetryText(str);
this.aphorismPoetryService.save(algorithmicProblem);
}
}
} else {
AphorismPoetry aphorismPoetry = new AphorismPoetry();
QueryWrapper<AphorismPoetry> wrapper = new QueryWrapper<>();
wrapper.eq("poetry_text", poetryText);
wrapper.eq("is_delete", 0);
final AphorismPoetry one = this.aphorismPoetryService.getOne(wrapper);
if (one == null) {
BeanUtils.copyProperties(poetryQuery, aphorismPoetry);
this.aphorismPoetryService.save(aphorismPoetry);
return Result.ok();
} else {
return Result.error("该诗词已存在");
}
}
return Result.ok();
}
/**
* 更新面试题
*
* @param query
* @return
*/
@PostMapping("/update")
public Result update(@RequestBody AphorismPoetryQuery query) {
AphorismPoetry aphorismPoetry = new AphorismPoetry();
BeanUtils.copyProperties(query, aphorismPoetry);
return Result.ok(this.aphorismPoetryService.updateById(aphorismPoetry));
}
/**
* 删除面试题
*
* @param id
* @return
*/
@GetMapping("/delete")
public Result delete(@RequestParam("id") Integer id) {
AphorismPoetry aphorismPoetry = new AphorismPoetry();
aphorismPoetry.setIsDelete(1);
QueryWrapper<AphorismPoetry> wrapper = new QueryWrapper<>();
wrapper.eq("id", id);
return Result.ok(this.aphorismPoetryService.update(aphorismPoetry, wrapper));
}
}
package com.kwan.springbootkwan.entity;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import lombok.Data;
import java.util.Date;
/**
* 名言警句(AphorismPoetry)表实体类
*
* @author makejava
* @since 2023-10-09 11:13:13
*/
@SuppressWarnings("serial")
@Data
public class AphorismPoetry extends Model<AphorismPoetry> {
/**
* 主键id
*/
private Integer id;
/**
* 诗词内容
*/
private String poetryText;
/**
* 创建时间
*/
private Date createTime;
/**
* 逻辑删除,0未删除,1已删除
*/
private Integer isDelete;
}
package com.kwan.springbootkwan.entity.dto;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.kwan.springbootkwan.entity.AphorismPoetry;
import com.kwan.springbootkwan.mapstruct.FromConverter;
import lombok.Data;
import org.mapstruct.Mapper;
import org.mapstruct.ReportingPolicy;
import org.mapstruct.factory.Mappers;
import java.util.Date;
/**
* 诗词实体类
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/10/9 11:19
*/
@Data
@SuppressWarnings("serial")
public class AphorismPoetryDTO extends Model<AphorismPoetryDTO> {
/**
* 主键id
*/
private Integer id;
/**
* 面试问题
*/
private String questionName;
/**
* 类型
*/
private Integer questionType;
/**
* 1~10的分值
*/
private Integer degreeOfImportance;
/**
* 1:简单;2:中等;3:困难
*/
private Integer degreeOfDifficulty;
/**
* 困难指数
*/
private Integer difficultyOfScore;
/**
* 力扣的问题号
*/
private Integer leetcodeNumber;
/**
* 力扣的问题链接
*/
private String leetcodeLink;
/**
* 创建时间
*/
private Date createTime;
@Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface Converter extends FromConverter<AphorismPoetryDTO, AphorismPoetry> {
AphorismPoetryDTO.Converter INSTANCE = Mappers.getMapper(AphorismPoetryDTO.Converter.class);
}
}
\ No newline at end of file
package com.kwan.springbootkwan.entity.query;
import lombok.Data;
@Data
public class AphorismPoetryQuery {
private Integer id;
private String poetryText;
private Integer addType;
}
\ No newline at end of file
package com.kwan.springbootkwan.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.kwan.springbootkwan.entity.AphorismPoetry;
/**
* 名言警句(AphorismPoetry)表数据库访问层
*
* @author makejava
* @since 2023-10-09 11:13:12
*/
public interface AphorismPoetryMapper extends BaseMapper<AphorismPoetry> {
}
package com.kwan.springbootkwan.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.kwan.springbootkwan.entity.AphorismPoetry;
/**
* 名言警句(AphorismPoetry)表服务接口
*
* @author makejava
* @since 2023-10-09 11:13:13
*/
public interface AphorismPoetryService extends IService<AphorismPoetry> {
}
package com.kwan.springbootkwan.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kwan.springbootkwan.mapper.AphorismPoetryMapper;
import com.kwan.springbootkwan.entity.AphorismPoetry;
import com.kwan.springbootkwan.service.AphorismPoetryService;
import org.springframework.stereotype.Service;
/**
* 名言警句(AphorismPoetry)表服务实现类
*
* @author makejava
* @since 2023-10-09 11:13:13
*/
@Service("aphorismPoetryService")
public class AphorismPoetryServiceImpl extends ServiceImpl<AphorismPoetryMapper, AphorismPoetry> implements AphorismPoetryService {
}
<?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="com.kwan.springbootkwan.mapper.AphorismPoetryMapper">
</mapper>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册