Advertisement
Guest User

Untitled

a guest
Jan 17th, 2020
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.22 KB | None | 0 0
  1. package com.example.po_project.model;
  2.  
  3. import javax.persistence.*;
  4.  
  5. @Table(name = "typdokumentu")
  6. @Entity
  7. public class TypDokumentu {
  8.  
  9.     @Id
  10.     @Column(name = "id")
  11.     @GeneratedValue(strategy = GenerationType.AUTO)
  12.     private Long id;
  13.  
  14.     @Column(name = "nazwa")
  15.     private String nazwa;
  16.  
  17.     public TypDokumentu() {
  18.     }
  19.  
  20.     public Long getId() {
  21.         return id;
  22.     }
  23.  
  24.     public void setId(Long id) {
  25.         this.id = id;
  26.     }
  27.  
  28.     public String getNazwa() {
  29.         return nazwa;
  30.     }
  31.  
  32.     public void setNazwa(String nazwa) {
  33.         this.nazwa = nazwa;
  34.     }
  35.  
  36.     @Override
  37.     public String toString() {
  38.         return "TypDokumentu{" +
  39.                 "id=" + id +
  40.                 ", nazwa='" + nazwa + '\'' +
  41.                 '}';
  42.     }
  43. }
  44.  
  45.  
  46.  
  47.  
  48. package com.example.po_project.repozytorium;
  49.  
  50. import com.example.po_project.model.TypDokumentu;
  51. import org.springframework.data.jdbc.repository.query.Query;
  52. import org.springframework.data.jpa.repository.JpaRepository;
  53.  
  54. public interface TypDokumentuRepozytorium extends JpaRepository<TypDokumentu,Long> {
  55.     @Query("SELECT * FROM typdokumentu WHERE nazwa=:nazwa")
  56.     TypDokumentu getTypDokumentuByNazwa(String nazwa);
  57. }
  58.  
  59.  
  60. package com.example.po_project.serwis;
  61.  
  62. import com.example.po_project.model.TypDokumentu;
  63. import com.example.po_project.repozytorium.TypDokumentuRepozytorium;
  64. import org.springframework.stereotype.Service;
  65.  
  66. @Service
  67. public class TypDokumentuSerwisImpl implements TypDokumentuSerwis {
  68.  
  69.     private TypDokumentuRepozytorium typDokumentuRepozytorium;
  70.  
  71.     public TypDokumentuSerwisImpl(TypDokumentuRepozytorium typDokumentuRepozytorium) {
  72.         this.typDokumentuRepozytorium = typDokumentuRepozytorium;
  73.     }
  74.  
  75.     @Override
  76.     public TypDokumentu getTypDokumentuByNazwa(String nazwa) {
  77.         return typDokumentuRepozytorium.getTypDokumentuByNazwa(nazwa);
  78.     }
  79.  
  80.     public TypDokumentu add(TypDokumentu dokumentu){
  81.         return typDokumentuRepozytorium.save(dokumentu);
  82.     }
  83. }
  84.  
  85.  
  86. package com.example.po_project.kontroler;
  87.  
  88. import com.example.po_project.dto.TypDokumentuDto;
  89. import com.example.po_project.model.TypDokumentu;
  90. import com.example.po_project.serwis.TypDokumentuSerwisImpl;
  91. import org.modelmapper.ModelMapper;
  92. import org.springframework.web.bind.annotation.*;
  93.  
  94. @RestController
  95. @RequestMapping("/typDokumentu")
  96. @CrossOrigin
  97. public class TypDokumentuKontroler {
  98.  
  99.     private TypDokumentuSerwisImpl typDokumentuSerwis;
  100.     private ModelMapper modelMapper;
  101.  
  102.     public TypDokumentuKontroler(TypDokumentuSerwisImpl typDokumentuSerwis, ModelMapper modelMapper) {
  103.         this.typDokumentuSerwis = typDokumentuSerwis;
  104.         this.modelMapper = modelMapper;
  105.     }
  106.  
  107.     @GetMapping("/{nazwa}")
  108.     public TypDokumentuDto getTypDokumentuByNazwa(@PathVariable(value = "nazwa")String nazwa){
  109.         return convertToDto(typDokumentuSerwis.getTypDokumentuByNazwa(nazwa));
  110.     }
  111.  
  112.     private TypDokumentuDto convertToDto(TypDokumentu typDokumentu){
  113.         return modelMapper.map(typDokumentu,TypDokumentuDto.class);
  114.     }
  115.  
  116.     @PostMapping("/dodaj")
  117.     public String add(@RequestBody TypDokumentu dokumentu){
  118.         typDokumentuSerwis.add(dokumentu);
  119.         return "OK";
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement