diff --git a/han-note-gateway/src/main/resources/application.yml b/han-note-gateway/src/main/resources/application.yml index d01cb9a..f8913b7 100644 --- a/han-note-gateway/src/main/resources/application.yml +++ b/han-note-gateway/src/main/resources/application.yml @@ -16,6 +16,12 @@ spring: - Path=/user/** filters: - StripPrefix=1 + - id: note + uri: lb://han-note-note + predicates: + - Path=/note/** + filters: + - StripPrefix=1 data: redis: database: 5 # Redis 数据库索引(默认为 0) diff --git a/han-note-kv/han-note-kv-api/src/main/java/com/hanserwei/hannote/kv/api/KeyValueFeignApi.java b/han-note-kv/han-note-kv-api/src/main/java/com/hanserwei/hannote/kv/api/KeyValueFeignApi.java index 46ce29f..d37a23d 100644 --- a/han-note-kv/han-note-kv-api/src/main/java/com/hanserwei/hannote/kv/api/KeyValueFeignApi.java +++ b/han-note-kv/han-note-kv-api/src/main/java/com/hanserwei/hannote/kv/api/KeyValueFeignApi.java @@ -3,6 +3,7 @@ package com.hanserwei.hannote.kv.api; import com.hanserwei.framework.common.response.Response; import com.hanserwei.hannote.kv.constant.ApiConstants; import com.hanserwei.hannote.kv.dto.req.AddNoteContentReqDTO; +import com.hanserwei.hannote.kv.dto.req.DeleteNoteContentReqDTO; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; @@ -19,6 +20,6 @@ public interface KeyValueFeignApi { Response findNoteContent(@RequestBody AddNoteContentReqDTO addNoteContentReqDTO); @PostMapping(value = PREFIX + "/note/content/delete") - Response deleteNoteContent(@RequestBody AddNoteContentReqDTO addNoteContentReqDTO); + Response deleteNoteContent(@RequestBody DeleteNoteContentReqDTO deleteNoteContentReqDTO); } \ No newline at end of file diff --git a/han-note-kv/han-note-kv-api/src/main/java/com/hanserwei/hannote/kv/dto/req/AddNoteContentReqDTO.java b/han-note-kv/han-note-kv-api/src/main/java/com/hanserwei/hannote/kv/dto/req/AddNoteContentReqDTO.java index c5299db..8212450 100644 --- a/han-note-kv/han-note-kv-api/src/main/java/com/hanserwei/hannote/kv/dto/req/AddNoteContentReqDTO.java +++ b/han-note-kv/han-note-kv-api/src/main/java/com/hanserwei/hannote/kv/dto/req/AddNoteContentReqDTO.java @@ -13,8 +13,8 @@ import lombok.NoArgsConstructor; @Builder public class AddNoteContentReqDTO { - @NotNull(message = "笔记 ID 不能为空") - private Long noteId; + @NotNull(message = "笔记内容 UUID 不能为空") + private String uuid; @NotBlank(message = "笔记内容不能为空") private String content; diff --git a/han-note-kv/han-note-kv-api/src/main/java/com/hanserwei/hannote/kv/dto/req/DeleteNoteContentReqDTO.java b/han-note-kv/han-note-kv-api/src/main/java/com/hanserwei/hannote/kv/dto/req/DeleteNoteContentReqDTO.java index 7def538..151ce76 100644 --- a/han-note-kv/han-note-kv-api/src/main/java/com/hanserwei/hannote/kv/dto/req/DeleteNoteContentReqDTO.java +++ b/han-note-kv/han-note-kv-api/src/main/java/com/hanserwei/hannote/kv/dto/req/DeleteNoteContentReqDTO.java @@ -12,7 +12,7 @@ import lombok.NoArgsConstructor; @Builder public class DeleteNoteContentReqDTO { - @NotBlank(message = "笔记 ID 不能为空") - private String noteId; + @NotBlank(message = "笔记 UUID 不能为空") + private String uuid; } \ No newline at end of file diff --git a/han-note-kv/han-note-kv-api/src/main/java/com/hanserwei/hannote/kv/dto/req/FindNoteContentReqDTO.java b/han-note-kv/han-note-kv-api/src/main/java/com/hanserwei/hannote/kv/dto/req/FindNoteContentReqDTO.java index 77f43ba..39505b6 100644 --- a/han-note-kv/han-note-kv-api/src/main/java/com/hanserwei/hannote/kv/dto/req/FindNoteContentReqDTO.java +++ b/han-note-kv/han-note-kv-api/src/main/java/com/hanserwei/hannote/kv/dto/req/FindNoteContentReqDTO.java @@ -12,7 +12,7 @@ import lombok.NoArgsConstructor; @Builder public class FindNoteContentReqDTO { - @NotBlank(message = "笔记 ID 不能为空") - private String noteId; + @NotBlank(message = "笔记 UUID 不能为空") + private String uuid; } \ No newline at end of file diff --git a/han-note-kv/han-note-kv-biz/src/main/java/com/hanserwei/hannote/kv/biz/service/impl/NoteContentServiceImpl.java b/han-note-kv/han-note-kv-biz/src/main/java/com/hanserwei/hannote/kv/biz/service/impl/NoteContentServiceImpl.java index 03e06a8..c2fadec 100644 --- a/han-note-kv/han-note-kv-biz/src/main/java/com/hanserwei/hannote/kv/biz/service/impl/NoteContentServiceImpl.java +++ b/han-note-kv/han-note-kv-biz/src/main/java/com/hanserwei/hannote/kv/biz/service/impl/NoteContentServiceImpl.java @@ -27,12 +27,12 @@ public class NoteContentServiceImpl implements NoteContentService { @Override public Response addNoteContent(AddNoteContentReqDTO addNoteContentReqDTO) { // 笔记ID - Long noteId = addNoteContentReqDTO.getNoteId(); + String noteId = addNoteContentReqDTO.getUuid(); // 笔记内容 String content = addNoteContentReqDTO.getContent(); NoteContentDO noteContent = NoteContentDO.builder() - .id(UUID.randomUUID()) + .id(UUID.fromString(noteId)) .content(content) .build(); @@ -44,7 +44,7 @@ public class NoteContentServiceImpl implements NoteContentService { @Override public Response findNoteContent(FindNoteContentReqDTO findNoteContentReqDTO) { // 笔记ID - String noteId = findNoteContentReqDTO.getNoteId(); + String noteId = findNoteContentReqDTO.getUuid(); Optional optional = noteContentRepository.findById(UUID.fromString(noteId)); if (optional.isEmpty()){ throw new ApiException(ResponseCodeEnum.NOTE_CONTENT_NOT_FOUND); @@ -60,7 +60,7 @@ public class NoteContentServiceImpl implements NoteContentService { @Override public Response deleteNoteContent(DeleteNoteContentReqDTO deleteNoteContentReqDTO) { - String noteId = deleteNoteContentReqDTO.getNoteId(); + String noteId = deleteNoteContentReqDTO.getUuid(); noteContentRepository.deleteById(UUID.fromString(noteId)); return Response.success(); } diff --git a/han-note-note/han-note-note-biz/pom.xml b/han-note-note/han-note-note-biz/pom.xml index 1b8aa87..8fa4e54 100644 --- a/han-note-note/han-note-note-biz/pom.xml +++ b/han-note-note/han-note-note-biz/pom.xml @@ -91,6 +91,15 @@ commons-pool2 + + com.hanserwei + han-note-kv-api + + + + com.hanserwei + han-note-distributed-id-generator-api + diff --git a/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/HannoteNoteBizApplication.java b/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/HannoteNoteBizApplication.java index 6087c23..c7115fc 100644 --- a/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/HannoteNoteBizApplication.java +++ b/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/HannoteNoteBizApplication.java @@ -1,9 +1,13 @@ package com.hanserwei.hannote.note.biz; +import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication +@MapperScan("com.hanserwei.hannote.note.biz.domain.mapper") +@EnableFeignClients(basePackages = "com.hanserwei.hannote") public class HannoteNoteBizApplication { public static void main(String[] args) { SpringApplication.run(HannoteNoteBizApplication.class, args); diff --git a/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/controller/NoteController.java b/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/controller/NoteController.java new file mode 100644 index 0000000..4376781 --- /dev/null +++ b/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/controller/NoteController.java @@ -0,0 +1,29 @@ +package com.hanserwei.hannote.note.biz.controller; + +import com.hanserwei.framework.biz.operationlog.aspect.ApiOperationLog; +import com.hanserwei.framework.common.response.Response; +import com.hanserwei.hannote.note.biz.model.vo.PublishNoteReqVO; +import com.hanserwei.hannote.note.biz.service.NoteService; +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; + +@RestController +@RequestMapping("/note") +@Slf4j +public class NoteController { + + @Resource + private NoteService noteService; + + @PostMapping(value = "/publish") + @ApiOperationLog(description = "笔记发布") + public Response publishNote(@Validated @RequestBody PublishNoteReqVO publishNoteReqVO) { + return noteService.publishNote(publishNoteReqVO); + } + +} diff --git a/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/domain/dataobject/ChannelDO.java b/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/domain/dataobject/ChannelDO.java index 2f459d5..070ea64 100644 --- a/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/domain/dataobject/ChannelDO.java +++ b/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/domain/dataobject/ChannelDO.java @@ -4,15 +4,18 @@ import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; -import java.util.Date; import lombok.AllArgsConstructor; +import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; +import java.time.LocalDateTime; + /** * 频道表 */ @Data +@Builder @AllArgsConstructor @NoArgsConstructor @TableName(value = "t_channel") @@ -33,13 +36,13 @@ public class ChannelDO { * 创建时间 */ @TableField(value = "create_time") - private Date createTime; + private LocalDateTime createTime; /** * 更新时间 */ @TableField(value = "update_time") - private Date updateTime; + private LocalDateTime updateTime; /** * 逻辑删除(0:未删除 1:已删除) diff --git a/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/domain/dataobject/ChannelTopicRelDO.java b/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/domain/dataobject/ChannelTopicRelDO.java index b311fbc..c04e90c 100644 --- a/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/domain/dataobject/ChannelTopicRelDO.java +++ b/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/domain/dataobject/ChannelTopicRelDO.java @@ -4,15 +4,18 @@ import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; -import java.util.Date; import lombok.AllArgsConstructor; +import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; +import java.time.LocalDateTime; + /** * 频道-话题关联表 */ @Data +@Builder @AllArgsConstructor @NoArgsConstructor @TableName(value = "t_channel_topic_rel") @@ -39,11 +42,11 @@ public class ChannelTopicRelDO { * 创建时间 */ @TableField(value = "create_time") - private Date createTime; + private LocalDateTime createTime; /** * 更新时间 */ @TableField(value = "update_time") - private Date updateTime; + private LocalDateTime updateTime; } \ No newline at end of file diff --git a/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/domain/dataobject/NoteDO.java b/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/domain/dataobject/NoteDO.java index a46c3a4..ff511ed 100644 --- a/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/domain/dataobject/NoteDO.java +++ b/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/domain/dataobject/NoteDO.java @@ -4,15 +4,18 @@ import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; -import java.util.Date; import lombok.AllArgsConstructor; +import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; +import java.time.LocalDateTime; + /** * 笔记表 */ @Data +@Builder @AllArgsConstructor @NoArgsConstructor @TableName(value = "t_note") @@ -63,7 +66,7 @@ public class NoteDO { * 类型(0:图文 1:视频) */ @TableField(value = "`type`") - private Byte type; + private Integer type; /** * 笔记图片链接(逗号隔开) @@ -81,23 +84,29 @@ public class NoteDO { * 可见范围(0:公开,所有人可见 1:仅对自己可见) */ @TableField(value = "visible") - private Byte visible; + private Integer visible; /** * 创建时间 */ @TableField(value = "create_time") - private Date createTime; + private LocalDateTime createTime; /** * 更新时间 */ @TableField(value = "update_time") - private Date updateTime; + private LocalDateTime updateTime; /** * 状态(0:待审核 1:正常展示 2:被删除(逻辑删除) 3:被下架) */ @TableField(value = "`status`") - private Byte status; + private Integer status; + + /** + * 笔记内容UUID + */ + @TableField(value = "content_uuid") + private String contentUuid; } \ No newline at end of file diff --git a/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/domain/dataobject/TopicDO.java b/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/domain/dataobject/TopicDO.java index 00635eb..d35cdec 100644 --- a/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/domain/dataobject/TopicDO.java +++ b/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/domain/dataobject/TopicDO.java @@ -4,15 +4,18 @@ import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; -import java.util.Date; import lombok.AllArgsConstructor; +import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; +import java.time.LocalDateTime; + /** * 话题表 */ @Data +@Builder @AllArgsConstructor @NoArgsConstructor @TableName(value = "t_topic") @@ -33,13 +36,13 @@ public class TopicDO { * 创建时间 */ @TableField(value = "create_time") - private Date createTime; + private LocalDateTime createTime; /** * 更新时间 */ @TableField(value = "update_time") - private Date updateTime; + private LocalDateTime updateTime; /** * 逻辑删除(0:未删除 1:已删除) diff --git a/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/domain/mapper/ChannelDOMapper.java b/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/domain/mapper/ChannelDOMapper.java index bb4d210..b01a1e5 100644 --- a/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/domain/mapper/ChannelDOMapper.java +++ b/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/domain/mapper/ChannelDOMapper.java @@ -2,6 +2,8 @@ package com.hanserwei.hannote.note.biz.domain.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.hanserwei.hannote.note.biz.domain.dataobject.ChannelDO; +import org.apache.ibatis.annotations.Mapper; +@Mapper public interface ChannelDOMapper extends BaseMapper { } \ No newline at end of file diff --git a/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/domain/mapper/ChannelTopicRelDOMapper.java b/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/domain/mapper/ChannelTopicRelDOMapper.java index 2f26c5e..c89eeab 100644 --- a/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/domain/mapper/ChannelTopicRelDOMapper.java +++ b/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/domain/mapper/ChannelTopicRelDOMapper.java @@ -2,6 +2,8 @@ package com.hanserwei.hannote.note.biz.domain.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.hanserwei.hannote.note.biz.domain.dataobject.ChannelTopicRelDO; +import org.apache.ibatis.annotations.Mapper; +@Mapper public interface ChannelTopicRelDOMapper extends BaseMapper { } \ No newline at end of file diff --git a/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/domain/mapper/NoteDOMapper.java b/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/domain/mapper/NoteDOMapper.java index f4b1df1..c1a77ee 100644 --- a/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/domain/mapper/NoteDOMapper.java +++ b/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/domain/mapper/NoteDOMapper.java @@ -2,6 +2,8 @@ package com.hanserwei.hannote.note.biz.domain.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.hanserwei.hannote.note.biz.domain.dataobject.NoteDO; +import org.apache.ibatis.annotations.Mapper; +@Mapper public interface NoteDOMapper extends BaseMapper { } \ No newline at end of file diff --git a/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/domain/mapper/TopicDOMapper.java b/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/domain/mapper/TopicDOMapper.java index 6592009..649db65 100644 --- a/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/domain/mapper/TopicDOMapper.java +++ b/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/domain/mapper/TopicDOMapper.java @@ -2,6 +2,8 @@ package com.hanserwei.hannote.note.biz.domain.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.hanserwei.hannote.note.biz.domain.dataobject.TopicDO; +import org.apache.ibatis.annotations.Mapper; +@Mapper public interface TopicDOMapper extends BaseMapper { } \ No newline at end of file diff --git a/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/enums/ResponseCodeEnum.java b/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/enums/ResponseCodeEnum.java index 093916d..df34cdb 100644 --- a/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/enums/ResponseCodeEnum.java +++ b/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/enums/ResponseCodeEnum.java @@ -13,6 +13,8 @@ public enum ResponseCodeEnum implements BaseExceptionInterface { PARAM_NOT_VALID("NOTE-10001", "参数错误"), // ----------- 业务异常状态码 ----------- + NOTE_TYPE_ERROR("NOTE-20000", "未知的笔记类型"), + NOTE_PUBLISH_FAIL("NOTE-20001", "笔记发布失败"), ; // 异常码 diff --git a/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/model/vo/PublishNoteReqVO.java b/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/model/vo/PublishNoteReqVO.java new file mode 100644 index 0000000..146de5c --- /dev/null +++ b/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/model/vo/PublishNoteReqVO.java @@ -0,0 +1,29 @@ +package com.hanserwei.hannote.note.biz.model.vo; + +import jakarta.validation.constraints.NotNull; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.List; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@Builder +public class PublishNoteReqVO { + + @NotNull(message = "笔记类型不能为空") + private Integer type; + + private List imgUris; + + private String videoUri; + + private String title; + + private String content; + + private Long topicId; +} \ No newline at end of file diff --git a/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/rpc/DistributedIdGeneratorRpcService.java b/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/rpc/DistributedIdGeneratorRpcService.java new file mode 100644 index 0000000..71ec24a --- /dev/null +++ b/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/rpc/DistributedIdGeneratorRpcService.java @@ -0,0 +1,22 @@ +package com.hanserwei.hannote.note.biz.rpc; + +import com.hanserwei.hannote.distributed.id.generator.api.DistributedIdGeneratorFeignApi; +import jakarta.annotation.Resource; +import org.springframework.stereotype.Component; + +@Component +public class DistributedIdGeneratorRpcService { + + @Resource + private DistributedIdGeneratorFeignApi distributedIdGeneratorFeignApi; + + /** + * 生成雪花算法 ID + * + * @return 雪花算法 ID + */ + public String getSnowflakeId() { + return distributedIdGeneratorFeignApi.getSnowflakeId("test"); + } + +} \ No newline at end of file diff --git a/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/rpc/KeyValueRpcService.java b/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/rpc/KeyValueRpcService.java new file mode 100644 index 0000000..460b008 --- /dev/null +++ b/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/rpc/KeyValueRpcService.java @@ -0,0 +1,50 @@ +package com.hanserwei.hannote.note.biz.rpc; + +import com.hanserwei.framework.common.response.Response; +import com.hanserwei.hannote.kv.api.KeyValueFeignApi; +import com.hanserwei.hannote.kv.dto.req.AddNoteContentReqDTO; +import com.hanserwei.hannote.kv.dto.req.DeleteNoteContentReqDTO; +import jakarta.annotation.Resource; +import org.springframework.stereotype.Component; + +import java.util.Objects; + +@Component +public class KeyValueRpcService { + + @Resource + private KeyValueFeignApi keyValueFeignApi; + + /** + * 保存笔记内容 + * + * @param uuid 笔记UUID + * @param content 笔记内容 + * @return 是否成功 + */ + public boolean saveNoteContent(String uuid, String content) { + AddNoteContentReqDTO addNoteContentReqDTO = new AddNoteContentReqDTO(); + addNoteContentReqDTO.setUuid(uuid); + addNoteContentReqDTO.setContent(content); + + Response response = keyValueFeignApi.addNoteContent(addNoteContentReqDTO); + + return Objects.nonNull(response) && response.isSuccess(); + } + + /** + * 删除笔记内容 + * + * @param uuid 笔记UUID + * @return 是否成功 + */ + public boolean deleteNoteContent(String uuid) { + DeleteNoteContentReqDTO deleteNoteContentReqDTO = new DeleteNoteContentReqDTO(); + deleteNoteContentReqDTO.setUuid(uuid); + + Response response = keyValueFeignApi.deleteNoteContent(deleteNoteContentReqDTO); + + return Objects.nonNull(response) && response.isSuccess(); + } + +} \ No newline at end of file diff --git a/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/service/ChannelDOService.java b/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/service/ChannelDOService.java new file mode 100644 index 0000000..30b2fb4 --- /dev/null +++ b/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/service/ChannelDOService.java @@ -0,0 +1,8 @@ +package com.hanserwei.hannote.note.biz.service; + +import com.hanserwei.hannote.note.biz.domain.dataobject.ChannelDO; +import com.baomidou.mybatisplus.extension.service.IService; +public interface ChannelDOService extends IService{ + + +} diff --git a/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/service/ChannelTopicRelDOService.java b/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/service/ChannelTopicRelDOService.java new file mode 100644 index 0000000..d79a830 --- /dev/null +++ b/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/service/ChannelTopicRelDOService.java @@ -0,0 +1,8 @@ +package com.hanserwei.hannote.note.biz.service; + +import com.hanserwei.hannote.note.biz.domain.dataobject.ChannelTopicRelDO; +import com.baomidou.mybatisplus.extension.service.IService; +public interface ChannelTopicRelDOService extends IService{ + + +} diff --git a/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/service/NoteService.java b/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/service/NoteService.java new file mode 100644 index 0000000..af596cc --- /dev/null +++ b/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/service/NoteService.java @@ -0,0 +1,17 @@ +package com.hanserwei.hannote.note.biz.service; + +import com.baomidou.mybatisplus.extension.service.IService; +import com.hanserwei.framework.common.response.Response; +import com.hanserwei.hannote.note.biz.domain.dataobject.NoteDO; +import com.hanserwei.hannote.note.biz.model.vo.PublishNoteReqVO; + +public interface NoteService extends IService { + + /** + * 笔记发布 + * @param publishNoteReqVO 笔记发布请求 + * @return 笔记发布结果 + */ + Response publishNote(PublishNoteReqVO publishNoteReqVO); + +} \ No newline at end of file diff --git a/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/service/TopicDOService.java b/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/service/TopicDOService.java new file mode 100644 index 0000000..dfd1ad9 --- /dev/null +++ b/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/service/TopicDOService.java @@ -0,0 +1,8 @@ +package com.hanserwei.hannote.note.biz.service; + +import com.hanserwei.hannote.note.biz.domain.dataobject.TopicDO; +import com.baomidou.mybatisplus.extension.service.IService; +public interface TopicDOService extends IService{ + + +} diff --git a/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/service/impl/ChannelDOServiceImpl.java b/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/service/impl/ChannelDOServiceImpl.java new file mode 100644 index 0000000..aa1babf --- /dev/null +++ b/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/service/impl/ChannelDOServiceImpl.java @@ -0,0 +1,12 @@ +package com.hanserwei.hannote.note.biz.service.impl; + +import com.hanserwei.hannote.note.biz.service.ChannelDOService; +import org.springframework.stereotype.Service; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.hanserwei.hannote.note.biz.domain.dataobject.ChannelDO; +import com.hanserwei.hannote.note.biz.domain.mapper.ChannelDOMapper; + +@Service +public class ChannelDOServiceImpl extends ServiceImpl implements ChannelDOService { + +} diff --git a/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/service/impl/ChannelTopicRelDOServiceImpl.java b/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/service/impl/ChannelTopicRelDOServiceImpl.java new file mode 100644 index 0000000..574b950 --- /dev/null +++ b/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/service/impl/ChannelTopicRelDOServiceImpl.java @@ -0,0 +1,12 @@ +package com.hanserwei.hannote.note.biz.service.impl; + +import com.hanserwei.hannote.note.biz.service.ChannelTopicRelDOService; +import org.springframework.stereotype.Service; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.hanserwei.hannote.note.biz.domain.mapper.ChannelTopicRelDOMapper; +import com.hanserwei.hannote.note.biz.domain.dataobject.ChannelTopicRelDO; + +@Service +public class ChannelTopicRelDOServiceImpl extends ServiceImpl implements ChannelTopicRelDOService { + +} diff --git a/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/service/impl/NoteServiceImpl.java b/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/service/impl/NoteServiceImpl.java new file mode 100644 index 0000000..bea94ad --- /dev/null +++ b/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/service/impl/NoteServiceImpl.java @@ -0,0 +1,135 @@ +package com.hanserwei.hannote.note.biz.service.impl; + +import cn.hutool.core.collection.CollUtil; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.google.common.base.Preconditions; +import com.hanserwei.framework.biz.context.holder.LoginUserContextHolder; +import com.hanserwei.framework.common.exception.ApiException; +import com.hanserwei.framework.common.response.Response; +import com.hanserwei.hannote.note.biz.domain.dataobject.NoteDO; +import com.hanserwei.hannote.note.biz.domain.mapper.NoteDOMapper; +import com.hanserwei.hannote.note.biz.enums.NoteStatusEnum; +import com.hanserwei.hannote.note.biz.enums.NoteTypeEnum; +import com.hanserwei.hannote.note.biz.enums.NoteVisibleEnum; +import com.hanserwei.hannote.note.biz.enums.ResponseCodeEnum; +import com.hanserwei.hannote.note.biz.model.vo.PublishNoteReqVO; +import com.hanserwei.hannote.note.biz.rpc.DistributedIdGeneratorRpcService; +import com.hanserwei.hannote.note.biz.rpc.KeyValueRpcService; +import com.hanserwei.hannote.note.biz.service.NoteService; +import com.hanserwei.hannote.note.biz.service.TopicDOService; +import jakarta.annotation.Resource; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.springframework.stereotype.Service; + +import java.time.LocalDateTime; +import java.util.List; +import java.util.Objects; +import java.util.UUID; + +@Slf4j +@Service +public class NoteServiceImpl extends ServiceImpl implements NoteService { + @Resource + private DistributedIdGeneratorRpcService distributedIdGeneratorRpcService; + @Resource + private KeyValueRpcService keyValueRpcService; + @Resource + private TopicDOService topicDOService; + + @Override + public Response publishNote(PublishNoteReqVO publishNoteReqVO) { + // 笔记类型 + Integer type = publishNoteReqVO.getType(); + + // 获取对应的枚举类型 + NoteTypeEnum noteTypeEnum = NoteTypeEnum.valueOf(type); + + // 若非图文或视频则抛异常 + if (Objects.isNull(noteTypeEnum)) { + throw new ApiException(ResponseCodeEnum.NOTE_TYPE_ERROR); + } + + String imgUris = null; + // 笔记内容是否为空,默认为空,即true + boolean isContentEmpty = true; + String videoUri = null; + switch (noteTypeEnum) { + case IMAGE_TEXT -> { + List imgUriList = publishNoteReqVO.getImgUris(); + //校验图片是否为空 + Preconditions.checkArgument(CollUtil.isNotEmpty(imgUriList), "笔记图片不能为空!"); + //校验图片数目 + Preconditions.checkArgument(imgUriList.size() <= 8, "图片不能超过8张!"); + //把图片uri拼接成字符串,逗号隔开 + imgUris = String.join(",", imgUriList); + } + case VIDEO -> { + videoUri = publishNoteReqVO.getVideoUri(); + //校验视频是否为空 + Preconditions.checkArgument(StringUtils.isNoneBlank(videoUri), "笔记视频不能为空!"); + } + default -> { + } + } + + // RPC:调用分布式ID生成服务,生成笔记ID + String snowflakeId = distributedIdGeneratorRpcService.getSnowflakeId(); + // 笔记内容UUID + String contentUuid = null; + // 笔记内容 + String content = publishNoteReqVO.getContent(); + // 若用户填写了笔记内容,则调用KV服务 + if (StringUtils.isNotBlank(content)) { + isContentEmpty = false; + // 生成笔记内容UUID + contentUuid = UUID.randomUUID().toString(); + // RPC:调用KV服务,保存笔记内容 + boolean isSaveSuccess = keyValueRpcService.saveNoteContent(contentUuid, content); + // 若保存笔记内容失败,则抛异常 + if (!isSaveSuccess) { + throw new ApiException(ResponseCodeEnum.NOTE_PUBLISH_FAIL); + } + } + + // 话题 + Long topicId = publishNoteReqVO.getTopicId(); + String topicName = null; + if (Objects.nonNull(topicId)) { + //获取话题名称 + topicName = topicDOService.getById(topicId).getName(); + } + + // 发布者ID + Long creatorId = LoginUserContextHolder.getUserId(); + + // 构建笔记对象 + NoteDO noteDO = NoteDO.builder() + .id(Long.valueOf(snowflakeId)) + .isContentEmpty(isContentEmpty) + .creatorId(creatorId) + .imgUris(imgUris) + .title(publishNoteReqVO.getTitle()) + .topicId(publishNoteReqVO.getTopicId()) + .topicName(topicName) + .type(type) + .visible(NoteVisibleEnum.PUBLIC.getCode()) + .createTime(LocalDateTime.now()) + .updateTime(LocalDateTime.now()) + .status(NoteStatusEnum.NORMAL.getCode()) + .isTop(Boolean.FALSE) + .videoUri(videoUri) + .contentUuid(contentUuid) + .build(); + try { + boolean isSaveSuccess = this.save(noteDO); + } catch (Exception e) { + log.error("保存笔记失败!", e); + // RPC:调用KV服务,删除笔记内容 + if (StringUtils.isNotBlank(contentUuid)) { + keyValueRpcService.deleteNoteContent(contentUuid); + } + } + return Response.success(); + } +} diff --git a/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/service/impl/TopicDOServiceImpl.java b/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/service/impl/TopicDOServiceImpl.java new file mode 100644 index 0000000..46f99a3 --- /dev/null +++ b/han-note-note/han-note-note-biz/src/main/java/com/hanserwei/hannote/note/biz/service/impl/TopicDOServiceImpl.java @@ -0,0 +1,12 @@ +package com.hanserwei.hannote.note.biz.service.impl; + +import com.hanserwei.hannote.note.biz.service.TopicDOService; +import org.springframework.stereotype.Service; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.hanserwei.hannote.note.biz.domain.dataobject.TopicDO; +import com.hanserwei.hannote.note.biz.domain.mapper.TopicDOMapper; + +@Service +public class TopicDOServiceImpl extends ServiceImpl implements TopicDOService { + +} diff --git a/han-note-note/han-note-note-biz/src/main/resources/mapperxml/NoteDOMapper.xml b/han-note-note/han-note-note-biz/src/main/resources/mapperxml/NoteDOMapper.xml index a03020b..e972445 100644 --- a/han-note-note/han-note-note-biz/src/main/resources/mapperxml/NoteDOMapper.xml +++ b/han-note-note/han-note-note-biz/src/main/resources/mapperxml/NoteDOMapper.xml @@ -18,10 +18,11 @@ + id, title, is_content_empty, creator_id, topic_id, topic_name, is_top, `type`, img_uris, - video_uri, visible, create_time, update_time, `status` + video_uri, visible, create_time, update_time, `status`, content_uuid \ No newline at end of file diff --git a/sql/createData.sql b/sql/createData.sql index 35a249b..649faf2 100644 --- a/sql/createData.sql +++ b/sql/createData.sql @@ -15,3 +15,12 @@ VALUES (1, 1, 1, now(), now(), b'0'); INSERT INTO `t_role_permission_rel` (`id`, `role_id`, `permission_id`, `create_time`, `update_time`, `is_deleted`) VALUES (2, 1, 2, now(), now(), b'0'); + +INSERT INTO `han_note`.`t_channel` (`name`, `create_time`, `update_time`, `is_deleted`) VALUES ('美食', now(), now(), 0); +INSERT INTO `han_note`.`t_channel` (`name`, `create_time`, `update_time`, `is_deleted`) VALUES ('娱乐', now(), now(), 0); + +INSERT INTO `han_note`.`t_topic` (`name`, `create_time`, `update_time`, `is_deleted`) VALUES ('高分美剧推荐', now(), now(), 0); +INSERT INTO `han_note`.`t_topic` (`name`, `create_time`, `update_time`, `is_deleted`) VALUES ('下饭综艺推荐', now(), now(), 0); + +INSERT INTO `han_note`.`t_channel_topic_rel` (`channel_id`, `topic_id`, `create_time`, `update_time`) VALUES (2, 1, now(), now()); +INSERT INTO `han_note`.`t_channel_topic_rel` (`channel_id`, `topic_id`, `create_time`, `update_time`) VALUES (2, 2, now(), now()); diff --git a/sql/createTable.sql b/sql/createTable.sql index 4c8e2c3..394b510 100644 --- a/sql/createTable.sql +++ b/sql/createTable.sql @@ -146,4 +146,6 @@ CREATE TABLE `t_note` KEY `idx_status` (`status`) ) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 - COLLATE = utf8mb4_unicode_ci COMMENT ='笔记表'; \ No newline at end of file + COLLATE = utf8mb4_unicode_ci COMMENT ='笔记表'; + +ALTER table t_note add column `content_uuid` varchar(36) DEFAULT '' COMMENT '笔记内容UUID';