Skip to content

Commit e4c2209

Browse files
author
Ivan Verhun
committed
implemented simple DSL to create appartaments
1 parent 4649086 commit e4c2209

File tree

5 files changed

+88
-6
lines changed

5 files changed

+88
-6
lines changed

dsl-executor-service/src/main/java/com/lohika/jclub/dsl/service/DslController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class DslController {
3535

3636
@GetMapping(path = "/{scriptName}")
3737
public Object runScript(@PathVariable(name = "scriptName") String scriptName) throws IOException {
38-
File file = new File(basepath + scriptName + ".groovy");
38+
File file = new File(basepath + scriptName + ".mydsl");
3939
String script = new String(Files.readAllBytes(Paths.get(file.getPath())));
4040

4141
MyDsl dsl = new MyDsl(ratingServiceClient, storageServiceClient);

dsl-scripts/demo.mydsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ String myEmail = 'bot@company.name'
44

55
(1..100).each {
66
String location = "location-${it}"
7-
double myPrice = 2
8-
double mySqft = 1 + (it as int)
7+
int myPrice = 2
8+
int mySqft = 1 + (it as int)
99
def rating = rating location, myPrice, mySqft
1010

1111
if (rating > 400) {

dsl-scripts/simple.mydsl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
apartment {
2-
location "location"
2+
location "location11"
33
price 1
44
sqft 1
55
phone 'phone'
66
realtorName 'realtorName'
77
mail 'mail'
88
}
99

10-
apartment("location", {
10+
apartment("location22", {
1111
price 1
1212
sqft 1
1313
phone 'phone'
@@ -16,10 +16,13 @@ apartment("location", {
1616
})
1717

1818

19-
apartment("location") {
19+
apartment("location33") {
20+
location "location44"
2021
price 1
2122
sqft 1
2223
phone 'phone'
2324
realtorName 'realtorName'
2425
mail 'mail'
26+
location "location55"
2527
}
28+
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.lohika.jclub.dsl
2+
3+
import com.lohika.jclub.storage.client.Apartment
4+
5+
//@Builder(prefix = "", builderStrategy = SimpleStrategy.class)
6+
class ApartmentDsl {
7+
String location ="location"
8+
Integer price =2
9+
Integer sqft =2
10+
String phone ='phone'
11+
String realtorName ='realtorName'
12+
String mail ='mail'
13+
14+
void location(String location) {
15+
this.location = location
16+
}
17+
18+
void price(Integer price) {
19+
this.price = price
20+
}
21+
22+
void sqft(Integer sqft) {
23+
this.sqft = sqft
24+
}
25+
26+
void phone(String phone) {
27+
this.phone = phone
28+
}
29+
30+
void realtorName(String realtorName) {
31+
this.realtorName = realtorName
32+
}
33+
34+
void mail(String mail) {
35+
this.mail = mail
36+
}
37+
38+
def toEntity() {
39+
Apartment.builder()
40+
.location(location)
41+
.price(price)
42+
.sqft(sqft)
43+
.phone(phone)
44+
.realtorName(realtorName)
45+
.mail(mail)
46+
.build()
47+
}
48+
}

dsl/src/main/groovy/com/lohika/jclub/dsl/MyDsl.groovy

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,39 @@ class MyDsl {
1313
private RatingServiceClient ratingServiceClient
1414
private StorageServiceClient storageServiceClient
1515

16+
def list = []
17+
1618
MyDsl(RatingServiceClient ratingServiceClient, StorageServiceClient storageServiceClient) {
1719
this.storageServiceClient = storageServiceClient
1820
this.ratingServiceClient = ratingServiceClient
1921
}
22+
23+
def rating (location, myPrice, mySqft) {
24+
Apartment a = Apartment.builder()
25+
.sqft (mySqft)
26+
.location (location)
27+
.price(myPrice)
28+
.build()
29+
30+
ratingServiceClient.getRating(a).rating
31+
}
32+
33+
def apartment (Closure closure) {
34+
ApartmentDsl a = new ApartmentDsl()
35+
closure.delegate = a
36+
closure()
37+
38+
storageServiceClient.create(a.toEntity())
39+
list.add(a)
40+
}
41+
42+
def apartment (String location, Closure closure) {
43+
ApartmentDsl a = new ApartmentDsl()
44+
a.location = location
45+
closure.delegate = a
46+
closure()
47+
48+
storageServiceClient.create(a.toEntity())
49+
list.add(a)
50+
}
2051
}

0 commit comments

Comments
 (0)