Skip to content

Commit

Permalink
Fixed mapping configurations for Customer, Medication and Appointment…
Browse files Browse the repository at this point in the history
…, added getAllCustomers method inside customerService class. New properties on application.properties for secure http requests.

To test the changes on the example data row:
In Postman, choose 'Basic Auth',
Username: test
Password: password
  • Loading branch information
FruTooTi committed Dec 20, 2023
1 parent 345c657 commit 376b89a
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/customers")
public class CustomerController {
Expand All @@ -32,7 +34,17 @@ public ResponseEntity<CustomerDTO> getCustomerById(@PathVariable Integer id) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
@GetMapping("/all")
public ResponseEntity<List<CustomerDTO>> getAllCustomers(){
List<CustomerDTO> response = customerService.getAllCustomers();

if(response != null){
return new ResponseEntity<>(response, HttpStatus.OK);
}
else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
// @PostMapping
// public ResponseEntity<CustomerDTO> createCustomer(@RequestBody CustomerDTO customerDTO) {
// CustomerDTO createdCustomer = customerService.createCustomer(customerDTO);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
import com.production.ehayvanbackendapi.Services.MedicationService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/medications")
public class MedicationController {
private final MedicationService medicationService;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ public class AppointmentDTO {
private Integer petID;
private Date appointmentDate;

public AppointmentDTO(){

}
public AppointmentDTO(Integer appointmentID, Integer petOwnerID, Integer vetID, Integer petID, Date appointmentDate) {
this.appointmentID = appointmentID;
this.petOwnerID = petOwnerID;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ public class CustomerDTO {
private Integer vetID;
private Integer ownerID;

public CustomerDTO(){

}
public CustomerDTO(Integer userID, String name, String surname, String email,
String password, Integer userTypeID, Integer vetID, Integer ownerID) {
this.userID = userID;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ public class MedicationDTO {
private Integer scheduleID;
private Integer petID;

public MedicationDTO(){

}
public MedicationDTO(Integer medicationID, String medicationName, Integer
medTypeID, Integer scheduleID, Integer petID) {
this.medicationID = medicationID;
Expand Down Expand Up @@ -55,11 +57,4 @@ public Integer getPetID() {
public void setPetID(Integer petID) {
this.petID = petID;
}







}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.production.ehayvanbackendapi.Mappers;

import com.production.ehayvanbackendapi.DTO.AppointmentDTO;
import com.production.ehayvanbackendapi.DTO.CustomerDTO;
import com.production.ehayvanbackendapi.Entities.Appointment;
import com.production.ehayvanbackendapi.Entities.Customer;
import org.modelmapper.ModelMapper;
import org.springframework.stereotype.Component;

Expand All @@ -11,6 +13,14 @@ public class AppointmentMapper {

public AppointmentMapper(ModelMapper modelMapper) {
this.modelMapper = modelMapper;
this.modelMapper.getConfiguration().setAmbiguityIgnored(true);
this.modelMapper.createTypeMap(Appointment.class, AppointmentDTO.class).addMappings(
mapper -> {
mapper.map(src -> src.getPetID().getPetID(), AppointmentDTO::setPetID);
mapper.map(src -> src.getVetID().getVetID(), AppointmentDTO::setVetID);
mapper.map(src -> src.getPetOwnerID().getPetOwnerID(), AppointmentDTO::setPetOwnerID);
}
);
}

public AppointmentDTO convertToDto(Appointment appointment) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,17 @@ public class CustomerMapper {

public CustomerMapper(ModelMapper modelMapper) {
this.modelMapper = modelMapper;
this.modelMapper.createTypeMap(Customer.class, CustomerDTO.class).addMappings(
mapper -> {
mapper.map(src -> src.getVet().getVetID(), CustomerDTO::setVetID);
mapper.map(src -> src.getOwner().getPetOwnerID(), CustomerDTO::setOwnerID);
mapper.map(src -> src.getUserTypeID().getUserTypeID(), CustomerDTO::setUserTypeID);
}
);
}

public CustomerDTO convertToDto(Customer customer) {
return modelMapper.map(customer, CustomerDTO.class);
return modelMapper.map(customer, CustomerDTO.class);
}

public Customer convertToEntity(CustomerDTO customerDTO) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ public class MedicationMapper {

public MedicationMapper(ModelMapper modelMapper) {
this.modelMapper = modelMapper;
this.modelMapper.createTypeMap(Medication.class, MedicationDTO.class).addMappings(
mapper -> {
mapper.map(src -> src.getPetID().getPetID(), MedicationDTO::setPetID);
mapper.map(src -> src.getScheduleID().getScheduleID(), MedicationDTO::setScheduleID);
mapper.map(src -> src.getMedTypeID().getMedTypeID(), MedicationDTO::setMedTypeID);
}
);
}

public MedicationDTO convertToDto(Medication medication) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.production.ehayvanbackendapi.Mappers.CustomerMapper;
import com.production.ehayvanbackendapi.Repositories.CustomerRepository;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -32,6 +33,14 @@ public CustomerDTO getCustomerById(Integer id) {
// return customerMapper.convertToDto(savedCustomer);
// }

public List<CustomerDTO> getAllCustomers(){
List<Customer> customerList = customerRepository.findAll();
List<CustomerDTO> customerDtoList = new ArrayList<>();
for(Customer cust: customerList){
customerDtoList.add(customerMapper.convertToDto(cust));
}
return customerDtoList;
}
// Other service methods for creating, updating, and deleting customers
}

2 changes: 2 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.PostgreSQLDialect
spring.security.user.name=test
spring.security.user.password=password

0 comments on commit 376b89a

Please sign in to comment.