Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Criminal #25

Merged
merged 5 commits into from
Mar 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<Criminal> create(@RequestBody Criminal criminal) {
Criminal createdCriminal = criminalService.create(criminal);
return new ResponseEntity<>(createdCriminal, HttpStatus.CREATED);
}

@DeleteMapping("{id}")
public ResponseEntity<Void> delete(@PathVariable Long id) {
criminalService.delete(id);
return new ResponseEntity<>(HttpStatus.OK);
}

@GetMapping("{id}")
public ResponseEntity<Optional<Criminal>> findById(@PathVariable Long id) {
Optional<Criminal> foundCriminal = criminalService.findById(id);
return new ResponseEntity<>(foundCriminal, HttpStatus.OK);
}

@GetMapping
public ResponseEntity<Iterable<Criminal>> findAll() {
Iterable<Criminal> criminals = criminalService.findAll();
return new ResponseEntity<>(criminals, HttpStatus.OK);
}

}
54 changes: 54 additions & 0 deletions src/main/java/se/iths/crimedatabase/entity/Criminal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package se.iths.crimedatabase.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.time.LocalDate;

@Entity
public class Criminal {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String firstName;
private String lastName;
private LocalDate dateOfBirth;

helenahalldiniths marked this conversation as resolved.
Show resolved Hide resolved
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;
}

public LocalDate getDateOfBirth() {
return dateOfBirth;
}

public Criminal setDateOfBirth(LocalDate dateOfBirth) {
this.dateOfBirth = dateOfBirth;
return this;
}

}
Original file line number Diff line number Diff line change
@@ -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<Criminal, Long> {
}
35 changes: 35 additions & 0 deletions src/main/java/se/iths/crimedatabase/service/CriminalService.java
Original file line number Diff line number Diff line change
@@ -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<Criminal> findById(Long id) {
return criminalRepository.findById(id);
}

public Iterable<Criminal> findAll() {
return criminalRepository.findAll();
}

}