Skip to content

Commit

Permalink
remove: removing the entity Person from the entire code
Browse files Browse the repository at this point in the history
  • Loading branch information
PedroVidalDev committed Apr 25, 2024
1 parent 0345b9f commit c61b7ca
Show file tree
Hide file tree
Showing 26 changed files with 29 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public ResponseEntity<List<AccessDataComplete>> getAll(){

@GetMapping("/byRa/{ra}")
public ResponseEntity<List<AccessDataComplete>> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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){
}

This file was deleted.

9 changes: 2 additions & 7 deletions src/main/java/com/pedro/sphynx/domain/AccessService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -26,9 +24,6 @@ public class AccessService {
@Autowired
private ConsumerRepository consumerRepository;

@Autowired
private PersonRepository personRepository;

@Autowired
private LocalRepository localRepository;

Expand All @@ -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())){
Expand Down
16 changes: 4 additions & 12 deletions src/main/java/com/pedro/sphynx/domain/ConsumerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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){
Expand All @@ -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);
}
Expand All @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/pedro/sphynx/domain/MessageService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}
}
}
24 changes: 0 additions & 24 deletions src/main/java/com/pedro/sphynx/infrastructure/entities/Person.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

public interface AccessRepository extends JpaRepository<Access, Long> {

List<Access> findAllByConsumerPersonRa(String ra);
List<Access> findAllByConsumerRa(String ra);

List<Access> findAllByLocalName(String local);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
import org.springframework.data.jpa.repository.JpaRepository;

public interface ConsumerRepository extends JpaRepository<Consumer, Long> {
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);
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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)
);
8 changes: 0 additions & 8 deletions src/main/resources/db/migration/V2__create-table-persons.sql

This file was deleted.

This file was deleted.

This file was deleted.

2 changes: 1 addition & 1 deletion src/main/resources/messagesEn.properties
Original file line number Diff line number Diff line change
@@ -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.

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/messagesPt.properties
Original file line number Diff line number Diff line change
@@ -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.

Expand Down

0 comments on commit c61b7ca

Please sign in to comment.