Skip to content

Commit 92c9217

Browse files
committed
next lec
1 parent c220e00 commit 92c9217

File tree

10 files changed

+131
-6
lines changed

10 files changed

+131
-6
lines changed

src/main/java/com/liveitcourses/firstproject/controller/MainApplication.java renamed to src/main/java/com/liveitcourses/firstproject/MainApplication.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
packagecom.liveitcourses.firstproject.controller;
1+
packagecom.liveitcourses.firstproject;
22

33
importlombok.extern.log4j.Log4j2;
44
importorg.springframework.boot.SpringApplication;

‎src/main/java/com/liveitcourses/firstproject/controller/HelloWorldBean.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
packagecom.liveitcourses.firstproject.controller;
22

3+
// model classes
4+
// pojo plain old java objects
5+
// DTO data transfer objects
6+
// domain object
7+
// Entities
8+
// DAO
39
publicclassHelloWorldBean{
410

511
privateStringmessage;

‎src/main/java/com/liveitcourses/firstproject/controller/UserController.java‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
importjava.util.List;
44

55
importcom.liveitcourses.firstproject.dto.User;
6+
importcom.liveitcourses.firstproject.exception.UserNotFoundException;
67
importcom.liveitcourses.firstproject.service.UserDaoService;
78
importorg.springframework.beans.factory.annotation.Autowired;
8-
importorg.springframework.web.bind.annotation.GetMapping;
9-
importorg.springframework.web.bind.annotation.PathVariable;
10-
importorg.springframework.web.bind.annotation.RestController;
9+
importorg.springframework.web.bind.annotation.*;
1110

1211
@RestController
1312
publicclassUserController{
@@ -25,4 +24,19 @@ public User retrieveUser(@PathVariable int id){
2524
returnservice.findOne(id);
2625
}
2726

27+
// input - details of user
28+
// output - CREATED & Return the created URI
29+
@PostMapping("/users")
30+
publicvoidcreateUser(@RequestBodyUseruser){
31+
UsersavedUser = service.save(user);
32+
}
33+
34+
@DeleteMapping("/users/{id}")
35+
publicvoiddeleteUser(@PathVariableintid){
36+
Useruser = service.deleteById(id);
37+
38+
if(user==null)
39+
thrownewUserNotFoundException("id-"+ id);
40+
}
41+
2842
}

‎src/main/java/com/liveitcourses/firstproject/dto/User.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
packagecom.liveitcourses.firstproject.dto;
22

3+
importorg.springframework.context.annotation.Bean;
4+
35
importjava.util.Date;
46

57
publicclassUser{
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
packagecom.liveitcourses.firstproject.exception;
2+
3+
importjava.util.Date;
4+
5+
importorg.springframework.http.HttpStatus;
6+
importorg.springframework.http.ResponseEntity;
7+
importorg.springframework.web.bind.annotation.ControllerAdvice;
8+
importorg.springframework.web.bind.annotation.ExceptionHandler;
9+
importorg.springframework.web.bind.annotation.RestController;
10+
importorg.springframework.web.context.request.WebRequest;
11+
importorg.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
12+
13+
14+
@ControllerAdvice
15+
@RestController
16+
publicclassCustomizedResponseEntityExceptionHandlerextendsResponseEntityExceptionHandler{
17+
18+
@ExceptionHandler(Exception.class)
19+
publicfinalResponseEntity<Object> handleAllExceptions(Exceptionex, WebRequestrequest){
20+
ExceptionResponseexceptionResponse = newExceptionResponse(newDate(), ex.getMessage(),
21+
request.getDescription(false));
22+
returnnewResponseEntity(exceptionResponse, HttpStatus.INTERNAL_SERVER_ERROR);
23+
}
24+
25+
@ExceptionHandler(UserNotFoundException.class)
26+
publicfinalResponseEntity<Object> handleUserNotFoundException(UserNotFoundExceptionex, WebRequestrequest){
27+
ExceptionResponseexceptionResponse = newExceptionResponse(newDate(), ex.getMessage(),
28+
request.getDescription(false));
29+
returnnewResponseEntity(exceptionResponse, HttpStatus.NOT_FOUND);
30+
}
31+
32+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
packagecom.liveitcourses.firstproject.exception;
2+
3+
importjava.util.Date;
4+
5+
publicclassExceptionResponse{
6+
privateDatetimestamp;
7+
privateStringmessage;
8+
privateStringdetails;
9+
10+
publicExceptionResponse(Datetimestamp, Stringmessage, Stringdetails){
11+
super();
12+
this.timestamp = timestamp;
13+
this.message = message;
14+
this.details = details;
15+
}
16+
17+
publicDategetTimestamp(){
18+
returntimestamp;
19+
}
20+
21+
publicStringgetMessage(){
22+
returnmessage;
23+
}
24+
25+
publicStringgetDetails(){
26+
returndetails;
27+
}
28+
29+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
packagecom.liveitcourses.firstproject.exception;
2+
3+
importorg.springframework.http.HttpStatus;
4+
importorg.springframework.web.bind.annotation.ResponseStatus;
5+
6+
@ResponseStatus(HttpStatus.NOT_FOUND)
7+
publicclassUserNotFoundExceptionextendsRuntimeException{
8+
publicUserNotFoundException(Stringmessage){
9+
super(message);
10+
}
11+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
packagecom.liveitcourses.firstproject.service;
2+
3+
importorg.springframework.stereotype.Component;
4+
importorg.springframework.stereotype.Service;
5+
6+
@Service
7+
publicclassPostService{
8+
9+
10+
//get method
11+
12+
// inserting record
13+
14+
// deletion
15+
16+
// updating
17+
18+
}

‎src/main/java/com/liveitcourses/firstproject/service/UserDaoService.java‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
importjava.util.ArrayList;
44
importjava.util.Date;
5+
importjava.util.Iterator;
56
importjava.util.List;
67

78
importcom.liveitcourses.firstproject.dto.User;
@@ -41,4 +42,16 @@ public User findOne(int id){
4142
returnnull;
4243
}
4344

45+
publicUserdeleteById(intid){
46+
Iterator<User> iterator = users.iterator();
47+
while (iterator.hasNext()){
48+
Useruser = iterator.next();
49+
if (user.getId() == id){
50+
iterator.remove();
51+
returnuser;
52+
}
53+
}
54+
returnnull;
55+
}
56+
4457
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#logging.level.org.springframework = debug
1+
logging.level.org.springframework = debug
22

3-
logging.level.org.springframework = info
3+
#logging.level.org.springframework = info
44
spring.messages.basename=messages

0 commit comments

Comments
(0)