From d43e1f5f5c0d70ca1b0ed4eed4b5364bf53e5685 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Mon, 24 Jan 2022 19:31:36 +0100 Subject: add example in spring --- .../src/main/java/it/dmi/tsdw/petshop/Pet.java | 71 ++++++++++++++++++++++ .../it/dmi/tsdw/petshop/PetshopApplication.java | 13 ++++ .../src/main/java/it/dmi/tsdw/petshop/Race.java | 42 +++++++++++++ .../tsdw/petshop/controllers/PetController.java | 71 ++++++++++++++++++++++ .../tsdw/petshop/controllers/RaceController.java | 71 ++++++++++++++++++++++ .../tsdw/petshop/repositories/PetRepository.java | 14 +++++ .../tsdw/petshop/repositories/RaceRepository.java | 10 +++ .../it/dmi/tsdw/petshop/services/PetService.java | 44 ++++++++++++++ .../it/dmi/tsdw/petshop/services/RaceService.java | 44 ++++++++++++++ 9 files changed, 380 insertions(+) create mode 100644 Year_3/TSDWL/SPRING/petshop/src/main/java/it/dmi/tsdw/petshop/Pet.java create mode 100644 Year_3/TSDWL/SPRING/petshop/src/main/java/it/dmi/tsdw/petshop/PetshopApplication.java create mode 100644 Year_3/TSDWL/SPRING/petshop/src/main/java/it/dmi/tsdw/petshop/Race.java create mode 100644 Year_3/TSDWL/SPRING/petshop/src/main/java/it/dmi/tsdw/petshop/controllers/PetController.java create mode 100644 Year_3/TSDWL/SPRING/petshop/src/main/java/it/dmi/tsdw/petshop/controllers/RaceController.java create mode 100644 Year_3/TSDWL/SPRING/petshop/src/main/java/it/dmi/tsdw/petshop/repositories/PetRepository.java create mode 100644 Year_3/TSDWL/SPRING/petshop/src/main/java/it/dmi/tsdw/petshop/repositories/RaceRepository.java create mode 100644 Year_3/TSDWL/SPRING/petshop/src/main/java/it/dmi/tsdw/petshop/services/PetService.java create mode 100644 Year_3/TSDWL/SPRING/petshop/src/main/java/it/dmi/tsdw/petshop/services/RaceService.java (limited to 'Year_3/TSDWL/SPRING/petshop/src/main/java') diff --git a/Year_3/TSDWL/SPRING/petshop/src/main/java/it/dmi/tsdw/petshop/Pet.java b/Year_3/TSDWL/SPRING/petshop/src/main/java/it/dmi/tsdw/petshop/Pet.java new file mode 100644 index 0000000..770c3ce --- /dev/null +++ b/Year_3/TSDWL/SPRING/petshop/src/main/java/it/dmi/tsdw/petshop/Pet.java @@ -0,0 +1,71 @@ +package it.dmi.tsdw.petshop; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.ManyToOne; +import javax.persistence.Table; +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; + + +import org.hibernate.validator.constraints.Length; + +@Table(name = "pets") +@Entity +public class Pet { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Column + @NotNull + @NotBlank + @Length(max=30) + private String name; + + @Column + private String born; + + @ManyToOne + @NotNull + private Race race; + + public Race getRace() { + return race; + } + + public void setRace(Race race) { + this.race = race; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getBorn() { + return born; + } + + public void setBorn(String born) { + this.born = born; + } + + public Pet() { + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } +} \ No newline at end of file diff --git a/Year_3/TSDWL/SPRING/petshop/src/main/java/it/dmi/tsdw/petshop/PetshopApplication.java b/Year_3/TSDWL/SPRING/petshop/src/main/java/it/dmi/tsdw/petshop/PetshopApplication.java new file mode 100644 index 0000000..83d038e --- /dev/null +++ b/Year_3/TSDWL/SPRING/petshop/src/main/java/it/dmi/tsdw/petshop/PetshopApplication.java @@ -0,0 +1,13 @@ +package it.dmi.tsdw.petshop; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class PetshopApplication { + + public static void main(String[] args) { + SpringApplication.run(PetshopApplication.class, args); + } + +} diff --git a/Year_3/TSDWL/SPRING/petshop/src/main/java/it/dmi/tsdw/petshop/Race.java b/Year_3/TSDWL/SPRING/petshop/src/main/java/it/dmi/tsdw/petshop/Race.java new file mode 100644 index 0000000..4f08fe9 --- /dev/null +++ b/Year_3/TSDWL/SPRING/petshop/src/main/java/it/dmi/tsdw/petshop/Race.java @@ -0,0 +1,42 @@ +package it.dmi.tsdw.petshop; + + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; + +@Entity +public class Race { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Column + @NotBlank + @NotNull + private String name; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Race() { + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } +} \ No newline at end of file diff --git a/Year_3/TSDWL/SPRING/petshop/src/main/java/it/dmi/tsdw/petshop/controllers/PetController.java b/Year_3/TSDWL/SPRING/petshop/src/main/java/it/dmi/tsdw/petshop/controllers/PetController.java new file mode 100644 index 0000000..f862a2c --- /dev/null +++ b/Year_3/TSDWL/SPRING/petshop/src/main/java/it/dmi/tsdw/petshop/controllers/PetController.java @@ -0,0 +1,71 @@ +package it.dmi.tsdw.petshop.controllers; + +import java.net.URI; +import java.net.URISyntaxException; +import java.util.List; +import java.util.Optional; + +import javax.validation.Valid; + +import org.springframework.beans.factory.annotation.Autowired; +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 it.dmi.tsdw.petshop.Pet; +import it.dmi.tsdw.petshop.services.PetService; + +@RestController +@RequestMapping("/api") +public class PetController { + + @Autowired + private PetService service; + + @GetMapping("/pets") + public ResponseEntity> getAllPets() { + List entityList = service.getAllPets(); + return ResponseEntity.ok(entityList); + } + + @GetMapping("/pets/{id}") + public ResponseEntity getPet(@PathVariable long id) { + Optional entity = service.getPet(id); + if(entity.isPresent()) + return ResponseEntity.ok(entity.get()); + else + return ResponseEntity.notFound().build(); + } + + @PostMapping("/pets") + public ResponseEntity addPet(@Valid @RequestBody Pet e) throws URISyntaxException { + if (e.getId() != null) { + return ResponseEntity.badRequest().build(); + } + Pet entity = service.addPet(e); + return ResponseEntity.created(new URI("/api/resourcePath" + entity.getId())).body(entity); + } + + @PutMapping("/pets") + public ResponseEntity updatePet(@Valid @RequestBody Pet e) { + if (e.getId() == null) + return ResponseEntity.notFound().build(); + Pet entity = service.updatePet(e); + return ResponseEntity.ok(entity); + } + + @DeleteMapping("/pets/{id}") + public ResponseEntity deletePet(@PathVariable long id) { + if (service.getPet(id).isEmpty()) + return ResponseEntity.notFound().build(); + + service.deletePet(id); + return ResponseEntity.ok().build(); + } +} \ No newline at end of file diff --git a/Year_3/TSDWL/SPRING/petshop/src/main/java/it/dmi/tsdw/petshop/controllers/RaceController.java b/Year_3/TSDWL/SPRING/petshop/src/main/java/it/dmi/tsdw/petshop/controllers/RaceController.java new file mode 100644 index 0000000..c2089ef --- /dev/null +++ b/Year_3/TSDWL/SPRING/petshop/src/main/java/it/dmi/tsdw/petshop/controllers/RaceController.java @@ -0,0 +1,71 @@ +package it.dmi.tsdw.petshop.controllers; + +import java.net.URI; +import java.net.URISyntaxException; +import java.util.List; +import java.util.Optional; + +import javax.validation.Valid; + +import org.springframework.beans.factory.annotation.Autowired; +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 it.dmi.tsdw.petshop.Race; +import it.dmi.tsdw.petshop.services.RaceService; + +@RestController +@RequestMapping("/api") +public class RaceController { + + @Autowired + private RaceService service; + + @GetMapping("/races") + public ResponseEntity> getAllRaces() { + List entityList = service.getAllRaces(); + return ResponseEntity.ok(entityList); + } + + @GetMapping("/races/{id}") + public ResponseEntity getRace(@PathVariable long id) { + Optional entity = service.getRace(id); + if(entity.isPresent()) + return ResponseEntity.ok(entity.get()); + else + return ResponseEntity.notFound().build(); + } + + @PostMapping("/races") + public ResponseEntity addRace(@Valid @RequestBody Race e) throws URISyntaxException { + if (e.getId() != null) { + return ResponseEntity.badRequest().build(); + } + Race entity = service.addRace(e); + return ResponseEntity.created(new URI("/api/races" + entity.getId())).body(entity); + } + + @PutMapping("/races") + public ResponseEntity updateRace(@Valid @RequestBody Race e) { + if (e.getId() == null) + return ResponseEntity.notFound().build(); + Race entity = service.updateRace(e); + return ResponseEntity.ok(entity); + } + + @DeleteMapping("/races/{id}") + public ResponseEntity deleteRace(@PathVariable long id) { + if (service.getRace(id).isEmpty()) + return ResponseEntity.notFound().build(); + + service.deleteRace(id); + return ResponseEntity.ok().build(); + } +} \ No newline at end of file diff --git a/Year_3/TSDWL/SPRING/petshop/src/main/java/it/dmi/tsdw/petshop/repositories/PetRepository.java b/Year_3/TSDWL/SPRING/petshop/src/main/java/it/dmi/tsdw/petshop/repositories/PetRepository.java new file mode 100644 index 0000000..145e47e --- /dev/null +++ b/Year_3/TSDWL/SPRING/petshop/src/main/java/it/dmi/tsdw/petshop/repositories/PetRepository.java @@ -0,0 +1,14 @@ +package it.dmi.tsdw.petshop.repositories; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import it.dmi.tsdw.petshop.Pet; +import it.dmi.tsdw.petshop.Race; + +@Repository +public interface PetRepository extends JpaRepository{ + public Race findByRaceId(Long race_id); + public Race findByRaceName(String name); + +} diff --git a/Year_3/TSDWL/SPRING/petshop/src/main/java/it/dmi/tsdw/petshop/repositories/RaceRepository.java b/Year_3/TSDWL/SPRING/petshop/src/main/java/it/dmi/tsdw/petshop/repositories/RaceRepository.java new file mode 100644 index 0000000..a9f475a --- /dev/null +++ b/Year_3/TSDWL/SPRING/petshop/src/main/java/it/dmi/tsdw/petshop/repositories/RaceRepository.java @@ -0,0 +1,10 @@ +package it.dmi.tsdw.petshop.repositories; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import it.dmi.tsdw.petshop.Race; + +@Repository +public interface RaceRepository extends JpaRepository{ +} \ No newline at end of file diff --git a/Year_3/TSDWL/SPRING/petshop/src/main/java/it/dmi/tsdw/petshop/services/PetService.java b/Year_3/TSDWL/SPRING/petshop/src/main/java/it/dmi/tsdw/petshop/services/PetService.java new file mode 100644 index 0000000..6dcd07b --- /dev/null +++ b/Year_3/TSDWL/SPRING/petshop/src/main/java/it/dmi/tsdw/petshop/services/PetService.java @@ -0,0 +1,44 @@ +package it.dmi.tsdw.petshop.services; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import it.dmi.tsdw.petshop.Pet; +import it.dmi.tsdw.petshop.repositories.PetRepository; + +@Service +public class PetService { + + @Autowired + private PetRepository repository; + + public Pet addPet(Pet e) { + return repository.save(e); + } + + public Optional getPet(Long id) { + return repository.findById(id); + } + + public List getAllPets() { + List output = new ArrayList(); + repository.findAll().forEach(output::add); + return output; + } + + public Pet updatePet(Pet e) { + return repository.save(e); + } + + public void deletePet(Pet e) { + repository.delete(e); +} + + public void deletePet(Long id) { + repository.deleteById(id); + } +} \ No newline at end of file diff --git a/Year_3/TSDWL/SPRING/petshop/src/main/java/it/dmi/tsdw/petshop/services/RaceService.java b/Year_3/TSDWL/SPRING/petshop/src/main/java/it/dmi/tsdw/petshop/services/RaceService.java new file mode 100644 index 0000000..16828a7 --- /dev/null +++ b/Year_3/TSDWL/SPRING/petshop/src/main/java/it/dmi/tsdw/petshop/services/RaceService.java @@ -0,0 +1,44 @@ +package it.dmi.tsdw.petshop.services; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import it.dmi.tsdw.petshop.Race; +import it.dmi.tsdw.petshop.repositories.RaceRepository; + +@Service +public class RaceService { + + @Autowired + private RaceRepository repository; + + public Race addRace(Race e) { + return repository.save(e); + } + + public Optional getRace(Long id) { + return repository.findById(id); + } + + public List getAllRaces() { + List output = new ArrayList(); + repository.findAll().forEach(output::add); + return output; + } + + public Race updateRace(Race e) { + return repository.save(e); + } + + public void deleteRace(Race e) { + repository.delete(e); + } + + public void deleteRace(Long id) { + repository.deleteById(id); + } +} \ No newline at end of file -- cgit v1.2.3-18-g5258