Skip to content
Open
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
Empty file added fileLogger
Empty file.
Empty file added fileLogger.1
Empty file.
Empty file added fileLogger.1.lck
Empty file.
Empty file added fileLogger.lck
Empty file.
14 changes: 14 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package com.ausy_technologies.demospring.Controller;

import com.ausy_technologies.demospring.Exceptions.ErrorResponse;
import com.ausy_technologies.demospring.Model.DAO.Role;
import com.ausy_technologies.demospring.Model.DAO.User;
import com.ausy_technologies.demospring.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
Expand All @@ -18,59 +22,154 @@ public class UserController {


@PostMapping("/addRole")
public Role saveRole(@RequestBody Role role) {


public ResponseEntity<Role> saveRole(@RequestBody Role role) {
Role roleAdded = this.userService.saveRole(role);
return roleAdded;
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("Response ","saveRole");
return ResponseEntity.status(HttpStatus.CREATED).headers(httpHeaders).body(roleAdded);
}




@PostMapping("/addUser")
public User saveUser(@RequestBody User user) {
public ResponseEntity<User> saveUser(@RequestBody User user) {
User userAdded = this.userService.saveUser(user);
return userAdded;
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("Response","saveUser");
return ResponseEntity.status(HttpStatus.ACCEPTED).headers(httpHeaders).body(userAdded);
}

@PostMapping("/addUser2/{idRole}")
public User saveUser2(@RequestBody User user, @PathVariable int idRole)
public ResponseEntity<User> saveUser2(@RequestBody User user, @PathVariable int idRole)
{
return this.userService.saveUser2(user,idRole);
User usedAdded = userService.saveUser2(user,idRole);
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("Response","saveUser2");
return ResponseEntity.status(HttpStatus.OK).headers(httpHeaders).body(usedAdded);

}

@PostMapping("/addUser3/{roleList}")
public User saveUser3(@RequestBody User user , @PathVariable List<Role> roleList)
public ResponseEntity<User> saveUser3(@RequestBody User user , @PathVariable List<Role> roleList)
{
return this.userService.saveUser3(user,roleList);
User userAdded = userService.saveUser3(user,roleList);
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("Response","saveUser3");
return ResponseEntity.status(HttpStatus.CREATED).headers(httpHeaders).body(userAdded);
}

@GetMapping("/findRoleBy/{id}")
public Role findRoleById(@PathVariable int id)
@GetMapping("/getRole/{id}")
public ResponseEntity<Role> findRoleById(@PathVariable int id)
{
return this.userService.findRoleById(id);
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("Response","findRoleById");
Role roleSearched = null;
try {
roleSearched = this.userService.findRoleById(id);
}catch (ErrorResponse e){
ErrorResponse.LogError(e);
return ResponseEntity.status(HttpStatus.NOT_FOUND).headers(httpHeaders).body(null);
}

return ResponseEntity.status(HttpStatus.FOUND).headers(httpHeaders).body(roleSearched);
}

@GetMapping("/findAllRoles")
public List<Role> findAllRoles()
public ResponseEntity<List<Role>> findAllRoles()
{
return userService.findAllRoles();
List<Role> allRoles = userService.findAllRoles();
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("Response","findAllRoles");
if(allRoles.size() == 0 ){
return ResponseEntity.status(HttpStatus.NO_CONTENT).headers(httpHeaders).body(null);
}
return ResponseEntity.status(HttpStatus.FOUND).headers(httpHeaders).body(allRoles);
}


@GetMapping("/allUsers")
public List<User> findAllUsers()
public ResponseEntity<List<User>> findAllUsers()
{
return this.userService.findAllUsers();
List<User> allUsers = this.userService.findAllUsers();
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("Response","findAllUsers");
if(allUsers.size() == 0 ){
return ResponseEntity.status(HttpStatus.NO_CONTENT).headers(httpHeaders).body(null);
}
return ResponseEntity.status(HttpStatus.FOUND).headers(httpHeaders).body(allUsers);
}

@DeleteMapping("/deleteUserById/{id}")
@DeleteMapping("/deleteUser/{id}")
public void deleteUser(@PathVariable int id)
{
this.userService.deleteUserById(id);

}

@PutMapping("/updateUser")
public ResponseEntity<User> updateUser(@RequestParam int id, @RequestBody User user){
User updatedUser = null;
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("Response","updateUser");
try{
updatedUser = userService.updateUser(id,user);

}catch (ErrorResponse errorResponse){
ErrorResponse.LogError(errorResponse);
return ResponseEntity.status(HttpStatus.NOT_FOUND).headers(httpHeaders).body(null);
}

return ResponseEntity.status(HttpStatus.RESET_CONTENT).headers(httpHeaders).body(updatedUser);
}

@PutMapping("/updateUser2/{roleList}")
public ResponseEntity<User> updateUser2(@RequestParam int id , @PathVariable List<Role> roleList){
User updatedUser = null ;
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("Response","updateUser");
try{
updatedUser = userService.updateUser2(id,roleList);
}catch (ErrorResponse e){
ErrorResponse.LogError(e);
return ResponseEntity.status(HttpStatus.NOT_FOUND).headers(httpHeaders).body(null);
}

return ResponseEntity.status(HttpStatus.RESET_CONTENT).headers(httpHeaders).body(updatedUser);
}

@PutMapping("/updateRole/{name}")
public ResponseEntity<Role> updateRole(@PathVariable String name, @RequestParam int id){
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("Response","updateRole");
Role updatedRole = null;
try {
updatedRole = userService.updateRole(id,name);
}catch (ErrorResponse e){
ErrorResponse.LogError(e);
return ResponseEntity.status(HttpStatus.NOT_FOUND).headers(httpHeaders).body(null);
}

return ResponseEntity.status(HttpStatus.RESET_CONTENT).headers(httpHeaders).body(updatedRole);
}

@DeleteMapping("deleteRole/{id}")
public void deleteRolebyId(@PathVariable int id){
userService.deleteRolebyId(id);
}

@GetMapping("getUser/{id}")
public ResponseEntity<User> findUserById(@PathVariable int id){
User searchedUser = null ;
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("Response","getuser");
try{
searchedUser = userService.findUserById(id);
}catch (ErrorResponse e){
ErrorResponse.LogError(e);
return ResponseEntity.status(HttpStatus.NOT_FOUND).headers(httpHeaders).body(null);
}
return ResponseEntity.status(HttpStatus.FOUND).headers(httpHeaders).body(searchedUser);
}

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package com.ausy_technologies.demospring;

import com.ausy_technologies.demospring.Exceptions.ErrorResponse;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.logging.Logger;

@SpringBootApplication
public class DemospringApplication {

public static final Logger lgr = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
public static void main(String[] args) {
ErrorResponse.setupLogger();
SpringApplication.run(DemospringApplication.class, args);

}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.ausy_technologies.demospring.Exceptions;

import com.fasterxml.jackson.annotation.JsonFormat;
import org.springframework.http.HttpStatus;

import java.time.LocalDateTime;
import java.util.List;

public class ApiError {
private HttpStatus status;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy hh:mm:ss")
private LocalDateTime timestamp;
private String message;
private String debugMessage;
private List<ApiSubError> subErrors;

private ApiError() {
timestamp = LocalDateTime.now();
}

ApiError(HttpStatus status) {
this();
this.status = status;
}

ApiError(HttpStatus status, Throwable ex) {
this();
this.status = status;
this.message = "Unexpected error";
this.debugMessage = ex.getLocalizedMessage();
}

ApiError(HttpStatus status, String message, Throwable ex) {
this();
this.status = status;
this.message = message;
this.debugMessage = ex.getLocalizedMessage();
}

public HttpStatus getStatus() {
return status;
}

public void setStatus(HttpStatus status) {
this.status = status;
}

public void setTimestamp(LocalDateTime timestamp) {
this.timestamp = timestamp;
}

public void setMessage(String message) {
this.message = message;
}

public void setDebugMessage(String debugMessage) {
this.debugMessage = debugMessage;
}

public void setSubErrors(List<ApiSubError> subErrors) {
this.subErrors = subErrors;
}

public LocalDateTime getTimestamp() {
return timestamp;
}

public String getMessage() {
return message;
}

public String getDebugMessage() {
return debugMessage;
}

public List<ApiSubError> getSubErrors() {
return subErrors;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.ausy_technologies.demospring.Exceptions;

public abstract class ApiSubError {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.ausy_technologies.demospring.Exceptions;


import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;

@Data
@EqualsAndHashCode(callSuper = false)
@AllArgsConstructor
public class ApiValidationError extends ApiSubError {
private String object;
private String field;
private Object rejectedValue;
private String message;

ApiValidationError(String object, String message) {
this.object = object;
this.message = message;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.ausy_technologies.demospring.Exceptions;

import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ErrorResponse extends RuntimeException {
public static final Logger lgr = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
private static boolean isDebug = java.lang.management.ManagementFactory.getRuntimeMXBean().
getInputArguments().toString().contains("jdwp");

private String errorMessage;
private int errorId;
public ErrorResponse(){

}

public ErrorResponse(String errorMessage, int errorId) {
super(errorMessage);
this.errorMessage = errorMessage;
this.errorId = errorId;
}

public ErrorResponse(Throwable cause, String errorMessage, int errorId) {
super(errorMessage, cause);
this.errorMessage = errorMessage;
this.errorId = errorId;
}
public static void setupLogger() {
lgr.setLevel(Level.FINE);

try {

FileHandler fh = new FileHandler("fileLogger");
lgr.addHandler(fh);
fh.setLevel(Level.FINE);
}
catch (IOException e){
lgr.log(Level.SEVERE,"The logger file was not create");
}
}


public static void LogError(ErrorResponse e){
if(isDebug) {
e.printStackTrace();
}
else {
lgr.log(Level.SEVERE,e.getErrorMessage());
}

}

public String getErrorMessage() {
return errorMessage;
}

public int getErrorId() {
return errorId;
}
}
Loading