Skip to content

Commit

Permalink
Functional CRUD operations for "Medication" entity.
Browse files Browse the repository at this point in the history
  • Loading branch information
FruTooTi committed Dec 31, 2023
1 parent 3dacd35 commit 2784d12
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ public class MedType{
@OneToMany(mappedBy = "MedTypeID")
private List<Medication> medications;

public int getMedTypeID() {
public Integer getMedTypeID() {
return MedTypeID;
}
public void setMedTypeID(int medTypeID) {
public void setMedTypeID(Integer medTypeID) {
MedTypeID = medTypeID;
}
public String getMedType() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class Medication {
@ManyToOne
@JoinColumn(name="MedTypeID", referencedColumnName = "MedTypeID")
private MedType MedTypeID;
@ManyToOne
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name="ScheduleID", referencedColumnName = "ScheduleID")
private Schedule ScheduleID;
@ManyToOne
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class Pet {
public Integer getPetID() {
return PetID;
}
public void setPetID(int petID) {
public void setPetID(Integer petID) {
PetID = petID;
}
public String getPetName() {
Expand All @@ -39,7 +39,7 @@ public void setPetName(String petName) {
public Integer getAge() {
return Age;
}
public void setAge(int age) {
public void setAge(Integer age) {
Age = age;
}
public PetType getPetTypeID() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,19 @@ public MedicationMapper(ModelMapper modelMapper) {
mapper.map(src -> src.getPetID().getPetID(), MedicationDTO::setPetID);
mapper.map(src -> src.getMedTypeID().getMedTypeID(), MedicationDTO::setMedTypeID);
mapper.map(src -> src.getScheduleID().getScheduleID(), MedicationDTO::setScheduleID);
mapper.map(Medication::getScheduleID, MedicationDTO::setScheduleID);
}
);
this.modelMapper.createTypeMap(CreateOrUpdateMedicationDTO.class, Medication.class).addMappings(
mapper -> {
mapper.skip(CreateOrUpdateMedicationDTO::getScheduleID,
(dest, v) -> dest.getScheduleID().setScheduleID((Integer) v));
mapper.skip(src -> src.getPetID(),
(dest, v) -> dest.getPetID().setPetID((Integer) v));
mapper.skip(src -> src.getMedTypeID(),
(dest, v) -> dest.getMedTypeID().setMedTypeID((Integer) v));
mapper.skip(src -> src.getMedTypeID(),
(dest, v) -> dest.getMedTypeID().setMedType((String) v));
mapper.map(CreateOrUpdateMedicationDTO::getScheduleID, Medication::setScheduleID);
mapper.skip(Medication::setMedicationID);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import com.production.ehayvanbackendapi.DTO.MedicationDTO;
import com.production.ehayvanbackendapi.DTO.request.CreateOrUpdateMedicationDTO;
import com.production.ehayvanbackendapi.Entities.MedType;
import com.production.ehayvanbackendapi.Entities.Medication;
import com.production.ehayvanbackendapi.Entities.Pet;
import com.production.ehayvanbackendapi.Mappers.MedicationMapper;
import com.production.ehayvanbackendapi.Repositories.MedicationRepository;
import net.sf.jsqlparser.statement.create.table.CreateTable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

Expand All @@ -29,12 +32,24 @@ public MedicationDTO postMedication(CreateOrUpdateMedicationDTO medicationDto){
// Create a new medication from the dto.
Medication newMedication = medicationMapper.convertToEntity(medicationDto);

// Set the assigned pet for the medication.
Pet assignedPet = new Pet();
assignedPet.setPetID(medicationDto.getPetID());
newMedication.setPetID(assignedPet);

// Set the assigned med type for the medication.
MedType assignedMedType = new MedType();
assignedMedType.setMedTypeID(medicationDto.getMedTypeID());
newMedication.setMedTypeID(assignedMedType);

// Attempt posting new medication data to DB. If fails, return null.
try{
medicationRepository.save(newMedication);
return medicationMapper.convertToDto(newMedication);
MedicationDTO response = medicationMapper.convertToDto(newMedication);
return response;
}
catch (Exception e){
System.out.println(e);
return null;
}
}
Expand All @@ -46,6 +61,21 @@ public MedicationDTO updateMedication(Integer id, CreateOrUpdateMedicationDTO up
// If target medication is present, update it.
if(targetMedication.isPresent()){
Medication updatedMedication = medicationMapper.mapExistingEntity(updatedDto, targetMedication.get());

// If pet is different, change the pet.
if(updatedDto.getPetID() != targetMedication.get().getPetID().getPetID()){
Pet updatedPet = new Pet();
updatedPet.setPetID(updatedDto.getPetID());
targetMedication.get().setPetID(updatedPet);
}

// If med type is different, change the med type.
if(updatedDto.getMedTypeID() != targetMedication.get().getMedTypeID().getMedTypeID()){
MedType updatedMedType = new MedType();
updatedMedType.setMedTypeID(updatedDto.getMedTypeID());
targetMedication.get().setMedTypeID(updatedMedType);
}

medicationRepository.save(updatedMedication);
return medicationMapper.convertToDto(updatedMedication);
}
Expand Down

0 comments on commit 2784d12

Please sign in to comment.