Skip to content

Commit

Permalink
fix: attachment ref remove issue (#670)
Browse files Browse the repository at this point in the history
* optimize: 优化附件删除校验逻辑

* feat: add AttachmentReferenceEndpoint.removeAllByTypeAndReferenceId api.

* build: gen new api-client v0.15.7.1

* fix: 修复批量移除绑定无法移除不存在的附件的附件引用问题

* optimize: 附件删除时不校验附件关系
  • Loading branch information
chivehao authored Sep 19, 2024
1 parent b870c57 commit d2db49d
Show file tree
Hide file tree
Showing 20 changed files with 302 additions and 55 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

更新日志文档,版本顺序从新到旧,最新版本在最前(上)面。

# 0.15.7

- 优化附件删除校验逻辑
- 修复批量移除绑定无法移除不存在的附件的附件引用问题
- 附件删除时不校验附件关系

# 0.15.6

## 新特性
Expand Down
3 changes: 3 additions & 0 deletions api/src/main/java/run/ikaros/api/core/task/TaskOperate.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package run.ikaros.api.core.task;

import java.time.Duration;
import reactor.core.publisher.Mono;
import run.ikaros.api.plugin.AllowPluginOperate;

public interface TaskOperate extends AllowPluginOperate {
Mono<Void> submit(String name, Runnable runnable);

Mono<Void> submit(String name, Runnable runnable, Duration delay);

Mono<Void> cancel(String name);
}
2 changes: 1 addition & 1 deletion console/packages/api-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ npm publish
选择当前目录下的更改进行`git add .`

```bash
git commit -am "build: gen new api-client v0.15.5.1"
git commit -am "build: gen new api-client v0.15.7.1"
```

合成版(powershell),升级 package.json 版本,并启动服务端后,在 api-client 路径下:
Expand Down
2 changes: 1 addition & 1 deletion console/packages/api-client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@runikaros/api-client",
"version": "0.15.5.1",
"version": "0.15.7.1",
"description": "Project ikaros console api-client package",
"type": "module",
"scripts": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,61 @@ export const V1alpha1AttachmentReferenceApiAxiosParamCreator = function (
options: localVarRequestOptions,
};
},
/**
* Remove references by type and referenceId
* @param {AttachmentReference} [attachmentReference]
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
removeAllByTypeAndReferenceId: async (
attachmentReference?: AttachmentReference,
options: AxiosRequestConfig = {}
): Promise<RequestArgs> => {
const localVarPath = `/api/v1alpha1/attachment/references`;
// use dummy base URL string because the URL constructor only accepts absolute URLs.
const localVarUrlObj = new URL(localVarPath, DUMMY_BASE_URL);
let baseOptions;
if (configuration) {
baseOptions = configuration.baseOptions;
}

const localVarRequestOptions = {
method: "DELETE",
...baseOptions,
...options,
};
const localVarHeaderParameter = {} as any;
const localVarQueryParameter = {} as any;

// authentication BasicAuth required
// http basic authentication required
setBasicAuthToObject(localVarRequestOptions, configuration);

// authentication BearerAuth required
// http bearer authentication required
await setBearerAuthToObject(localVarHeaderParameter, configuration);

localVarHeaderParameter["Content-Type"] = "application/json";

setSearchParams(localVarUrlObj, localVarQueryParameter);
let headersFromBaseOptions =
baseOptions && baseOptions.headers ? baseOptions.headers : {};
localVarRequestOptions.headers = {
...localVarHeaderParameter,
...headersFromBaseOptions,
...options.headers,
};
localVarRequestOptions.data = serializeDataIfNeeded(
attachmentReference,
localVarRequestOptions,
configuration
);

return {
url: toPathString(localVarUrlObj),
options: localVarRequestOptions,
};
},
/**
* Remove by type and attachmentId and referenceId
* @param {AttachmentReference} [attachmentReference]
Expand Down Expand Up @@ -512,6 +567,30 @@ export const V1alpha1AttachmentReferenceApiFp = function (
configuration
);
},
/**
* Remove references by type and referenceId
* @param {AttachmentReference} [attachmentReference]
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
async removeAllByTypeAndReferenceId(
attachmentReference?: AttachmentReference,
options?: AxiosRequestConfig
): Promise<
(axios?: AxiosInstance, basePath?: string) => AxiosPromise<void>
> {
const localVarAxiosArgs =
await localVarAxiosParamCreator.removeAllByTypeAndReferenceId(
attachmentReference,
options
);
return createRequestFunction(
localVarAxiosArgs,
globalAxios,
BASE_PATH,
configuration
);
},
/**
* Remove by type and attachmentId and referenceId
* @param {AttachmentReference} [attachmentReference]
Expand Down Expand Up @@ -640,6 +719,23 @@ export const V1alpha1AttachmentReferenceApiFactory = function (
)
.then((request) => request(axios, basePath));
},
/**
* Remove references by type and referenceId
* @param {V1alpha1AttachmentReferenceApiRemoveAllByTypeAndReferenceIdRequest} requestParameters Request parameters.
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
removeAllByTypeAndReferenceId(
requestParameters: V1alpha1AttachmentReferenceApiRemoveAllByTypeAndReferenceIdRequest = {},
options?: AxiosRequestConfig
): AxiosPromise<void> {
return localVarFp
.removeAllByTypeAndReferenceId(
requestParameters.attachmentReference,
options
)
.then((request) => request(axios, basePath));
},
/**
* Remove by type and attachmentId and referenceId
* @param {V1alpha1AttachmentReferenceApiRemoveByTypeAndAttachmentIdAndReferenceIdRequest} requestParameters Request parameters.
Expand Down Expand Up @@ -737,6 +833,20 @@ export interface V1alpha1AttachmentReferenceApiMatchingAttachmentsForEpisodeRequ
readonly batchMatchingEpisodeAttachment: BatchMatchingEpisodeAttachment;
}

/**
* Request parameters for removeAllByTypeAndReferenceId operation in V1alpha1AttachmentReferenceApi.
* @export
* @interface V1alpha1AttachmentReferenceApiRemoveAllByTypeAndReferenceIdRequest
*/
export interface V1alpha1AttachmentReferenceApiRemoveAllByTypeAndReferenceIdRequest {
/**
*
* @type {AttachmentReference}
* @memberof V1alpha1AttachmentReferenceApiRemoveAllByTypeAndReferenceId
*/
readonly attachmentReference?: AttachmentReference;
}

/**
* Request parameters for removeByTypeAndAttachmentIdAndReferenceId operation in V1alpha1AttachmentReferenceApi.
* @export
Expand Down Expand Up @@ -846,6 +956,25 @@ export class V1alpha1AttachmentReferenceApi extends BaseAPI {
.then((request) => request(this.axios, this.basePath));
}

/**
* Remove references by type and referenceId
* @param {V1alpha1AttachmentReferenceApiRemoveAllByTypeAndReferenceIdRequest} requestParameters Request parameters.
* @param {*} [options] Override http request option.
* @throws {RequiredError}
* @memberof V1alpha1AttachmentReferenceApi
*/
public removeAllByTypeAndReferenceId(
requestParameters: V1alpha1AttachmentReferenceApiRemoveAllByTypeAndReferenceIdRequest = {},
options?: AxiosRequestConfig
) {
return V1alpha1AttachmentReferenceApiFp(this.configuration)
.removeAllByTypeAndReferenceId(
requestParameters.attachmentReference,
options
)
.then((request) => request(this.axios, this.basePath));
}

/**
* Remove by type and attachmentId and referenceId
* @param {V1alpha1AttachmentReferenceApiRemoveByTypeAndAttachmentIdAndReferenceIdRequest} requestParameters Request parameters.
Expand Down
8 changes: 4 additions & 4 deletions console/packages/api-client/src/api/v1alpha1-indices-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,14 @@ export const V1alpha1IndicesApiAxiosParamCreator = function (
localVarQueryParameter["highlightPostTag"] = highlightPostTag;
}

if (highlightPreTag !== undefined) {
localVarQueryParameter["highlightPreTag"] = highlightPreTag;
}

if (keyword !== undefined) {
localVarQueryParameter["keyword"] = keyword;
}

if (highlightPreTag !== undefined) {
localVarQueryParameter["highlightPreTag"] = highlightPreTag;
}

setSearchParams(localVarUrlObj, localVarQueryParameter);
let headersFromBaseOptions =
baseOptions && baseOptions.headers ? baseOptions.headers : {};
Expand Down
4 changes: 1 addition & 3 deletions console/src/locales/en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -549,9 +549,7 @@ module:
message:
operate:
remove-episode-all-att-refs:
waring: >-
Invalid operation, the current episode is not bound to any
resource files
waring: Invalid operation, the current episode is null.
success: Successfully removed all attachment bindings of the episode
remove-episode-att-ref:
success: >-
Expand Down
2 changes: 1 addition & 1 deletion console/src/locales/zh-CN.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ module:
message:
operate:
remove-episode-all-att-refs:
waring: 操作无效,您当前剧集并未绑定资源附件
waring: 操作无效,您当前操作的剧集为空。
success: 移除剧集所有附件绑定成功
remove-episode-att-ref:
success: 移除剧集单个附件绑定成功
Expand Down
7 changes: 2 additions & 5 deletions console/src/modules/content/subject/EpisodeDetailsDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,10 @@ const removeEpisodeAllAttachmentRefs = async () => {
// @ts-ignore
if (
!episode.value ||
!episode.value.resources ||
episode.value.resources.length === 0
!episode.value.resources
) {
ElMessage.warning(
t(
'module.subject.dialog.episode.details.message.operate.remove-episode-all-att-refs.waring'
)
t('module.subject.dialog.episode.details.message.operate.remove-episode-all-att-refs.waring')
);
return;
}
Expand Down
20 changes: 6 additions & 14 deletions console/src/modules/content/subject/SubjectDetails.vue
Original file line number Diff line number Diff line change
Expand Up @@ -517,20 +517,12 @@ const deleteBatchingAttachments = async () => {
// console.debug('deleteBatchingAttachments subject episodes', subject.value.episodes)
batchCancenMatchingSubjectButtonLoading.value = true;
await subject.value.episodes?.forEach(async (ep) => {
if (ep && ep.resources && ep.resources.length > 0) {
await ep.resources.forEach(async (epres) => {
const attId = epres.attachmentId;
await apiClient.attachmentRef
.removeByTypeAndAttachmentIdAndReferenceId({
attachmentReference: {
type: 'EPISODE',
attachmentId: attId,
referenceId: ep.id,
},
})
.catch((e) => console.error(e));
});
}
await apiClient.attachmentRef.removeAllByTypeAndReferenceId({
attachmentReference: {
type: 'EPISODE',
referenceId: ep.id
}
});
});
batchCancenMatchingSubjectButtonLoading.value = false;
ElMessage.success(
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=0.15.6
version=0.15.7
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ public RouterFunction<ServerResponse> endpoint() {
.requestBody(Builder.requestBodyBuilder()
.implementation(AttachmentReference.class)))

.DELETE("/attachment/references", this::removeAllByTypeAndReferenceId,
builder -> builder.operationId("RemoveAllByTypeAndReferenceId")
.tag(tag).description("Remove references by type and referenceId")
.requestBody(Builder.requestBodyBuilder()
.implementation(AttachmentReference.class)))


.GET("/attachment/references", this::findAllByTypeAndAttachmentId,
builder -> builder.tag(tag)
.operationId("FindAllByTypeAndAttachmentId")
Expand Down Expand Up @@ -108,6 +115,13 @@ private Mono<ServerResponse> removeByTypeAndAttachmentIdAndReferenceId(ServerReq
.then(ServerResponse.ok().bodyValue("Delete success."));
}

private Mono<ServerResponse> removeAllByTypeAndReferenceId(ServerRequest request) {
return request.bodyToMono(AttachmentReference.class)
.flatMap(attachmentReference -> service.removeAllByTypeAndReferenceId(
attachmentReference.getType(), attachmentReference.getReferenceId()))
.then(ServerResponse.ok().bodyValue("Delete success."));
}

private Mono<ServerResponse> findAllByTypeAndAttachmentId(ServerRequest request) {
Optional<String> typeOp = request.queryParam("type");
if (typeOp.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,28 @@
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;
import run.ikaros.server.core.attachment.event.AttachmentRemoveEvent;
import run.ikaros.server.store.repository.AttachmentReferenceRepository;
import run.ikaros.server.store.repository.AttachmentRelationRepository;

@Slf4j
@Component
public class AttachmentRemoveListener {
private final AttachmentRelationRepository relationRepository;
private final AttachmentReferenceRepository referenceRepository;

public AttachmentRemoveListener(AttachmentRelationRepository relationRepository) {
public AttachmentRemoveListener(AttachmentRelationRepository relationRepository,
AttachmentReferenceRepository referenceRepository) {
this.relationRepository = relationRepository;
this.referenceRepository = referenceRepository;
}

/**
* 清除存在附件关系.
* 清除存在的附件关系.
*/
@EventListener(AttachmentRemoveEvent.class)
public Mono<Void> onAttachmentRemoveEvent(AttachmentRemoveEvent event) {
Long attId = event.getEntity().getId();
return relationRepository.findAllByAttachmentId(attId)
.flatMap(relationRepository::delete)
.then();
return relationRepository.deleteAllByAttachmentId(attId)
.then(referenceRepository.deleteAllByAttachmentId(attId));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ Flux<AttachmentReference> findAllByTypeAndAttachmentId(

Mono<Void> removeById(Long attachmentRefId);

Mono<Void> removeAllByTypeAndReferenceId(AttachmentReferenceType type, Long referenceId);

Mono<Void> removeByTypeAndAttachmentIdAndReferenceId(
AttachmentReferenceType type, Long attachmentId, Long referenceId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ public Mono<Void> removeById(Long attachmentRefId) {
return repository.deleteById(attachmentRefId);
}

@Override
public Mono<Void> removeAllByTypeAndReferenceId(AttachmentReferenceType type,
Long referenceId) {
Assert.notNull(type, "'type' must not null.");
Assert.isTrue(referenceId > 0, "'referenceId' must gt 0.");
return repository.deleteAllByTypeAndReferenceId(type, referenceId);
}

@Override
public Mono<Void> removeByTypeAndAttachmentIdAndReferenceId(AttachmentReferenceType type,
Long attachmentId,
Expand Down
Loading

0 comments on commit d2db49d

Please sign in to comment.