initial commit this project for test architecture
This commit is contained in:
15
src/main/java/com/example/mmad/testapp/TestAAppApplication.java
Executable file
15
src/main/java/com/example/mmad/testapp/TestAAppApplication.java
Executable 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);
|
||||
}
|
||||
|
||||
}
|
50
src/main/java/com/example/mmad/testapp/controller/PersonController.java
Executable file
50
src/main/java/com/example/mmad/testapp/controller/PersonController.java
Executable file
@ -0,0 +1,50 @@
|
||||
package com.example.mmad.testapp.controller;
|
||||
|
||||
import com.example.mmad.testapp.model.PersonModel;
|
||||
import com.example.mmad.testapp.service.PersonService;
|
||||
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;
|
||||
|
||||
@Autowired
|
||||
public void setPersonService(PersonService personService) {
|
||||
this.personService = personService;
|
||||
}
|
||||
|
||||
@PostMapping("/list")
|
||||
@ResponseStatus(HttpStatus.OK)
|
||||
public String listPosts() {
|
||||
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");
|
||||
}
|
||||
}
|
25
src/main/java/com/example/mmad/testapp/entity/PersonEntity.java
Executable file
25
src/main/java/com/example/mmad/testapp/entity/PersonEntity.java
Executable 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;
|
||||
}
|
26
src/main/java/com/example/mmad/testapp/mapper/PersonMapper.java
Executable file
26
src/main/java/com/example/mmad/testapp/mapper/PersonMapper.java
Executable 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);
|
||||
}
|
12
src/main/java/com/example/mmad/testapp/model/PersonModel.java
Executable file
12
src/main/java/com/example/mmad/testapp/model/PersonModel.java
Executable 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;
|
||||
}
|
22
src/main/java/com/example/mmad/testapp/repository/PersonRepository.java
Executable file
22
src/main/java/com/example/mmad/testapp/repository/PersonRepository.java
Executable 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);
|
||||
}
|
41
src/main/java/com/example/mmad/testapp/service/PersonService.java
Executable file
41
src/main/java/com/example/mmad/testapp/service/PersonService.java
Executable 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()));
|
||||
}
|
||||
}
|
12
src/main/java/seq-query
Executable file
12
src/main/java/seq-query
Executable 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;
|
||||
|
||||
|
5
src/main/resources/application.properties
Executable file
5
src/main/resources/application.properties
Executable file
@ -0,0 +1,5 @@
|
||||
spring.application.name=service-b
|
||||
eureka.client.service-url.defaultZone=http://localhost:8761/eureka
|
||||
spring.config.import=configserver:http://localhost:8888/
|
||||
eureka.client.register-with-eureka=true
|
||||
eureka.client.fetch-registry=true
|
19
src/main/resources/db/changelog/changes/liquibase-1.1.xml
Executable file
19
src/main/resources/db/changelog/changes/liquibase-1.1.xml
Executable 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>
|
21
src/main/resources/db/changelog/changes/liquibase-1.2.xml
Executable file
21
src/main/resources/db/changelog/changes/liquibase-1.2.xml
Executable 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>
|
10
src/main/resources/db/changelog/liquibase-master.xml
Executable file
10
src/main/resources/db/changelog/liquibase-master.xml
Executable 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>
|
Reference in New Issue
Block a user