Commit 84039c52 authored by dbhuller's avatar dbhuller

adds exception package, student address controller and student address service

parent 4c2305c0
......@@ -16,6 +16,10 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
runtimeOnly 'com.h2database:h2'
testImplementation 'com.h2database:h2'
}
test {
......
package service.student.studentaddressservice.controller;
import org.apache.coyote.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import service.student.studentaddressservice.exception.ResourceNotFoundException;
import service.student.studentaddressservice.model.StudentAddress;
import service.student.studentaddressservice.service.StudentAddressService;
@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/api/v1")
public class StudentAddressController {
@Autowired
StudentAddressService studentAddressService;
@GetMapping("/studentaddress/{email}")
public ResponseEntity<StudentAddress> getStudentAddress(@PathVariable("email") String email) throws
ResourceNotFoundException {
StudentAddress studentAddress =
studentAddressService.getStudentAddressByEmail(email).orElseThrow(() -> new ResourceNotFoundException("Email not found: " + email));
return ResponseEntity.ok(studentAddress);
}
}
package service.student.studentaddressservice.exception;
import java.util.Date;
public class ErrorDetails {
private Date timeStamp;
private String message;
private String details;
public ErrorDetails(Date timeStamp, String message, String details) {
this.timeStamp = timeStamp;
this.message = message;
this.details = details;
}
public Date getTimeStamp() {
return timeStamp;
}
public void setTimeStamp(Date timeStamp) {
this.timeStamp = timeStamp;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getDetails() {
return details;
}
public void setDetails(String details) {
this.details = details;
}
}
package service.student.studentaddressservice.exception;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
import java.util.Date;
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
public ResponseEntity<?> resourceNotFoundException(ResourceNotFoundException e, WebRequest webRequest) {
ErrorDetails errorDetails = new ErrorDetails(new Date(), e.getMessage(), webRequest.getDescription(false));
return new ResponseEntity<>(errorDetails, HttpStatus.NOT_FOUND);
}
}
package service.student.studentaddressservice.exception;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(value = HttpStatus.NOT_FOUND)
public class ResourceNotFoundException extends Exception {
public ResourceNotFoundException(String message) {
super(message);
}
}
......@@ -10,7 +10,7 @@ public class StudentAddress {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Id
// @Id
private String email;
private String state;
......
......@@ -6,6 +6,7 @@ import org.springframework.data.domain.Example;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import service.student.studentaddressservice.StudentAddressServiceApplication;
import service.student.studentaddressservice.exception.ResourceNotFoundException;
import service.student.studentaddressservice.model.StudentAddress;
import service.student.studentaddressservice.repository.StudentAddressRepository;
......@@ -29,9 +30,10 @@ public class StudentAddressService {
return studentAddressRepository.save(studentAddressDetails);
}
public StudentAddress updateStudentAddress(StudentAddress studentAddressDetails, String email) {
public StudentAddress updateStudentAddress(StudentAddress studentAddressDetails, String email)
throws ResourceNotFoundException {
StudentAddress studentAddressToUpdate =
getStudentAddressByEmail(email).orElseThrow(() -> new NullPointerException("Email not found: " + email)); //TEMPORARY FIX
getStudentAddressByEmail(email).orElseThrow(() -> new ResourceNotFoundException("Email Not found: " + email));
studentAddressToUpdate.setStreetAddress(studentAddressDetails.getStreetAddress());
studentAddressToUpdate.setState(studentAddressDetails.getState());
studentAddressToUpdate.setCity(studentAddressDetails.getCity());
......
DROP TABLE IF EXISTS ADDRESS;
CREATE TABLE ADDRESS (
ID INT AUTO_INCREMENT PRIMARY KEY,
EMAIL VARCHAR (255) PRIMARY KEY,
ID INT AUTO_INCREMENT NOT NULL PRIMARY KEY,
EMAIL VARCHAR (255) NOT NULL,
STREET_ADDRESS VARCHAR (255) NOT NULL,
STATE VARCHAR (255) NOT NULL,
CITY VARCHAR (255) NOT NULL,
......@@ -10,6 +10,6 @@ CREATE TABLE ADDRESS (
APT_NUM VARCHAR (255)
);
INSERT INTO ADDRESS VALUES (1, 'faker1@fakemail.com', '867 Fake Ln', 'CA', 'Fremont', '90210'),
(2, 'faker2@fakemail.com', '868 Fake Ln', 'CA', 'Fremont', '90210'),
(3, 'faker3@fakemail.com', '869 Fake Ln', 'CA', 'Fremont', '90210');
\ No newline at end of file
INSERT INTO ADDRESS VALUES (1, 'faker1@fakemail.com', '867 Fake Ln', 'CA', 'Fremont', '90210', NULL),
(2, 'faker2@fakemail.com', '868 Fake Ln', 'CA', 'Fremont', '90210', NULL),
(3, 'faker3@fakemail.com', '869 Fake Ln', 'CA', 'Fremont', '90210', NULL);
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment