Skip to content

Commit

Permalink
chore: search for vets
Browse files Browse the repository at this point in the history
  • Loading branch information
ramesz6 committed Sep 19, 2024
1 parent c569a65 commit cdce7f5
Show file tree
Hide file tree
Showing 24 changed files with 564 additions and 99 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.greenfoxacademy.backend.controller;

import com.greenfoxacademy.backend.dtos.PetListResponseDto;
import com.greenfoxacademy.backend.services.pet.PetService;
import java.security.Principal;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* A REST controller that handles operations related to users' pets.
*
* @author Your Name
*/
@RequiredArgsConstructor
@RestController
public class PetController {
private final PetService petService;

@GetMapping("/pets")
public ResponseEntity<PetListResponseDto> getPets(Principal owner) {
return ResponseEntity.status(HttpStatus.OK).body(petService.getOwnerPets(owner.getName()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
Expand All @@ -20,7 +21,8 @@ public class VetController {
private final VetService vetService;

@GetMapping("/search-vet")
public ResponseEntity<?> searchVet() {
return ResponseEntity.status(HttpStatus.OK).body(vetService.getAll());
public ResponseEntity<?> searchVet(@RequestParam String word) {

return ResponseEntity.status(HttpStatus.OK).body(vetService.getAll(word));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.greenfoxacademy.backend.dtos;

import jakarta.validation.constraints.FutureOrPresent;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.PastOrPresent;
import java.util.Date;
import lombok.Data;

/**
* A data transfer object for pet details.
*
* @author Your Name
*/
@Data
public class PetDetailsDto {
@NotBlank
String name;
@NotBlank
String breed;
@NotBlank
String sex;
@PastOrPresent(message = "The birth date must be in the past or present")
Date birthDate;
@PastOrPresent(message = "The last check-up date must be in the past or present")
Date lastCheckUp;
@FutureOrPresent(message = "The next check-up date must be in the future or present")
Date nextCheckUp;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.greenfoxacademy.backend.dtos;

import java.util.List;

/**
* A data transfer object for a list of pets.
*
*/
public record PetListResponseDto(
List<PetDetailsDto> pets
){
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.greenfoxacademy.backend.models;

import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;
import jakarta.persistence.Id;
import jakarta.persistence.*;
import lombok.Data;

/**
Expand Down Expand Up @@ -31,20 +29,18 @@
* @see jakarta.persistence.Embeddable
*/

@Embeddable
@Entity
@Table(name = "_clinicAddress")
@Data
public class ClinicAddress {
@Id
private Long id;
@OneToOne
private ClinicDetails clinicDetails;
private String city;
@Column(length = 4)
private int zip;
private String street;
private double longitude;
private double latitude;

public String addressToString() {
return zip + " " + city + " " + street;
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.greenfoxacademy.backend.models;

import jakarta.persistence.Embeddable;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.*;
import lombok.Data;

/**
Expand All @@ -22,10 +19,15 @@
*/

@Data
@Embeddable
@Entity
@Table(name = "_clinicDetails")
public class ClinicDetails {

@Id
private Long id;
private String clinicName;
@OneToOne
private ClinicAddress clinicAddress;
@OneToOne
private Vet vet;

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.greenfoxacademy.backend.models;

import jakarta.persistence.Entity;
import jakarta.persistence.Table;
import jakarta.persistence.Transient;
import jakarta.persistence.*;
import java.util.Collection;
import java.util.List;
import lombok.AllArgsConstructor;
Expand Down Expand Up @@ -37,6 +35,7 @@
@Entity
@Table(name = "_vet")
public class Vet extends User {
@OneToOne
private ClinicDetails clinicDetails;

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.greenfoxacademy.backend.repositories;

import com.greenfoxacademy.backend.models.ClinicAddress;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;

/**
* ClinicAddressRepository provides database access methods for ClinicAddress entities.
* It extends JpaRepository to perform CRUD operations.
*/
public interface ClinicAddressRepository extends JpaRepository<ClinicAddress, Long> {

/**
* Retrieves a list of clinic addresses based on partial matches for zip code, city, or street.
*
* @param zip the partial or full zip code of the clinic address.
* @param city the partial or full city name of the clinic address.
* @param street the partial or full street name of the clinic address.
* @return a list of clinic addresses matching the given criteria.
*/
List<ClinicAddress> findAllByZipContainingOrCityContainingOrStreetContaining(
int zip, String city, String street);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.greenfoxacademy.backend.repositories;

import com.greenfoxacademy.backend.models.ClinicDetails;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;


/**
* ClinicDetailsRepository provides database access methods for ClinicDetails entities.
* It extends JpaRepository to perform CRUD operations.
*/
public interface ClinicDetailsRepository extends JpaRepository<ClinicDetails, Long> {

/**
* Retrieves a list of clinic details where the clinic name contains the specified word.
*
* @param word a partial or full word to search within clinic names.
* @return a list of clinic details matching the specified word in their names.
*/
List<ClinicDetails> findAllByClinicNameContaining(String word);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
package com.greenfoxacademy.backend.repositories;

import com.greenfoxacademy.backend.models.Vet;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;

public interface VetRepository extends JpaRepository<Vet,Long> {
/**
* VetRepository provides database access methods for Vet entities.
* It extends JpaRepository to perform CRUD operations.
*/
public interface VetRepository extends JpaRepository<Vet, Long> {

/**
* Retrieves a list of vets based on the first or last name.
*
* @param firstName the first name of the vet.
* @param lastName the last name of the vet.
* @return a list of vets matching the given first or last name.
*/
List<Vet> findAllByFirstNameOrLastName(String firstName, String lastName);

/**
* Retrieves a list of vets based on the clinic name.
*
* @param clinicName the name of the clinic.
* @return a list of vets working at the clinic with the given name.
*/
List<Vet> findAllByClinicDetailsClinicName(String clinicName);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.greenfoxacademy.backend.services.pet;

import com.greenfoxacademy.backend.dtos.PetListResponseDto;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Service;

/**
* Retrieves the pets of the specified owner.
*
* @param name The name of the owner.
* @return A response containing the owner's pets.
*/

public interface PetService {
PetListResponseDto getOwnerPets(String name);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.greenfoxacademy.backend.services.pet;

import com.greenfoxacademy.backend.dtos.PetDetailsDto;
import com.greenfoxacademy.backend.dtos.PetListResponseDto;
import com.greenfoxacademy.backend.models.Pet;
import com.greenfoxacademy.backend.repositories.PetRepository;
import com.greenfoxacademy.backend.services.user.owner.OwnerService;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.modelmapper.ModelMapper;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
* Retrieves a list of pets owned by the user with the specified email.
*/
@Service
@RequiredArgsConstructor
public class PetServiceImpl implements PetService {
private final PetRepository petRepository;
private final OwnerService ownerService;
private final ModelMapper modelMapper = new ModelMapper();

/**
* Retrieves a list of pets owned by the user with the specified email.
*
* @param email the email of the pet owner
* @return a {@link PetListResponseDto} containing the list of pets
* @throws UsernameNotFoundException if the user with the specified email is not found
*/
@Override
public PetListResponseDto getOwnerPets(String email) {
List<Pet> petList = petRepository
.findAllByOwnerId(ownerService.findByEmail(email).getId());

List<PetDetailsDto> petDtoList = petList.stream()
.map(pet -> modelMapper.map(pet, PetDetailsDto.class))
.collect(Collectors.toList());

return new PetListResponseDto(petDtoList);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
import com.greenfoxacademy.backend.services.auth.AuthService;
import com.greenfoxacademy.backend.services.mail.EmailService;
import jakarta.transaction.Transactional;

import java.util.UUID;

import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package com.greenfoxacademy.backend.services.user.vet;

import com.greenfoxacademy.backend.dtos.VetListResponseDto;
import org.springframework.stereotype.Service;

/**
* VetService defines the business logic for handling veterinarian-related data and
* operations.
* This interface provides methods for retrieving veterinarian data.
*/

@Service
public interface VetService {

VetListResponseDto getAll();
VetListResponseDto getAll(String word);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,44 @@

import com.greenfoxacademy.backend.dtos.VetDetailsDto;
import com.greenfoxacademy.backend.dtos.VetListResponseDto;
import com.greenfoxacademy.backend.models.ClinicAddress;
import com.greenfoxacademy.backend.models.ClinicDetails;
import com.greenfoxacademy.backend.models.Vet;
import com.greenfoxacademy.backend.repositories.ClinicAddressRepository;
import com.greenfoxacademy.backend.repositories.ClinicDetailsRepository;
import com.greenfoxacademy.backend.repositories.VetRepository;
import lombok.RequiredArgsConstructor;
import org.modelmapper.ModelMapper;

import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.modelmapper.ModelMapper;
import org.springframework.stereotype.Service;

/**
* VetServiceImpl is the implementation of the VetService interface.
* It provides functionality for retrieving veterinarian data.
*/

@Service
@RequiredArgsConstructor
public class VetServiceImpl implements VetService {

private final VetRepository vetRepository;
private final ClinicAddressRepository clinicAddressRepository;
private final ClinicDetailsRepository clinicDetailsRepository;
private final ModelMapper modelMapper;

@Override
public VetListResponseDto getAll() {
List<Vet> vetList = vetRepository.findAll();
public VetListResponseDto getAll(String word) {
List<Vet> vetList = vetRepository.findAllByFirstNameOrLastName(word, word);

List<ClinicAddress> clinicAddressList = clinicAddressRepository
.findAllByZipContainingOrCityContainingOrStreetContaining(
Integer.parseInt(word), word, word);

vetList.addAll( clinicAddressList.stream()
.map(a -> a.getClinicDetails().getVet()).toList());

vetList.addAll(clinicDetailsRepository.findAllByClinicNameContaining(word)
.stream().map(ClinicDetails::getVet).toList());

List<VetDetailsDto> vetDtoList = vetList.stream()
.map(vet -> modelMapper.map(vet, VetDetailsDto.class))
Expand Down
Loading

0 comments on commit cdce7f5

Please sign in to comment.