四、创建个人中心页面
1、后端编写API
1.1、建表、pojo、mapper、service接口
- 我是习惯用命令去建表,这里我们要实现三个api所以,要新建一个bot表来存放数据,可以用以下的sql语句来建表
CREATE TABLE `kob`.`bot` (
`id` int NOT NULL AUTO_INCREMENT,
`user_id` int NOT NULL,
`title` varchar(100) NULL,
`description` varchar(300) NULL,
`content` varchar(10000) NULL,
`rating` int NULL DEFAULT 1500,
`createtime` datetime NULL,
`modifytime` datetime NULL,
PRIMARY KEY (`id`)
);
- 编写对应的pojo类
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Bot {
@TableId(type = IdType.AUTO)
private Integer id;
private Integer userId;
private String title;
private String description;
private String content;
private Integer rating;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date createtime;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date modifytime;
}
- 编写mapper层
- 编写service接口
public interface AddService {
public Map<String, String> add(Map<String, String> data);
}
public interface GetListService {
public List<Bot> getlist();
}
public interface RemoveService {
public Map<String, String> remove(Map<String, String> data);
}
public interface UpdateService {
public Map<String, String> update(Map<String, String> map);
}
1.2、编写AddServiceImpl和AddController
- 编写AddServiceImpl
@Service
public class AddServiceImpl implements AddService {
@Autowired
private BotMapper botMapper;
@Override
public Map<String, String> add(Map<String, String> data) {
UsernamePasswordAuthenticationToken authenticationToken =
(UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
UserDetailsImpl loginUser = (UserDetailsImpl)authenticationToken.getPrincipal();
User user = loginUser.getUser();
String title = data.get("title");
String description = data.get("dsecription");
String content = data.get("content");
Map<String, String> map = new HashMap<>();
if(title == null || title.length() == 0){
map.put("error_message", "标题不能为空");
return map;
}
if(title.length() > 100) {
map.put("error_message", "标题长度不能大于100");
return map;
}
if(description == null || description.length() == 0){
description = "这个用户很懒,什么也没写";
}
if(description.length() > 300){
map.put("error_message", "Bot描述长度不能大于300");
return map;
}
if(content == null || content.length() == 0){
map.put("error_message", "代码不能为空");
return map;
}
if(content.length() > 10000){
map.put("error_message", "代码长度不能大于100000");
return map;
}
Date date = new Date();
Bot bot = new Bot(null, user.getId(), title, description, content, 1500, date, date);
botMapper.insert(bot);
map.put("error_message", "success");
return map;
}
}
- 编写AddController
- 然后在前端请求测试一下
- 可以返回succes然后在数据库中能出现加的内容即完成
1.3、编写RemoveServiceImpl和RemoveController
@Service
public class RemoveServiceImpl implements RemoveService {
@Autowired
private BotMapper botMapper;
@Override
public Map<String, String> remove(Map<String, String> data) {
int bot_id = Integer.parseInt(data.get("bot_id"));
System.out.println(bot_id);
UsernamePasswordAuthenticationToken authenticationToken =
(UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
UserDetailsImpl loginUser = (UserDetailsImpl) authenticationToken.getPrincipal();
User user = loginUser.getUser();
Bot bot = botMapper.selectById(bot_id);
Map<String, String> map = new HashMap<>();
if(bot == null){
map.put("error_message", "Bot不存在或已经删除");
return map;
}
if(!bot.getUserId().equals(user.getId())){
map.put("error_message", "没有权限删除该Bot");
return map;
}
botMapper.deleteById(bot_id);
map.put("error_message", "success");
return map;
}
}
- 前端测试一下
1.4、编写UpdateServiceImpl和UpdateController
@Service
public class UpdateServiceImpl implements UpdateService {
@Autowired
private BotMapper botMapper;
@Override
public Map<String, String> update(Map<String, String> data) {
UsernamePasswordAuthenticationToken authenticationToken =
(UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
UserDetailsImpl loginUser= (UserDetailsImpl) authenticationToken.getPrincipal();
User user = loginUser.getUser();
int bot_id = Integer.parseInt(data.get("bot_id"));
String title = data.get("title");
String description = data.get("description");
String content = data.get("content");
Map<String, String> map = new HashMap<>();
if(title == null || title.length() == 0){
map.put("error_message", "标题不能为空");
return map;
}
if(title.length() > 100) {
map.put("error_message", "标题长度不能大于100");
return map;
}
if(description == null || description.length() == 0){
description = "这个用户很懒,什么也没写";
}
if(description.length() > 300){
map.put("error_message", "Bot描述长度不能大于300");
return map;
}
if(content == null || content.length() == 0){
map.put("error_message", "代码不能为空");
return map;
}
if(content.length() > 10000){
map.put("error_message", "代码长度不能大于100000");
return map;
}
Bot bot = botMapper.selectById(bot_id);
if(bot == null){
map.put("error_message", "Bot不存在或已被删除");
return map;
}
if(!bot.getUserId().equals(user.getId())){
map.put("error_message", "没有权限修改该Bot");
return map;
}
Bot new_bot = new Bot(bot.getId(), user.getId(), title, description, content, bot.getRating(), bot.getCreatetime(), new Date());
botMapper.updateById(new_bot);
map.put("error_message", "success");
return map;
}
}
- 然后在前端测试一下
1.5、编写GetListServiceImpl和GetListController
- 然后去前端测试一下