Skip to content

Commit ac408bb

Browse files
committed
lombok and log4j2 implemented, refactoring and readme updated
1 parent 937fac7 commit ac408bb

File tree

22 files changed

+340
-256
lines changed

22 files changed

+340
-256
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,8 @@ git clone https://github.com/cemserit/spring-data-examples.git
1414
### Run Redis
1515
```
1616
./gradlew :spring-data-redis:bootRun
17+
```
18+
### Swagger UI
19+
```
20+
http://localhost:8080/swagger-ui.html
1721
```

build.gradle

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
buildscript {
22
ext {
3-
springBootVersion = '2.0.1.RELEASE'
3+
springBootVersion = '2.0.4.RELEASE'
44
swaggerVersion = '2.9.2'
5+
lombokVersion = '1.18.2'
56
}
67
repositories {
78
mavenCentral()
@@ -29,8 +30,17 @@ subprojects {
2930

3031
sourceCompatibility = 1.8
3132

33+
configurations {
34+
all {
35+
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
36+
}
37+
}
38+
3239
dependencies {
40+
compile 'org.springframework.boot:spring-boot-starter-log4j2'
41+
3342
compile "io.springfox:springfox-swagger2:${swaggerVersion}"
3443
compile "io.springfox:springfox-swagger-ui:${swaggerVersion}"
44+
compileOnly group: 'org.projectlombok', name: 'lombok', version: "${lombokVersion}"
3545
}
3646
}

core-library/src/main/java/com/cemserit/core/constant/ControllerConstant.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package com.cemserit.core.constant;
22

3-
/**
4-
* Created by cemserit on 21.08.2018.
5-
*/
63
public final class ControllerConstant {
74
public static final String MESSAGE_KEY = "message";
85
public static final String ERROR_KEY = "error";
@@ -22,4 +19,7 @@ public final class ControllerConstant {
2219
public static final String PERSON_EMAIL_CANNOT_NULL_EMPTY_ERROR = "person_email_cannot_null_empty";
2320
public static final String PERSON_EMAIL_CANNOT_NULL_EMPTY_ERROR_DESCRIPTION = "Person email cannot be null or empty!";
2421

22+
public static final String PERSON_UUID_CANNOT_NULL_ERROR = "person_uuid_cannot_null";
23+
public static final String PERSON_UUID_CANNOT_NULL_ERROR_DESCRIPTION = "Person uuid cannot be null!";
24+
2525
}

core-library/src/main/java/com/cemserit/core/controller/AbstractController.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import static com.cemserit.core.constant.ControllerConstant.*;
1111

12+
@SuppressWarnings("unchecked")
1213
public abstract class AbstractController {
1314

1415
protected ResponseEntity<Object> createResponse(final String message, final HttpStatus httpStatus) {

spring-data-cassandra/README.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ for more details [cassandra.apache.org](http://cassandra.apache.org) <br>
1616
```
1717
sudo docker run --name dev-cassandra -p 7000:7000 -p 7001:7001 -p 9160:9160 -p 9042:9042 -d cassandra:3.11.2
1818
```
19+
##### Cassandra cqlsh
20+
```
21+
docker exec -it __CONTAINER_ID__ cqlsh
22+
```
1923
## Examples
2024
### Save person
2125
```
@@ -54,20 +58,15 @@ curl -X GET \
5458
### Get person (find by age)
5559
```
5660
curl -X GET \
57-
http://localhost:8080/persons/custom_param?age=27
61+
http://localhost:8080/persons?age=27
5862
```
5963
### Get person (find by email and age)
6064
```
6165
curl -X GET \
62-
http://localhost:8080/persons/custom_params[email protected]&age=27
66+
http://localhost:8080/[email protected]&age=27
6367
```
6468
### Delete person
6569
```
6670
curl -X DELETE \
6771
http://localhost:8080/persons/8781d82d-08bc-4148-b00f-4ad7750b4934
68-
```
69-
### Delete all person
70-
```
71-
curl -X DELETE \
72-
http://localhost:8080/persons
7372
```

spring-data-cassandra/src/main/java/com/cemserit/cassandra/configuration/CasssandraConfig.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import org.springframework.data.cassandra.config.AbstractCassandraConfiguration;
66
import org.springframework.data.cassandra.config.SchemaAction;
77

8-
import java.util.Arrays;
8+
import java.util.Collections;
99
import java.util.List;
1010

1111
@Configuration
@@ -28,7 +28,7 @@ protected List<String> getStartupScripts() {
2828
+ "AND replication = { 'replication_factor' : %s, 'class' : '%s' };",
2929
keyspaceName, durableWrites, replicationFactor, replicationClass);
3030

31-
return Arrays.asList(script);
31+
return Collections.singletonList(script);
3232
}
3333

3434
@Override

spring-data-cassandra/src/main/java/com/cemserit/cassandra/controller/PersonController.java

Lines changed: 70 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,75 +2,117 @@
22

33
import com.cemserit.cassandra.model.Person;
44
import com.cemserit.cassandra.service.PersonService;
5+
import com.cemserit.core.constant.ControllerConstant;
6+
import com.cemserit.core.controller.AbstractController;
57
import io.swagger.annotations.Api;
68
import org.springframework.beans.factory.annotation.Autowired;
79
import org.springframework.http.HttpStatus;
810
import org.springframework.http.MediaType;
911
import org.springframework.http.ResponseEntity;
1012
import org.springframework.web.bind.annotation.*;
1113

12-
import java.util.Collections;
14+
import java.util.Optional;
1315
import java.util.UUID;
1416

17+
import static com.cemserit.core.constant.ControllerConstant.*;
18+
1519
@RestController
1620
@RequestMapping(value = "/persons", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
17-
@Api(value = "Person Controller Api", tags = {"person Controller"})
18-
public class PersonController {
21+
@Api(value = "Person Controller Api", tags = {"Person Controller"})
22+
public class PersonController extends AbstractController {
1923

2024
@Autowired
2125
private PersonService personService;
2226

2327
@GetMapping("/{uuid}")
2428
public ResponseEntity<Object> getPerson(@PathVariable UUID uuid) {
25-
return new ResponseEntity<>(personService.getPerson(uuid), HttpStatus.OK);
29+
Optional<Person> personOptional = personService.getPerson(uuid);
30+
31+
if (!personOptional.isPresent())
32+
return createErrorResponse(PERSON_NOT_FOUND_ERROR,
33+
ControllerConstant.PERSON_NOT_FOUND_ERROR_DESCRIPTION,
34+
HttpStatus.BAD_REQUEST);
35+
36+
return createResponse(personOptional.get(), HttpStatus.OK);
2637
}
2738

28-
@GetMapping("/custom_param")
29-
public ResponseEntity<Object> getPersons(@RequestParam("age") int age) {
30-
return new ResponseEntity<>(personService.getPerson(age), HttpStatus.OK);
39+
@GetMapping(params = "age")
40+
public ResponseEntity<Object> getPersonsByAge(@RequestParam("age") int age) {
41+
return createResponse(personService.getPerson(age), HttpStatus.OK);
3142
}
3243

33-
@GetMapping("/custom_params")
34-
public ResponseEntity<Object> getPerson(@RequestParam("email") String email,
35-
@RequestParam("age") int age) {
36-
return new ResponseEntity<>(personService.getPerson(email, age), HttpStatus.OK);
44+
@GetMapping(params = {"email", "age"})
45+
public ResponseEntity<Object> getPersonsByEmailAndAge(@RequestParam("email") String email,
46+
@RequestParam("age") int age) {
47+
return createResponse(personService.getPerson(email, age), HttpStatus.OK);
3748
}
3849

3950
@GetMapping
4051
public ResponseEntity<Object> getPersons() {
41-
return new ResponseEntity<>(personService.getPersons(), HttpStatus.OK);
52+
return createResponse(personService.getPersons(), HttpStatus.OK);
4253
}
4354

4455
@PostMapping
4556
public ResponseEntity<Object> savePerson(@RequestBody Person person) {
46-
personService.savePerson(person);
47-
String message = "Person[" + person.getName() + "] saved";
4857

49-
return new ResponseEntity<>(Collections.singletonMap("message", message), HttpStatus.OK);
58+
if (person == null)
59+
return createErrorResponse(PERSON_CANNOT_NULL_ERROR,
60+
ControllerConstant.PERSON_ALREADY_EXITS_ERROR_DESCRIPTION,
61+
HttpStatus.BAD_REQUEST);
62+
63+
if (person.getUuid() == null)
64+
return createErrorResponse(PERSON_UUID_CANNOT_NULL_ERROR,
65+
ControllerConstant.PERSON_UUID_CANNOT_NULL_ERROR_DESCRIPTION,
66+
HttpStatus.BAD_REQUEST);
67+
68+
Optional<Person> personOptional = personService.getPerson(person.getUuid());
69+
70+
if (personOptional.isPresent())
71+
return createErrorResponse(PERSON_ALREADY_EXITS_ERROR,
72+
ControllerConstant.PERSON_ALREADY_EXITS_ERROR_DESCRIPTION,
73+
HttpStatus.BAD_REQUEST);
74+
75+
personService.savePerson(person);
76+
return createResponse(person, HttpStatus.OK);
5077
}
5178

5279
@PutMapping("/{uuid}")
5380
public ResponseEntity<Object> updatePerson(@RequestBody Person person,
5481
@PathVariable UUID uuid) {
55-
boolean result = personService.updatePerson(uuid, person);
56-
String message = result ? "Person[" + person.getName() + "] updated" :
57-
"Person[" + person.getName() + "] cannot be found";
82+
if (uuid == null)
83+
return createErrorResponse(PERSON_UUID_CANNOT_NULL_ERROR,
84+
ControllerConstant.PERSON_UUID_CANNOT_NULL_ERROR_DESCRIPTION,
85+
HttpStatus.BAD_REQUEST);
86+
87+
Optional<Person> personOptional = personService.getPerson(uuid);
5888

59-
return new ResponseEntity<>(Collections.singletonMap("message", message), HttpStatus.OK);
89+
if (!personOptional.isPresent())
90+
return createErrorResponse(PERSON_NOT_FOUND_ERROR,
91+
ControllerConstant.PERSON_NOT_FOUND_ERROR_DESCRIPTION,
92+
HttpStatus.BAD_REQUEST);
93+
94+
person.setUuid(uuid);
95+
personService.savePerson(person);
96+
return createResponse(person, HttpStatus.OK);
6097
}
6198

6299
@DeleteMapping("/{uuid}")
63100
public ResponseEntity<Object> deletePerson(@PathVariable UUID uuid) {
64-
boolean result = personService.deletePerson(uuid);
65-
String message = result ? "Person[" + uuid + "] deleted" : "Person[" + uuid + "] cannot be found";
66101

67-
return new ResponseEntity<>(Collections.singletonMap("message", message), HttpStatus.OK);
68-
}
102+
if (uuid == null)
103+
return createErrorResponse(PERSON_UUID_CANNOT_NULL_ERROR,
104+
ControllerConstant.PERSON_UUID_CANNOT_NULL_ERROR_DESCRIPTION,
105+
HttpStatus.BAD_REQUEST);
69106

70-
@DeleteMapping
71-
public ResponseEntity<Object> deleteAllPerson() {
72-
personService.deletePersons();
73-
return new ResponseEntity<>(Collections.singletonMap("message", "Persons deleted"), HttpStatus.OK);
74-
}
107+
Optional<Person> personOptional = personService.getPerson(uuid);
75108

109+
if (!personOptional.isPresent())
110+
return createErrorResponse(PERSON_NOT_FOUND_ERROR,
111+
ControllerConstant.PERSON_NOT_FOUND_ERROR_DESCRIPTION,
112+
HttpStatus.BAD_REQUEST);
113+
114+
personService.deletePerson(uuid);
115+
String message = String.format("Person[%s] deleted.", personOptional.get().getName());
116+
return createResponse(message, HttpStatus.OK);
117+
}
76118
}
Lines changed: 8 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package com.cemserit.cassandra.model;
22

33
import com.datastax.driver.core.DataType;
4+
import lombok.AllArgsConstructor;
5+
import lombok.Data;
6+
import lombok.NoArgsConstructor;
7+
import lombok.ToString;
48
import org.springframework.data.cassandra.core.cql.PrimaryKeyType;
59
import org.springframework.data.cassandra.core.mapping.CassandraType;
610
import org.springframework.data.cassandra.core.mapping.PrimaryKeyColumn;
@@ -9,6 +13,10 @@
913
import java.util.UUID;
1014

1115
@Table("person")
16+
@Data
17+
@AllArgsConstructor
18+
@NoArgsConstructor
19+
@ToString
1220
public class Person {
1321

1422
@CassandraType(type = DataType.Name.UUID)
@@ -17,56 +25,4 @@ public class Person {
1725
private String email;
1826
private String name;
1927
private int age;
20-
21-
public Person() {
22-
}
23-
24-
public Person(UUID uuid, String email, String name, int age) {
25-
this.uuid = uuid;
26-
this.email = email;
27-
this.name = name;
28-
this.age = age;
29-
}
30-
31-
public UUID getUuid() {
32-
return uuid;
33-
}
34-
35-
public String getEmail() {
36-
return email;
37-
}
38-
39-
public String getName() {
40-
return name;
41-
}
42-
43-
public int getAge() {
44-
return age;
45-
}
46-
47-
public void setUuid(UUID uuid) {
48-
this.uuid = uuid;
49-
}
50-
51-
public void setEmail(String email) {
52-
this.email = email;
53-
}
54-
55-
public void setName(String name) {
56-
this.name = name;
57-
}
58-
59-
public void setAge(int age) {
60-
this.age = age;
61-
}
62-
63-
@Override
64-
public String toString() {
65-
return "Person{" +
66-
"uuid=" + uuid +
67-
", email='" + email + '\'' +
68-
", name='" + name + '\'' +
69-
", age=" + age +
70-
'}';
71-
}
7228
}

spring-data-cassandra/src/main/java/com/cemserit/cassandra/repository/PersonRepository.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
import org.springframework.data.repository.CrudRepository;
66
import org.springframework.data.repository.query.Param;
77

8+
import java.util.List;
89
import java.util.Optional;
910
import java.util.UUID;
10-
import java.util.stream.Stream;
1111

1212
public interface PersonRepository extends CrudRepository<Person, UUID> {
1313

@@ -18,9 +18,9 @@ public interface PersonRepository extends CrudRepository<Person, UUID> {
1818
Optional<Person> findByUuid(UUID uuid);
1919

2020
@Query(allowFiltering = true)
21-
Optional<Person> findByEmailAndAge(String email, int age);
21+
List<Person> findByEmailAndAge(String email, int age);
2222

2323
// Custom query
2424
@Query("SELECT * FROM PERSON WHERE age=:age ALLOW FILTERING")
25-
Stream<Person> findByAgeStream(@Param("age") int age);
25+
List<Person> findByAge(@Param("age") int age);
2626
}

0 commit comments

Comments
 (0)