From 0230d316fb74c95f6252ac78e092405e4730e87a Mon Sep 17 00:00:00 2001 From: flykhan Date: Wed, 22 Feb 2023 22:40:30 +0800 Subject: [PATCH] =?UTF-8?q?Bot=20=E6=93=8D=E4=BD=9C=E7=9A=84=E5=90=8E?= =?UTF-8?q?=E7=AB=AF=20API=E3=80=80=E5=AE=9A=E4=B9=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/user/bot/AddController.java | 18 ++++ .../user/bot/GetListController.java | 22 +++++ .../controller/user/bot/RemoveController.java | 20 +++++ .../controller/user/bot/UpdateController.java | 20 +++++ .../com/kob/backend/mapper/BotMapper.java | 9 ++ .../main/java/com/kob/backend/pojo/Bot.java | 34 ++++++++ .../service/impl/user/bot/AddServiceImpl.java | 78 +++++++++++++++++ .../impl/user/bot/GetListServiceImpl.java | 33 +++++++ .../impl/user/bot/RemoveServiceImpl.java | 53 ++++++++++++ .../impl/user/bot/UpdateServiceImpl.java | 85 +++++++++++++++++++ .../backend/service/user/bot/AddService.java | 7 ++ .../service/user/bot/GetListService.java | 9 ++ .../service/user/bot/RemoveService.java | 6 ++ .../service/user/bot/UpdateService.java | 7 ++ 14 files changed, 401 insertions(+) create mode 100644 backend/src/main/java/com/kob/backend/controller/user/bot/AddController.java create mode 100644 backend/src/main/java/com/kob/backend/controller/user/bot/GetListController.java create mode 100644 backend/src/main/java/com/kob/backend/controller/user/bot/RemoveController.java create mode 100644 backend/src/main/java/com/kob/backend/controller/user/bot/UpdateController.java create mode 100644 backend/src/main/java/com/kob/backend/mapper/BotMapper.java create mode 100644 backend/src/main/java/com/kob/backend/pojo/Bot.java create mode 100644 backend/src/main/java/com/kob/backend/service/impl/user/bot/AddServiceImpl.java create mode 100644 backend/src/main/java/com/kob/backend/service/impl/user/bot/GetListServiceImpl.java create mode 100644 backend/src/main/java/com/kob/backend/service/impl/user/bot/RemoveServiceImpl.java create mode 100644 backend/src/main/java/com/kob/backend/service/impl/user/bot/UpdateServiceImpl.java create mode 100644 backend/src/main/java/com/kob/backend/service/user/bot/AddService.java create mode 100644 backend/src/main/java/com/kob/backend/service/user/bot/GetListService.java create mode 100644 backend/src/main/java/com/kob/backend/service/user/bot/RemoveService.java create mode 100644 backend/src/main/java/com/kob/backend/service/user/bot/UpdateService.java diff --git a/backend/src/main/java/com/kob/backend/controller/user/bot/AddController.java b/backend/src/main/java/com/kob/backend/controller/user/bot/AddController.java new file mode 100644 index 0000000..2a958aa --- /dev/null +++ b/backend/src/main/java/com/kob/backend/controller/user/bot/AddController.java @@ -0,0 +1,18 @@ +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.*; + +import java.util.Map; + +@RestController +public class AddController { + @Autowired + private AddService addService; + + @PostMapping("/user/bot/add/") + public Map add(@RequestParam Map data){ + return addService.add(data); + } +} diff --git a/backend/src/main/java/com/kob/backend/controller/user/bot/GetListController.java b/backend/src/main/java/com/kob/backend/controller/user/bot/GetListController.java new file mode 100644 index 0000000..69c4848 --- /dev/null +++ b/backend/src/main/java/com/kob/backend/controller/user/bot/GetListController.java @@ -0,0 +1,22 @@ +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.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; +import java.util.Map; + +@RestController +public class GetListController { + @Autowired + private GetListService getListService; + + @GetMapping("/user/bot/getlist/") + public List getList(){ + return getListService.getList(); + } +} diff --git a/backend/src/main/java/com/kob/backend/controller/user/bot/RemoveController.java b/backend/src/main/java/com/kob/backend/controller/user/bot/RemoveController.java new file mode 100644 index 0000000..630f709 --- /dev/null +++ b/backend/src/main/java/com/kob/backend/controller/user/bot/RemoveController.java @@ -0,0 +1,20 @@ +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 remove(@RequestParam Map data){ + return removeService.remove(data); + } +} diff --git a/backend/src/main/java/com/kob/backend/controller/user/bot/UpdateController.java b/backend/src/main/java/com/kob/backend/controller/user/bot/UpdateController.java new file mode 100644 index 0000000..8ee59e8 --- /dev/null +++ b/backend/src/main/java/com/kob/backend/controller/user/bot/UpdateController.java @@ -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 update(@RequestParam Map data){ + return updateService.update(data); + } +} diff --git a/backend/src/main/java/com/kob/backend/mapper/BotMapper.java b/backend/src/main/java/com/kob/backend/mapper/BotMapper.java new file mode 100644 index 0000000..51c3372 --- /dev/null +++ b/backend/src/main/java/com/kob/backend/mapper/BotMapper.java @@ -0,0 +1,9 @@ +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 { +} diff --git a/backend/src/main/java/com/kob/backend/pojo/Bot.java b/backend/src/main/java/com/kob/backend/pojo/Bot.java new file mode 100644 index 0000000..0517f3f --- /dev/null +++ b/backend/src/main/java/com/kob/backend/pojo/Bot.java @@ -0,0 +1,34 @@ +//用于将 Mysql 表 Bot 转换为 class +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 +@AllArgsConstructor +@NoArgsConstructor +public class Bot { +// 主键自增 + @TableId(type = IdType.AUTO) + private Integer id; + +// 注意:在pojo中需要定义成userId,在queryWrapper中的名称仍然为user_id + private Integer userId; + private String title; + private String description; + private String content; + private Integer rating; +// pojo中定义日期格式的注解:@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") + private Date createtime; + + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") + private Date modifytime; + +} diff --git a/backend/src/main/java/com/kob/backend/service/impl/user/bot/AddServiceImpl.java b/backend/src/main/java/com/kob/backend/service/impl/user/bot/AddServiceImpl.java new file mode 100644 index 0000000..2a89bc6 --- /dev/null +++ b/backend/src/main/java/com/kob/backend/service/impl/user/bot/AddServiceImpl.java @@ -0,0 +1,78 @@ +package com.kob.backend.service.impl.user.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 注入接口,调用 BotMapper 来修改数据库 + @Autowired + private BotMapper botMapper; + + @Override + public Map add(Map 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 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 != null && description.length() > 300) { + map.put("error_message", "描述信息长度不能大于300"); + return map; + } + + if (content == null || content.length() == 0) { + map.put("error_message", "代码不能为空"); + return map; + } + + if (content.length() > 10000) { + map.put("error_message", "代码长度不能超过一万"); + return map; + } + +// 定义当前时间 + Date now = new Date(); +// 定义一个新 Bot ;创建时间和修改时间开始应该一样 + Bot bot = new Bot(null, user.getId(), title, description, content, 1500, now, now); + +// 将新建的 Bot 注入数据库中 + botMapper.insert(bot); + map.put("error_message", "success"); + + return map; + } +} diff --git a/backend/src/main/java/com/kob/backend/service/impl/user/bot/GetListServiceImpl.java b/backend/src/main/java/com/kob/backend/service/impl/user/bot/GetListServiceImpl.java new file mode 100644 index 0000000..37e6bc9 --- /dev/null +++ b/backend/src/main/java/com/kob/backend/service/impl/user/bot/GetListServiceImpl.java @@ -0,0 +1,33 @@ +package com.kob.backend.service.impl.user.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 getList() { + UsernamePasswordAuthenticationToken authenticationToken = + (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication(); + UserDetailsImpl loginUser = (UserDetailsImpl) authenticationToken.getPrincipal(); + User user = loginUser.getUser(); + + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.eq("user_id",user.getId()); + + return botMapper.selectList(queryWrapper); + } +} diff --git a/backend/src/main/java/com/kob/backend/service/impl/user/bot/RemoveServiceImpl.java b/backend/src/main/java/com/kob/backend/service/impl/user/bot/RemoveServiceImpl.java new file mode 100644 index 0000000..60f763f --- /dev/null +++ b/backend/src/main/java/com/kob/backend/service/impl/user/bot/RemoveServiceImpl.java @@ -0,0 +1,53 @@ +package com.kob.backend.service.impl.user.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 remove(Map data) { +// 先取出当前用户信息 + UsernamePasswordAuthenticationToken authenticationToken = + (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication(); + UserDetailsImpl loginUser = (UserDetailsImpl) authenticationToken.getPrincipal(); + User user = loginUser.getUser(); + +// 从用户输入参数判断要删除的 bot id + int bot_id = Integer.parseInt(data.get("bot_id")); + Bot bot = botMapper.selectById(bot_id); + +// 定义返回值 + Map map = new HashMap<>(); + +// 如果 bot 不存在 + if (bot == null) { + map.put("error_message", "Bot 不存在或已被删除"); + return map; + } + +// 如果 bot 的 user_id 不等于 User 的 id + if (!bot.getUserId().equals(user.getId())) { + map.put("error_message", "你无权删除别人的 bot"); + return map; + } + +// 判断成功后进行删除 + botMapper.deleteById(bot_id); + map.put("error_message", "success"); + return map; + } +} diff --git a/backend/src/main/java/com/kob/backend/service/impl/user/bot/UpdateServiceImpl.java b/backend/src/main/java/com/kob/backend/service/impl/user/bot/UpdateServiceImpl.java new file mode 100644 index 0000000..3a87148 --- /dev/null +++ b/backend/src/main/java/com/kob/backend/service/impl/user/bot/UpdateServiceImpl.java @@ -0,0 +1,85 @@ +package com.kob.backend.service.impl.user.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 update(Map 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 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 != null && description.length() > 300) { + map.put("error_message", "描述信息长度不能大于300"); + return map; + } + if (content == null || content.length() == 0) { + map.put("error_message", "代码不能为空"); + return map; + } + if (content.length() > 10000) { + map.put("error_message", "代码长度不能超过一万"); + 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(); + Date createTime = bot.getCreatetime(); + int rating = bot.getRating(); + + Bot newBot = new Bot(bot.getId(),user.getId(),title,description,content,rating,createTime,now); + + botMapper.updateById(newBot); + map.put("error_message","success"); + return map; + } +} diff --git a/backend/src/main/java/com/kob/backend/service/user/bot/AddService.java b/backend/src/main/java/com/kob/backend/service/user/bot/AddService.java new file mode 100644 index 0000000..5bc0d1c --- /dev/null +++ b/backend/src/main/java/com/kob/backend/service/user/bot/AddService.java @@ -0,0 +1,7 @@ +package com.kob.backend.service.user.bot; + +import java.util.Map; + +public interface AddService { + public Map add(Map data); +} diff --git a/backend/src/main/java/com/kob/backend/service/user/bot/GetListService.java b/backend/src/main/java/com/kob/backend/service/user/bot/GetListService.java new file mode 100644 index 0000000..6997651 --- /dev/null +++ b/backend/src/main/java/com/kob/backend/service/user/bot/GetListService.java @@ -0,0 +1,9 @@ +package com.kob.backend.service.user.bot; + +import com.kob.backend.pojo.Bot; + +import java.util.List; + +public interface GetListService { + List getList(); // getList() 不需要传参数 +} diff --git a/backend/src/main/java/com/kob/backend/service/user/bot/RemoveService.java b/backend/src/main/java/com/kob/backend/service/user/bot/RemoveService.java new file mode 100644 index 0000000..3f256b9 --- /dev/null +++ b/backend/src/main/java/com/kob/backend/service/user/bot/RemoveService.java @@ -0,0 +1,6 @@ +package com.kob.backend.service.user.bot; + +import java.util.Map; +public interface RemoveService { + public Map remove(Map data); +} diff --git a/backend/src/main/java/com/kob/backend/service/user/bot/UpdateService.java b/backend/src/main/java/com/kob/backend/service/user/bot/UpdateService.java new file mode 100644 index 0000000..d05e02d --- /dev/null +++ b/backend/src/main/java/com/kob/backend/service/user/bot/UpdateService.java @@ -0,0 +1,7 @@ +package com.kob.backend.service.user.bot; + +import java.util.Map; + +public interface UpdateService { + Map update(Map data); +}