Skip to content
Snippets Groups Projects
Commit c492175b authored by Zetian Huang's avatar Zetian Huang
Browse files

user info part

parent f9ff9a09
No related branches found
No related tags found
No related merge requests found
package com.example.freshonline.controller;
import com.alibaba.fastjson.JSONObject;
import com.example.freshonline.enums.respVerifyRule.VerifyRule;
import com.example.freshonline.model.User;
import com.example.freshonline.service.OrderService;
import com.example.freshonline.service.UserService;
import com.example.freshonline.utils.RespBuilder;
import com.example.freshonline.utils.ValidationChecker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Autowired
private UserService userService;
@Autowired
private OrderService orderService;
@GetMapping("/user")
public JSONObject getUserInfo(@RequestParam("user_id") Integer userId){
return RespBuilder.create(userService.getUserById(userId),VerifyRule.NOT_NULL,"success","no such user");
}
@GetMapping("/user/update_password")
public JSONObject updateUserPassword(@RequestParam("user_id") String userId,
@RequestParam("old_pass") String oldPass,
@RequestParam("new_pass") String newPass){
Integer userIdInt = (new ValidationChecker()).str2int(userId,1);
if(userService.checkUser(userIdInt,oldPass)==null)
return RespBuilder.create(null, VerifyRule.NOT_NULL,"success","Wrong old password");
else{
String newToken="newToken";
User user = new User();
user.setId(userIdInt);
user.setPassword(newPass);
userService.updateUserSelective(user);
return RespBuilder.create(newToken, VerifyRule.NOT_NULL,"success","Update fail");
}
}
@GetMapping("/user/update_address")
public JSONObject updateUserAddress(@RequestParam("user_id") Integer userId,
@RequestParam("address") String address){
User user = new User();
user.setId(userId);
user.setLocation(address);
userService.updateUserSelective(user);
return RespBuilder.create(userService.getUserById(userId), VerifyRule.NOT_NULL,"success","Update fail");
}
@GetMapping("/user/{user_id}/orders")
public JSONObject serachUserOrders(@PathVariable("user_id") Integer id){
return RespBuilder.create(orderService.getOrderByUserId(id),VerifyRule.COLLECTION_NOT_EMPTY,"success","Query error");
}
}
......@@ -93,4 +93,6 @@ public interface OrderMapper {
* @mbg.generated Fri Jan 28 11:53:04 EST 2022
*/
int updateByPrimaryKey(Order record);
List<Order> selectByUserId(Integer userId);
}
\ No newline at end of file
......@@ -93,4 +93,6 @@ public interface UserMapper {
* @mbg.generated Fri Jan 28 11:53:04 EST 2022
*/
int updateByPrimaryKey(User record);
User selectUserByIdPassword(User user);
}
\ No newline at end of file
package com.example.freshonline.service;
import com.example.freshonline.dao.OrderMapper;
import com.example.freshonline.model.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class OrderService {
@Autowired
private OrderMapper orderMapper;
public List<Order> getOrderByUserId(Integer userId){
return orderMapper.selectByUserId(userId);
}
}
package com.example.freshonline.service;
import com.example.freshonline.dao.UserMapper;
import com.example.freshonline.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public User getUserById(Integer id){
return userMapper.selectByPrimaryKey(id);
}
public String updateUserSelective(User user){
userMapper.updateByPrimaryKeySelective(user);
return "";
}
public User checkUser(Integer userId,String passWord){
User user = new User();
user.setId(userId);
user.setPassword(passWord);
return userMapper.selectUserByIdPassword(user);
}
}
......@@ -4,7 +4,7 @@
spring.datasource.url=jdbc:mysql://localhost:3306/FreshOnline?useUnicode=true&characterEncoding=utf8&allowPublicKeyRetrieval=true&useSSL=false
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=
spring.datasource.password=root
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.example.freshonline.dao
\ No newline at end of file
mybatis.type-aliases-package=com.example.freshonline.dao
......@@ -331,4 +331,15 @@
location = #{location,jdbcType=VARCHAR}
where order_id = #{orderId,jdbcType=INTEGER}
</update>
<select id="selectByUserId" parameterType="java.lang.Integer" resultMap="BaseResultMap">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Fri Jan 28 11:53:04 EST 2022.
-->
select
<include refid="Base_Column_List" />
from `order`
where user_id = #{userId,jdbcType=INTEGER}
</select>
</mapper>
\ No newline at end of file
......@@ -315,4 +315,15 @@
location = #{location,jdbcType=VARCHAR}
where id = #{id,jdbcType=INTEGER}
</update>
<select id="selectUserByIdPassword" parameterType="com.example.freshonline.model.User" resultMap="BaseResultMap">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Fri Jan 28 11:53:04 EST 2022.
-->
select
<include refid="Base_Column_List" />
from user
where id = #{id,jdbcType=INTEGER} and password = #{password,jdbcType=VARCHAR}
</select>
</mapper>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment