From f6aeec6c88a3eed97f9a06d9778998ec137b7fb6 Mon Sep 17 00:00:00 2001 From: HelenaHalldinITHS Date: Tue, 8 Mar 2022 18:20:21 +0100 Subject: [PATCH 1/5] Add criminal entity --- .../iths/crimedatabase/entity/Criminal.java | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/main/java/se/iths/crimedatabase/entity/Criminal.java diff --git a/src/main/java/se/iths/crimedatabase/entity/Criminal.java b/src/main/java/se/iths/crimedatabase/entity/Criminal.java new file mode 100644 index 0000000..cdcc664 --- /dev/null +++ b/src/main/java/se/iths/crimedatabase/entity/Criminal.java @@ -0,0 +1,43 @@ +package se.iths.crimedatabase.entity; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +@Entity +public class Criminal { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + private String firstName; + private String lastName; + + public Long getId() { + return id; + } + + public Criminal setId(Long id) { + this.id = id; + return this; + } + + public String getFirstName() { + return firstName; + } + + public Criminal setFirstName(String firstName) { + this.firstName = firstName; + return this; + } + + public String getLastName() { + return lastName; + } + + public Criminal setLastName(String lastName) { + this.lastName = lastName; + return this; + } + +} From 4bef1ba6445ae2d053296bf38d7b48d4a2e640ed Mon Sep 17 00:00:00 2001 From: HelenaHalldinITHS Date: Tue, 8 Mar 2022 18:21:55 +0100 Subject: [PATCH 2/5] Add criminal repository --- .../crimedatabase/repository/CriminalRepository.java | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 src/main/java/se/iths/crimedatabase/repository/CriminalRepository.java diff --git a/src/main/java/se/iths/crimedatabase/repository/CriminalRepository.java b/src/main/java/se/iths/crimedatabase/repository/CriminalRepository.java new file mode 100644 index 0000000..6548779 --- /dev/null +++ b/src/main/java/se/iths/crimedatabase/repository/CriminalRepository.java @@ -0,0 +1,9 @@ +package se.iths.crimedatabase.repository; + +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; +import se.iths.crimedatabase.entity.Criminal; + +@Repository +public interface CriminalRepository extends CrudRepository { +} From 525c2fa24e002ccc10f43270af62a0f46064956c Mon Sep 17 00:00:00 2001 From: HelenaHalldinITHS Date: Tue, 8 Mar 2022 18:28:27 +0100 Subject: [PATCH 3/5] Add criminal service --- .../service/CriminalService.java | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/main/java/se/iths/crimedatabase/service/CriminalService.java diff --git a/src/main/java/se/iths/crimedatabase/service/CriminalService.java b/src/main/java/se/iths/crimedatabase/service/CriminalService.java new file mode 100644 index 0000000..f9847d8 --- /dev/null +++ b/src/main/java/se/iths/crimedatabase/service/CriminalService.java @@ -0,0 +1,35 @@ +package se.iths.crimedatabase.service; + +import org.springframework.stereotype.Service; +import se.iths.crimedatabase.entity.Criminal; +import se.iths.crimedatabase.repository.CriminalRepository; + +import javax.persistence.EntityNotFoundException; +import java.util.Optional; + +@Service +public class CriminalService { + private final CriminalRepository criminalRepository; + + public CriminalService(CriminalRepository criminalService) { + this.criminalRepository = criminalService; + } + + public Criminal create(Criminal criminal) { + return criminalRepository.save(criminal); + } + + public void delete(Long id) { + Criminal foundCriminal = criminalRepository.findById(id).orElseThrow(EntityNotFoundException::new); + criminalRepository.deleteById(foundCriminal.getId()); + } + + public Optional findById(Long id) { + return criminalRepository.findById(id); + } + + public Iterable findAll() { + return criminalRepository.findAll(); + } + +} From 4412400e89af1a7e510d58d56a8d9b953360c85c Mon Sep 17 00:00:00 2001 From: HelenaHalldinITHS Date: Tue, 8 Mar 2022 18:37:06 +0100 Subject: [PATCH 4/5] Add criminal controller --- .../controller/CriminalController.java | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/main/java/se/iths/crimedatabase/controller/CriminalController.java diff --git a/src/main/java/se/iths/crimedatabase/controller/CriminalController.java b/src/main/java/se/iths/crimedatabase/controller/CriminalController.java new file mode 100644 index 0000000..6d3cd09 --- /dev/null +++ b/src/main/java/se/iths/crimedatabase/controller/CriminalController.java @@ -0,0 +1,44 @@ +package se.iths.crimedatabase.controller; + +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; +import se.iths.crimedatabase.entity.Criminal; +import se.iths.crimedatabase.service.CriminalService; + +import java.util.Optional; + +@RestController +@RequestMapping("criminals") +public class CriminalController { + private final CriminalService criminalService; + + public CriminalController(CriminalService criminalService) { + this.criminalService = criminalService; + } + + @PostMapping + public ResponseEntity create(@RequestBody Criminal criminal) { + Criminal createdCriminal = criminalService.create(criminal); + return new ResponseEntity<>(createdCriminal, HttpStatus.CREATED); + } + + @DeleteMapping("{id}") + public ResponseEntity delete(@PathVariable Long id) { + criminalService.delete(id); + return new ResponseEntity<>(HttpStatus.OK); + } + + @GetMapping("{id}") + public ResponseEntity> findById(@PathVariable Long id) { + Optional foundCriminal = criminalService.findById(id); + return new ResponseEntity<>(foundCriminal, HttpStatus.OK); + } + + @GetMapping + public ResponseEntity> findAll() { + Iterable criminals = criminalService.findAll(); + return new ResponseEntity<>(criminals, HttpStatus.OK); + } + +} From 0ad0b9b4fc149ba51ae2e27649e79dc32ef53d12 Mon Sep 17 00:00:00 2001 From: HelenaHalldinITHS Date: Wed, 9 Mar 2022 12:50:29 +0100 Subject: [PATCH 5/5] Add field dateOfBirth to Criminal --- .../java/se/iths/crimedatabase/entity/Criminal.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/main/java/se/iths/crimedatabase/entity/Criminal.java b/src/main/java/se/iths/crimedatabase/entity/Criminal.java index cdcc664..0dd0613 100644 --- a/src/main/java/se/iths/crimedatabase/entity/Criminal.java +++ b/src/main/java/se/iths/crimedatabase/entity/Criminal.java @@ -4,6 +4,7 @@ import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; +import java.time.LocalDate; @Entity public class Criminal { @@ -12,6 +13,7 @@ public class Criminal { private Long id; private String firstName; private String lastName; + private LocalDate dateOfBirth; public Long getId() { return id; @@ -40,4 +42,13 @@ public Criminal setLastName(String lastName) { return this; } + public LocalDate getDateOfBirth() { + return dateOfBirth; + } + + public Criminal setDateOfBirth(LocalDate dateOfBirth) { + this.dateOfBirth = dateOfBirth; + return this; + } + }