Skip to content

Commit 78fbf52

Browse files
committed
feat: generate builder and create app sample code
Signed-off-by: Otavio Santana <[email protected]>
1 parent 2f45592 commit 78fbf52

File tree

3 files changed

+88
-1
lines changed

3 files changed

+88
-1
lines changed

src/main/java/org/soujava/demos/mongodb/document/App.java

+19-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.eclipse.jnosql.mapping.document.DocumentTemplate;
1919

2020
import java.util.List;
21+
import java.util.Set;
2122
import java.util.logging.Logger;
2223

2324

@@ -29,7 +30,24 @@ public static void main(String[] args) {
2930
var faker = new Faker();
3031
LOGGER.info("Starting the application");
3132
try (SeContainer container = SeContainerInitializer.newInstance().initialize()) {
32-
Category category = new Category("category", "Category");
33+
var template = container.select(DocumentTemplate.class).get();
34+
var electronicsCategory = new Category("Electronics", "All electronics");
35+
var computerCategory = new Category("Computers", "All computers");
36+
var tags = List.of("smartphone", "tablet", "laptop");
37+
var manufacturer = new Manufacturer("Apple", "One Infinite Loop Cupertino, CA 95014", "+1-408-996-1010");
38+
Product macBookPro = Product.builder().categories(Set.of(electronicsCategory, computerCategory))
39+
.manufacturer(manufacturer)
40+
.name("MacBook Pro")
41+
.tags(tags)
42+
.build();
43+
44+
Product product = template.insert(macBookPro);
45+
46+
LOGGER.info("Product saved: " + product);
47+
template.select(Product.class).where("id")
48+
.eq(product.getId())
49+
.result().forEach(p -> LOGGER.info("Product found: " + p));
50+
3351

3452
}
3553
}

src/main/java/org/soujava/demos/mongodb/document/Product.java

+34
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,36 @@ public class Product {
2929
@Column
3030
private Set<Category> categories;
3131

32+
Product(String name, Manufacturer manufacturer, List<String> tags, Set<Category> categories) {
33+
this.name = name;
34+
this.manufacturer = manufacturer;
35+
this.tags = tags;
36+
this.categories = categories;
37+
}
38+
39+
Product() {
40+
}
41+
42+
public String getId() {
43+
return id;
44+
}
45+
46+
public String getName() {
47+
return name;
48+
}
49+
50+
public Manufacturer getManufacturer() {
51+
return manufacturer;
52+
}
53+
54+
public List<String> getTags() {
55+
return tags;
56+
}
57+
58+
public Set<Category> getCategories() {
59+
return categories;
60+
}
61+
3262
@Override
3363
public boolean equals(Object o) {
3464
if (o == null || getClass() != o.getClass()) {
@@ -53,4 +83,8 @@ public String toString() {
5383
", categories=" + categories +
5484
'}';
5585
}
86+
87+
public static ProductBuilder builder() {
88+
return new ProductBuilder();
89+
}
5690
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.soujava.demos.mongodb.document;
2+
3+
import java.util.List;
4+
import java.util.Set;
5+
6+
public class ProductBuilder {
7+
private String name;
8+
private Manufacturer manufacturer;
9+
private List<String> tags;
10+
private Set<Category> categories;
11+
12+
public ProductBuilder name(String name) {
13+
this.name = name;
14+
return this;
15+
}
16+
17+
public ProductBuilder manufacturer(Manufacturer manufacturer) {
18+
this.manufacturer = manufacturer;
19+
return this;
20+
}
21+
22+
public ProductBuilder tags(List<String> tags) {
23+
this.tags = tags;
24+
return this;
25+
}
26+
27+
public ProductBuilder categories(Set<Category> categories) {
28+
this.categories = categories;
29+
return this;
30+
}
31+
32+
public Product build() {
33+
return new Product(name, manufacturer, tags, categories);
34+
}
35+
}

0 commit comments

Comments
 (0)