-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9bb319f
commit e209e8e
Showing
40 changed files
with
902 additions
and
142 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
18 changes: 18 additions & 0 deletions
18
starter/critter/src/main/java/com/udacity/jdnd/course3/critter/config/DataSourceConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.udacity.jdnd.course3.critter.config; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.boot.jdbc.DataSourceBuilder; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import javax.sql.DataSource; | ||
@Configuration | ||
public class DataSourceConfig { | ||
@Bean | ||
@ConfigurationProperties(prefix = "spring.datasource") | ||
public DataSource getDataSource() { | ||
DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create(); | ||
dataSourceBuilder.url("jdbc:mysql://localhost:3306/critter?useSSL=false&serverTimezone=UTC"); | ||
return dataSourceBuilder.build(); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...dnd/course3/critter/user/CustomerDTO.java → ...jdnd/course3/critter/dto/CustomerDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
...dnd/course3/critter/user/EmployeeDTO.java → ...jdnd/course3/critter/dto/EmployeeDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
...rse3/critter/user/EmployeeRequestDTO.java → ...urse3/critter/dto/EmployeeRequestDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
...city/jdnd/course3/critter/pet/PetDTO.java → ...city/jdnd/course3/critter/dto/PetDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...course3/critter/schedule/ScheduleDTO.java → ...jdnd/course3/critter/dto/ScheduleDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
starter/critter/src/main/java/com/udacity/jdnd/course3/critter/enpoint/PetController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.udacity.jdnd.course3.critter.enpoint; | ||
|
||
import com.udacity.jdnd.course3.critter.dto.PetDTO; | ||
import com.udacity.jdnd.course3.critter.model.Customer; | ||
import com.udacity.jdnd.course3.critter.model.Pet; | ||
import com.udacity.jdnd.course3.critter.service.CustomerService; | ||
import com.udacity.jdnd.course3.critter.service.PetService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.modelmapper.ModelMapper; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* Handles web requests related to Pets. | ||
*/ | ||
@RestController | ||
@RequestMapping("/pet") | ||
@RequiredArgsConstructor | ||
public class PetController { | ||
private final PetService petService; | ||
private final CustomerService customerService; | ||
@PostMapping | ||
public PetDTO savePet(@RequestBody PetDTO petDTO) { | ||
Customer customer = new Customer(); | ||
if (petDTO.getOwnerId() != 0) { | ||
customer = customerService.getCustomer(petDTO.getOwnerId()); | ||
} | ||
|
||
Pet pet = convertPetDTOToEntity(petDTO); | ||
pet.setOwner(customer); | ||
Pet savedPet = petService.save(pet); | ||
if (customer!= null) { | ||
customer.addPet(savedPet); | ||
} | ||
return convertPetEntityToDTO(savedPet); | ||
} | ||
|
||
private PetDTO convertPetEntityToDTO(Pet savedPet) { | ||
ModelMapper modelMapper = new ModelMapper(); | ||
return modelMapper.map(savedPet, PetDTO.class); | ||
} | ||
|
||
private Pet convertPetDTOToEntity(PetDTO petDTO) { | ||
ModelMapper modelMapper = new ModelMapper(); | ||
return modelMapper.map(petDTO, Pet.class); | ||
} | ||
|
||
@GetMapping("/{petId}") | ||
public PetDTO getPet(@PathVariable long petId) { | ||
Pet pet = petService.getPetById(petId); | ||
return convertPetEntityToDTO(pet); | ||
} | ||
|
||
@GetMapping | ||
public List<PetDTO> getPets(){ | ||
List<PetDTO> petsDTO = new ArrayList<>(); | ||
List<Pet> pets = petService.getAllPets(); | ||
pets.forEach(pet -> { | ||
petsDTO.add(convertPetEntityToDTO(pet)); | ||
}); | ||
return petsDTO; | ||
} | ||
|
||
@GetMapping("/owner/{ownerId}") | ||
public List<PetDTO> getPetsByOwner(@PathVariable long ownerId) { | ||
List<PetDTO> petsDTO = new ArrayList<>(); | ||
List<Pet> pets = petService.getPetsByOwner(ownerId); | ||
pets.forEach(pet -> { | ||
petsDTO.add(convertPetEntityToDTO(pet)); | ||
}); | ||
return petsDTO; | ||
} | ||
} |
118 changes: 118 additions & 0 deletions
118
...er/critter/src/main/java/com/udacity/jdnd/course3/critter/enpoint/ScheduleController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package com.udacity.jdnd.course3.critter.enpoint; | ||
|
||
import com.udacity.jdnd.course3.critter.dto.ScheduleDTO; | ||
import com.udacity.jdnd.course3.critter.exception.ResponseNotFoundException; | ||
import com.udacity.jdnd.course3.critter.model.Employee; | ||
import com.udacity.jdnd.course3.critter.model.Pet; | ||
import com.udacity.jdnd.course3.critter.model.Schedule; | ||
import com.udacity.jdnd.course3.critter.service.EmployeeService; | ||
import com.udacity.jdnd.course3.critter.service.PetService; | ||
import com.udacity.jdnd.course3.critter.service.ScheduleService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.modelmapper.ModelMapper; | ||
import org.springframework.beans.BeanUtils; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.*; | ||
|
||
/** | ||
* Handles web requests related to Schedules. | ||
*/ | ||
@RestController | ||
@RequestMapping("/schedule") | ||
@RequiredArgsConstructor | ||
public class ScheduleController { | ||
private final ScheduleService scheduleService; | ||
private final PetService petService; | ||
private final EmployeeService employeeService; | ||
@PostMapping | ||
public ScheduleDTO createSchedule(@RequestBody ScheduleDTO scheduleDTO) { | ||
return convertEntityToScheduleDTO(scheduleService.saveSchedule(convertScheduleDTOToEntity(scheduleDTO))); | ||
} | ||
|
||
private Schedule convertScheduleDTOToEntity(ScheduleDTO scheduleDTO) { | ||
ModelMapper modelMapper = new ModelMapper(); | ||
Schedule schedule = modelMapper.map(scheduleDTO, Schedule.class); | ||
scheduleDTO.setActivities(scheduleDTO.getActivities()); | ||
Map<Long, Employee> employeeMap = new HashMap<>(); | ||
for (Long employeeId : scheduleDTO.getEmployeeIds()) { | ||
Optional<Employee> optionalEmployee = Optional.ofNullable(employeeService.getEmployee(employeeId)); | ||
if (!optionalEmployee.isPresent()) { | ||
throw new ResponseNotFoundException("Employee not found"); | ||
} else { | ||
employeeMap.put(employeeId, optionalEmployee.get()); | ||
} | ||
} | ||
schedule.setEmployeeIds(new ArrayList<>(employeeMap.values())); | ||
Map<Long, Pet> petMap = new HashMap<>(); | ||
for (Long petId : scheduleDTO.getPetIds()) { | ||
Optional<Pet> optionalPet = Optional.ofNullable(petService.getPetById(petId)); | ||
if (!optionalPet.isPresent()) { | ||
throw new ResponseNotFoundException("Pet not found"); | ||
} else { | ||
petMap.put(petId, optionalPet.get()); | ||
} | ||
} | ||
schedule.setPetIds(new ArrayList<>(petMap.values())); | ||
return schedule; | ||
} | ||
|
||
private ScheduleDTO convertEntityToScheduleDTO(Schedule schedule) { | ||
ScheduleDTO scheduleDTO = new ScheduleDTO(); | ||
BeanUtils.copyProperties(schedule, scheduleDTO); | ||
scheduleDTO.setActivities(schedule.getActivities()); | ||
List<Pet> pets = schedule.getPetIds(); | ||
List<Long> petIds = new ArrayList<>(); | ||
for (Pet pet : pets) { | ||
petIds.add(pet.getId()); | ||
} | ||
scheduleDTO.setPetIds(petIds); | ||
List<Employee> employees = schedule.getEmployeeIds(); | ||
List<Long> employeeIds = new ArrayList<>(); | ||
for (Employee employee : employees) { | ||
employeeIds.add(employee.getId()); | ||
} | ||
scheduleDTO.setEmployeeIds(employeeIds); | ||
return scheduleDTO; | ||
} | ||
|
||
@GetMapping | ||
public List<ScheduleDTO> getAllSchedules() { | ||
List<Schedule> schedules = scheduleService.getAllSchedules(); | ||
List<ScheduleDTO> scheduleDTOS = new ArrayList<>(); | ||
for (Schedule schedule : schedules) { | ||
scheduleDTOS.add(convertEntityToScheduleDTO(schedule)); | ||
} | ||
return scheduleDTOS; | ||
} | ||
|
||
@GetMapping("/pet/{petId}") | ||
public List<ScheduleDTO> getScheduleForPet(@PathVariable long petId) { | ||
List<Schedule> schedules = scheduleService.getSchedulesByPetId(petId); | ||
List<ScheduleDTO> scheduleDTOS = new ArrayList<>(); | ||
for (Schedule schedule : schedules) { | ||
scheduleDTOS.add(convertEntityToScheduleDTO(schedule)); | ||
} | ||
return scheduleDTOS; | ||
} | ||
|
||
@GetMapping("/employee/{employeeId}") | ||
public List<ScheduleDTO> getScheduleForEmployee(@PathVariable long employeeId) { | ||
List<Schedule> schedules = scheduleService.getSchedulesByEmployee(employeeId); | ||
List<ScheduleDTO> scheduleDTOS = new ArrayList<>(); | ||
for (Schedule schedule : schedules) { | ||
scheduleDTOS.add(convertEntityToScheduleDTO(schedule)); | ||
} | ||
return scheduleDTOS; | ||
} | ||
|
||
@GetMapping("/customer/{customerId}") | ||
public List<ScheduleDTO> getScheduleForCustomer(@PathVariable long customerId) { | ||
List<Schedule> schedules = scheduleService.getSchedulesByCustomer(customerId); | ||
List<ScheduleDTO> scheduleDTOS = new ArrayList<>(); | ||
for (Schedule schedule : schedules) { | ||
scheduleDTOS.add(convertEntityToScheduleDTO(schedule)); | ||
} | ||
return scheduleDTOS; | ||
} | ||
} |
Oops, something went wrong.