initial commit this project for test architecture

This commit is contained in:
esmailian
2025-04-27 09:42:09 +03:30
commit 2641387bc9
16 changed files with 490 additions and 0 deletions

View File

@ -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);
}
}

View File

@ -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");
}
}

View File

@ -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;
}

View File

@ -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<PersonModel> entitiesToModels(List<PersonEntity> entityList);
}

View File

@ -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;
}

View File

@ -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<PersonEntity, Long> {
@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);
}

View File

@ -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()));
}
}

View File

@ -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);
}
}

12
src/main/java/seq-query Executable file
View File

@ -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;

View File

@ -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

View File

@ -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

View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd">
<property name="now" value="UNIX_TIMESTAMP()" dbms="mysql"/>
<property name="now" value="sysdate" dbms="oracle"/>
<property name="now" value="now()" dbms="postgresql"/>
<changeSet id="1.1-1-mmad" author="mmad">
<insert schemaName="testapp" tableName="person">
<column name="id" value="1"/>
<column name="first_name" value="mohammad"/>
<column name="last_name" value="Reza"/>
</insert>
</changeSet>
</databaseChangeLog>

View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd">
<property name="now" value="UNIX_TIMESTAMP()" dbms="mysql"/>
<property name="now" value="sysdate" dbms="oracle"/>
<property name="now" value="now()" dbms="postgresql"/>
<changeSet id="1.2-1-mmad" author="mmad">
<createTable schemaName="testapp" tableName="person" remarks="اشخاص">
<column name="id" type="bigint" remarks="شناسه اشخاص ">
<constraints nullable="false" primaryKey="true" primaryKeyName="pk_person_id"/>
</column>
<column name="first_name" type="VARCHAR(255)" remarks="نام"/>
<column name="last_name" type="VARCHAR(255)" remarks="نام خانوادگی"/>
</createTable>
</changeSet>
</databaseChangeLog>

View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd">
<include file="changes/liquibase-1.2.xml" relativeToChangelogFile="true"/>
<include file="changes/liquibase-1.1.xml" relativeToChangelogFile="true"/>
</databaseChangeLog>

View File

@ -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() {
}
}