Created crud opration for company api's

This commit is contained in:
rajesh
2024-09-28 21:40:14 +05:30
parent ee80959329
commit d45fce59ec
26 changed files with 884 additions and 29 deletions

View File

@@ -0,0 +1,97 @@
package net.gepafin.tendermanagement.web.rest.api;
import java.util.List;
import java.util.Map;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.ExampleObject;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import jakarta.servlet.http.HttpServletRequest;
import net.gepafin.tendermanagement.model.request.CompanyRequest;
import net.gepafin.tendermanagement.model.response.CompanyResponse;
import net.gepafin.tendermanagement.model.util.Response;
import net.gepafin.tendermanagement.web.rest.api.errors.ErrorConstants;
public interface CompanyApi {
@Operation(summary = "Api to create company", responses = { @ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "404", description = "Not Found", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {
@ExampleObject(value = ErrorConstants.NOTFOUND_ERROR_EXAMPLE) })),
@ApiResponse(responseCode = "401", description = "Unauthorized", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {
@ExampleObject(value = ErrorConstants.UNAUTHORIZED_ERROR_EXAMPLE) })),
@ApiResponse(responseCode = "400", description = "Bad Request", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {
@ExampleObject(value = ErrorConstants.BADREQUEST_ERROR_EXAMPLE) })) })
@PostMapping(value = "", produces = { "application/json" })
ResponseEntity<Response<CompanyResponse>> createCompany(HttpServletRequest request,
@Parameter(description = "Company request object", required = true) @RequestBody CompanyRequest companyRequest);
@Operation(summary = "Api to update company", responses = { @ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "404", description = "Not Found", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {
@ExampleObject(value = ErrorConstants.NOTFOUND_ERROR_EXAMPLE) })),
@ApiResponse(responseCode = "401", description = "Unauthorized", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {
@ExampleObject(value = ErrorConstants.UNAUTHORIZED_ERROR_EXAMPLE) })),
@ApiResponse(responseCode = "400", description = "Bad Request", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {
@ExampleObject(value = ErrorConstants.BADREQUEST_ERROR_EXAMPLE) })) })
@PutMapping(value = "/{companyId}", produces = { "application/json" })
ResponseEntity<Response<CompanyResponse>> updateCompany(HttpServletRequest request,
@Parameter(description = "The company id", required = true) @PathVariable("companyId") Long companyId,
@Parameter(description = "Company request object", required = true) @RequestBody CompanyRequest companyRequest);
@Operation(summary = "Api to delete company", responses = { @ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "404", description = "Not Found", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {
@ExampleObject(value = ErrorConstants.NOTFOUND_ERROR_EXAMPLE) })),
@ApiResponse(responseCode = "401", description = "Unauthorized", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {
@ExampleObject(value = ErrorConstants.UNAUTHORIZED_ERROR_EXAMPLE) })),
@ApiResponse(responseCode = "400", description = "Bad Request", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {
@ExampleObject(value = ErrorConstants.BADREQUEST_ERROR_EXAMPLE) })) })
@DeleteMapping(value = "/{companyId}", produces = { "application/json" })
ResponseEntity<Response<Void>> deleteCompany(HttpServletRequest request,
@Parameter(description = "The company id", required = true) @PathVariable("companyId") Long companyId);
@Operation(summary = "Api to get company", responses = { @ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "404", description = "Not Found", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {
@ExampleObject(value = ErrorConstants.NOTFOUND_ERROR_EXAMPLE) })),
@ApiResponse(responseCode = "401", description = "Unauthorized", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {
@ExampleObject(value = ErrorConstants.UNAUTHORIZED_ERROR_EXAMPLE) })),
@ApiResponse(responseCode = "400", description = "Bad Request", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {
@ExampleObject(value = ErrorConstants.BADREQUEST_ERROR_EXAMPLE) })) })
@GetMapping(value = "/{companyId}", produces = { "application/json" })
ResponseEntity<Response<CompanyResponse>> getCompany(HttpServletRequest request,
@Parameter(description = "The company id", required = true) @PathVariable("companyId") Long companyId);
@Operation(summary = "Api to get company by user Id", responses = {
@ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "404", description = "Not Found", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {
@ExampleObject(value = ErrorConstants.NOTFOUND_ERROR_EXAMPLE) })),
@ApiResponse(responseCode = "401", description = "Unauthorized", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {
@ExampleObject(value = ErrorConstants.UNAUTHORIZED_ERROR_EXAMPLE) })),
@ApiResponse(responseCode = "400", description = "Bad Request", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {
@ExampleObject(value = ErrorConstants.BADREQUEST_ERROR_EXAMPLE) })) })
@GetMapping(value = "/user/{userId}", produces = { "application/json" })
ResponseEntity<Response<List<CompanyResponse>>> getCompanyByUserId(HttpServletRequest request,
@Parameter(description = "The company id", required = true) @PathVariable("userId") Long userId);
@Operation(summary = "Api to check vatNumber", responses = { @ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "404", description = "Not Found", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {
@ExampleObject(value = ErrorConstants.NOTFOUND_ERROR_EXAMPLE) })),
@ApiResponse(responseCode = "401", description = "Unauthorized", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {
@ExampleObject(value = ErrorConstants.UNAUTHORIZED_ERROR_EXAMPLE) })),
@ApiResponse(responseCode = "400", description = "Bad Request", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {
@ExampleObject(value = ErrorConstants.BADREQUEST_ERROR_EXAMPLE) })) })
@GetMapping(value = "/vatNumber", produces = { "application/json" })
ResponseEntity<Response<Map<String,Object>>> checkVatNumber(HttpServletRequest request,
@Parameter(description = "The vatNumber of company", required = true) @RequestParam("vatNumber") String vatNumber);
}

View File

@@ -0,0 +1,22 @@
package net.gepafin.tendermanagement.web.rest.api.errors;
import org.springframework.http.HttpStatus;
import feign.FeignException;
public class FeignClientForbiddenException extends FeignException{
private static final long serialVersionUID = 1L;
private final HttpStatus status;
public FeignClientForbiddenException(HttpStatus status,String message) {
super(403,message);
this.status = status;
}
public HttpStatus getStatus() {
return status;
}
}

View File

@@ -0,0 +1,22 @@
package net.gepafin.tendermanagement.web.rest.api.errors;
import org.springframework.http.HttpStatus;
import feign.FeignException;
public class FeignClientNotFoundException extends FeignException{
private static final long serialVersionUID = 1L;
private final HttpStatus status;
public FeignClientNotFoundException(HttpStatus status,String message) {
super(404,message);
this.status = status;
}
public HttpStatus getStatus() {
return status;
}
}

View File

@@ -0,0 +1,22 @@
package net.gepafin.tendermanagement.web.rest.api.errors;
import org.springframework.http.HttpStatus;
import feign.FeignException;
public class FeignClientUnauthorizedException extends FeignException{
private static final long serialVersionUID = 1L;
private final HttpStatus status;
public FeignClientUnauthorizedException(HttpStatus status,String message) {
super(401,message);
this.status = status;
}
public HttpStatus getStatus() {
return status;
}
}

View File

@@ -0,0 +1,22 @@
package net.gepafin.tendermanagement.web.rest.api.errors;
import org.springframework.http.HttpStatus;
import feign.FeignException;
public class FeignClientValidationException extends FeignException{
private static final long serialVersionUID = 1L;
private final HttpStatus status;
public FeignClientValidationException(HttpStatus status,String message) {
super(400,message);
this.status = status;
}
public HttpStatus getStatus() {
return status;
}
}

View File

@@ -2,9 +2,11 @@ package net.gepafin.tendermanagement.web.rest.api.errors;
import net.gepafin.tendermanagement.config.Translator;
import net.gepafin.tendermanagement.model.util.Response;
import net.gepafin.tendermanagement.util.Utils;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -14,6 +16,9 @@ import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import feign.FeignException;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authorization.AuthorizationDeniedException;
@@ -122,4 +127,54 @@ public class GlobalExceptionHandler {
}
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
@ExceptionHandler(FeignClientValidationException.class)
@ResponseBody
public Map<String, Object> handleFeignClientBadRequestException(final Throwable ex) {
log.error(ex.getMessage());
log.error(ex.getLocalizedMessage(), ex);
String exceptionString = ex.getMessage().substring(ex.getMessage().indexOf("]: [") + 4, ex.getMessage().length() - 1);
return Utils.convertIntoJson(exceptionString);
}
@ResponseStatus(value = HttpStatus.FORBIDDEN)
@ExceptionHandler(FeignClientForbiddenException.class)
@ResponseBody
public Map<String, Object> handleFeignClientForbiddenException(final Throwable ex) {
log.error(ex.getMessage());
log.error(ex.getLocalizedMessage(), ex);
String exceptionString = ex.getMessage().substring(ex.getMessage().indexOf("]: [") + 4, ex.getMessage().length() - 1);
return Utils.convertIntoJson(exceptionString);
}
@ResponseStatus(value = HttpStatus.UNAUTHORIZED)
@ExceptionHandler(FeignClientUnauthorizedException.class)
@ResponseBody
public Map<String, Object> handleFeignClientUnauthorizedException(final Throwable ex) {
log.error(ex.getMessage());
log.error(ex.getLocalizedMessage(), ex);
String exceptionString = ex.getMessage().substring(ex.getMessage().indexOf("]: [") + 4, ex.getMessage().length() - 1);
return Utils.convertIntoJson(exceptionString);
}
@ResponseStatus(value = HttpStatus.NOT_FOUND)
@ExceptionHandler(FeignClientNotFoundException.class)
@ResponseBody
public Map<String, Object> handleFeignClientNotFoundException(final Throwable ex) {
log.error(ex.getMessage());
log.error(ex.getLocalizedMessage(), ex);
String exceptionString = ex.getMessage().substring(ex.getMessage().indexOf("]: [") + 4, ex.getMessage().length() - 1);
return Utils.convertIntoJson(exceptionString);
}
@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(FeignException.class)
@ResponseBody
public Map<String, Object> handleFeignException(final Throwable ex) {
log.error(ex.getMessage());
log.error(ex.getLocalizedMessage(), ex);
String exceptionString = ex.getMessage().substring(ex.getMessage().indexOf("]: [") + 4, ex.getMessage().length() - 1);
return Utils.convertIntoJson(exceptionString);
}
}

View File

@@ -0,0 +1,89 @@
package net.gepafin.tendermanagement.web.rest.api.impl;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import jakarta.servlet.http.HttpServletRequest;
import net.gepafin.tendermanagement.config.Translator;
import net.gepafin.tendermanagement.constants.GepafinConstant;
import net.gepafin.tendermanagement.model.request.CompanyRequest;
import net.gepafin.tendermanagement.model.response.CompanyResponse;
import net.gepafin.tendermanagement.model.util.Response;
import net.gepafin.tendermanagement.service.CompanyService;
import net.gepafin.tendermanagement.web.rest.api.CompanyApi;
import net.gepafin.tendermanagement.web.rest.api.errors.Status;
@RestController
@RequestMapping("${openapi.gepafin.base-path:/v1/company}")
public class CompanyApiController implements CompanyApi{
private final Logger log = LoggerFactory.getLogger(CompanyApiController.class);
@Autowired
private CompanyService companyService;
@Override
public ResponseEntity<Response<CompanyResponse>> createCompany(HttpServletRequest request,
CompanyRequest companyRequest) {
log.info("Create company with - Request Body: {}", companyRequest);
CompanyResponse data = companyService.createCompany(request, companyRequest);
return ResponseEntity.status(HttpStatus.CREATED)
.body(new Response<>(data, Status.SUCCESS, Translator.toLocale(GepafinConstant.COMPANY_CREATED_SUCCESS_MSG)));
}
@Override
public ResponseEntity<Response<CompanyResponse>> updateCompany(HttpServletRequest request, Long companyId,
CompanyRequest companyRequest) {
log.info("Update company with - Request Body: {}", companyRequest);
CompanyResponse data = companyService.updateCompany(request, companyId, companyRequest);
return ResponseEntity.status(HttpStatus.OK)
.body(new Response<>(data, Status.SUCCESS, Translator.toLocale(GepafinConstant.COMPANY_UPDATED_SUCCESS_MSG)));
}
@Override
public ResponseEntity<Response<CompanyResponse>> getCompany(HttpServletRequest request, Long companyId) {
log.info("Get company with id: {}", companyId);
CompanyResponse data = companyService.getCompany(request, companyId);
return ResponseEntity.status(HttpStatus.OK)
.body(new Response<>(data, Status.SUCCESS, Translator.toLocale(GepafinConstant.COMPANY_GET_SUCCESS_MSG)));
}
@Override
public ResponseEntity<Response<Void>> deleteCompany(HttpServletRequest request, Long companyId) {
log.info("Delete company with id: {}", companyId);
companyService.deleteCompany(request, companyId);
return ResponseEntity.status(HttpStatus.OK)
.body(new Response<>(null, Status.SUCCESS, Translator.toLocale(GepafinConstant.COMPANY_DELETE_SUCCESS_MSG)));
}
@Override
public ResponseEntity<Response<List<CompanyResponse>>> getCompanyByUserId(HttpServletRequest request, Long userId) {
log.info("Get company with userId: {}", userId);
List<CompanyResponse> data = companyService.getCompanyByUserId(request, userId);
return ResponseEntity.status(HttpStatus.OK)
.body(new Response<>(data, Status.SUCCESS, Translator.toLocale(GepafinConstant.COMPANY_GET_SUCCESS_MSG)));
}
@Override
public ResponseEntity<Response<Map<String,Object>>> checkVatNumber(HttpServletRequest request, String vatNumber) {
log.info("check VatNumber with: {}", vatNumber);
Map<String,Object> data = companyService.checkVatNumber(request, vatNumber);
return ResponseEntity.status(HttpStatus.OK)
.body(new Response<>(data, Status.SUCCESS, Translator.toLocale(GepafinConstant.CHECK_VATNUMBER_SUCCESS_MSG)));
}
}