From c61b7ca0f22b19a1424351c945b7f3726d887c25 Mon Sep 17 00:00:00 2001 From: PedroVidalDev Date: Thu, 25 Apr 2024 20:59:30 -0300 Subject: [PATCH] remove: removing the entity Person from the entire code --- .../controller/AccessRegisterController.java | 2 +- .../controller/ConsumerController.java | 4 +--- .../dtos/consumer/ConsumerDataComplete.java | 5 ++-- .../dtos/consumer/ConsumerDataEditInput.java | 2 +- .../dtos/person/PersonDataComplete.java | 9 ------- .../pedro/sphynx/domain/AccessService.java | 9 ++----- .../pedro/sphynx/domain/ConsumerService.java | 16 ++++--------- .../pedro/sphynx/domain/MessageService.java | 2 +- .../infrastructure/entities/Consumer.java | 10 +------- .../infrastructure/entities/Person.java | 24 ------------------- .../repository/AccessRepository.java | 2 +- .../repository/ConsumerRepository.java | 11 +++++---- .../repository/PersonRepository.java | 11 --------- .../migration/V2__create-table-consumers.sql | 7 ++++++ .../db/migration/V2__create-table-persons.sql | 8 ------- .../migration/V3__create-table-consumers.sql | 8 ------- ...locals.sql => V3__create-table-locals.sql} | 0 ...l => V4__create-table-accessRegisters.sql} | 0 ...mers-add-column-dtcreate-and-dtupdate.sql} | 0 ...ot.sql => V6__add-to-table-users-root.sql} | 0 ... V7__alter-table-local-add-permission.sql} | 0 ...8__add-to-table-persons-default-values.sql | 7 ------ ..._alter-table-consumers-add-permission.sql} | 0 ...ble-accessRegisters-add-column-status.sql} | 0 src/main/resources/messagesEn.properties | 2 +- src/main/resources/messagesPt.properties | 2 +- 26 files changed, 29 insertions(+), 112 deletions(-) delete mode 100644 src/main/java/com/pedro/sphynx/application/dtos/person/PersonDataComplete.java delete mode 100644 src/main/java/com/pedro/sphynx/infrastructure/entities/Person.java delete mode 100644 src/main/java/com/pedro/sphynx/infrastructure/repository/PersonRepository.java create mode 100644 src/main/resources/db/migration/V2__create-table-consumers.sql delete mode 100644 src/main/resources/db/migration/V2__create-table-persons.sql delete mode 100644 src/main/resources/db/migration/V3__create-table-consumers.sql rename src/main/resources/db/migration/{V4__create-table-locals.sql => V3__create-table-locals.sql} (100%) rename src/main/resources/db/migration/{V5__create-table-accessRegisters.sql => V4__create-table-accessRegisters.sql} (100%) rename src/main/resources/db/migration/{V6__alter-table-consumers-add-column-dtcreate-and-dtupdate.sql => V5__alter-table-consumers-add-column-dtcreate-and-dtupdate.sql} (100%) rename src/main/resources/db/migration/{V7__add-to-table-users-root.sql => V6__add-to-table-users-root.sql} (100%) rename src/main/resources/db/migration/{V9__alter-table-local-add-permission.sql => V7__alter-table-local-add-permission.sql} (100%) delete mode 100644 src/main/resources/db/migration/V8__add-to-table-persons-default-values.sql rename src/main/resources/db/migration/{V10__alter-table-consumers-add-permission.sql => V8__alter-table-consumers-add-permission.sql} (100%) rename src/main/resources/db/migration/{V11__alter-table-accessRegisters-add-column-status.sql => V9__alter-table-accessRegisters-add-column-status.sql} (100%) diff --git a/src/main/java/com/pedro/sphynx/application/controller/AccessRegisterController.java b/src/main/java/com/pedro/sphynx/application/controller/AccessRegisterController.java index 0243d05..5819b02 100644 --- a/src/main/java/com/pedro/sphynx/application/controller/AccessRegisterController.java +++ b/src/main/java/com/pedro/sphynx/application/controller/AccessRegisterController.java @@ -49,7 +49,7 @@ public ResponseEntity> getAll(){ @GetMapping("/byRa/{ra}") public ResponseEntity> getAllByRa(@PathVariable String ra){ - var listAccess = repository.findAllByConsumerPersonRa(ra).stream().map(AccessDataComplete::new).toList(); + var listAccess = repository.findAllByConsumerRa(ra).stream().map(AccessDataComplete::new).toList(); return ResponseEntity.ok(listAccess); } diff --git a/src/main/java/com/pedro/sphynx/application/controller/ConsumerController.java b/src/main/java/com/pedro/sphynx/application/controller/ConsumerController.java index a764a31..7ebf979 100644 --- a/src/main/java/com/pedro/sphynx/application/controller/ConsumerController.java +++ b/src/main/java/com/pedro/sphynx/application/controller/ConsumerController.java @@ -7,14 +7,12 @@ import com.pedro.sphynx.domain.ConsumerService; import com.pedro.sphynx.domain.MessageService; import com.pedro.sphynx.infrastructure.repository.ConsumerRepository; -import com.pedro.sphynx.infrastructure.repository.PersonRepository; import jakarta.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.*; -import java.util.ArrayList; import java.util.List; @RestController @@ -52,7 +50,7 @@ public ResponseEntity update(@PathVariable String ra, @RequestBody @Valid Consum @DeleteMapping("/{ra}") @Transactional public ResponseEntity delete(@PathVariable String ra){ - repository.deleteByPersonRa(ra); + repository.deleteByRa(ra); return ResponseEntity.noContent().build(); } diff --git a/src/main/java/com/pedro/sphynx/application/dtos/consumer/ConsumerDataComplete.java b/src/main/java/com/pedro/sphynx/application/dtos/consumer/ConsumerDataComplete.java index 10e119e..de352a3 100644 --- a/src/main/java/com/pedro/sphynx/application/dtos/consumer/ConsumerDataComplete.java +++ b/src/main/java/com/pedro/sphynx/application/dtos/consumer/ConsumerDataComplete.java @@ -1,11 +1,10 @@ package com.pedro.sphynx.application.dtos.consumer; -import com.pedro.sphynx.application.dtos.person.PersonDataComplete; import com.pedro.sphynx.infrastructure.entities.Consumer; -public record ConsumerDataComplete (Long id, PersonDataComplete person, String tag, int permission){ +public record ConsumerDataComplete (Long id, String ra, String tag, int permission){ public ConsumerDataComplete(Consumer consumer){ - this(consumer.getId(), new PersonDataComplete(consumer.getPerson()), consumer.getTag(), consumer.getPermission()); + this(consumer.getId(), consumer.getRa(), consumer.getTag(), consumer.getPermission()); } } diff --git a/src/main/java/com/pedro/sphynx/application/dtos/consumer/ConsumerDataEditInput.java b/src/main/java/com/pedro/sphynx/application/dtos/consumer/ConsumerDataEditInput.java index 5a8bf99..2db3a91 100644 --- a/src/main/java/com/pedro/sphynx/application/dtos/consumer/ConsumerDataEditInput.java +++ b/src/main/java/com/pedro/sphynx/application/dtos/consumer/ConsumerDataEditInput.java @@ -3,5 +3,5 @@ import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotNull; -public record ConsumerDataEditInput(@NotBlank String tag, String permission){ +public record ConsumerDataEditInput(@NotBlank String tag, int permission){ } diff --git a/src/main/java/com/pedro/sphynx/application/dtos/person/PersonDataComplete.java b/src/main/java/com/pedro/sphynx/application/dtos/person/PersonDataComplete.java deleted file mode 100644 index 82b36b2..0000000 --- a/src/main/java/com/pedro/sphynx/application/dtos/person/PersonDataComplete.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.pedro.sphynx.application.dtos.person; - -import com.pedro.sphynx.infrastructure.entities.Person; - -public record PersonDataComplete(Long id, String ra, String name, String gender) { - public PersonDataComplete(Person data){ - this(data.getId(), data.getRa(), data.getName(), data.getGender()); - } -} diff --git a/src/main/java/com/pedro/sphynx/domain/AccessService.java b/src/main/java/com/pedro/sphynx/domain/AccessService.java index 109358b..0a48e61 100644 --- a/src/main/java/com/pedro/sphynx/domain/AccessService.java +++ b/src/main/java/com/pedro/sphynx/domain/AccessService.java @@ -5,12 +5,10 @@ import com.pedro.sphynx.application.dtos.consumer.ConsumerDataComplete; import com.pedro.sphynx.application.dtos.local.LocalDataComplete; import com.pedro.sphynx.infrastructure.entities.Access; -import com.pedro.sphynx.infrastructure.entities.Person; import com.pedro.sphynx.infrastructure.exceptions.Validation; import com.pedro.sphynx.infrastructure.repository.AccessRepository; import com.pedro.sphynx.infrastructure.repository.ConsumerRepository; import com.pedro.sphynx.infrastructure.repository.LocalRepository; -import com.pedro.sphynx.infrastructure.repository.PersonRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -26,9 +24,6 @@ public class AccessService { @Autowired private ConsumerRepository consumerRepository; - @Autowired - private PersonRepository personRepository; - @Autowired private LocalRepository localRepository; @@ -51,8 +46,8 @@ public AccessDataComplete validateCreation(AccessDataInput data, String language } ConsumerDataComplete consumer = new ConsumerDataComplete(consumerRepository.findByTag(data.tag())); - if(!personRepository.existsByRa(consumer.person().ra())){ - throw new Validation(messages.getString("error.raDontExistsInPerson")); + if(!consumerRepository.existsByRa(consumer.ra())){ + throw new Validation(messages.getString("error.raDontExistsInConsumer")); } if(!localRepository.existsByMac(data.local())){ diff --git a/src/main/java/com/pedro/sphynx/domain/ConsumerService.java b/src/main/java/com/pedro/sphynx/domain/ConsumerService.java index 1191f47..a8aebd1 100644 --- a/src/main/java/com/pedro/sphynx/domain/ConsumerService.java +++ b/src/main/java/com/pedro/sphynx/domain/ConsumerService.java @@ -6,7 +6,6 @@ import com.pedro.sphynx.infrastructure.entities.Consumer; import com.pedro.sphynx.infrastructure.exceptions.Validation; import com.pedro.sphynx.infrastructure.repository.ConsumerRepository; -import com.pedro.sphynx.infrastructure.repository.PersonRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -19,9 +18,6 @@ public class ConsumerService { @Autowired private ConsumerRepository consumerRepository; - @Autowired - private PersonRepository personRepository; - private ResourceBundle messages = ResourceBundle.getBundle("messagesEn"); public void defineMessagesLanguage(String language){ @@ -36,16 +32,12 @@ else if(language.equals("en-US")){ public ConsumerDataComplete createVerify(ConsumerDataInput data, String language){ defineMessagesLanguage(language); - if(consumerRepository.existsByPersonRa(data.ra())){ + if(consumerRepository.existsByRa(data.ra())){ throw new Validation(messages.getString("error.raAlreadyExists")); } - else if(!personRepository.existsByRa(data.ra())){ - throw new Validation(messages.getString("error.raDontExistsInPerson")); - } - else{ - Consumer consumer = new Consumer(null, personRepository.findOneByRa(data.ra()), data.tag(), data.permission(), LocalDateTime.now(), null); + Consumer consumer = new Consumer(null, data.ra(), data.tag(), data.permission(), LocalDateTime.now(), null); consumerRepository.save(consumer); return new ConsumerDataComplete(consumer); } @@ -54,8 +46,8 @@ else if(!personRepository.existsByRa(data.ra())){ public ConsumerDataComplete updateVerify(ConsumerDataEditInput data, String ra, String language){ defineMessagesLanguage(language); - if(consumerRepository.existsByPersonRa(ra)){ - var consumer = consumerRepository.getReferenceByPersonRa(ra); + if(consumerRepository.existsByRa(ra)){ + var consumer = consumerRepository.getReferenceByRa(ra); consumer.actualizeData(data); return new ConsumerDataComplete(consumer); } else{ diff --git a/src/main/java/com/pedro/sphynx/domain/MessageService.java b/src/main/java/com/pedro/sphynx/domain/MessageService.java index 2effaca..0667a50 100644 --- a/src/main/java/com/pedro/sphynx/domain/MessageService.java +++ b/src/main/java/com/pedro/sphynx/domain/MessageService.java @@ -17,6 +17,6 @@ else if(language.equals("en-US")){ messages = ResourceBundle.getBundle("messagesEn"); } - return new MessageDTO(status, messages.getString("success." + status), object); + return new MessageDTO(status, messages.getString("success." + String.valueOf(status)), object); } } diff --git a/src/main/java/com/pedro/sphynx/infrastructure/entities/Consumer.java b/src/main/java/com/pedro/sphynx/infrastructure/entities/Consumer.java index 3321627..0c44424 100644 --- a/src/main/java/com/pedro/sphynx/infrastructure/entities/Consumer.java +++ b/src/main/java/com/pedro/sphynx/infrastructure/entities/Consumer.java @@ -19,10 +19,7 @@ public class Consumer { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; - @OneToOne(fetch = FetchType.LAZY) - @JoinColumn(name = "person_id") - private Person person; - + private String ra; private String tag; private int permission; private LocalDateTime dtcreate; @@ -33,10 +30,5 @@ public void actualizeData(ConsumerDataEditInput data) { this.tag = data.tag(); this.dtupdate = LocalDateTime.now(); } - - if(data.permission() != null){ - this.tag = data.tag(); - this.dtupdate = LocalDateTime.now(); - } } } diff --git a/src/main/java/com/pedro/sphynx/infrastructure/entities/Person.java b/src/main/java/com/pedro/sphynx/infrastructure/entities/Person.java deleted file mode 100644 index cb417b3..0000000 --- a/src/main/java/com/pedro/sphynx/infrastructure/entities/Person.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.pedro.sphynx.infrastructure.entities; - -import jakarta.persistence.*; -import lombok.AllArgsConstructor; -import lombok.EqualsAndHashCode; -import lombok.Getter; -import lombok.NoArgsConstructor; - -@Table(name="persons") -@Entity(name="Person") -@Getter -@NoArgsConstructor -@AllArgsConstructor -@EqualsAndHashCode(of = "id") -public class Person { - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; - - private String ra; - private String name; - private String gender; - -} diff --git a/src/main/java/com/pedro/sphynx/infrastructure/repository/AccessRepository.java b/src/main/java/com/pedro/sphynx/infrastructure/repository/AccessRepository.java index e31bc05..edbd678 100644 --- a/src/main/java/com/pedro/sphynx/infrastructure/repository/AccessRepository.java +++ b/src/main/java/com/pedro/sphynx/infrastructure/repository/AccessRepository.java @@ -8,7 +8,7 @@ public interface AccessRepository extends JpaRepository { - List findAllByConsumerPersonRa(String ra); + List findAllByConsumerRa(String ra); List findAllByLocalName(String local); diff --git a/src/main/java/com/pedro/sphynx/infrastructure/repository/ConsumerRepository.java b/src/main/java/com/pedro/sphynx/infrastructure/repository/ConsumerRepository.java index 1e3acd3..3910df2 100644 --- a/src/main/java/com/pedro/sphynx/infrastructure/repository/ConsumerRepository.java +++ b/src/main/java/com/pedro/sphynx/infrastructure/repository/ConsumerRepository.java @@ -4,13 +4,14 @@ import org.springframework.data.jpa.repository.JpaRepository; public interface ConsumerRepository extends JpaRepository { - boolean existsByPersonRa(String ra); - - Consumer getReferenceByPersonRa(String ra); - - void deleteByPersonRa(String ra); boolean existsByTag(String tag); Consumer findByTag(String ra); + + boolean existsByRa(String ra); + + Consumer getReferenceByRa(String ra); + + void deleteByRa(String ra); } diff --git a/src/main/java/com/pedro/sphynx/infrastructure/repository/PersonRepository.java b/src/main/java/com/pedro/sphynx/infrastructure/repository/PersonRepository.java deleted file mode 100644 index 6fe0b42..0000000 --- a/src/main/java/com/pedro/sphynx/infrastructure/repository/PersonRepository.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.pedro.sphynx.infrastructure.repository; - -import com.pedro.sphynx.infrastructure.entities.Consumer; -import com.pedro.sphynx.infrastructure.entities.Person; -import org.springframework.data.jpa.repository.JpaRepository; - -public interface PersonRepository extends JpaRepository { - boolean existsByRa(String ra); - - Person findOneByRa(String ra); -} diff --git a/src/main/resources/db/migration/V2__create-table-consumers.sql b/src/main/resources/db/migration/V2__create-table-consumers.sql new file mode 100644 index 0000000..57baf98 --- /dev/null +++ b/src/main/resources/db/migration/V2__create-table-consumers.sql @@ -0,0 +1,7 @@ +create table if not exists consumers( + id bigint not null auto_increment, + ra varchar(255) not null, + tag varchar(255) not null, + + primary key(id) +); \ No newline at end of file diff --git a/src/main/resources/db/migration/V2__create-table-persons.sql b/src/main/resources/db/migration/V2__create-table-persons.sql deleted file mode 100644 index 2c41fcf..0000000 --- a/src/main/resources/db/migration/V2__create-table-persons.sql +++ /dev/null @@ -1,8 +0,0 @@ -create table if not exists persons( - id bigint not null auto_increment, - ra varchar(255) not null, - name varchar(255) not null, - gender varchar(255) not null, - - primary key(id) -); \ No newline at end of file diff --git a/src/main/resources/db/migration/V3__create-table-consumers.sql b/src/main/resources/db/migration/V3__create-table-consumers.sql deleted file mode 100644 index 9155e00..0000000 --- a/src/main/resources/db/migration/V3__create-table-consumers.sql +++ /dev/null @@ -1,8 +0,0 @@ -create table if not exists consumers( - id bigint not null auto_increment, - person_id bigint not null, - tag varchar(255) not null, - - primary key(id), - constraint fk_schedules_person_id foreign key (person_id) references persons(id) -); \ No newline at end of file diff --git a/src/main/resources/db/migration/V4__create-table-locals.sql b/src/main/resources/db/migration/V3__create-table-locals.sql similarity index 100% rename from src/main/resources/db/migration/V4__create-table-locals.sql rename to src/main/resources/db/migration/V3__create-table-locals.sql diff --git a/src/main/resources/db/migration/V5__create-table-accessRegisters.sql b/src/main/resources/db/migration/V4__create-table-accessRegisters.sql similarity index 100% rename from src/main/resources/db/migration/V5__create-table-accessRegisters.sql rename to src/main/resources/db/migration/V4__create-table-accessRegisters.sql diff --git a/src/main/resources/db/migration/V6__alter-table-consumers-add-column-dtcreate-and-dtupdate.sql b/src/main/resources/db/migration/V5__alter-table-consumers-add-column-dtcreate-and-dtupdate.sql similarity index 100% rename from src/main/resources/db/migration/V6__alter-table-consumers-add-column-dtcreate-and-dtupdate.sql rename to src/main/resources/db/migration/V5__alter-table-consumers-add-column-dtcreate-and-dtupdate.sql diff --git a/src/main/resources/db/migration/V7__add-to-table-users-root.sql b/src/main/resources/db/migration/V6__add-to-table-users-root.sql similarity index 100% rename from src/main/resources/db/migration/V7__add-to-table-users-root.sql rename to src/main/resources/db/migration/V6__add-to-table-users-root.sql diff --git a/src/main/resources/db/migration/V9__alter-table-local-add-permission.sql b/src/main/resources/db/migration/V7__alter-table-local-add-permission.sql similarity index 100% rename from src/main/resources/db/migration/V9__alter-table-local-add-permission.sql rename to src/main/resources/db/migration/V7__alter-table-local-add-permission.sql diff --git a/src/main/resources/db/migration/V8__add-to-table-persons-default-values.sql b/src/main/resources/db/migration/V8__add-to-table-persons-default-values.sql deleted file mode 100644 index 5b77ea7..0000000 --- a/src/main/resources/db/migration/V8__add-to-table-persons-default-values.sql +++ /dev/null @@ -1,7 +0,0 @@ -INSERT INTO persons(ra, name, gender) values ('2920482311001', 'Kaik', 'M'); -INSERT INTO persons(ra, name, gender) values ('2920482311002', 'Ana', 'F'); -INSERT INTO persons(ra, name, gender) values ('2920482311003', 'Mickael', 'M'); -INSERT INTO persons(ra, name, gender) values ('2920482311004', 'Paulo', 'M'); -INSERT INTO persons(ra, name, gender) values ('2920482311005', 'Luis', 'M'); -INSERT INTO persons(ra, name, gender) values ('2920482311006', 'Nata', 'M'); -INSERT INTO persons(ra, name, gender) values ('2920482311007', 'Gustavo', 'M'); \ No newline at end of file diff --git a/src/main/resources/db/migration/V10__alter-table-consumers-add-permission.sql b/src/main/resources/db/migration/V8__alter-table-consumers-add-permission.sql similarity index 100% rename from src/main/resources/db/migration/V10__alter-table-consumers-add-permission.sql rename to src/main/resources/db/migration/V8__alter-table-consumers-add-permission.sql diff --git a/src/main/resources/db/migration/V11__alter-table-accessRegisters-add-column-status.sql b/src/main/resources/db/migration/V9__alter-table-accessRegisters-add-column-status.sql similarity index 100% rename from src/main/resources/db/migration/V11__alter-table-accessRegisters-add-column-status.sql rename to src/main/resources/db/migration/V9__alter-table-accessRegisters-add-column-status.sql diff --git a/src/main/resources/messagesEn.properties b/src/main/resources/messagesEn.properties index 2759235..f369211 100644 --- a/src/main/resources/messagesEn.properties +++ b/src/main/resources/messagesEn.properties @@ -1,6 +1,6 @@ error.raDontExists=Informed RA dont exists. error.raAlreadyExists=Informed RA already exists in the database. -error.raDontExistsInPerson=Informed RA dont exists in the persons database. +error.raDontExistsInConsumer=Informed RA dont exists in the persons database. error.tagDontExists=Informed TAG dont exists in database. diff --git a/src/main/resources/messagesPt.properties b/src/main/resources/messagesPt.properties index e4d6c32..883fc73 100644 --- a/src/main/resources/messagesPt.properties +++ b/src/main/resources/messagesPt.properties @@ -1,6 +1,6 @@ error.raDontExists=RA informado nao existe. error.raAlreadyExists=RA informado ja existe no banco. -error.raDontExistsInPerson=RA informado nao existe na tabela de doscentes. +error.raDontExistsInConsumer=RA informado nao existe na tabela de doscentes. error.tagDontExists=TAG informada nao existe no banco.