Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ------------------------------------------------------------------------------------------------------------
- entities
- ------------------------------------------------------------------------------------------------------------
- package com.upc.demo.entities;
- import java.sql.Date;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.GeneratedValue;
- import javax.persistence.GenerationType;
- import javax.persistence.Id;
- import javax.persistence.JoinColumn;
- import javax.persistence.ManyToOne;
- import javax.persistence.Table;
- import javax.validation.constraints.Size;
- @Entity
- @Table(name="medical_appointment")
- public class CitaMedica {
- @Id
- @GeneratedValue(strategy=GenerationType.IDENTITY)
- private int id;
- @Column(name="date", nullable=false)
- private Date date;
- @Column(name="hour", nullable=false)
- private int hora;
- @ManyToOne
- @JoinColumn(name="seat_id", nullable=false)
- private Sede sedeId;
- @ManyToOne
- @JoinColumn(name="doctor_id", nullable=false)
- private Doctor doctorId;
- @Size(min=2, max=80,message="nombre entre 2 a 80")
- @Column(name="patient_name", nullable=false, length=80)
- private String patientName;
- public CitaMedica() { }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public Date getDate() {
- return date;
- }
- public void setDate(Date date) {
- this.date = date;
- }
- public int getHora() {
- return hora;
- }
- public void setHora(int hora) {
- this.hora = hora;
- }
- public Sede getSedeId() {
- return sedeId;
- }
- public void setSedeId(Sede sedeId) {
- this.sedeId = sedeId;
- }
- public Doctor getDoctorId() {
- return doctorId;
- }
- public void setDoctorId(Doctor doctorId) {
- this.doctorId = doctorId;
- }
- public String getPatientName() {
- return patientName;
- }
- public void setPatientName(String patientName) {
- this.patientName = patientName;
- }
- }
- ------------------------------------------------------------------------------------------------------------
- package com.upc.demo.entities;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.GeneratedValue;
- import javax.persistence.GenerationType;
- import javax.persistence.Id;
- import javax.persistence.JoinColumn;
- import javax.persistence.ManyToOne;
- import javax.persistence.Table;
- import javax.validation.constraints.Size;
- @Entity
- @Table(name="doctor")
- public class Doctor {
- @Id
- @GeneratedValue(strategy=GenerationType.IDENTITY)
- private int id;
- @Size(min=2,max=80)
- @Column(name="name",nullable=false,length=80)
- private String name;
- @Size(min=2,max=80)
- @Column(name="last_name",nullable=false,length=80)
- private String lastName;
- @ManyToOne
- @JoinColumn(name="speciality_id")
- private Especialidad specialityId;
- public Doctor() { }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getLastName() {
- return lastName;
- }
- public void setLastName(String lastName) {
- this.lastName = lastName;
- }
- public Especialidad getSpecialityId() {
- return specialityId;
- }
- public void setSpecialityId(Especialidad specialityId) {
- this.specialityId = specialityId;
- }
- }
- ------------------------------------------------------------------------------------------------------------
- package com.upc.demo.entities;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.GeneratedValue;
- import javax.persistence.GenerationType;
- import javax.persistence.Id;
- import javax.persistence.JoinColumn;
- import javax.persistence.ManyToOne;
- import javax.persistence.Table;
- import javax.validation.constraints.Size;
- @Entity
- @Table(name="specialities")
- public class Especialidad {
- @Id
- @GeneratedValue(strategy=GenerationType.IDENTITY)
- private int id;
- @Size(min=4, message="min de 4")
- @Column(name="name",nullable=false, length=80)
- private String name;
- @ManyToOne
- @JoinColumn(name="seat_id")
- private Sede sedeId;
- public Especialidad() { }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public Sede getSedeId() {
- return sedeId;
- }
- public void setSedeId(Sede sedeId) {
- this.sedeId = sedeId;
- }
- }
- ------------------------------------------------------------------------------------------------------------
- package com.upc.demo.entities;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.GeneratedValue;
- import javax.persistence.GenerationType;
- import javax.persistence.Id;
- import javax.persistence.Table;
- import javax.validation.constraints.Size;
- @Entity
- @Table(name = "seats")
- public class Sede {
- @Id
- @GeneratedValue(strategy=GenerationType.IDENTITY)
- private int id;
- @Size(min=2, max=50, message="nombre entre 2-50")
- @Column(name="name",nullable=false)
- private String name;
- public Sede() { }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- }
- ------------------------------------------------------------------------------------------------------------
- repository
- ------------------------------------------------------------------------------------------------------------
- package com.upc.demo.repository;
- import java.util.List;
- import org.springframework.data.jpa.repository.JpaRepository;
- import org.springframework.stereotype.Repository;
- import com.upc.demo.entities.CitaMedica;
- @Repository
- public interface CitaMedicaRepository extends JpaRepository<CitaMedica, Integer>{
- List<CitaMedica> findByPatientName(String patientName);
- }
- ------------------------------------------------------------------------------------------------------------
- package com.upc.demo.repository;
- import java.util.List;
- import org.springframework.data.jpa.repository.JpaRepository;
- import org.springframework.stereotype.Repository;
- import com.upc.demo.entities.Doctor;
- import com.upc.demo.entities.Especialidad;
- @Repository
- public interface DoctorRepository extends JpaRepository<Doctor, Integer>{
- List<Doctor> findBySpecialityId(Especialidad id);
- }
- ------------------------------------------------------------------------------------------------------------
- package com.upc.demo.repository;
- import java.util.List;
- import org.springframework.data.jpa.repository.JpaRepository;
- import org.springframework.stereotype.Repository;
- import com.upc.demo.entities.Especialidad;
- import com.upc.demo.entities.Sede;
- @Repository
- public interface EspecialidadRepository extends JpaRepository<Especialidad, Integer>{
- List<Especialidad> findBySedeId(Sede id);
- }
- ------------------------------------------------------------------------------------------------------------
- package com.upc.demo.repository;
- import org.springframework.data.jpa.repository.JpaRepository;
- import org.springframework.stereotype.Repository;
- import com.upc.demo.entities.Sede;
- @Repository
- public interface SedeRepository extends JpaRepository<Sede, Integer>{
- }
- ------------------------------------------------------------------------------------------------------------
- package com.upc.demo.service;
- import java.util.List;
- import java.util.Optional;
- public interface CrudService<T> {
- List<T> findAll() throws Exception;
- T save(T t)throws Exception;
- T update(T t)throws Exception;
- Optional<T> findById(Integer id)throws Exception;
- void deleteById(Integer id)throws Exception;
- void deleteAll()throws Exception;
- }
- ------------------------------------------------------------------------------------------------------------
- package com.upc.demo.service;
- import java.util.List;
- import com.upc.demo.entities.Especialidad;
- import com.upc.demo.entities.Sede;
- public interface EspecialidadService extends CrudService<Especialidad>{
- List<Especialidad> findBySedeId(Sede id);
- }
- ------------------------------------------------------------------------------------------------------------
- package com.upc.demo.service;
- import com.upc.demo.entities.Sede;
- public interface SedeService extends CrudService<Sede>{
- }
- ------------------------------------------------------------------------------------------------------------
- service.implementation
- ------------------------------------------------------------------------------------------------------------
- package com.upc.demo.service.implementation;
- import java.util.List;
- import java.util.Optional;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import com.upc.demo.entities.Especialidad;
- import com.upc.demo.entities.Sede;
- import com.upc.demo.repository.EspecialidadRepository;
- import com.upc.demo.service.EspecialidadService;
- @Service
- public class EspecialidadServiceImplementation implements EspecialidadService {
- @Autowired
- private EspecialidadRepository especialidadRepository;
- @Transactional(readOnly=true)
- @Override
- public List<Especialidad> findAll() throws Exception {
- // TODO Auto-generated method stub
- return especialidadRepository.findAll();
- }
- @Transactional
- @Override
- public Especialidad save(Especialidad t) throws Exception {
- // TODO Auto-generated method stub
- return especialidadRepository.save(t);
- }
- @Transactional
- @Override
- public Especialidad update(Especialidad t) throws Exception {
- // TODO Auto-generated method stub
- return especialidadRepository.save(t);
- }
- @Transactional(readOnly=true)
- @Override
- public Optional<Especialidad> findById(Integer id) throws Exception {
- // TODO Auto-generated method stub
- return especialidadRepository.findById(id);
- }
- @Transactional
- @Override
- public void deleteById(Integer id) throws Exception {
- // TODO Auto-generated method stub
- especialidadRepository.deleteById(id);
- }
- @Transactional
- @Override
- public void deleteAll() throws Exception {
- // TODO Auto-generated method stub
- especialidadRepository.deleteAll();
- }
- @Transactional
- @Override
- public List<Especialidad> findBySedeId(Sede id) {
- // TODO Auto-generated method stub
- return especialidadRepository.findBySedeId(id);
- }
- }
- ------------------------------------------------------------------------------------------------------------
- package com.upc.demo.service.implementation;
- import java.util.List;
- import java.util.Optional;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import com.upc.demo.entities.Sede;
- import com.upc.demo.repository.SedeRepository;
- import com.upc.demo.service.SedeService;
- @Service
- public class SedeServiceImplementation implements SedeService {
- @Autowired
- private SedeRepository sedeRepository;
- @Transactional(readOnly=true)
- @Override
- public List<Sede> findAll() throws Exception {
- // TODO Auto-generated method stub
- return sedeRepository.findAll();
- }
- @Transactional
- @Override
- public Sede save(Sede t) throws Exception {
- // TODO Auto-generated method stub
- return sedeRepository.save(t);
- }
- @Transactional
- @Override
- public Sede update(Sede t) throws Exception {
- // TODO Auto-generated method stub
- return sedeRepository.save(t);
- }
- @Transactional(readOnly=true)
- @Override
- public Optional<Sede> findById(Integer id) throws Exception {
- // TODO Auto-generated method stub
- return sedeRepository.findById(id);
- }
- @Transactional
- @Override
- public void deleteById(Integer id) throws Exception {
- // TODO Auto-generated method stub
- sedeRepository.deleteById(id);
- }
- @Transactional
- @Override
- public void deleteAll() throws Exception {
- // TODO Auto-generated method stub
- sedeRepository.deleteAll();
- }
- }
- ------------------------------------------------------------------------------------------------------------
- controller
- ------------------------------------------------------------------------------------------------------------
- package com.upc.demo.controller;
- import java.net.URI;
- import java.util.ArrayList;
- import java.util.List;
- import javax.validation.Valid;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.http.HttpStatus;
- import org.springframework.http.MediaType;
- 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;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
- import com.upc.demo.entities.Especialidad;
- import com.upc.demo.entities.Sede;
- import com.upc.demo.service.EspecialidadService;
- import io.swagger.annotations.Api;
- import io.swagger.annotations.ApiOperation;
- @RestController
- @Api(value="Rest de especialidades")
- @RequestMapping("/api/especialidades")
- public class EspecialidadController {
- @Autowired
- private EspecialidadService especialidadService;
- @ApiOperation("devuelve las especialidades")
- @GetMapping(produces=MediaType.APPLICATION_JSON_VALUE)
- public ResponseEntity<List<Especialidad>> fetchPatients(){
- List<Especialidad> patients;
- try {
- patients = especialidadService.findAll();
- return new ResponseEntity<List<Especialidad>>(patients, HttpStatus.OK);
- } catch (Exception e) {
- e.printStackTrace();
- return new ResponseEntity<List<Especialidad>>(HttpStatus.INTERNAL_SERVER_ERROR);
- }
- }
- @ApiOperation("devuelve un paciente por sede")
- @GetMapping(value = "/sedes/{id}", produces=MediaType.APPLICATION_JSON_VALUE)
- public ResponseEntity<List<Especialidad>> fetchPatientByDni(@PathVariable("id") int id){
- try {
- List<Especialidad> paseos = new ArrayList<>();
- Sede sede = new Sede();
- sede.setId(id);
- paseos = especialidadService.findBySedeId(sede);
- return new ResponseEntity<List<Especialidad>>(paseos, HttpStatus.OK);
- } catch (Exception e) {
- return new ResponseEntity<List<Especialidad>>(HttpStatus.INTERNAL_SERVER_ERROR);
- }
- }
- @ApiOperation("guarda paciente")
- @PostMapping(produces=MediaType.APPLICATION_JSON_VALUE, consumes=MediaType.APPLICATION_JSON_VALUE)
- public ResponseEntity<Object> savePatient(@Valid @RequestBody Especialidad patient){
- Especialidad pat = new Especialidad();
- try {
- pat = especialidadService.save(patient);
- URI location = ServletUriComponentsBuilder
- .fromCurrentRequest()
- .path("/{id}")
- .buildAndExpand(pat.getId())
- .toUri();
- return ResponseEntity.created(location).build();
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- return new ResponseEntity<Object>(HttpStatus.INTERNAL_SERVER_ERROR);
- }
- }
- }
- ------------------------------------------------------------------------------------------------------------
- package com.upc.demo.controller;
- import java.net.URI;
- import java.util.List;
- import java.util.Optional;
- import javax.validation.Valid;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.http.HttpStatus;
- import org.springframework.http.MediaType;
- import org.springframework.http.ResponseEntity;
- import org.springframework.web.bind.annotation.DeleteMapping;
- 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.PutMapping;
- import org.springframework.web.bind.annotation.RequestBody;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
- import com.upc.demo.entities.Sede;
- import com.upc.demo.service.SedeService;
- import io.swagger.annotations.Api;
- import io.swagger.annotations.ApiOperation;
- @RestController
- @Api(value="Rest de sedes")
- @RequestMapping("/api/sedes")
- public class SedeController {
- @Autowired
- private SedeService sedeService;
- @ApiOperation("devuelve las sedes")
- @GetMapping(produces=MediaType.APPLICATION_JSON_VALUE)
- public ResponseEntity<List<Sede>> fetchPatients(){
- List<Sede> patients;
- try {
- patients = sedeService.findAll();
- return new ResponseEntity<List<Sede>>(patients, HttpStatus.OK);
- } catch (Exception e) {
- e.printStackTrace();
- return new ResponseEntity<List<Sede>>(HttpStatus.INTERNAL_SERVER_ERROR);
- }
- }
- @ApiOperation("devuelve una sede")
- @GetMapping(value = "/{id}", produces=MediaType.APPLICATION_JSON_VALUE)
- public ResponseEntity<Sede> fetchPatient(@PathVariable("id") Integer id){
- Optional<Sede> patient;
- try {
- patient = sedeService.findById(id);
- return new ResponseEntity<Sede>(patient.get(), HttpStatus.OK);
- } catch (Exception e) {
- e.printStackTrace();
- return new ResponseEntity<Sede>(HttpStatus.INTERNAL_SERVER_ERROR);
- }
- }
- @ApiOperation("guarda sede")
- @PostMapping(produces=MediaType.APPLICATION_JSON_VALUE, consumes=MediaType.APPLICATION_JSON_VALUE)
- public ResponseEntity<Object> savePatient(@Valid @RequestBody Sede patient){
- Sede pat = new Sede();
- try {
- pat = sedeService.save(patient);
- URI location = ServletUriComponentsBuilder
- .fromCurrentRequest()
- .path("/{id}")
- .buildAndExpand(pat.getId())
- .toUri();
- return ResponseEntity.created(location).build();
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- return new ResponseEntity<Object>(HttpStatus.INTERNAL_SERVER_ERROR);
- }
- }
- @ApiOperation("actualiza una sede")
- @PutMapping(produces=MediaType.APPLICATION_JSON_VALUE, consumes=MediaType.APPLICATION_JSON_VALUE)
- public ResponseEntity<Object> updatePatient(@Valid @RequestBody Sede patient){
- try {
- sedeService.update(patient);
- return new ResponseEntity<Object>(HttpStatus.OK);
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- return new ResponseEntity<Object>(HttpStatus.INTERNAL_SERVER_ERROR);
- }
- }
- @ApiOperation("elimina una sede")
- @DeleteMapping(value = "/{id}", produces=MediaType.APPLICATION_JSON_VALUE)
- public ResponseEntity<String> deletePatient(@PathVariable("id") Integer id){
- Optional<Sede> patient;
- try {
- patient = sedeService.findById(id);
- if (!patient.isPresent()) {
- return new ResponseEntity<String>(HttpStatus.NOT_FOUND);
- }
- else {
- sedeService.deleteById(id);
- return new ResponseEntity<String>("Se elimino la sede", HttpStatus.OK);
- }
- } catch (Exception e) {
- e.printStackTrace();
- return new ResponseEntity<String>(HttpStatus.INTERNAL_SERVER_ERROR);
- }
- }
- @ApiOperation("elimina todas las sdes")
- @DeleteMapping(produces=MediaType.APPLICATION_JSON_VALUE)
- public ResponseEntity<String> deleteAllPatients(){
- try {
- sedeService.deleteAll();
- return new ResponseEntity<String>("Se elimino todas las sedes", HttpStatus.OK);
- } catch (Exception e) {
- e.printStackTrace();
- return new ResponseEntity<String>(HttpStatus.INTERNAL_SERVER_ERROR);
- }
- }
- }
- ------------------------------------------------------------------------------------------------------------
- config
- ------------------------------------------------------------------------------------------------------------
- package com.upc.demo.config;
- import java.util.ArrayList;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import springfox.documentation.service.ApiInfo;
- import springfox.documentation.service.VendorExtension;
- import springfox.documentation.spi.DocumentationType;
- import springfox.documentation.spring.web.plugins.Docket;
- import springfox.documentation.swagger2.annotations.EnableSwagger2;
- @Configuration
- @EnableSwagger2
- public class SwaggerConfig {
- public static final springfox.documentation.service.Contact DEFAULT_CONTACT = new springfox.documentation.service.Contact("Karique", "http://github.com/karique01",
- "kariquekeiter@gmail.com");
- @SuppressWarnings("rawtypes")
- public static final ApiInfo DEFAULT_API_INFO = new ApiInfo("Farma Api Documentation", "Farma Api Documentation", "1.0",
- "PREMIUM", DEFAULT_CONTACT, "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0",
- new ArrayList<VendorExtension>());
- @Bean
- public Docket api() {
- return new Docket(DocumentationType.SWAGGER_2).apiInfo(DEFAULT_API_INFO);
- }
- }
- ------------------------------------------------------------------------------------------------------------
- application.yml
- ------------------------------------------------------------------------------------------------------------
- spring:
- datasource:
- driver-class-name: org.postgresql.Driver
- password: admin
- url: jdbc:postgresql://localhost/primi
- username: postgres
- jpa:
- database: POSTGRESQL
- hibernate:
- ddl-auto: update
- show-sql: 'false'
- properties.hibernate.temp.use_jdbc_metadata_defaults: false
- server:
- port: 9099
- ------------------------------------------------------------------------------------------------------------
- pom.xml
- ------------------------------------------------------------------------------------------------------------
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.upc</groupId>
- <artifactId>primi</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>jar</packaging>
- <name>primi</name>
- <description>Pet walk api rest</description>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.1.0.RELEASE</version>
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
- <java.version>1.8</java.version>
- </properties>
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-data-jpa</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-devtools</artifactId>
- <scope>runtime</scope>
- </dependency>
- <dependency>
- <groupId>org.postgresql</groupId>
- <artifactId>postgresql</artifactId>
- <scope>runtime</scope>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>io.springfox</groupId>
- <artifactId>springfox-swagger2</artifactId>
- <version>2.9.2</version>
- </dependency>
- <dependency>
- <groupId>io.springfox</groupId>
- <artifactId>springfox-swagger-ui</artifactId>
- <version>2.9.2</version>
- </dependency>
- </dependencies>
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
- </project>
- ------------------------------------------------------------------------------------------------------------
- dependencias iniciales
- DevTools
- JPA
- PostgreSql
- Web
- ------------------------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement