Created
September 13, 2022 03:12
-
-
Save vnkdj5/27744e5ecd90a7648bf3d8563aff26f4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.gyansagar.services; | |
import com.gyansagar.exceptions.DuplicateEntryException; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.stereotype.Service; | |
import com.gyansagar.entities.Buyer; | |
import com.gyansagar.repository.BuyerRepository; | |
@Service | |
public class BuyerService | |
{ | |
@Autowired | |
BuyerRepository brepo; | |
public Buyer add(Buyer b) throws DuplicateEntryException { | |
try{ | |
return brepo.save(b); | |
}catch (Exception e){ | |
throw new DuplicateEntryException("Record already exist"); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.gyansagar.exceptions; | |
public class DuplicateEntryException extends Exception { | |
public DuplicateEntryException() { | |
super("Resource already exists."); | |
} | |
public DuplicateEntryException(String message) { | |
super(message); | |
} | |
public DuplicateEntryException(String message, Throwable cause) { | |
super(message, cause); | |
} | |
public DuplicateEntryException(Throwable cause) { | |
super(cause); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.gyansagar.exceptions; | |
import com.gyansagar.dto.ErrorDto; | |
import org.springframework.http.HttpHeaders; | |
import org.springframework.http.HttpStatus; | |
import org.springframework.http.ResponseEntity; | |
import org.springframework.validation.FieldError; | |
import org.springframework.web.bind.MethodArgumentNotValidException; | |
import org.springframework.web.bind.annotation.ControllerAdvice; | |
import org.springframework.web.bind.annotation.ExceptionHandler; | |
import org.springframework.web.bind.annotation.RestController; | |
import org.springframework.web.context.request.WebRequest; | |
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; | |
import java.util.HashMap; | |
import java.util.Map; | |
@ControllerAdvice | |
@RestController | |
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler { | |
@ExceptionHandler({NotFoundException.class}) | |
public final ResponseEntity handleNotFountExceptions(Exception ex, WebRequest request) { | |
ErrorDto response = new ErrorDto(ex.getMessage()); | |
return new ResponseEntity(response, HttpStatus.NOT_FOUND); | |
} | |
@ExceptionHandler(DuplicateEntryException.class) | |
public final ResponseEntity handleNotFountExceptions1(Exception ex, WebRequest request) { | |
ErrorDto response = new ErrorDto(ex.getMessage()); | |
return new ResponseEntity(response, HttpStatus.NOT_FOUND); | |
} | |
@Override | |
protected ResponseEntity<Object> handleMethodArgumentNotValid( | |
MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status, WebRequest request) { | |
Map<String, String> errors = new HashMap<>(); | |
ex.getBindingResult().getAllErrors().forEach((error) -> { | |
String fieldName = ((FieldError) error).getField(); | |
String errorMessage = error.getDefaultMessage(); | |
errors.put(fieldName, errorMessage); | |
}); | |
ErrorDto response = new ErrorDto(ex.getMessage()); | |
return handleExceptionInternal(ex, response, headers, status, request); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.gyansagar.controllers; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.web.bind.annotation.CrossOrigin; | |
import org.springframework.web.bind.annotation.PostMapping; | |
import org.springframework.web.bind.annotation.RequestBody; | |
import org.springframework.web.bind.annotation.RestController; | |
import com.gyansagar.dto.ErrorDto; | |
import com.gyansagar.entities.Login; | |
import com.gyansagar.exceptions.NotFoundException; | |
import com.gyansagar.services.LoginService; | |
@CrossOrigin(origins = "http://localhost:3000") | |
@RestController | |
public class LoginController | |
{ | |
@Autowired | |
LoginService lservice; | |
@PostMapping("/logincheck") | |
public Object checkLogin(@RequestBody Login l) throws NotFoundException { | |
return lservice.checkLogin(l.getUsername(), l.getPassword()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment