Skip to content

Commit

Permalink
Creation and getById is implemented to the appliction(all the layers)…
Browse files Browse the repository at this point in the history
… for medication
  • Loading branch information
ituitis20-karadagd20 authored and CihatAltiparmak committed Dec 19, 2023
1 parent ccb07dd commit 62e372b
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 5 deletions.
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);
}
}
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;
}







}
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);
}
}

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.
}

0 comments on commit 62e372b

Please sign in to comment.