Skip to content

Commit 024c70b

Browse files
author
th37rose
committed
Copy folder api logic into bundle api.
1 parent 528826c commit 024c70b

File tree

21 files changed

+1182
-1
lines changed

21 files changed

+1182
-1
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.lowcoder.domain.bundle.model;
2+
3+
4+
import jakarta.annotation.Nullable;
5+
import lombok.Getter;
6+
import lombok.NoArgsConstructor;
7+
import lombok.Setter;
8+
import org.lowcoder.sdk.models.HasIdAndAuditing;
9+
import org.springframework.data.mongodb.core.mapping.Document;
10+
11+
@Getter
12+
@Setter
13+
@Document
14+
@NoArgsConstructor
15+
public class Bundle extends HasIdAndAuditing {
16+
17+
private String userId;
18+
@Nullable
19+
private String name;
20+
private String title;
21+
private String description;
22+
private String category;
23+
private String type;
24+
private String image;
25+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package org.lowcoder.domain.bundle.model;
2+
3+
public record BundleElement(String bundleId, String elementId, int position) {
4+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.lowcoder.domain.bundle.model;
2+
3+
public enum BundleType {
4+
5+
APPLICATION,
6+
MODULE,
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.lowcoder.domain.bundle.repository;
2+
3+
import org.lowcoder.domain.bundle.model.Bundle;
4+
import org.springframework.data.mongodb.repository.ReactiveMongoRepository;
5+
import org.springframework.stereotype.Repository;
6+
import reactor.core.publisher.Flux;
7+
8+
@Repository
9+
public interface BundleRepository extends ReactiveMongoRepository<Bundle, String> {
10+
11+
Flux<Bundle> findByUserId(String userId);
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.lowcoder.domain.bundle.service;
2+
3+
import org.lowcoder.domain.bundle.model.BundleElement;
4+
import reactor.core.publisher.Flux;
5+
import reactor.core.publisher.Mono;
6+
7+
import java.util.List;
8+
9+
public interface BundleElementRelationService {
10+
Mono<Boolean> deleteByBundleIds(List<String> bundleIds);
11+
12+
Mono<Boolean> deleteByElementId(String elementId);
13+
14+
Mono<Void> create(String bundleId, String elementId);
15+
16+
Flux<BundleElement> getByElementIds(List<String> elementIds);
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.lowcoder.domain.bundle.service;
2+
3+
import lombok.RequiredArgsConstructor;
4+
import org.lowcoder.domain.bundle.model.BundleElement;
5+
import org.lowcoder.infra.birelation.BiRelationService;
6+
import org.springframework.stereotype.Service;
7+
import reactor.core.publisher.Flux;
8+
import reactor.core.publisher.Mono;
9+
10+
import java.util.List;
11+
12+
import static org.lowcoder.infra.birelation.BiRelationBizType.BUNDLE_ELEMENT;
13+
14+
@RequiredArgsConstructor
15+
@Service
16+
public class BundleElementRelationServiceImpl implements BundleElementRelationService {
17+
18+
private final BiRelationService biRelationService;
19+
20+
@Override
21+
public Mono<Boolean> deleteByBundleIds(List<String> bundleIds) {
22+
return biRelationService.removeAllBiRelations(BUNDLE_ELEMENT, bundleIds);
23+
}
24+
25+
@Override
26+
public Mono<Boolean> deleteByElementId(String elementId) {
27+
return biRelationService.removeAllBiRelationsByTargetId(BUNDLE_ELEMENT, elementId);
28+
}
29+
30+
@Override
31+
public Mono<Void> create(String bundleId, String elementId) {
32+
return biRelationService.addBiRelation(BUNDLE_ELEMENT, bundleId, elementId, null, null)
33+
.then();
34+
}
35+
36+
@Override
37+
public Flux<BundleElement> getByElementIds(List<String> elementIds) {
38+
return biRelationService.getByTargetIds(BUNDLE_ELEMENT, elementIds)
39+
.map(biRelation -> new BundleElement(biRelation.getSourceId(), biRelation.getTargetId(), Integer.parseInt(biRelation.getExtParam1())));
40+
}
41+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package org.lowcoder.domain.bundle.service;
2+
3+
import jakarta.annotation.Nonnull;
4+
import jakarta.annotation.Nullable;
5+
import lombok.Getter;
6+
import lombok.Setter;
7+
8+
import java.util.*;
9+
import java.util.function.Consumer;
10+
import java.util.function.Function;
11+
12+
@Getter
13+
@Setter
14+
public class BundleNode<T, F> implements Node<T, F> {
15+
16+
private final F self;
17+
private BundleNode<T, F> parent;
18+
protected final Collection<Node<T, F>> children;
19+
@Nonnull
20+
private final Function<F, String> idExtractor;
21+
@Nonnull
22+
private final Function<F, String> parentIdExtractor;
23+
24+
BundleNode(F self, @Nonnull Function<F, String> idExtractor, @Nonnull Function<F, String> parentIdExtractor,
25+
@Nullable Comparator<Node<T, F>> comparator) {
26+
this.self = self;
27+
this.idExtractor = idExtractor;
28+
this.parentIdExtractor = parentIdExtractor;
29+
this.children = comparator == null ? new ArrayList<>() : new PriorityQueue<>(comparator);
30+
}
31+
32+
public String id() {
33+
return idExtractor.apply(self);
34+
}
35+
36+
@Override
37+
public String parentId() {
38+
return parentIdExtractor.apply(self);
39+
}
40+
41+
public final List<F> getBundleChildren() {
42+
return children.stream()
43+
.filter(node -> node instanceof BundleNode<T, F>)
44+
.map(node -> ((BundleNode<T, F>) node).getSelf())
45+
.toList();
46+
}
47+
48+
public final List<T> getElementChildren() {
49+
return children.stream()
50+
.filter(node -> node instanceof ElementNode<T, F>)
51+
.map(node -> ((ElementNode<T, F>) node).getSelf())
52+
.toList();
53+
}
54+
55+
public final List<F> getAllBundleChildren() {
56+
return this.children.stream()
57+
.map(node -> {
58+
if (node instanceof BundleNode<T, F> bundleNode) {
59+
F self = bundleNode.getSelf();
60+
List<F> bundleChildren = bundleNode.getAllBundleChildren();
61+
List<F> all = new ArrayList<>(bundleChildren);
62+
if (self != null) {
63+
all.add(self);
64+
}
65+
return all;
66+
}
67+
return new ArrayList<F>();
68+
})
69+
.flatMap(List::stream)
70+
.toList();
71+
}
72+
73+
74+
/**
75+
* @param consumer will be called for children node first, then by parent node
76+
*/
77+
public void postOrderIterate(Consumer<Node<T, F>> consumer) {
78+
children.forEach(node -> {
79+
if (node instanceof BundleNode<T, F> bundleNode) {
80+
bundleNode.postOrderIterate(consumer);
81+
return;
82+
}
83+
consumer.accept(node);
84+
});
85+
consumer.accept(this);
86+
}
87+
88+
public final int depth() {
89+
if (parent == null) {
90+
return 1;
91+
}
92+
return parent.depth() + 1;
93+
}
94+
95+
@Override
96+
public String toString() {
97+
return "BundleNode{" +
98+
"self=" + self +
99+
", children=" + children +
100+
'}';
101+
}
102+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.lowcoder.domain.bundle.service;
2+
3+
import org.lowcoder.domain.bundle.model.Bundle;
4+
import reactor.core.publisher.Flux;
5+
import reactor.core.publisher.Mono;
6+
7+
import java.util.Collection;
8+
9+
public interface BundleService {
10+
Mono<Boolean> updateById(String id, Bundle resource);
11+
12+
Mono<Bundle> findById(String id);
13+
14+
Mono<Bundle> create(Bundle bundle);
15+
16+
Flux<Bundle> findByUserId(String bundleId);
17+
18+
Mono<Void> deleteAllById(Collection<String> ids);
19+
20+
Mono<Boolean> exist(String id);
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package org.lowcoder.domain.bundle.service;
2+
3+
import lombok.RequiredArgsConstructor;
4+
import org.lowcoder.domain.bundle.model.Bundle;
5+
import org.lowcoder.domain.bundle.repository.BundleRepository;
6+
import org.lowcoder.infra.mongo.MongoUpsertHelper;
7+
import org.lowcoder.sdk.constants.FieldName;
8+
import org.lowcoder.sdk.exception.BizError;
9+
import org.lowcoder.sdk.exception.BizException;
10+
import org.springframework.stereotype.Service;
11+
import reactor.core.publisher.Flux;
12+
import reactor.core.publisher.Mono;
13+
14+
import java.util.Collection;
15+
16+
import static org.lowcoder.sdk.exception.BizError.NO_RESOURCE_FOUND;
17+
18+
@RequiredArgsConstructor
19+
@Service
20+
public class BundleServiceImpl implements BundleService {
21+
22+
private final BundleRepository repository;
23+
private final MongoUpsertHelper mongoUpsertHelper;
24+
25+
@Override
26+
public Mono<Boolean> updateById(String id, Bundle resource) {
27+
if (id == null) {
28+
return Mono.error(new BizException(BizError.INVALID_PARAMETER, "INVALID_PARAMETER", FieldName.ID));
29+
}
30+
31+
return mongoUpsertHelper.updateById(resource, id);
32+
}
33+
34+
@Override
35+
public Mono<Bundle> findById(String id) {
36+
if (id == null) {
37+
return Mono.error(new BizException(BizError.INVALID_PARAMETER, "INVALID_PARAMETER", FieldName.ID));
38+
}
39+
40+
return repository.findById(id)
41+
.switchIfEmpty(Mono.error(new BizException(BizError.NO_RESOURCE_FOUND, "FOLDER_NOT_FOUND", id)));
42+
}
43+
44+
@Override
45+
public Mono<Bundle> create(Bundle folder) {
46+
return repository.save(folder);
47+
}
48+
49+
@Override
50+
public Flux<Bundle> findByUserId(String userId) {
51+
return repository.findByUserId(userId);
52+
}
53+
54+
@Override
55+
public Mono<Void> deleteAllById(Collection<String> ids) {
56+
return repository.deleteAllById(ids);
57+
}
58+
59+
@Override
60+
public Mono<Boolean> exist(String id) {
61+
return findById(id)
62+
.hasElement()
63+
.onErrorResume(throwable -> {
64+
if (throwable instanceof BizException bizException && bizException.getError() == NO_RESOURCE_FOUND) {
65+
return Mono.just(false);
66+
}
67+
return Mono.error(throwable);
68+
});
69+
}
70+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.lowcoder.domain.bundle.service;
2+
3+
import jakarta.annotation.Nonnull;
4+
import lombok.Getter;
5+
import lombok.Setter;
6+
7+
import java.util.function.Function;
8+
9+
@Setter
10+
@Getter
11+
public class ElementNode<T, F> implements Node<T, F> {
12+
13+
@Nonnull
14+
private final T self;
15+
@Nonnull
16+
private BundleNode<T, F> parent;
17+
@Nonnull
18+
private final Function<T, String> parentIdExtractor;
19+
20+
ElementNode(@Nonnull T self, @Nonnull Function<T, String> parentIdExtractor) {
21+
this.self = self;
22+
this.parentIdExtractor = parentIdExtractor;
23+
}
24+
25+
@Override
26+
public String parentId() {
27+
return parentIdExtractor.apply(self);
28+
}
29+
30+
@Override
31+
public String toString() {
32+
return "ElementNode{" +
33+
"self=" + self +
34+
'}';
35+
}
36+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.lowcoder.domain.bundle.service;
2+
3+
public interface Node<T, F> {
4+
5+
String parentId();
6+
7+
void setParent(BundleNode<T, F> bundleNode);
8+
}

0 commit comments

Comments
 (0)