-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
controller, dto, mapper is added for pet type and services are updated.
- Loading branch information
1 parent
92ea5e3
commit 345c657
Showing
7 changed files
with
124 additions
and
18 deletions.
There are no files selected for viewing
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
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
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
37 changes: 37 additions & 0 deletions
37
src/main/java/com/production/ehayvanbackendapi/Controllers/PetTypeController.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,37 @@ | ||
package com.production.ehayvanbackendapi.Controllers; | ||
|
||
import com.production.ehayvanbackendapi.DTO.PetTypeDTO; | ||
import com.production.ehayvanbackendapi.Services.PetTypeService; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@RestController | ||
@RequestMapping("/api/pettypes") | ||
public class PetTypeController { | ||
private final PetTypeService petTypeService; | ||
|
||
public PetTypeController(PetTypeService petTypeService) { | ||
this.petTypeService = petTypeService; | ||
} | ||
|
||
@GetMapping("/{id}") | ||
public ResponseEntity<PetTypeDTO> getPetTypeById(@PathVariable Integer id) { | ||
PetTypeDTO petTypeDTO = petTypeService.getPetTypeById(id); | ||
|
||
if (petTypeDTO != null) { | ||
return new ResponseEntity<>(petTypeDTO, HttpStatus.OK); | ||
} else { | ||
return new ResponseEntity<>(HttpStatus.NOT_FOUND); | ||
} | ||
} | ||
|
||
// @PostMapping | ||
// public ResponseEntity<PetTypeDTO> savePetType(@RequestBody PetTypeDTO petTypeDTO) { | ||
// PetTypeDTO savedPetType = petTypeService.savePetType(petTypeDTO); | ||
// return new ResponseEntity<>(savedPetType, HttpStatus.CREATED); | ||
// } | ||
|
||
|
||
} | ||
|
36 changes: 36 additions & 0 deletions
36
src/main/java/com/production/ehayvanbackendapi/DTO/PetTypeDTO.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 |
---|---|---|
@@ -1,4 +1,40 @@ | ||
package com.production.ehayvanbackendapi.DTO; | ||
|
||
import java.util.List; | ||
|
||
public class PetTypeDTO { | ||
private Integer petTypeID; | ||
private String type; | ||
private List<PetDTO> pets; | ||
|
||
public PetTypeDTO(Integer petTypeID, String type, List<PetDTO> pets) { | ||
this.petTypeID = petTypeID; | ||
this.type = type; | ||
this.pets = pets; | ||
} | ||
|
||
public Integer getPetTypeID() { | ||
return petTypeID; | ||
} | ||
|
||
public void setPetTypeID(Integer petTypeID) { | ||
this.petTypeID = petTypeID; | ||
} | ||
|
||
public String getType() { | ||
return type; | ||
} | ||
|
||
public void setType(String type) { | ||
this.type = type; | ||
} | ||
|
||
public List<PetDTO> getPets() { | ||
return pets; | ||
} | ||
|
||
public void setPets(List<PetDTO> pets) { | ||
this.pets = pets; | ||
} | ||
} | ||
|
24 changes: 24 additions & 0 deletions
24
src/main/java/com/production/ehayvanbackendapi/Mappers/PetTypeMapper.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,24 @@ | ||
package com.production.ehayvanbackendapi.Mappers; | ||
|
||
import com.production.ehayvanbackendapi.DTO.PetTypeDTO; | ||
import com.production.ehayvanbackendapi.Entities.PetType; | ||
import org.modelmapper.ModelMapper; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class PetTypeMapper { | ||
private final ModelMapper modelMapper; | ||
|
||
public PetTypeMapper(ModelMapper modelMapper) { | ||
this.modelMapper = modelMapper; | ||
} | ||
|
||
public PetTypeDTO convertToDto(PetType petType) { | ||
return modelMapper.map(petType, PetTypeDTO.class); | ||
} | ||
|
||
public PetType convertToEntity(PetTypeDTO petTypeDTO) { | ||
return modelMapper.map(petTypeDTO, PetType.class); | ||
} | ||
} | ||
|
15 changes: 12 additions & 3 deletions
15
src/main/java/com/production/ehayvanbackendapi/Services/PetTypeService.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 |
---|---|---|
@@ -1,17 +1,26 @@ | ||
package com.production.ehayvanbackendapi.Services; | ||
|
||
import com.production.ehayvanbackendapi.DTO.PetTypeDTO; | ||
import com.production.ehayvanbackendapi.Entities.PetType; | ||
import com.production.ehayvanbackendapi.Mappers.PetTypeMapper; | ||
import com.production.ehayvanbackendapi.Repositories.PetTypeRepository; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
|
||
@Service | ||
public class PetTypeService { | ||
private final PetTypeRepository petTypeRepository; | ||
private final PetTypeMapper petTypeMapper; | ||
|
||
public PetTypeService(PetTypeRepository petTypeRepository) { | ||
@Autowired | ||
public PetTypeService(PetTypeRepository petTypeRepository, PetTypeMapper petTypeMapper) { | ||
this.petTypeRepository = petTypeRepository; | ||
this.petTypeMapper = petTypeMapper; | ||
} | ||
public List<PetType> getAllPetTypes(){ | ||
return petTypeRepository.findAll(); | ||
public PetTypeDTO getPetTypeById(Integer id) { | ||
PetType petType = petTypeRepository.findById(id).orElse(null); | ||
return petType != null ? petTypeMapper.convertToDto(petType) : null; | ||
} | ||
} |