文章目录
- 一. 地址簿
- 1.查询地址列表
- AddressBookController
- AddressBookService
- AddressBookServiceImpl
- AddressBookMapper
- 2.新增地址
- AddressBookController
- AddressBookService
- AddressBookServiceImpl
- 3.修改地址
- AddressBookController
- AddressBookService
- AddressBookServiceImpl
- AddressBookController
- AddressBookService
- AddressBookServiceImpl
- 4.删除地址
- AddressBookController
- AddressBookService
- AddressBookServiceImpl
- 5.设置默认地址
- AddressBookController
- AddressBookService
- AddressBookServiceImpl
- 6.查询默认地址
一. 地址簿
1.查询地址列表
AddressBookController
package com.sky.controller.user;
import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/user/addressBook")
@Api(tags = "通讯录")
@Slf4j
public class AddressBookController {
@Autowired
private AddressBookService addressBookService;
@PostMapping("/list")
@ApiOperation(value = "通讯录列表")
public Result<List<AddressBook>> list() {
log.info("通讯录列表");
AddressBook addressBook = new AddressBook();
addressBook.setUserId(BaseContext.getCurrentId());
List<AddressBook> list = addressBookService.list(addressBook);
return Result.success(list);
}
}
AddressBookService
package com.sky.service;
import com.sky.entity.AddressBook;
import java.util.List;
public interface AddressBookService {
List<AddressBook> list(AddressBook addressBook);
}
AddressBookServiceImpl
package com.sky.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sky.entity.AddressBook;
import com.sky.mapper.AddressBookMapper;
import com.sky.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
@Slf4j
public class AddressBookServiceImpl implements AddressBookService {
@Autowired
private AddressBookMapper addressBookMapper;
@Override
public List<AddressBook> list(AddressBook addressBook) {
LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
if (addressBook.getUserId() != null) {
queryWrapper.eq(AddressBook::getUserId, addressBook.getUserId());
}
if (addressBook.getPhone() != null) {
queryWrapper.eq(AddressBook::getPhone, addressBook.getPhone());
}
if (addressBook.getIsDefault() != null) {
queryWrapper.eq(AddressBook::getIsDefault, addressBook.getIsDefault());
}
return addressBookMapper.selectList(queryWrapper);
}
}
AddressBookMapper
package com.sky.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sky.entity.AddressBook;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface AddressBookMapper extends BaseMapper<AddressBook> {
}
2.新增地址
AddressBookController
@GetMapping
@ApiOperation(value = "保存通讯录")
public Result save(@RequestBody AddressBook addressBook) {
log.info("保存通讯录:{}", addressBook);
addressBookService.save(addressBook);
return Result.success();
}
AddressBookService
void save(AddressBook addressBook);
}
AddressBookServiceImpl
@Override
public void save(AddressBook addressBook) {
addressBook.setUserId(BaseContext.getCurrentId());
addressBook.setIsDefault(0);
addressBookMapper.insert(addressBook);
}
3.修改地址
- 根据id查询地址
AddressBookController
@GetMapping("/{id}")
@ApiOperation(value = "根据id查询通讯录")
public Result<AddressBook> getById(@PathVariable Long id) {
log.info("根据id查询通讯录:{}", id);
AddressBook addressBook = addressBookService.getById(id);
return Result.success(addressBook);
}
AddressBookService
AddressBook getById(Long id);
}
AddressBookServiceImpl
@Override
public AddressBook getById(Long id) {
return addressBookMapper.selectById(id);
}
- 根据id修改地址
AddressBookController
@PutMapping
@ApiOperation(value = "更新通讯录")
public Result update(@RequestBody AddressBook addressBook) {
log.info("更新通讯录:{}", addressBook);
addressBookService.updateById(addressBook);
return Result.success();
}
AddressBookService
void updateById(AddressBook addressBook);
AddressBookServiceImpl
@Override
public void updateById(AddressBook addressBook) {
addressBookMapper.updateById(addressBook);
}
4.删除地址
AddressBookController
@DeleteMapping
@ApiOperation(value = "根据id删除通讯录")
public Result delete(Long id) {
log.info("删除通讯录:{}", id);
addressBookService.removeById(id);
return Result.success();
}
AddressBookService
void removeById(Long id);
AddressBookServiceImpl
@Override
public void removeById(Long id) {
addressBookMapper.deleteById(id);
}
5.设置默认地址
AddressBookController
@PutMapping("/default")
@ApiOperation("设置默认地址")
public Result setDefault(@RequestBody AddressBook addressBook) {
addressBookService.setDefault(addressBook);
return Result.success();
}
AddressBookService
void setDefault(AddressBook addressBook);
AddressBookServiceImpl
@Override
public void setDefault(AddressBook addressBook) {
addressBook.setIsDefault(0);
addressBook.setUserId(BaseContext.getCurrentId());
LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
addressBookMapper.update(addressBook, queryWrapper);
addressBook.setIsDefault(1);
addressBookMapper.updateById(addressBook);
}
6.查询默认地址
AddressBookController
@GetMapping("/default")
@ApiOperation("获取默认地址")
public Result<AddressBook> getDefault() {
AddressBook addressBook = new AddressBook();
addressBook.setIsDefault(1);
addressBook.setUserId(BaseContext.getCurrentId());
List<AddressBook> list = addressBookService.list(addressBook);
if (list != null && list.size() == 1) {
return Result.success(list.get(0));
}
return Result.error("没有查询到默认地址");
}