-
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.
Creation and getById is implemented to the appliction(all the layers)…
… for medication
- Loading branch information
1 parent
ccb07dd
commit 62e372b
Showing
4 changed files
with
140 additions
and
5 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
src/main/java/com/production/ehayvanbackendapi/Controllers/MedicationController.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,33 @@ | ||
package com.production.ehayvanbackendapi.Controllers; | ||
|
||
import com.production.ehayvanbackendapi.DTO.MedicationDTO; | ||
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; | ||
|
||
public class MedicationController { | ||
private final MedicationService medicationService; | ||
|
||
public MedicationController(MedicationService medicationService) { | ||
this.medicationService = medicationService; | ||
} | ||
@GetMapping("/{id}") | ||
public ResponseEntity<MedicationDTO> getMedicationById(@PathVariable Integer id) { | ||
MedicationDTO medicationDTO = medicationService.getMedicationById(id); | ||
|
||
if (medicationDTO != null) { | ||
return new ResponseEntity<>(medicationDTO, HttpStatus.OK); | ||
} else { | ||
return new ResponseEntity<>(HttpStatus.NOT_FOUND); | ||
} | ||
} | ||
@PostMapping | ||
public ResponseEntity<MedicationDTO> saveMedication(@RequestBody MedicationDTO medicationDTO) { | ||
MedicationDTO savedMedication = medicationService.saveMedication(medicationDTO); | ||
return new ResponseEntity<>(savedMedication, HttpStatus.CREATED); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
src/main/java/com/production/ehayvanbackendapi/DTO/MedicationDTO.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,65 @@ | ||
package com.production.ehayvanbackendapi.DTO; | ||
|
||
public class MedicationDTO { | ||
private Integer medicationID; | ||
private String medicationName; | ||
private Integer medTypeID; | ||
private Integer scheduleID; | ||
private Integer petID; | ||
|
||
|
||
public MedicationDTO(Integer medicationID, String medicationName, Integer | ||
medTypeID, Integer scheduleID, Integer petID) { | ||
this.medicationID = medicationID; | ||
this.medicationName = medicationName; | ||
this.medTypeID = medTypeID; | ||
this.scheduleID = scheduleID; | ||
this.petID = petID; | ||
} | ||
public Integer getMedicationID() { | ||
return medicationID; | ||
} | ||
|
||
public void setMedicationID(Integer medicationID) { | ||
this.medicationID = medicationID; | ||
} | ||
|
||
public String getMedicationName() { | ||
return medicationName; | ||
} | ||
|
||
public void setMedicationName(String medicationName) { | ||
this.medicationName = medicationName; | ||
} | ||
|
||
public Integer getMedTypeID() { | ||
return medTypeID; | ||
} | ||
|
||
public void setMedTypeID(Integer medTypeID) { | ||
this.medTypeID = medTypeID; | ||
} | ||
|
||
public Integer getScheduleID() { | ||
return scheduleID; | ||
} | ||
|
||
public void setScheduleID(Integer scheduleID) { | ||
this.scheduleID = scheduleID; | ||
} | ||
|
||
public Integer getPetID() { | ||
return petID; | ||
} | ||
|
||
public void setPetID(Integer petID) { | ||
this.petID = petID; | ||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/com/production/ehayvanbackendapi/Mappers/MedicationMapper.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.MedicationDTO; | ||
import com.production.ehayvanbackendapi.Entities.Medication; | ||
import org.modelmapper.ModelMapper; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class MedicationMapper { | ||
private final ModelMapper modelMapper; | ||
|
||
public MedicationMapper(ModelMapper modelMapper) { | ||
this.modelMapper = modelMapper; | ||
} | ||
|
||
public MedicationDTO convertToDto(Medication medication) { | ||
return modelMapper.map(medication, MedicationDTO.class); | ||
} | ||
|
||
public Medication convertToEntity(MedicationDTO medicationDTO) { | ||
return modelMapper.map(medicationDTO, Medication.class); | ||
} | ||
} | ||
|
27 changes: 22 additions & 5 deletions
27
src/main/java/com/production/ehayvanbackendapi/Services/MedicationService.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,34 @@ | ||
package com.production.ehayvanbackendapi.Services; | ||
|
||
import com.production.ehayvanbackendapi.DTO.MedicationDTO; | ||
import com.production.ehayvanbackendapi.Entities.Medication; | ||
import com.production.ehayvanbackendapi.Mappers.MedicationMapper; | ||
import com.production.ehayvanbackendapi.Repositories.MedicationRepository; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
|
||
@Service | ||
public class MedicationService { | ||
private final MedicationRepository medicationRepository; | ||
private final MedicationMapper medicationMapper; | ||
|
||
public MedicationService(MedicationRepository medicationRepository) { | ||
@Autowired | ||
public MedicationService(MedicationRepository medicationRepository, MedicationMapper medicationMapper) { | ||
this.medicationRepository = medicationRepository; | ||
this.medicationMapper = medicationMapper; | ||
} | ||
|
||
public MedicationDTO getMedicationById(Integer id) { | ||
Medication medication = medicationRepository.findById(id).orElse(null); | ||
return medication != null ? medicationMapper.convertToDto(medication) : null; | ||
} | ||
public List<Medication> getAllMedications(){ | ||
return medicationRepository.findAll(); | ||
|
||
public MedicationDTO saveMedication(MedicationDTO medicationDTO) { | ||
Medication medication = medicationMapper.convertToEntity(medicationDTO); | ||
Medication savedMedication = medicationRepository.save(medication); | ||
return medicationMapper.convertToDto(savedMedication); | ||
} | ||
|
||
// Other service methods for updating, deleting medications, etc. | ||
} | ||
|