6.9 KiB
6.9 KiB
title, url, publishedTime
| title | url | publishedTime |
|---|---|---|
| 笔记内容删除接口开发 - 犬小哈专栏 | https://www.quanxiaoha.com/column/10325.html | null |
本小节中,我们将为 KV 键值存储服务添加 —— 笔记内容删除接口。
接口定义
接口地址
POST /kv/note/content/delete
入参
{
"noteId": "15382b55-b351-4d11-ac1a-860d7bc005fb" // 笔记 ID
}
出参
{
"success": true,
"message": null,
"errorCode": null,
"data": null
}
创建入参 DTO
编辑 xiaohashu-kv-api 模块,在 /dto/req 包下创建 DeleteNoteContentReqDTO 入参实体类,代码如下:
package com.quanxiaoha.xiaohashu.kv.dto.req;
import jakarta.validation.constraints.NotBlank;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author: 犬小哈
* @date: 2024/4/7 15:17
* @version: v1.0.0
* @description: 笔记内容删除
**/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class DeleteNoteContentReqDTO {
@NotBlank(message = "笔记 ID 不能为空")
private String noteId;
}
编写 service 业务层
接着,回到 xiaohashu-kv-biz 业务模块中,编辑 NoteContentService 业务接口,声明一个删除笔记内容方法,代码如下:
package com.quanxiaoha.xiaohashu.kv.biz.service;
import com.quanxiaoha.framework.common.response.Response;
import com.quanxiaoha.xiaohashu.kv.dto.req.AddNoteContentReqDTO;
import com.quanxiaoha.xiaohashu.kv.dto.req.DeleteNoteContentReqDTO;
import com.quanxiaoha.xiaohashu.kv.dto.req.FindNoteContentReqDTO;
import com.quanxiaoha.xiaohashu.kv.dto.rsp.FindNoteContentRspDTO;
/**
* @author: 犬小哈
* @date: 2024/4/7 15:41
* @version: v1.0.0
* @description: 笔记内容存储业务
**/
public interface NoteContentService {
// 省略...
/**
* 删除笔记内容
*
* @param deleteNoteContentReqDTO
* @return
*/
Response<?> deleteNoteContent(DeleteNoteContentReqDTO deleteNoteContentReqDTO);
}
在其实现类 NoteContentServiceImpl 中,实现上述方法,代码如下:
package com.quanxiaoha.xiaohashu.kv.biz.service.impl;
import com.quanxiaoha.framework.common.exception.BizException;
import com.quanxiaoha.framework.common.response.Response;
import com.quanxiaoha.xiaohashu.kv.biz.domain.dataobject.NoteContentDO;
import com.quanxiaoha.xiaohashu.kv.biz.domain.repository.NoteContentRepository;
import com.quanxiaoha.xiaohashu.kv.biz.enums.ResponseCodeEnum;
import com.quanxiaoha.xiaohashu.kv.biz.service.NoteContentService;
import com.quanxiaoha.xiaohashu.kv.dto.req.AddNoteContentReqDTO;
import com.quanxiaoha.xiaohashu.kv.dto.req.DeleteNoteContentReqDTO;
import com.quanxiaoha.xiaohashu.kv.dto.req.FindNoteContentReqDTO;
import com.quanxiaoha.xiaohashu.kv.dto.rsp.FindNoteContentRspDTO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.Optional;
import java.util.UUID;
/**
* @author: 犬小哈
* @date: 2024/4/7 15:41
* @version: v1.0.0
* @description: 笔记内容 Key-Value 业务
**/
@Service
@Slf4j
public class NoteContentServiceImpl implements NoteContentService {
@Resource
private NoteContentRepository noteContentRepository;
// 省略...
/**
* 删除笔记内容
*
* @param deleteNoteContentReqDTO
* @return
*/
@Override
public Response<?> deleteNoteContent(DeleteNoteContentReqDTO deleteNoteContentReqDTO) {
// 笔记 ID
String noteId = deleteNoteContentReqDTO.getNoteId();
// 删除笔记内容
noteContentRepository.deleteById(UUID.fromString(noteId));
return Response.success();
}
}
添加 controller 接口
编辑 NoteContentController 控制器,添加 /kv/note/content/delete 接口,代码如下:
package com.quanxiaoha.xiaohashu.kv.biz.controller;
import com.quanxiaoha.framework.common.response.Response;
import com.quanxiaoha.xiaohashu.kv.biz.service.NoteContentService;
import com.quanxiaoha.xiaohashu.kv.dto.req.AddNoteContentReqDTO;
import com.quanxiaoha.xiaohashu.kv.dto.req.DeleteNoteContentReqDTO;
import com.quanxiaoha.xiaohashu.kv.dto.req.FindNoteContentReqDTO;
import com.quanxiaoha.xiaohashu.kv.dto.rsp.FindNoteContentRspDTO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author: 犬小哈
* @date: 2024/4/4 13:22
* @version: v1.0.0
* @description: 笔记内容
**/
@RestController
@RequestMapping("/kv")
@Slf4j
public class NoteContentController {
@Resource
private NoteContentService noteContentService;
// 省略...
@PostMapping(value = "/note/content/delete")
public Response<?> deleteNoteContent(@Validated @RequestBody DeleteNoteContentReqDTO deleteNoteContentReqDTO) {
return noteContentService.deleteNoteContent(deleteNoteContentReqDTO);
}
}
自测一波
重启 KV 键值服务,并调试一波接口功能是否好使,如下图所示:
服务端响应成功,然后打开 Cassandra 命令行,确认一下该笔记 ID 对应的记录是否真的删除了,如下图所示,查询结果为空表示功能正常:
添加 Feign 客户端接口
最后,编辑 xiaohashu-kv-api 模块中的 KeyValueFeignApi 接口,将笔记内容删除接口提供出去,方便后续其他服务直接调用:
package com.quanxiaoha.xiaohashu.kv.api;
import com.quanxiaoha.framework.common.response.Response;
import com.quanxiaoha.xiaohashu.kv.constant.ApiConstants;
import com.quanxiaoha.xiaohashu.kv.dto.req.AddNoteContentReqDTO;
import com.quanxiaoha.xiaohashu.kv.dto.req.DeleteNoteContentReqDTO;
import com.quanxiaoha.xiaohashu.kv.dto.req.FindNoteContentReqDTO;
import com.quanxiaoha.xiaohashu.kv.dto.rsp.FindNoteContentRspDTO;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
/**
* @author: 犬小哈
* @date: 2024/4/13 22:56
* @version: v1.0.0
* @description: K-V 键值存储 Feign 接口
**/
@FeignClient(name = ApiConstants.SERVICE_NAME)
public interface KeyValueFeignApi {
String PREFIX = "/kv";
// 省略...
@PostMapping(value = PREFIX + "/note/content/delete")
Response<?> deleteNoteContent(@RequestBody DeleteNoteContentReqDTO deleteNoteContentReqDTO);
}