结组作业5

今天写调用chat接口的service的实现层代码

 1 package org.test.tongyuzhe.Service;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.stereotype.Service;
 5 import org.test.tongyuzhe.Mapper.ChatMapper;
 6 import org.test.tongyuzhe.Pojo.Chat;
 7 
 8 import java.util.List;
 9 
10 @Service
11 public class ChatService {
12     @Autowired
13     ChatMapper chatMapper;
14 
15     /**
16      * 通过userId来获取当前用户的历史问问题记录
17      * @param userId 用户id
18      * @return 问题和回答列表信息
19      */
20     public List<Chat> getMessage(Integer userId) {
21         List<Chat> list=chatMapper.getMessage(userId);
22         return list;
23     }
24 
25     /**
26      * 添加message(也就是保存用户提出的问题和答案等信息)
27      * @param chat
28      */
29     public void insertMessage(Chat chat) {
30        chatMapper.insertMessage(chat);
31     }
32 }

主要是一些方法的定义

还写了一部分在用户注册和登录时的一部分代码

 1 package org.test.tongyuzhe.Controller;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.http.ResponseEntity;
 5 import org.springframework.validation.annotation.Validated;
 6 import org.springframework.web.bind.annotation.PostMapping;
 7 import org.springframework.web.bind.annotation.RequestBody;
 8 import org.springframework.web.bind.annotation.RequestMapping;
 9 import org.springframework.web.bind.annotation.RestController;
10 import org.test.tongyuzhe.Pojo.Result;
11 import org.test.tongyuzhe.Pojo.User;
12 import org.test.tongyuzhe.Service.UserService;
13 import org.test.tongyuzhe.utils.JwtUtil;
14 import org.test.tongyuzhe.utils.Md5Util;
15 
16 import java.util.HashMap;
17 import java.util.Map;
18 
19 @RestController
20 @Validated
21 @RequestMapping("/user")
22 public class UserController {
23 
24     @Autowired
25     private UserService userService;
26 
27     @PostMapping("/login")
28     public Result<?> login(@RequestBody User user){
29 
30         //查询用户
31         User loginUser = userService.findByUserName(user.getUsername());
32 
33         //判断用户是否存在
34         if (loginUser == null){
35             return Result.error("用户名错误");
36         }
37         //判断密码是否正确 loginUser对象中password是密文
38         if(Md5Util.getMD5String(user.getPassword()).equals(loginUser.getPassword())){
39             //登录成功,生成Token
40             Map<String,Object> claims = new HashMap<>();
41             claims.put("id",loginUser.getUserId());
42             claims.put("username",loginUser.getUsername());
43             String token = JwtUtil.genToken(claims);
44             // System.out.println(token);
45             return Result.success(String.valueOf(loginUser.getRole()),token);
46         }
47 
48         return Result.error("密码错误");
49     }
50 
51 
52     @PostMapping("/register")
53     public Result<?> childRegister(@RequestBody User user){
54         //查询用户
55         User userQuery = userService.findByUserName(user.getUsername());
56         if (userQuery == null){
57             //注册
58             userService.register(user);
59             return Result.success();
60         }else {
61             //占用
62             return Result.error("用户名已被注册");
63         }
64 
65     }
66 
67 }

 

posted @ 2024-05-08 17:52  连师傅只会helloword  阅读(3)  评论(0编辑  收藏  举报