From 2641387bc9247cce1a1f1a315a514216cf5fc252 Mon Sep 17 00:00:00 2001 From: esmailian Date: Sun, 27 Apr 2025 09:42:09 +0330 Subject: [PATCH] initial commit this project for test architecture --- pom.xml | 162 ++++++++++++++++++ .../mmad/testapp/TestAAppApplication.java | 15 ++ .../testapp/controller/PersonController.java | 54 ++++++ .../mmad/testapp/entity/PersonEntity.java | 25 +++ .../mmad/testapp/mapper/PersonMapper.java | 26 +++ .../mmad/testapp/model/PersonModel.java | 12 ++ .../testapp/repository/PersonRepository.java | 22 +++ .../mmad/testapp/service/PersonService.java | 41 +++++ .../mmad/testapp/service/StreamProducer.java | 17 ++ src/main/java/seq-query | 12 ++ src/main/resources/application.properties | 19 ++ src/main/resources/application.yml | 22 +++ .../db/changelog/changes/liquibase-1.1.xml | 19 ++ .../db/changelog/changes/liquibase-1.2.xml | 21 +++ .../db/changelog/liquibase-master.xml | 10 ++ .../mmad/testapp/TestAppApplicationTests.java | 13 ++ 16 files changed, 490 insertions(+) create mode 100755 pom.xml create mode 100755 src/main/java/com/example/mmad/testapp/TestAAppApplication.java create mode 100755 src/main/java/com/example/mmad/testapp/controller/PersonController.java create mode 100755 src/main/java/com/example/mmad/testapp/entity/PersonEntity.java create mode 100755 src/main/java/com/example/mmad/testapp/mapper/PersonMapper.java create mode 100755 src/main/java/com/example/mmad/testapp/model/PersonModel.java create mode 100755 src/main/java/com/example/mmad/testapp/repository/PersonRepository.java create mode 100755 src/main/java/com/example/mmad/testapp/service/PersonService.java create mode 100644 src/main/java/com/example/mmad/testapp/service/StreamProducer.java create mode 100755 src/main/java/seq-query create mode 100755 src/main/resources/application.properties create mode 100755 src/main/resources/application.yml create mode 100755 src/main/resources/db/changelog/changes/liquibase-1.1.xml create mode 100755 src/main/resources/db/changelog/changes/liquibase-1.2.xml create mode 100755 src/main/resources/db/changelog/liquibase-master.xml create mode 100755 src/test/java/com/example/mmad/testapp/TestAppApplicationTests.java diff --git a/pom.xml b/pom.xml new file mode 100755 index 0000000..7ce2fed --- /dev/null +++ b/pom.xml @@ -0,0 +1,162 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 3.4.4 + + + com.example.mmad.testapp + testMicroservice + 0.0.1-SNAPSHOT + microservice-a + microservice-a + + 17 + 1.18.20 + 1.5.3.Final + + + + org.springframework.boot + spring-boot-starter-data-jpa + + + org.springframework.boot + spring-boot-starter-web + 3.2.6 + + + org.springframework.cloud + spring-cloud-starter-netflix-eureka-client + 4.1.1 + + + org.liquibase + liquibase-core + 4.29.2 + + + org.mapstruct + mapstruct + 1.5.3.Final + + + + + + + + + org.postgresql + postgresql + runtime + + + org.projectlombok + lombok + true + + + javax.validation + validation-api + 2.0.1.Final + + + org.apache.httpcomponents + httpclient + test + 4.5.14 + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-starter-web + + + + + + + + + + + org.springframework.boot + spring-boot-starter-amqp + + + org.springframework.amqp + spring-rabbit-stream + + + org.springframework.amqp + spring-rabbit-stream + 3.0.0 + + + + + + + org.springframework.cloud + spring-cloud-dependencies + 2023.0.1 + pom + import + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + org.projectlombok + lombok + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.9.0 + + ${java.version} + ${java.version} + + + org.projectlombok + lombok + ${org.projectlombok.version} + + + org.mapstruct + mapstruct-processor + ${org.mapstruct.version} + + + + -parameters + + + + + + diff --git a/src/main/java/com/example/mmad/testapp/TestAAppApplication.java b/src/main/java/com/example/mmad/testapp/TestAAppApplication.java new file mode 100755 index 0000000..d656229 --- /dev/null +++ b/src/main/java/com/example/mmad/testapp/TestAAppApplication.java @@ -0,0 +1,15 @@ +package com.example.mmad.testapp; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; + +@SpringBootApplication +@EnableDiscoveryClient +public class TestAAppApplication { + + public static void main(String[] args) { + SpringApplication.run(TestAAppApplication.class, args); + } + +} diff --git a/src/main/java/com/example/mmad/testapp/controller/PersonController.java b/src/main/java/com/example/mmad/testapp/controller/PersonController.java new file mode 100755 index 0000000..97e25d5 --- /dev/null +++ b/src/main/java/com/example/mmad/testapp/controller/PersonController.java @@ -0,0 +1,54 @@ +package com.example.mmad.testapp.controller; + +import com.example.mmad.testapp.model.PersonModel; +import com.example.mmad.testapp.service.PersonService; +import com.example.mmad.testapp.service.StreamProducer; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import javax.validation.Valid; + +@RestController +@RequestMapping("/person") +public class PersonController { + + private PersonService personService; + private StreamProducer streamProducer; + + @Autowired + public void setPersonService(PersonService personService, StreamProducer streamProducer) { + this.streamProducer = streamProducer; + this.personService = personService; + } + + @PostMapping("/list") + @ResponseStatus(HttpStatus.OK) + public String listPosts() { + streamProducer.sendMessage("hi hi hi"); + return "aaa"; + } + + @CrossOrigin(origins = "http://localhost:3001") + @PostMapping("/create") + public ResponseEntity create(@Valid @RequestBody PersonModel person) { + return ResponseEntity.ok(personService.createPerson(person)); + } + + @PostMapping("/update") + public ResponseEntity update(@Valid @RequestBody PersonModel person) { + return ResponseEntity.ok(personService.updatePerson(person)); + } + + @PostMapping("/get/{id}") + public ResponseEntity getPerson(@PathVariable Long id) { + return ResponseEntity.ok(personService.getPerson(id)); + } + + @PostMapping("/delete/{id}") + public ResponseEntity deletePerson(@PathVariable Long id) { + personService.deletePerson(id); + return ResponseEntity.ok("ok"); + } +} diff --git a/src/main/java/com/example/mmad/testapp/entity/PersonEntity.java b/src/main/java/com/example/mmad/testapp/entity/PersonEntity.java new file mode 100755 index 0000000..eb8df66 --- /dev/null +++ b/src/main/java/com/example/mmad/testapp/entity/PersonEntity.java @@ -0,0 +1,25 @@ +package com.example.mmad.testapp.entity; + +import jakarta.persistence.*; +import lombok.*; + +import static jakarta.persistence.GenerationType.SEQUENCE; + +@Entity +@Getter +@Setter +@Table(schema = "testapp", name = "person") +@Builder +@AllArgsConstructor +@NoArgsConstructor +public class PersonEntity { + + @Id + @SequenceGenerator(name = "seqTest", sequenceName = "testapp.person_seq", allocationSize = 1) + @GeneratedValue(strategy = SEQUENCE, generator = "seqTest") + private Long id; + @Column(name = "first_name") + private String firstName; + @Column(name = "last_name") + private String lastName; +} diff --git a/src/main/java/com/example/mmad/testapp/mapper/PersonMapper.java b/src/main/java/com/example/mmad/testapp/mapper/PersonMapper.java new file mode 100755 index 0000000..391c191 --- /dev/null +++ b/src/main/java/com/example/mmad/testapp/mapper/PersonMapper.java @@ -0,0 +1,26 @@ +package com.example.mmad.testapp.mapper; + +import com.example.mmad.testapp.entity.PersonEntity; +import com.example.mmad.testapp.model.PersonModel; +import org.mapstruct.Mapper; +import org.mapstruct.NullValueMappingStrategy; +import org.mapstruct.factory.Mappers; + +import java.util.List; + +@Mapper(nullValueMappingStrategy = NullValueMappingStrategy.RETURN_NULL + , componentModel = "spring" +) +public interface PersonMapper { + + static PersonMapper get() { + return Mappers.getMapper(PersonMapper.class); + } + + + PersonModel entityToModel(PersonEntity entity); + + PersonEntity modelToEntity(PersonModel model); + + List entitiesToModels(List entityList); +} diff --git a/src/main/java/com/example/mmad/testapp/model/PersonModel.java b/src/main/java/com/example/mmad/testapp/model/PersonModel.java new file mode 100755 index 0000000..283a583 --- /dev/null +++ b/src/main/java/com/example/mmad/testapp/model/PersonModel.java @@ -0,0 +1,12 @@ +package com.example.mmad.testapp.model; + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class PersonModel { + private Long id; + private String firstName; + private String lastName; +} diff --git a/src/main/java/com/example/mmad/testapp/repository/PersonRepository.java b/src/main/java/com/example/mmad/testapp/repository/PersonRepository.java new file mode 100755 index 0000000..638c7c7 --- /dev/null +++ b/src/main/java/com/example/mmad/testapp/repository/PersonRepository.java @@ -0,0 +1,22 @@ +package com.example.mmad.testapp.repository; + +import com.example.mmad.testapp.entity.PersonEntity; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; +import org.springframework.stereotype.Repository; + +@Repository +public interface PersonRepository extends JpaRepository { + + @Query(value = "select id,first_name,last_name from mmaddb.testapp.person where id =:id", nativeQuery = true) + PersonEntity gerPersonById(Long id); + + @Modifying + @Query(value = "delete from mmaddb.testapp.person where id=:id", nativeQuery = true) + void deleteById(Long id); + + @Modifying + @Query(value = "update mmaddb.testapp.person set first_name=:firstName, last_name=:lastName where id = :id",nativeQuery = true) + void updatePersonById(String firstName,String lastName,Long id); +} diff --git a/src/main/java/com/example/mmad/testapp/service/PersonService.java b/src/main/java/com/example/mmad/testapp/service/PersonService.java new file mode 100755 index 0000000..bf87028 --- /dev/null +++ b/src/main/java/com/example/mmad/testapp/service/PersonService.java @@ -0,0 +1,41 @@ +package com.example.mmad.testapp.service; + +import com.example.mmad.testapp.entity.PersonEntity; +import com.example.mmad.testapp.mapper.PersonMapper; +import com.example.mmad.testapp.model.PersonModel; +import com.example.mmad.testapp.repository.PersonRepository; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +public class PersonService { + + private final PersonRepository personRepository; + + public PersonService(PersonRepository personRepository) { + this.personRepository = personRepository; + } + + @Transactional(rollbackFor = Exception.class) + public PersonModel createPerson(PersonModel person) { + PersonEntity entity = PersonMapper.get().modelToEntity(person); + personRepository.save(entity); + return PersonMapper.get().entityToModel(entity); + } + + @Transactional(readOnly = true) + public PersonModel getPerson(Long id) { + return PersonMapper.get().entityToModel(personRepository.gerPersonById(id)); + } + + @Transactional(rollbackFor = Exception.class) + public void deletePerson(Long id) { + personRepository.deleteById(id); + } + + @Transactional(rollbackFor = Exception.class) + public PersonModel updatePerson(PersonModel model) { + personRepository.updatePersonById(model.getFirstName(), model.getLastName(), model.getId()); + return PersonMapper.get().entityToModel(personRepository.gerPersonById(model.getId())); + } +} diff --git a/src/main/java/com/example/mmad/testapp/service/StreamProducer.java b/src/main/java/com/example/mmad/testapp/service/StreamProducer.java new file mode 100644 index 0000000..a8e7672 --- /dev/null +++ b/src/main/java/com/example/mmad/testapp/service/StreamProducer.java @@ -0,0 +1,17 @@ +package com.example.mmad.testapp.service; + +import lombok.RequiredArgsConstructor; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.rabbit.stream.producer.RabbitStreamTemplate; +import org.springframework.stereotype.Service; + +@Service +@RequiredArgsConstructor +public class StreamProducer { + @Autowired + private RabbitStreamTemplate rabbitStreamTemplate; + + public void sendMessage(String message) { + rabbitStreamTemplate.convertAndSend(message); + } +} diff --git a/src/main/java/seq-query b/src/main/java/seq-query new file mode 100755 index 0000000..d5e98b1 --- /dev/null +++ b/src/main/java/seq-query @@ -0,0 +1,12 @@ +CREATE SEQUENCE testapp.person_seq + as bigint + INCREMENT 1 + START 2 + MINVALUE 2 + MAXVALUE 2800 + OWNED BY testapp.person.id; + +ALTER SEQUENCE testapp.person_seq + OWNER TO mmad; + + diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties new file mode 100755 index 0000000..22b3d8e --- /dev/null +++ b/src/main/resources/application.properties @@ -0,0 +1,19 @@ +spring.datasource.driver-class-name=org.postgresql.Driver + + +spring.datasource.url=jdbc:postgresql://85.198.8.43:7832/mmrztest +spring.datasource.username=mmrz +spring.datasource.password=fEWp7g44rHf8 + +#spring.datasource.url=jdbc:postgresql://localhost:5432/version +#spring.datasource.username=cloud +#spring.datasource.password=cloud + +#spring.datasource.url=jdbc:postgresql://localhost:5432/mmaddb +#spring.datasource.username=mmad +#spring.datasource.password=me9775 + +spring.liquibase.change-log=classpath:db/changelog/liquibase-master.xml + +spring.sql.init.mode=always + diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml new file mode 100755 index 0000000..2db2990 --- /dev/null +++ b/src/main/resources/application.yml @@ -0,0 +1,22 @@ +spring: + application: + name: servicea +server: + port: 8085 + +eureka: + client: + service-url: + defaultZone: http://localhost:8761/eureka + +rabbitmq: + stream: + name: my-stream + host: localhost + port: 5552 + + +logging: + level: + org.springframework.cloud.stream: DEBUG + org.springframework.amqp: DEBUG diff --git a/src/main/resources/db/changelog/changes/liquibase-1.1.xml b/src/main/resources/db/changelog/changes/liquibase-1.1.xml new file mode 100755 index 0000000..c5c6895 --- /dev/null +++ b/src/main/resources/db/changelog/changes/liquibase-1.1.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + diff --git a/src/main/resources/db/changelog/changes/liquibase-1.2.xml b/src/main/resources/db/changelog/changes/liquibase-1.2.xml new file mode 100755 index 0000000..e22fb6d --- /dev/null +++ b/src/main/resources/db/changelog/changes/liquibase-1.2.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/resources/db/changelog/liquibase-master.xml b/src/main/resources/db/changelog/liquibase-master.xml new file mode 100755 index 0000000..8934557 --- /dev/null +++ b/src/main/resources/db/changelog/liquibase-master.xml @@ -0,0 +1,10 @@ + + + + + + + diff --git a/src/test/java/com/example/mmad/testapp/TestAppApplicationTests.java b/src/test/java/com/example/mmad/testapp/TestAppApplicationTests.java new file mode 100755 index 0000000..f10d75e --- /dev/null +++ b/src/test/java/com/example/mmad/testapp/TestAppApplicationTests.java @@ -0,0 +1,13 @@ +package com.example.mmad.testapp; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class TestAppApplicationTests { + + @Test + void contextLoads() { + } + +}