version-1.9[实现Bot后端(4个API)]

pull/1/head
dyh 3 years ago
parent eff1294ca6
commit a6526c17d3

@ -1,79 +0,0 @@
//package com.kob.backend.controller.user;
//
//
////@ResponseBody 把return的结果变为Json等数据
////@Controller 返回jsp,html页面+跳转
////@Controller + @ResponseBody 返回json等数据
////@RestController 返回json等数据,不能是jsp,html【@Controller+@responseBody】
//
////@RequestMapping 给出外界访问方法的路径,或者说触发路径 ,触发条件
////@GetMapping
////PostMapping
//
////@AutoWired自动导入依赖的bean。[用到mapper层的接口时,数据库,写上去]
////byType方式。把配置好的Bean拿来用完成属性、方法的组装它可以对类成员变量、方法及构造函数进行标注完成自动装配的工作。
////当加上required=false就算找不到bean也不报错。
////它可以对类成员变量、方法及构造函数进行标注,让 spring 完成 bean 自动装配的工作。
//
////mapper接口 被mybatis实现
////接口实例调用mybatis的crud
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import com.kob.backend.mapper.UserMapper;
//import com.kob.backend.pojo.User;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
//import org.springframework.security.crypto.password.PasswordEncoder;
//import org.springframework.web.bind.annotation.GetMapping;
//import org.springframework.web.bind.annotation.PathVariable;
//import org.springframework.web.bind.annotation.RestController;
//
//import java.util.List;
////@@@@@@@@@@@@@@@ 实现了【增删查】 @@@@@@@@@@@@@@@@
//@RestController
//public class UserController {
//
// @Autowired
// UserMapper userMapper;
//
// /*查询所有用户*/
// @GetMapping("/user/all/")
// public List<User> getAll()
// {
// return userMapper.selectList(null);
// }
//
// /*查询单个用户*/
// @GetMapping("/user/{userId}/")
// public User getUser(@PathVariable int userId)
// {
// QueryWrapper<User> queryWrapper = new QueryWrapper<>();
// queryWrapper.eq("id",userId);
// return userMapper.selectOne(queryWrapper);
// // 范围遍历
// // public List<User> getUser(int userId)
// // queryWrapper.ge("id", 2).le("id", 3);
// // return userMapper.selectList(queryWrapper);
// }
//
// /*添加一个用户*/
// @GetMapping("/user/add/{userId}/{username}/{password}/")
// public String addUser(@PathVariable int userId,
// @PathVariable String username,
// @PathVariable String password)
// {
// PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
// String encoderPassword = passwordEncoder.encode(password);
// User user = new User(userId,username,encoderPassword,"00");
// userMapper.insert(user);
// return "Add User Sucessfully";
// }
//
// /*删除一个用户*/
// @GetMapping("/user/delete/{userId}/")
// public String deleteUser(@PathVariable int userId)
// {
// userMapper.deleteById(userId);
// return "Delete User Successfully";
// }
//
//}

@ -0,0 +1,22 @@
package com.kob.backend.controller.user.bot;
import com.kob.backend.service.user.bot.AddService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
@RestController
public class AddController {
@Autowired
private AddService addService;
@PostMapping("/user/bot/add/")
public Map<String ,String> add(@RequestParam Map<String,String> data)
{
return addService.add(data);
}
}

@ -0,0 +1,21 @@
package com.kob.backend.controller.user.bot;
import com.kob.backend.pojo.Bot;
import com.kob.backend.service.user.bot.GetListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class GetListController{
@Autowired
private GetListService getListService;
@GetMapping("/user/bot/getlist/")
public List<Bot> getList()
{
return getListService.getList();
}
}

@ -0,0 +1,21 @@
package com.kob.backend.controller.user.bot;
import com.kob.backend.service.user.bot.RemoveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
@RestController
public class RemoveController {
@Autowired
private RemoveService removeService;
@PostMapping("/user/bot/remove/")
public Map<String,String> remove(@RequestParam Map<String,String> data)
{
return removeService.remove(data);
}
}

@ -0,0 +1,20 @@
package com.kob.backend.controller.user.bot;
import com.kob.backend.service.user.bot.UpdateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
@RestController
public class UpdateController {
@Autowired
private UpdateService updateService;
@PostMapping("/user/bot/update/")
public Map<String,String> update(@RequestParam Map<String,String> data){
return updateService.update(data);
}
}

@ -0,0 +1,11 @@
package com.kob.backend.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.kob.backend.pojo.Bot;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface BotMapper extends BaseMapper<Bot> {
}

@ -0,0 +1,26 @@
package com.kob.backend.pojo;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
@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;
}

@ -0,0 +1,71 @@
package com.kob.backend.service.impl.bot;
import com.kob.backend.mapper.BotMapper;
import com.kob.backend.pojo.Bot;
import com.kob.backend.pojo.User;
import com.kob.backend.service.impl.utils.UserDetailsImpl;
import com.kob.backend.service.user.bot.AddService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@Service
public class AddServiceImpl implements AddService {
@Autowired
private BotMapper botMapper;
@Override
public Map<String, String> add(Map<String, String> data) {
//认证+获取user
UsernamePasswordAuthenticationToken authenticationToken =
(UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
UserDetailsImpl loginUser = (UserDetailsImpl) authenticationToken.getPrincipal();
User user = loginUser.getUser();
//传入参数
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","代码长度不能超过10000");
return map;
}
Date now = new Date();
Bot bot = new Bot(null,user.getId(),title,description,content,1500,now,now);
botMapper.insert(bot);
map.put("error_message","success");
return map;
}
}

@ -0,0 +1,35 @@
package com.kob.backend.service.impl.bot;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kob.backend.mapper.BotMapper;
import com.kob.backend.pojo.Bot;
import com.kob.backend.pojo.User;
import com.kob.backend.service.impl.utils.UserDetailsImpl;
import com.kob.backend.service.user.bot.GetListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class GetListServiceImpl implements GetListService {
@Autowired
private BotMapper botMapper;
@Override
public List<Bot> getList()
{
UsernamePasswordAuthenticationToken authenticationToken =
(UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
UserDetailsImpl loginUser = (UserDetailsImpl) authenticationToken.getPrincipal();
User user = loginUser.getUser();
QueryWrapper<Bot> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("user_id",user.getId());
List<Bot> bots = botMapper.selectList(queryWrapper);
return bots;
}
}

@ -0,0 +1,48 @@
package com.kob.backend.service.impl.bot;
import com.kob.backend.mapper.BotMapper;
import com.kob.backend.pojo.Bot;
import com.kob.backend.pojo.User;
import com.kob.backend.service.impl.utils.UserDetailsImpl;
import com.kob.backend.service.user.bot.RemoveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
@Service
public class RemoveServiceImpl implements RemoveService {
@Autowired
private BotMapper botMapper;
@Override
public Map<String, String> remove(Map<String, String> data) {
//认证+获取用户
UsernamePasswordAuthenticationToken authenticationToken =
(UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
UserDetailsImpl loginUser = (UserDetailsImpl)authenticationToken.getPrincipal();
User user = loginUser.getUser();
Map<String,String> map = new HashMap<>();
int bot_id = Integer.parseInt(data.get("bot_id"));
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;
}
botMapper.deleteById(bot_id);
map.put("error_message","success");
return map;
}
}

@ -0,0 +1,92 @@
package com.kob.backend.service.impl.bot;
import com.kob.backend.mapper.BotMapper;
import com.kob.backend.pojo.Bot;
import com.kob.backend.pojo.User;
import com.kob.backend.service.impl.utils.UserDetailsImpl;
import com.kob.backend.service.user.bot.UpdateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@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","代码长度不能超过10000");
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;
}
Date now = new Date();
Bot new_bot = new Bot(
bot.getId(),
user.getId(),
title,
description,
content,
bot.getRating(),
bot.getCreatetime(),
now
);
botMapper.updateById(new_bot);
map.put("error_message","success");
return map;
}
}

@ -0,0 +1,10 @@
package com.kob.backend.service.user.bot;
import org.springframework.stereotype.Service;
import java.util.Map;
public interface AddService {
public Map<String ,String> add(Map<String,String> data);
}

@ -0,0 +1,8 @@
package com.kob.backend.service.user.bot;
import com.kob.backend.pojo.Bot;
import java.util.List;
public interface GetListService {
public List<Bot> getList();
}

@ -0,0 +1,8 @@
package com.kob.backend.service.user.bot;
import java.util.Map;
public interface RemoveService {
public Map<String,String> remove(Map<String,String> data);
}

@ -0,0 +1,7 @@
package com.kob.backend.service.user.bot;
import java.util.Map;
public interface UpdateService {
public Map<String,String> update(Map<String,String> data);
}

@ -5,87 +5,13 @@
<script>
// import $ from "jquery"
import NavBar from './components/NavBar.vue';
import "bootstrap/dist/css/bootstrap.min.css";
import "bootstrap/dist/js/bootstrap.min.js";
export default {
components: { NavBar },
name:"app",
setup:()=>{
// // LoginService==>token
// $.ajax({
// url:"http://localhost:3000/user/account/token/",
// type:"post",
// data:{
// username:'yxc',
// password:'yxc',
// },
// success(resp){
// console.log(resp);
// },
// errror(resp){
// console.log(resp);
// }
// });
// // InfoService==>info
// $.ajax({
// url:"http://localhost:3000/user/account/info/",
// type:"get",
// headers:{
// Authorization:"Bearer "+"eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiI0MmEwMmM3NGIwM2I0ODIzOGJmMzliMGIwOWNiMzYxOSIsInN1YiI6IjEiLCJpc3MiOiJzZyIsImlhdCI6MTY2MDAxNTA1NiwiZXhwIjoxNjYxMjI0NjU2fQ.1KXxdlCQQ1i-xMjKoiocqFvWDgY24L9rLzo233z2Tfk",
// },
// success(resp)
// {
// console.log(resp);
// },
// errror(resp)
// {
// console.log(resp);
// }
// });
// // RegisterService==>register
// $.ajax({
// url:"http://localhost:3000/user/account/register/",
// type:"post",
// data:{
// username: "yxc3",
// password:"yxc3",
// confirmedPassword: "yxc3",
// },
// success(resp){
// console.log(resp);
// },
// errror(resp){
// console.log(resp);
// }
// });
// InfoService==>info
// $.ajax({
// url:"https://app165.acapp.acwing.com.cn/myspace/userlist/",
// type:"get",
// // headers:{
// // Authorization:"Bearer "+"eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiI0MmEwMmM3NGIwM2I0ODIzOGJmMzliMGIwOWNiMzYxOSIsInN1YiI6IjEiLCJpc3MiOiJzZyIsImlhdCI6MTY2MDAxNTA1NiwiZXhwIjoxNjYxMjI0NjU2fQ.1KXxdlCQQ1i-xMjKoiocqFvWDgY24L9rLzo233z2Tfk",
// // },
// success(resp)
// {
// console.log(resp);
// },
// errror(resp)
// {
// console.log(resp);
// }
// });
}
}
</script>

@ -44,10 +44,8 @@ export default {
store.commit("updateToken",jwt_token)//mutations
store.dispatch("getinfo",{//action
success(){
console.log(store.state.user.pulling_info)
router.push({name:"home"})
store.commit("updatePullingInfo",false)//
console.log(store.state.user.pulling_info)
},
error(){
store.commit("updatePullingInfo",false)
@ -76,7 +74,7 @@ export default {
})
},
error() {
error_message.value = "用户名or密码错误";
error_message.value = "用户名密码错误";
}
})
}

@ -6,10 +6,90 @@
<script>
import ContentField from "@/components/ContentField"
import $ from "jquery"
import { useStore } from "vuex";
export default {
components:{
ContentField
},
setup(){
const store = useStore();
// //bot add
// $.ajax({
// url:"http://localhost:3000/user/bot/add/",
// type:"post",
// data:{
// title:"My title ",
// description:"My description ",
// content:"My content ",
// },
// headers:{
// Authorization:"Bearer " + store.state.user.token,
// },
// success(resp){
// console.log(resp);
// },
// errror(resp){
// console.log(resp);
// }
// });
// //bot remove
// $.ajax({
// url:"http://localhost:3000/user/bot/remove/",
// type:"post",
// data:{
// bot_id : 5,
// },
// headers:{
// Authorization:"Bearer " + store.state.user.token,
// },
// success(resp){
// console.log(resp);
// },
// errror(resp){
// console.log(resp);
// }
// });
// //bot update
// $.ajax({
// url:"http://localhost:3000/user/bot/update/",
// type:"post",
// data:{
// bot_id : 8,
// title:"new title",
// description:"new description",
// content:"new content",
// },
// headers:{
// Authorization:"Bearer " + store.state.user.token,
// },
// success(resp){
// console.log(resp);
// },
// errror(resp){
// console.log(resp);
// }
// });
// //bot getlist
// $.ajax({
// url:"http://localhost:3000/user/bot/getlist/",
// type:"get",
// headers:{
// Authorization:"Bearer " + store.state.user.token,
// },
// success(resp){
// console.log(resp);
// },
// errror(resp){
// console.log(resp);
// }
// });
}
}
</script>

Loading…
Cancel
Save