添加地址相关接口

This commit is contained in:
muhua 2024-07-13 15:33:19 +08:00
parent 677b1df832
commit 753903b388
4 changed files with 112 additions and 9 deletions

View File

@ -1,5 +1,6 @@
package com.example.takeawaysystemserver.controller;
import com.example.takeawaysystemserver.entity.Address;
import com.example.takeawaysystemserver.model.dto.*;
import com.example.takeawaysystemserver.service.UserService;
import com.example.takeawaysystemserver.util.R;
@ -8,6 +9,8 @@ import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* @author Ethereal
* @date 2024/7/12
@ -50,4 +53,16 @@ public class UserController {
public R<String> updatePassword(@RequestHeader String token, UserPasswordDTO userPasswordDTO){
return userService.updatePassword(token,userPasswordDTO);
}
@GetMapping("get-addresses")
@ApiOperation(value = "获取用户地址")
public R<List<Address>> getAddresses(@RequestHeader String token){
return userService.getAddresses(token);
}
@PostMapping("add-address")
@ApiOperation(value = "添加地址")
public R<String> addAddress(@RequestHeader String token, @RequestBody AddressDTO addressDTO){
return userService.addAddress(token, addressDTO);
}
}

View File

@ -1,5 +1,7 @@
package com.example.takeawaysystemserver.model.dto;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@ -13,5 +15,39 @@ import lombok.NoArgsConstructor;
@AllArgsConstructor
@NoArgsConstructor
public class AddressDTO {
private String address;
/**
* 收货人
*/
private String consignee;
/**
* 手机号
*/
private String phone;
/**
* 省份
*/
private String province;
/**
* 市级名
*/
private String city;
/**
* 区级名
*/
private String district;
/**
* 详细地址
*/
private String detail;
/**
* 默认0否 1是
*/
private Integer isDefault;
}

View File

@ -1,13 +1,13 @@
package com.example.takeawaysystemserver.service;
import com.example.takeawaysystemserver.entity.Address;
import com.example.takeawaysystemserver.entity.User;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.takeawaysystemserver.model.dto.UserLoginDTO;
import com.example.takeawaysystemserver.model.dto.UserPasswordDTO;
import com.example.takeawaysystemserver.model.dto.UserRegisterDTO;
import com.example.takeawaysystemserver.model.dto.UserUpdateDTO;
import com.example.takeawaysystemserver.model.dto.*;
import com.example.takeawaysystemserver.util.R;
import java.util.List;
/**
* @author qiushengyu
* @description 针对表user的数据库操作Service
@ -49,4 +49,19 @@ public interface UserService extends IService<User> {
* @return 修改信息
*/
R<String> updatePassword(String token, UserPasswordDTO userPasswordDTO);
/**
* 获取用户地址
* @param token 用户token
* @return 地址列表
*/
R<List<Address>> getAddresses(String token);
/**
* 添加地址
* @param token 用户token
* @param addressDTO 地址DTO
* @return 添加信息
*/
R<String> addAddress(String token, AddressDTO addressDTO);
}

View File

@ -5,11 +5,10 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.takeawaysystemserver.constant.CommonConstant;
import com.example.takeawaysystemserver.entity.Address;
import com.example.takeawaysystemserver.entity.User;
import com.example.takeawaysystemserver.model.dto.UserLoginDTO;
import com.example.takeawaysystemserver.model.dto.UserPasswordDTO;
import com.example.takeawaysystemserver.model.dto.UserRegisterDTO;
import com.example.takeawaysystemserver.model.dto.UserUpdateDTO;
import com.example.takeawaysystemserver.model.dto.*;
import com.example.takeawaysystemserver.service.AddressService;
import com.example.takeawaysystemserver.service.RedisService;
import com.example.takeawaysystemserver.service.UserService;
import com.example.takeawaysystemserver.mapper.UserMapper;
@ -20,6 +19,7 @@ import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@ -36,6 +36,9 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User>
private final RedisService redisService;
@Resource
private AddressService addressService;
public UserServiceImpl(DESUtil desUtil, RedisService redisService) {
this.desUtil = desUtil;
this.redisService = redisService;
@ -112,6 +115,40 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User>
return R.fail("修改失败");
}
@Override
public R<List<Address>> getAddresses(String token) {
Integer id = Integer.parseInt(getUserId(token));
LambdaQueryWrapper<Address> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(Address::getUserId, id);
return R.ok(addressService.list(wrapper));
}
@Override
public R<String> addAddress(String token, AddressDTO addressDTO) {
Integer id = Integer.parseInt(getUserId(token));
Address address = new Address();
address.setUserId(id);
address.setCity(addressDTO.getCity());
address.setProvince(addressDTO.getProvince());
address.setPhone(addressDTO.getPhone());
address.setDetail(addressDTO.getDetail());
address.setConsignee(addressDTO.getConsignee());
address.setDistrict(addressDTO.getDistrict());
LambdaQueryWrapper<Address> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(Address::getUserId, id);
List<Address> list = addressService.list(wrapper);
if (list == null || list.isEmpty()){
address.setIsDefault(1);
}
address.setIsDefault(0);
boolean save = addressService.save(address);
if (save){
return R.ok("添加成功");
}else {
return R.fail("添加失败");
}
}
private Boolean checkUser(UserRegisterDTO userRegisterDTO){
User user = baseMapper.selectOne(new LambdaQueryWrapper<User>()
.select(User::getUsername)