From f46f5d16a81a4e726aa2f7ed49e3d55e865da2dd Mon Sep 17 00:00:00 2001 From: Jesus Hernandez Barrios Date: Wed, 11 Oct 2023 15:29:31 -0400 Subject: [PATCH 1/3] Remove repo layer --- .../clean/core/repo/CRUDRepository.java | 54 ------- .../root101/clean/core/repo/Converter.java | 42 ----- .../core/repo/DelegatedSpringJpaRepo.java | 149 ------------------ 3 files changed, 245 deletions(-) delete mode 100644 src/main/java/dev/root101/clean/core/repo/CRUDRepository.java delete mode 100644 src/main/java/dev/root101/clean/core/repo/Converter.java delete mode 100644 src/main/java/dev/root101/clean/core/repo/DelegatedSpringJpaRepo.java diff --git a/src/main/java/dev/root101/clean/core/repo/CRUDRepository.java b/src/main/java/dev/root101/clean/core/repo/CRUDRepository.java deleted file mode 100644 index b845641..0000000 --- a/src/main/java/dev/root101/clean/core/repo/CRUDRepository.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright 2022 Root101 (jhernandezb96@gmail.com, +53-5-426-8660). - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Or read it directly from LICENCE.txt file at the root of this project. - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package dev.root101.clean.core.repo; - -import java.util.List; - -/** - * - * @author Root101 (jhernandezb96@gmail.com, +53-5-426-8660) - * @author JesusHdez960717@Github - * @param - * @param - */ -public interface CRUDRepository { - - public Domain create(Domain newObject) throws RuntimeException; - - public List createAll(List newObjects) throws RuntimeException; - - public Domain edit(Domain objectToEdit) throws RuntimeException; - - public List editAll(List objectsToUpdate) throws RuntimeException; - - public void delete(Domain objectToDestroy) throws RuntimeException; - - public void deleteById(ID keyId) throws RuntimeException; - - public void deleteAllById(List keyIds) throws RuntimeException; - - public Domain findById(ID keyId) throws RuntimeException; - - public List findAllById(List keyId) throws RuntimeException; - - public List findAll() throws RuntimeException; - - public default long count() throws RuntimeException { - return findAll().size(); - } - -} diff --git a/src/main/java/dev/root101/clean/core/repo/Converter.java b/src/main/java/dev/root101/clean/core/repo/Converter.java deleted file mode 100644 index 8a2e51b..0000000 --- a/src/main/java/dev/root101/clean/core/repo/Converter.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright 2022 Root101 (jhernandezb96@gmail.com, +53-5-426-8660). - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Or read it directly from LICENCE.txt file at the root of this project. - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package dev.root101.clean.core.repo; - -import java.util.List; - -/** - * - * @author Root101 (jhernandezb96@gmail.com, +53-5-426-8660) - * @author JesusHdez960717@Github - * @param - * @param - */ -public interface Converter { - - public Domain toDomain(Entity entity) throws RuntimeException; - - public Entity toEntity(Domain domain) throws RuntimeException; - - public default List toDomainAll(List list) throws RuntimeException { - return list.stream().map((entity) -> toDomain(entity)).toList(); - } - - public default List toEntityAll(List list) throws RuntimeException {//convert entities to domain - return list.stream().map((domain) -> toEntity(domain)).toList(); - } - -} diff --git a/src/main/java/dev/root101/clean/core/repo/DelegatedSpringJpaRepo.java b/src/main/java/dev/root101/clean/core/repo/DelegatedSpringJpaRepo.java deleted file mode 100644 index 0eba0e2..0000000 --- a/src/main/java/dev/root101/clean/core/repo/DelegatedSpringJpaRepo.java +++ /dev/null @@ -1,149 +0,0 @@ -/* - * Copyright 2022 Root101 (jhernandezb96@gmail.com, +53-5-426-8660). - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Or read it directly from LICENCE.txt file at the root of this project. - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package dev.root101.clean.core.repo; - -import java.util.List; -import java.util.Optional; -import org.springframework.data.jpa.repository.JpaRepository; - -/** - * - * @author Root101 (jhernandezb96@gmail.com, +53-5-426-8660) - * @author JesusHdez960717@Github - * @param - * @param - * @param - * @param - * @param - */ -public class DelegatedSpringJpaRepo, SpringJpaRepo extends JpaRepository> implements CRUDRepository { - - protected final SpringJpaRepo jpaRepo; - protected final GeneralConverter converter; - - public DelegatedSpringJpaRepo(SpringJpaRepo externalRepo, GeneralConverter converter) { - this.jpaRepo = externalRepo; - this.converter = converter; - } - - protected SpringJpaRepo repo() { - return jpaRepo; - } - - @Override - public Domain create(Domain newObject) throws RuntimeException { - //convert domain to entity - Entity entity = converter.toEntity(newObject); - - //do the persist - entity = jpaRepo.save(entity); - - //convert the domain back - return converter.toDomain(entity); - } - - @Override - public List createAll(List newObjects) throws RuntimeException { - //convert domain to entity - List entities = converter.toEntityAll(newObjects); - - //do the persist - entities = jpaRepo.saveAll(entities); - - //convert the domain back - return converter.toDomainAll(entities); - } - - @Override - public Domain edit(Domain objectToUpdate) throws RuntimeException { - return create(objectToUpdate); - } - - @Override - public List editAll(List objectsToUpdate) throws RuntimeException { - return createAll(objectsToUpdate); - } - - @Override - public void delete(Domain objectToDestroy) throws RuntimeException { - //convert domain to entity - Entity entity = converter.toEntity(objectToDestroy); - - //do the persist - jpaRepo.delete(entity); - } - - @Override - public void deleteById(ID keyId) throws RuntimeException { - //do the destroy by key, returned the entity - jpaRepo.deleteById(keyId); - } - - @Override - public void deleteAllById(List keyIds) throws RuntimeException { - jpaRepo.deleteAllById(keyIds); - } - - @Override - public Domain findById(ID keyId) throws RuntimeException { - if (keyId == null) { - return null; - } - //do the findBy, returned the entity - Optional finded = jpaRepo.findById(keyId); - Entity entity = finded.isPresent() ? finded.get() : null; - - //check if entity exists - if (entity == null) { - return null; - } - - //convert the domain back - return converter.toDomain(entity); - } - - @Override - public List findAllById(List keyId) throws RuntimeException { - //do the findBy, returned the entity - List entities = jpaRepo.findAllById(keyId); - - //check if entity exists - if (entities == null) { - return null; - } - - //convert the domain back - return converter.toDomainAll(entities); - } - - @Override - public List findAll() throws RuntimeException { - List allEntities = jpaRepo.findAll(); - - if (allEntities == null) { - return null; - } - - return converter.toDomainAll(allEntities); - } - - @Override - public long count() throws RuntimeException { - return jpaRepo.count(); - } - -} From b9ae58f3ff97c476685481adee2a339c8426aab8 Mon Sep 17 00:00:00 2001 From: Jesus Hernandez Barrios Date: Wed, 11 Oct 2023 15:29:45 -0400 Subject: [PATCH 2/3] Update docs to v5.0.0 --- CHANGELOG.md | 6 +++ README.md | 147 +++++++-------------------------------------------- 2 files changed, 25 insertions(+), 128 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8177e69..7cb9987 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +* 5.0.0.RELEASE.20231011: + * **NEW VERSION** : + * **REMOVED** :x: : Removed `Repo` layer, use basic Spring Boot architecture from now on. + * **NEW** :+1: : Deploy new version `5.0`. + * **NEW** :+1: : **Transfer** package to `Root101` org & **Rename** it to `commons`. + * 4.8.1.RELEASE.20230919: * **GENERAL** : * **IMPROVEMENT** :raised: : Fix `Jackson` type name class & remove lombok dependencies due to conflict with another project. diff --git a/README.md b/README.md index 7716365..be8a988 100644 --- a/README.md +++ b/README.md @@ -5,9 +5,7 @@ Multilanguage: --> -## Clean Core [EN] - -###### (En proceso de renombrarlo 'utils' o algo asi) +## Root101 Commons [EN] This library aims to provide standards and utilities that make work easier when creating microservices. @@ -25,17 +23,16 @@ Docs updated for version: `4.8.1.RELEASE.20230919` - [1.6.2 - Enum](#1.6.2) - [1.6.3 - Size Exact](#1.6.3) - [2 - Exceptions](#2) -- [3 - Repo](#3) -- [4 - Rest](#4) - - [4.1 - Api response](#4.1) - - [4.2 - Response Extractor (Next)](#4.2) - - [4.3 - Rest Template utils (Next)](#4.3) -- [5 - Utils](#5) - - [5.1 - Jackson](#5.1) - - [5.2 - Enum mappeable](#5.2) - - [5.3 - Network](#5.3) - - [5.4 - Security Algos](#5.4) -- [6 - How to use this package](#6) +- [4 - Rest](#3) + - [4.1 - Api response](#3.1) + - [4.2 - Response Extractor (Next)](#3.2) + - [4.3 - Rest Template utils (Next)](#3.3) +- [5 - Utils](#4) + - [5.1 - Jackson](#4.1) + - [5.2 - Enum mappeable](#4.2) + - [5.3 - Network](#4.3) + - [5.4 - Security Algos](#4.4) +- [6 - How to use this package](#5) ## Validations - All native validations are loaded from the [`jakarta.validations.*` framework](https://mvnrepository.com/artifact/jakarta.validation/jakarta.validation-api). @@ -479,117 +476,11 @@ Para estandarizar el uso de las respuestas HTTP se crearon las excepciones(mas c - `Status Code`: **500** INTERNAL SERVER ERROR. - `Use Case`: Some unexpected error occurs on the server's side, and generally the cause is unknown, this exception is thrown. -## 3 - Repo - -In an architecture **=> Logic => Repo => Data Framework =>**, the `Repo` layer acts as an intermediary between the objects received from the data layer (`Entities`), and transforms them into objects of logic (`Domains`). - -Assuming that the `Data Framework` layer is the one provided by `Spring`, this section contains the classes to make the creation of this layer easier: - -Assuming an `Entity`: - -```java -//import & getters & setters are omited -@Entity -@Table(name = "parent", schema = "some-schema") -public class Parent implements Serializable { - - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - @Basic(optional = false) - @Column(name = "parent_id", nullable = false) - private Integer parentId; - - @NotNull - @Size(max = 255) - @Basic(optional = false) - @Column(name = "name", nullable = false, length = 255) - private String name; -} -``` - -Its equivalent `Domain` would be: - -```java -//import & getters & setters are omited -public class ParentDomaine { - - private Integer parentId; - - @NotNull - @Size(max = 255) - private String name; -} -``` - -A class is then needed to parse Domain <=> Entity, for which an instance of `Converter` is created: - -```java -@Service //Inyected here in Spring -public class ParentConverter implements Converter { - - @Override - public ParentDomain toDomain(Parent entity) throws RuntimeException { - return new ParentDomain( - entity.getParentId(), - entity.getName() - ); - } - - @Override - public Parent toEntity(ParentDomain domain) throws RuntimeException { - return new Parent( - domain.getParentId(), - domain.getName() - ); - } -} -``` - -A Spring Repository: - -```java -@Repository -public interface ParentJpaRepo extends JpaRepository { - - public Parent findByName(String name); -} -``` - -Having the `Entity`, the `Domain`, and the `Converter`, we can now implement our Repo: - -```java -@Service -public class ParentRepo extends DelegatedSpringJpaRepo { - - @Autowired - public ParentRepo( - ParentJpaRepo springRepo, // Spring Repository, inyected - ParentConverter converter // Our Converter, inyected - ) { - super( - springRepo, - converter - ); - } - - //this method is an example on how to comunicate with repo - public ParentDomain findByName(String name) { - Parent finded = repo().findByName(name); - return finded != null ? converter.toDomain(finded) : null; - } - -} -``` - -Then we inject the `Repo` into the logic layer and have access to all the `data` with automatic `Domain` <=> `Entity` conversions. - -**NOTE**: A complete example of use can be found in one of the modules already developed. - -## 4 - Rest +## 3 - Rest Oficial docs for HTTP Responses [here](https://datatracker.ietf.org/doc/html/rfc7231). -### 4.1 Api Response +### 3.1 Api Response The idea of `ApiResponse` is to generalize API responses to a standard. ALL API responses must follow this guideline. The `ApiResponse` class has: @@ -646,9 +537,9 @@ How to use it: - For generic responses you can use: `ApiResponse.build(status, message, data)`. - To extract a response from a `ResponseEntity` you can use: `ApiResponse.build(status, message, data)`, which by default says `status = response.getStatusCode().value()`, `message = response. getStatusCode().toString()` and `data = response.getBody()`. -## 5 - Utils +## 4 - Utils -### 5.1 - Jackson +### 4.1 - Jackson `Jackson` is a utility class for doing **fast** and low-value conversions of objects/strings. For operations related to business logic, it is recommended to use the ObjectMapper provided by Spring. @@ -688,7 +579,7 @@ For writing (Convert Object to String): **NOTE**: This class has some other functionalities for further read/write customization, as well as to convert/parse objects from one type to another. For more details consult the source code in `dev.root101.clean.core.utils.Jackson`. -### 5.2 - Enum mappeable +### 4.2 - Enum mappeable When you want to map an Enum to its list of elements without so much code at hand: Having the enum: @@ -771,14 +662,14 @@ The second argument being the mapping function, giving the answer in this exampl ] ``` -### 5.3 - Network +### 4.3 - Network The Network utility was developed to validate that a service is running on a port:ip. ```java Network.isRunning("127.0.0.1", 8080) ``` -### 5.4 - Security Algos +### 4.4 - Security Algos The security algorithms are a quick implementation of `AES` for encryption and `SHA-256` for hashing. To use `SHA-256`, access the static method: `SecurityAlgorithms.hash256(input)`, passing the initial string through parameters and waiting for the corresponding hash in response. @@ -798,7 +689,7 @@ To use `AES`: ``` -## How to use this package +## 5 - How to use this package At the moment this package is not published in [mvnrepository](https://mvnrepository.com/), so we have to upload it directly from `Github Packages`. In the `settings.gradle` add: From 34de07cb2952bfd42222e103819823b7c8985382 Mon Sep 17 00:00:00 2001 From: Jesus Hernandez Barrios Date: Wed, 11 Oct 2023 15:29:48 -0400 Subject: [PATCH 3/3] 5.0.0.RELEASE.20231011 --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 2c09cda..cb8e07a 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ plugins { group = 'dev.root101.clean' -version = '4.8.1.RELEASE.20230919' +version = '5.0.0.RELEASE.20231011' repositories { jcenter()