Skip to content

Commit

Permalink
[udacity] done project
Browse files Browse the repository at this point in the history
  • Loading branch information
sangpham27 committed Aug 30, 2024
1 parent 9bb319f commit e209e8e
Show file tree
Hide file tree
Showing 40 changed files with 902 additions and 142 deletions.
Binary file added .DS_Store
Binary file not shown.
Binary file added starter/.DS_Store
Binary file not shown.
Binary file added starter/critter/.DS_Store
Binary file not shown.
13 changes: 13 additions & 0 deletions starter/critter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,24 @@
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
<groupId>org.modelmapper.extensions</groupId>
<artifactId>modelmapper-spring</artifactId>
<version>2.4.1</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
Expand Down
Binary file added starter/critter/src/.DS_Store
Binary file not shown.
Binary file added starter/critter/src/main/.DS_Store
Binary file not shown.
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();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.udacity.jdnd.course3.critter.user;
package com.udacity.jdnd.course3.critter.dto;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package com.udacity.jdnd.course3.critter.user;
package com.udacity.jdnd.course3.critter.dto;

import com.udacity.jdnd.course3.critter.enums.EmployeeSkill;

import java.time.DayOfWeek;
import java.util.Set;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package com.udacity.jdnd.course3.critter.user;
package com.udacity.jdnd.course3.critter.dto;

import com.udacity.jdnd.course3.critter.enums.EmployeeSkill;

import java.time.LocalDate;
import java.util.Set;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package com.udacity.jdnd.course3.critter.pet;
package com.udacity.jdnd.course3.critter.dto;

import com.udacity.jdnd.course3.critter.enums.PetType;

import java.time.LocalDate;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.udacity.jdnd.course3.critter.schedule;
package com.udacity.jdnd.course3.critter.dto;

import com.udacity.jdnd.course3.critter.user.EmployeeSkill;
import com.udacity.jdnd.course3.critter.enums.EmployeeSkill;

import java.time.LocalDate;
import java.util.List;
Expand Down
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;
}
}
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;
}
}
Loading

0 comments on commit e209e8e

Please sign in to comment.