From 3cf3eaefaa32519d4fc17402c55c8db125bb7fd7 Mon Sep 17 00:00:00 2001 From: deepcloudlabs Date: Fri, 26 Jun 2020 14:34:19 +0300 Subject: [PATCH] initial commit --- .../CrmReactiveMicroserviceApplication.java | 17 +++ .../CustomerReactiveRestController.java | 35 +++++ .../com/example/crm/document/Customer.java | 128 ++++++++++++++++++ .../CustomerReactiveRepository.java | 16 +++ .../crm/service/CustomerReactiveService.java | 25 ++++ .../src/main/resources/application.properties | 7 + 6 files changed, 228 insertions(+) create mode 100644 crm-reactive-microservice/src/main/java/com/example/crm/CrmReactiveMicroserviceApplication.java create mode 100644 crm-reactive-microservice/src/main/java/com/example/crm/controller/CustomerReactiveRestController.java create mode 100644 crm-reactive-microservice/src/main/java/com/example/crm/document/Customer.java create mode 100644 crm-reactive-microservice/src/main/java/com/example/crm/repository/CustomerReactiveRepository.java create mode 100644 crm-reactive-microservice/src/main/java/com/example/crm/service/CustomerReactiveService.java create mode 100644 crm-reactive-microservice/src/main/resources/application.properties diff --git a/crm-reactive-microservice/src/main/java/com/example/crm/CrmReactiveMicroserviceApplication.java b/crm-reactive-microservice/src/main/java/com/example/crm/CrmReactiveMicroserviceApplication.java new file mode 100644 index 0000000..e9a37ce --- /dev/null +++ b/crm-reactive-microservice/src/main/java/com/example/crm/CrmReactiveMicroserviceApplication.java @@ -0,0 +1,17 @@ +package com.example.crm; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.data.mongodb.repository.config.EnableReactiveMongoRepositories; +import org.springframework.web.reactive.config.EnableWebFlux; + +@SpringBootApplication +@EnableWebFlux +@EnableReactiveMongoRepositories +public class CrmReactiveMicroserviceApplication { + + public static void main(String[] args) { + SpringApplication.run(CrmReactiveMicroserviceApplication.class, args); + } + +} diff --git a/crm-reactive-microservice/src/main/java/com/example/crm/controller/CustomerReactiveRestController.java b/crm-reactive-microservice/src/main/java/com/example/crm/controller/CustomerReactiveRestController.java new file mode 100644 index 0000000..78012b8 --- /dev/null +++ b/crm-reactive-microservice/src/main/java/com/example/crm/controller/CustomerReactiveRestController.java @@ -0,0 +1,35 @@ +package com.example.crm.controller; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.CrossOrigin; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.context.annotation.RequestScope; + +import com.example.crm.document.Customer; +import com.example.crm.service.CustomerReactiveService; + +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; + +@RestController +@RequestMapping("customers") +@CrossOrigin +public class CustomerReactiveRestController { + @Autowired + private CustomerReactiveService customerService; + + @GetMapping(params = { "pagesize", "pageno" }) + public Flux getAllCustomers(@RequestParam int pagesize, @RequestParam int pageno) { + return customerService.findAllCustomers(pagesize, pageno); + } + + @GetMapping("{identity}") + public Mono getCustomerByIdentity(@PathVariable String identity) { + return customerService.findCustomerByIdentity(identity); + } + +} diff --git a/crm-reactive-microservice/src/main/java/com/example/crm/document/Customer.java b/crm-reactive-microservice/src/main/java/com/example/crm/document/Customer.java new file mode 100644 index 0000000..f45d5f7 --- /dev/null +++ b/crm-reactive-microservice/src/main/java/com/example/crm/document/Customer.java @@ -0,0 +1,128 @@ +package com.example.crm.document; + +import org.springframework.data.annotation.Id; +import org.springframework.data.mongodb.core.mapping.Document; + +@Document(collection = "customers") +public class Customer { + @Id + private String identity; + private String fullname; + private String homeAddress; + private String businessAddress; + private String email; + private String sms; + private int birthDay; + private String photo; + + public Customer() { + } + + public Customer(String identity, String fullname, String homeAddress, String businessAddress, String email, + String sms, int birthDay) { + this.identity = identity; + this.fullname = fullname; + this.homeAddress = homeAddress; + this.businessAddress = businessAddress; + this.email = email; + this.sms = sms; + this.birthDay = birthDay; + } + + public String getIdentity() { + return identity; + } + + public void setIdentity(String identity) { + this.identity = identity; + } + + public String getFullname() { + return fullname; + } + + public void setFullname(String fullname) { + this.fullname = fullname; + } + + public String getHomeAddress() { + return homeAddress; + } + + public void setHomeAddress(String homeAddress) { + this.homeAddress = homeAddress; + } + + public String getBusinessAddress() { + return businessAddress; + } + + public void setBusinessAddress(String businessAddress) { + this.businessAddress = businessAddress; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getSms() { + return sms; + } + + public void setSms(String sms) { + this.sms = sms; + } + + public int getBirthDay() { + return birthDay; + } + + public void setBirthDay(int birthDay) { + this.birthDay = birthDay; + } + + public String getPhoto() { + return photo; + } + + public void setPhoto(String photo) { + this.photo = photo; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((identity == null) ? 0 : identity.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Customer other = (Customer) obj; + if (identity == null) { + if (other.identity != null) + return false; + } else if (!identity.equals(other.identity)) + return false; + return true; + } + + @Override + public String toString() { + return "Customer [identity=" + identity + ", fullname=" + fullname + ", homeAddress=" + homeAddress + + ", businessAddress=" + businessAddress + ", email=" + email + ", sms=" + sms + ", birthDay=" + + birthDay + "]"; + } + +} diff --git a/crm-reactive-microservice/src/main/java/com/example/crm/repository/CustomerReactiveRepository.java b/crm-reactive-microservice/src/main/java/com/example/crm/repository/CustomerReactiveRepository.java new file mode 100644 index 0000000..153a303 --- /dev/null +++ b/crm-reactive-microservice/src/main/java/com/example/crm/repository/CustomerReactiveRepository.java @@ -0,0 +1,16 @@ +package com.example.crm.repository; + +import org.springframework.data.domain.PageRequest; +import org.springframework.data.mongodb.repository.Query; +import org.springframework.data.mongodb.repository.ReactiveMongoRepository; + +import com.example.crm.document.Customer; + +import reactor.core.publisher.Flux; + +public interface CustomerReactiveRepository extends ReactiveMongoRepository{ + + @Query("{}") + Flux findAllFlux(PageRequest page); + +} diff --git a/crm-reactive-microservice/src/main/java/com/example/crm/service/CustomerReactiveService.java b/crm-reactive-microservice/src/main/java/com/example/crm/service/CustomerReactiveService.java new file mode 100644 index 0000000..232df3c --- /dev/null +++ b/crm-reactive-microservice/src/main/java/com/example/crm/service/CustomerReactiveService.java @@ -0,0 +1,25 @@ +package com.example.crm.service; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.PageRequest; +import org.springframework.stereotype.Service; + +import com.example.crm.document.Customer; +import com.example.crm.repository.CustomerReactiveRepository; + +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; + +@Service +public class CustomerReactiveService { + @Autowired + private CustomerReactiveRepository customerRepository; + + public Flux findAllCustomers(int pagesize, int pageno) { + return customerRepository.findAllFlux(PageRequest.of(pageno, pagesize)); + } + + public Mono findCustomerByIdentity(String identity) { + return customerRepository.findById(identity); + } +} diff --git a/crm-reactive-microservice/src/main/resources/application.properties b/crm-reactive-microservice/src/main/resources/application.properties new file mode 100644 index 0000000..ee7a5db --- /dev/null +++ b/crm-reactive-microservice/src/main/resources/application.properties @@ -0,0 +1,7 @@ +# REST API URL BASE +# http(s)://localhost:8200/crm/api/v1 +server.address=localhost +server.port=8200 + +#mongodb configuration +spring.data.mongodb.uri=mongodb://localhost:27017/crm \ No newline at end of file