summaryrefslogtreecommitdiff
path: root/Year_3/TSDWL/SPRING/petshop/src/main/java
diff options
context:
space:
mode:
Diffstat (limited to 'Year_3/TSDWL/SPRING/petshop/src/main/java')
-rw-r--r--Year_3/TSDWL/SPRING/petshop/src/main/java/it/dmi/tsdw/petshop/Pet.java71
-rw-r--r--Year_3/TSDWL/SPRING/petshop/src/main/java/it/dmi/tsdw/petshop/PetshopApplication.java13
-rw-r--r--Year_3/TSDWL/SPRING/petshop/src/main/java/it/dmi/tsdw/petshop/Race.java42
-rw-r--r--Year_3/TSDWL/SPRING/petshop/src/main/java/it/dmi/tsdw/petshop/controllers/PetController.java71
-rw-r--r--Year_3/TSDWL/SPRING/petshop/src/main/java/it/dmi/tsdw/petshop/controllers/RaceController.java71
-rw-r--r--Year_3/TSDWL/SPRING/petshop/src/main/java/it/dmi/tsdw/petshop/repositories/PetRepository.java14
-rw-r--r--Year_3/TSDWL/SPRING/petshop/src/main/java/it/dmi/tsdw/petshop/repositories/RaceRepository.java10
-rw-r--r--Year_3/TSDWL/SPRING/petshop/src/main/java/it/dmi/tsdw/petshop/services/PetService.java44
-rw-r--r--Year_3/TSDWL/SPRING/petshop/src/main/java/it/dmi/tsdw/petshop/services/RaceService.java44
9 files changed, 380 insertions, 0 deletions
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<List<Pet>> getAllPets() {
+ List<Pet> entityList = service.getAllPets();
+ return ResponseEntity.ok(entityList);
+ }
+
+ @GetMapping("/pets/{id}")
+ public ResponseEntity<Pet> getPet(@PathVariable long id) {
+ Optional<Pet> entity = service.getPet(id);
+ if(entity.isPresent())
+ return ResponseEntity.ok(entity.get());
+ else
+ return ResponseEntity.notFound().build();
+ }
+
+ @PostMapping("/pets")
+ public ResponseEntity<Pet> 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<Pet> 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<Void> 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<List<Race>> getAllRaces() {
+ List<Race> entityList = service.getAllRaces();
+ return ResponseEntity.ok(entityList);
+ }
+
+ @GetMapping("/races/{id}")
+ public ResponseEntity<Race> getRace(@PathVariable long id) {
+ Optional<Race> entity = service.getRace(id);
+ if(entity.isPresent())
+ return ResponseEntity.ok(entity.get());
+ else
+ return ResponseEntity.notFound().build();
+ }
+
+ @PostMapping("/races")
+ public ResponseEntity<Race> 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<Race> 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<Void> 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<Pet, Long>{
+ 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<Race, Long>{
+} \ 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<Pet> getPet(Long id) {
+ return repository.findById(id);
+ }
+
+ public List<Pet> getAllPets() {
+ List<Pet> output = new ArrayList<Pet>();
+ 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<Race> getRace(Long id) {
+ return repository.findById(id);
+ }
+
+ public List<Race> getAllRaces() {
+ List<Race> output = new ArrayList<Race>();
+ 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