苍穹外卖学习笔记(十七)

时间:2024-10-09 11:07:11

文章目录

  • 一. 地址簿
    • 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.查询默认地址
      • AddressBookController

一. 地址簿

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;

/**
 * @author Jie.
 * @description: TODO
 * @date 2024/10/8
 * @version: 1.0
 */
@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;

/**
 * @author Jie.
 * @description: TODO
 * @date 2024/10/8
 * @version: 1.0
 */
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;

/**
 * @author Jie.
 * @description: TODO
 * @date 2024/10/8
 * @version: 1.0
 */
@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;

/**
 * @author Jie.
 * @description: TODO
 * @date 2024/10/8
 * @version: 1.0
 */
@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.修改地址

  1. 根据id查询地址

AddressBookController

    /**
     * 根据id查询通讯录
     */
    @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

/**
     * 根据id查询通讯录
     */
    AddressBook getById(Long id);
}

AddressBookServiceImpl

   /**
     * 根据id查询通讯录
     */
    @Override
    public AddressBook getById(Long id) {
        return addressBookMapper.selectById(id);
    }
  1. 根据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

       /**
     * 根据id删除通讯录
     */
    @DeleteMapping
    @ApiOperation(value = "根据id删除通讯录")
    public Result delete(Long id) {
        log.info("删除通讯录:{}", id);
        addressBookService.removeById(id);
        return Result.success();
    }

AddressBookService

  /**
     * 根据id删除通讯录
     */
    void removeById(Long id);

AddressBookServiceImpl

       /**
     * 根据id删除通讯录
     */
    @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("没有查询到默认地址");
    }