Skip to content

Commit 0fcd8e3

Browse files
authored
Merge pull request #63 from rcsim/feature/standardize_code
Issue #62 - Padronização do código entre os endpoints
2 parents a75fd68 + 8bbbe95 commit 0fcd8e3

11 files changed

Lines changed: 64 additions & 53 deletions

File tree

docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ services:
2222
- SPRING_DATASOURCE_USERNAME=compose-postgres
2323
- SPRING_DATASOURCE_PASSWORD=compose-postgres
2424
- SPRING_JPA_HIBERNATE_DDL_AUTO=update
25+
- SPRING_PROFILES_ACTIVE=dev
2526
ports:
2627
- "8080:8080"
2728
networks:

src/main/java/br/com/postech30/challenge/controller/AddressController.java

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,68 +4,76 @@
44
import br.com.postech30.challenge.dto.ApplianceDTO;
55
import br.com.postech30.challenge.dto.DependentDTO;
66
import br.com.postech30.challenge.service.AddressService;
7+
import jakarta.transaction.Transactional;
78
import jakarta.validation.Valid;
89
import org.springframework.beans.factory.annotation.Autowired;
910
import org.springframework.data.domain.Page;
1011
import org.springframework.data.domain.Pageable;
1112
import org.springframework.http.HttpStatus;
1213
import org.springframework.http.ResponseEntity;
14+
import org.springframework.validation.annotation.Validated;
1315
import org.springframework.web.bind.annotation.*;
1416

1517
import java.util.List;
1618

1719
@RestController
1820
@RequestMapping(value = "/address")
21+
@Validated
22+
@Transactional
1923
public class AddressController {
2024

2125
@Autowired
22-
AddressService service;
26+
AddressService addressService;
27+
28+
public AddressController(AddressService addressService) {
29+
this.addressService = addressService;
30+
}
2331

2432
@PostMapping
25-
public ResponseEntity<String> addAddress(@RequestBody @Valid AddressDTO addressDTO) {
33+
public ResponseEntity<AddressDTO> addAddress(@RequestBody @Valid AddressDTO addressDTO) {
2634

27-
service.saveAddress(addressDTO);
28-
return ResponseEntity.status(HttpStatus.CREATED).body("Endereço cadastrado com sucesso!");
35+
var addressSave = addressService.saveAddress(addressDTO);
36+
return ResponseEntity.status(HttpStatus.CREATED).body(addressSave);
2937
}
3038

3139
@GetMapping
3240
public ResponseEntity<Page<AddressDTO>> getAddresses(
3341
@RequestParam(defaultValue = "") String search, Pageable pageable) {
3442

35-
Page<AddressDTO> page = service.search(search, pageable);
43+
Page<AddressDTO> page = addressService.search(search, pageable);
3644
return ResponseEntity.status(HttpStatus.OK).body(page);
3745
}
3846

3947
@GetMapping(value = "{id}")
4048
public ResponseEntity<AddressDTO> getAddressById(@PathVariable Long id) {
4149

42-
AddressDTO dto = service.findById(id);
50+
AddressDTO dto = addressService.findById(id);
4351
return ResponseEntity.status(HttpStatus.OK).body(dto);
4452
}
4553

4654
@DeleteMapping(value = "{id}")
4755
public ResponseEntity<String> removeAddress(@PathVariable Long id) {
4856

49-
service.delete(id);
57+
addressService.delete(id);
5058
return ResponseEntity.status(HttpStatus.OK).body("Endereço deletado com sucesso!");
5159
}
5260

5361
@PutMapping(value = "{id}")
5462
public ResponseEntity<String> updateAddress(@PathVariable Long id, @RequestBody @Valid AddressDTO addressDTO) {
5563

56-
service.update(id, addressDTO);
64+
addressService.update(id, addressDTO);
5765
return ResponseEntity.status(HttpStatus.OK).body("Endereço atualizado com sucesso!");
5866
}
5967

6068
@GetMapping(value = "{id}/dependents")
6169
public ResponseEntity<List<DependentDTO>> findDependentAddress(@PathVariable Long id) {
62-
var dependent = service.findDependentByAddressId(id);
70+
var dependent = addressService.findDependentByAddressId(id);
6371
return ResponseEntity.ok(dependent);
6472
}
6573

6674
@GetMapping(value = "{id}/appliances")
6775
public ResponseEntity<List<ApplianceDTO>> findAppliancesAddress(@PathVariable Long id) {
68-
var appliance = service.findApplianceByAddressId(id);
76+
var appliance = addressService.findApplianceByAddressId(id);
6977
return ResponseEntity.ok(appliance);
7078
}
7179
}

src/main/java/br/com/postech30/challenge/controller/ApplianceController.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818

1919

2020

21-
@Validated
22-
@Transactional
2321
@RestController
2422
@RequestMapping(value = "/appliance")
23+
@Validated
24+
@Transactional
2525
public class ApplianceController {
2626

2727
@Autowired
@@ -34,7 +34,7 @@ public ApplianceController(ApplianceService applianceService) {
3434
@PostMapping
3535
public ResponseEntity<ApplianceDTO> saveAppliance(@RequestBody @Valid ApplianceDTO applianceDTO) {
3636
var applianceSave = applianceService.saveAppliance(applianceDTO);
37-
return ResponseEntity.ok(applianceSave);
37+
return ResponseEntity.status(HttpStatus.CREATED).body(applianceSave);
3838
}
3939

4040

src/main/java/br/com/postech30/challenge/controller/DependentController.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import br.com.postech30.challenge.dto.DependentDTO;
55
import br.com.postech30.challenge.service.DependentService;
66
import jakarta.validation.Valid;
7+
import org.springframework.beans.factory.annotation.Autowired;
78
import org.springframework.data.domain.Page;
89
import org.springframework.data.domain.Pageable;
910
import org.springframework.http.HttpStatus;
@@ -20,9 +21,9 @@
2021
@Transactional
2122
public class DependentController {
2223

24+
@Autowired
2325
private final DependentService dependentService;
2426

25-
2627
public DependentController(DependentService dependentService) {
2728
this.dependentService = dependentService;
2829
}
@@ -42,7 +43,7 @@ public ResponseEntity<DependentDTO> findById(@PathVariable Long id) {
4243
@PostMapping
4344
public ResponseEntity<DependentDTO> save(@RequestBody @Valid DependentDTO dependentDTO) {
4445
var dependentSaved = dependentService.saveDependent(dependentDTO);
45-
return ResponseEntity.ok(dependentSaved);
46+
return ResponseEntity.status(HttpStatus.CREATED).body(dependentSaved);
4647
}
4748

4849
@PutMapping("/{id}")

src/main/java/br/com/postech30/challenge/dto/AddressDTO.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,37 @@
77
import lombok.AllArgsConstructor;
88
import lombok.Getter;
99
import lombok.NoArgsConstructor;
10+
import lombok.Setter;
1011

12+
@Getter
13+
@Setter
1114
@NoArgsConstructor
1215
@AllArgsConstructor
1316
public class AddressDTO {
14-
@Getter
17+
1518
private Long id;
19+
1620
@JsonProperty
17-
@Getter
1821
@NotBlank(message = "A rua é um campo de preenchimento obrigatório")
1922
private String street;
23+
2024
@JsonProperty
21-
@Getter
2225
@NotBlank(message = "O número é um campo de preenchimento obrigatório")
2326
private String number;
27+
2428
@JsonProperty
25-
@Getter
2629
@NotBlank(message = "O bairro é um campo de preenchimento obrigatório")
2730
private String district;
31+
2832
@JsonProperty
29-
@Getter
3033
@NotBlank(message = "A cidade é um campo de preenchimento obrigatório")
3134
private String city;
35+
3236
@JsonProperty
33-
@Getter
3437
@NotBlank(message = "O estado é um campo de preenchimento obrigatório")
3538
private String state;
3639

3740
@JsonProperty
38-
@Getter
3941
@NotNull(message = "O endereço precisa estar associado a um usuário")
4042
private Long userId;
4143

src/main/java/br/com/postech30/challenge/entity/Address.java

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import java.util.ArrayList;
77
import java.util.List;
88

9+
@Getter
10+
@Setter
911
@AllArgsConstructor
1012
@NoArgsConstructor
1113
@Entity
@@ -15,23 +17,18 @@ public class Address {
1517

1618
@Id
1719
@GeneratedValue(strategy = GenerationType.IDENTITY)
18-
@Getter
20+
1921
private Long id;
20-
@Getter
21-
@Setter
22+
2223
@Column(columnDefinition = "TEXT")
2324
private String street;
24-
@Getter
25-
@Setter
25+
2626
private String number;
27-
@Getter
28-
@Setter
27+
2928
private String district;
30-
@Getter
31-
@Setter
29+
3230
private String city;
33-
@Getter
34-
@Setter
31+
3532
private String state;
3633

3734
@Getter
@@ -42,8 +39,6 @@ public class Address {
4239
@OneToMany(mappedBy = "address", cascade = CascadeType.ALL, orphanRemoval = true)
4340
private List<Dependent> dependentList = new ArrayList<>();
4441

45-
@Getter
46-
@Setter
4742
private Long userId;
4843

4944
}

src/main/java/br/com/postech30/challenge/repository/AddressRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
@Repository
1212
public interface AddressRepository extends JpaRepository<Address, Long> {
13-
Address save(Address address);
13+
//Address save(Address address);
1414

1515
Page<Address> findByStreetIgnoreCaseContainingOrDistrictIgnoreCaseContainingOrCityIgnoreCaseContainingOrStateIgnoreCaseContaining(String street, String district, String city, String state, Pageable pageable);
1616
}

src/main/java/br/com/postech30/challenge/repository/ApplianceRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
@Repository
1212
public interface ApplianceRepository extends JpaRepository<Appliance, Long> {
13-
Appliance save(Appliance appliance);
13+
// Appliance save(Appliance appliance);
1414

1515
List<Appliance> findByAddress_Id(Long id);
1616

src/main/java/br/com/postech30/challenge/service/AddressService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
public interface AddressService {
1313

14-
void saveAddress(AddressDTO addressDTO);
14+
AddressDTO saveAddress(AddressDTO addressDTO);
1515

1616
AddressDTO findById(Long id);
1717

src/main/java/br/com/postech30/challenge/service/impl/AddressServiceImpl.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
public class AddressServiceImpl implements AddressService {
2626

2727
@Autowired
28-
private AddressRepository repository;
28+
private AddressRepository addressRepository;
2929
@Autowired
3030
DependentRepository dependentRepository;
3131
@Autowired
@@ -34,44 +34,45 @@ public class AddressServiceImpl implements AddressService {
3434
static final String RESPONSEENDERECONAOENCONTRADO = "Endereço não encontrado";
3535
@Override
3636
@Transactional
37-
public void saveAddress(AddressDTO addressDTO) {
38-
Address address = new Address();
39-
address = mapTo(addressDTO, address);
40-
repository.save(address);
37+
public AddressDTO saveAddress(AddressDTO addressDTO) {
38+
Address addressEntity = new Address();
39+
addressEntity = mapTo(addressDTO, addressEntity);
40+
return new AddressDTO(addressRepository.save(addressEntity));
4141
}
4242

43+
4344
@Override
4445
@Transactional(readOnly = true)
4546
public Page<AddressDTO> search(String text, Pageable pageable) {
4647
Page<Address> page;
4748
if (Objects.equals(text, "")) {
48-
page = repository.findAll(pageable);
49+
page = addressRepository.findAll(pageable);
4950
} else {
50-
page = repository.findByStreetIgnoreCaseContainingOrDistrictIgnoreCaseContainingOrCityIgnoreCaseContainingOrStateIgnoreCaseContaining(text, text, text, text, pageable);
51+
page = addressRepository.findByStreetIgnoreCaseContainingOrDistrictIgnoreCaseContainingOrCityIgnoreCaseContainingOrStateIgnoreCaseContaining(text, text, text, text, pageable);
5152
}
5253
return page.map(AddressDTO::new);
5354
}
5455

5556
@Override
5657
@Transactional
5758
public List<DependentDTO> findDependentByAddressId(Long id) {
58-
Address address = repository.getReferenceById(id);
59+
Address address = addressRepository.getReferenceById(id);
5960
List<Dependent> addressDependent = dependentRepository.findByAddress_Id(address.getId());
6061
return addressDependent.stream().map(DependentDTO::new).toList();
6162
}
6263

6364
@Override
6465
@Transactional
6566
public List<ApplianceDTO> findApplianceByAddressId(Long id) {
66-
Address address = repository.getReferenceById(id);
67+
Address address = addressRepository.getReferenceById(id);
6768
List<Appliance> addressAppliance =applianceRepository.findByAddress_Id(address.getId());
6869
return addressAppliance.stream().map(ApplianceDTO::new).toList();
6970
}
7071

7172
@Override
7273
@Transactional(readOnly = true)
7374
public AddressDTO findById(Long id) {
74-
Address address = repository.findById(id).orElseThrow(
75+
Address address = addressRepository.findById(id).orElseThrow(
7576
() -> new ResourceNotFoundException(RESPONSEENDERECONAOENCONTRADO));
7677

7778
return new AddressDTO(address);
@@ -80,23 +81,23 @@ public AddressDTO findById(Long id) {
8081
@Override
8182
@Transactional(propagation = Propagation.SUPPORTS)
8283
public void delete(Long id) {
83-
if (!repository.existsById(id)) {
84+
if (!addressRepository.existsById(id)) {
8485
throw new ResourceNotFoundException(RESPONSEENDERECONAOENCONTRADO);
8586
}
8687

87-
repository.deleteById(id);
88+
addressRepository.deleteById(id);
8889
}
8990

9091
@Override
9192
@Transactional
9293
public void update(Long id, AddressDTO addressDTO) {
93-
if (!repository.existsById(id)) {
94+
if (!addressRepository.existsById(id)) {
9495
throw new ResourceNotFoundException(RESPONSEENDERECONAOENCONTRADO);
9596
}
9697

97-
Address address = repository.getReferenceById(id);
98+
Address address = addressRepository.getReferenceById(id);
9899
address = mapTo(addressDTO, address);
99-
repository.save(address);
100+
addressRepository.save(address);
100101
}
101102

102103
public Address mapTo(AddressDTO dto, Address entity) {

0 commit comments

Comments
 (0)