Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.po_project.model;
- import javax.persistence.*;
- @Table(name = "typdokumentu")
- @Entity
- public class TypDokumentu {
- @Id
- @Column(name = "id")
- @GeneratedValue(strategy = GenerationType.AUTO)
- private Long id;
- @Column(name = "nazwa")
- private String nazwa;
- public TypDokumentu() {
- }
- public Long getId() {
- return id;
- }
- public void setId(Long id) {
- this.id = id;
- }
- public String getNazwa() {
- return nazwa;
- }
- public void setNazwa(String nazwa) {
- this.nazwa = nazwa;
- }
- @Override
- public String toString() {
- return "TypDokumentu{" +
- "id=" + id +
- ", nazwa='" + nazwa + '\'' +
- '}';
- }
- }
- package com.example.po_project.repozytorium;
- import com.example.po_project.model.TypDokumentu;
- import org.springframework.data.jdbc.repository.query.Query;
- import org.springframework.data.jpa.repository.JpaRepository;
- public interface TypDokumentuRepozytorium extends JpaRepository<TypDokumentu,Long> {
- @Query("SELECT * FROM typdokumentu WHERE nazwa=:nazwa")
- TypDokumentu getTypDokumentuByNazwa(String nazwa);
- }
- package com.example.po_project.serwis;
- import com.example.po_project.model.TypDokumentu;
- import com.example.po_project.repozytorium.TypDokumentuRepozytorium;
- import org.springframework.stereotype.Service;
- @Service
- public class TypDokumentuSerwisImpl implements TypDokumentuSerwis {
- private TypDokumentuRepozytorium typDokumentuRepozytorium;
- public TypDokumentuSerwisImpl(TypDokumentuRepozytorium typDokumentuRepozytorium) {
- this.typDokumentuRepozytorium = typDokumentuRepozytorium;
- }
- @Override
- public TypDokumentu getTypDokumentuByNazwa(String nazwa) {
- return typDokumentuRepozytorium.getTypDokumentuByNazwa(nazwa);
- }
- public TypDokumentu add(TypDokumentu dokumentu){
- return typDokumentuRepozytorium.save(dokumentu);
- }
- }
- package com.example.po_project.kontroler;
- import com.example.po_project.dto.TypDokumentuDto;
- import com.example.po_project.model.TypDokumentu;
- import com.example.po_project.serwis.TypDokumentuSerwisImpl;
- import org.modelmapper.ModelMapper;
- import org.springframework.web.bind.annotation.*;
- @RestController
- @RequestMapping("/typDokumentu")
- @CrossOrigin
- public class TypDokumentuKontroler {
- private TypDokumentuSerwisImpl typDokumentuSerwis;
- private ModelMapper modelMapper;
- public TypDokumentuKontroler(TypDokumentuSerwisImpl typDokumentuSerwis, ModelMapper modelMapper) {
- this.typDokumentuSerwis = typDokumentuSerwis;
- this.modelMapper = modelMapper;
- }
- @GetMapping("/{nazwa}")
- public TypDokumentuDto getTypDokumentuByNazwa(@PathVariable(value = "nazwa")String nazwa){
- return convertToDto(typDokumentuSerwis.getTypDokumentuByNazwa(nazwa));
- }
- private TypDokumentuDto convertToDto(TypDokumentu typDokumentu){
- return modelMapper.map(typDokumentu,TypDokumentuDto.class);
- }
- @PostMapping("/dodaj")
- public String add(@RequestBody TypDokumentu dokumentu){
- typDokumentuSerwis.add(dokumentu);
- return "OK";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement