Add hubid for user

This commit is contained in:
harish
2024-09-25 17:37:34 +05:30
parent d7e2098b33
commit f2a206991f
22 changed files with 818 additions and 1 deletions

View File

@@ -0,0 +1,100 @@
package net.gepafin.tendermanagement.web.rest.api;
import jakarta.servlet.http.HttpServletRequest;
import net.gepafin.tendermanagement.model.request.HubReq;
import net.gepafin.tendermanagement.model.response.HubResponseBean;
import net.gepafin.tendermanagement.model.util.Response;
import io.swagger.v3.oas.annotations.Operation;
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 io.swagger.v3.oas.annotations.Parameter;
import jakarta.validation.Valid;
import net.gepafin.tendermanagement.web.rest.api.errors.ErrorConstants;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Validated
@RequestMapping("/hub")
public interface HubApi {
@Operation(summary = "API to create a hub", 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) }))
})
@PreAuthorize("hasRole('ROLE_SUPER_ADMIN')")
@PostMapping(value = "", produces = "application/json")
ResponseEntity<Response<HubResponseBean>> createHub(HttpServletRequest request,
@Parameter(description = "Hub request object", required = true)
@Valid @RequestBody HubReq hubReq);
@Operation(summary = "API to update a hub", 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) }))
})
@PreAuthorize("hasRole('ROLE_SUPER_ADMIN')")
@PutMapping(value = "/{hubId}", produces = "application/json")
ResponseEntity<Response<HubResponseBean>> updateHub(HttpServletRequest request,
@Parameter(description = "The hub id", required = true)
@PathVariable("hubId") Long hubId,
@Parameter(description = "Hub request object", required = true)
@Valid @RequestBody HubReq hubReq);
@Operation(summary = "API to get a hub by 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) }))
})
@PreAuthorize("hasRole('ROLE_SUPER_ADMIN')")
@GetMapping(value = "/{hubId}", produces = "application/json")
ResponseEntity<Response<HubResponseBean>> getHubById(HttpServletRequest request,
@Parameter(description = "The hub id", required = true)
@PathVariable("hubId") Long hubId);
@Operation(summary = "API to get all hubs", 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) }))
})
@PreAuthorize("hasRole('ROLE_SUPER_ADMIN')")
@GetMapping(value = "", produces = "application/json")
ResponseEntity<Response<List<HubResponseBean>>> getAllHubs(HttpServletRequest request);
@Operation(summary = "API to delete a hub", 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) }))
})
@PreAuthorize("hasRole('ROLE_SUPER_ADMIN')")
@DeleteMapping(value = "/{hubId}")
ResponseEntity<Response<Void>> deleteHub(HttpServletRequest request,
@Parameter(description = "The hub id", required = true)
@PathVariable("hubId") Long hubId);
}

View File

@@ -21,6 +21,8 @@ import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Validated
public interface UserApi {
@@ -186,6 +188,36 @@ public interface UserApi {
@GetMapping(value = "/me",
produces = { "application/json" })
ResponseEntity<Response<UserResponseBean>> getValidUser(HttpServletRequest request);
@Operation(summary = "API to create user by hubId",
responses = {
@ApiResponse(responseCode = "200", description = "User created successfully"),
@ApiResponse(responseCode = "400", description = "Bad Request", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {
@ExampleObject(value = ErrorConstants.BADREQUEST_ERROR_EXAMPLE)})),
@ApiResponse(responseCode = "404", description = "Not Found", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {
@ExampleObject(value = ErrorConstants.NOTFOUND_ERROR_EXAMPLE)}))
})
@PreAuthorize("hasRole('ROLE_SUPER_ADMIN')")
@RequestMapping(value = "/hub/{hubId}",
produces = {"application/json"},
method = RequestMethod.POST)
ResponseEntity<Response<UserResponseBean>> createUserByHubId(
@Parameter(description = "The hubId", required = true) @PathVariable("hubId") String hubId,
@Parameter(description = "User request object for hubId", required = true) @Valid @RequestBody UserReq userReq);
@Operation(summary = "Api to get user by hubId",
responses = {
@ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "404", description = "User not found", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {
@ExampleObject(value = ErrorConstants.NOTFOUND_ERROR_EXAMPLE)})),
@ApiResponse(responseCode = "400", description = "Bad Request", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {
@ExampleObject(value = ErrorConstants.BADREQUEST_ERROR_EXAMPLE)}))
})
@PreAuthorize("hasRole('ROLE_SUPER_ADMIN')")
@RequestMapping(value = "/hub/{hubId}",
produces = {"application/json"},
method = RequestMethod.GET)
ResponseEntity<Response<List<UserResponseBean>>> getUserByHubId(
@Parameter(description = "The hubId", required = true) @PathVariable("hubId") String hubId);
}

View File

@@ -0,0 +1,80 @@
package net.gepafin.tendermanagement.web.rest.api.impl;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import net.gepafin.tendermanagement.config.Translator;
import net.gepafin.tendermanagement.constants.GepafinConstant;
import net.gepafin.tendermanagement.model.request.HubReq;
import net.gepafin.tendermanagement.model.response.HubResponseBean;
import net.gepafin.tendermanagement.model.util.Response;
import net.gepafin.tendermanagement.service.HubService;
import net.gepafin.tendermanagement.util.Validator;
import net.gepafin.tendermanagement.web.rest.api.HubApi;
import net.gepafin.tendermanagement.web.rest.api.errors.Status;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class HubApiController implements HubApi {
private final HubService hubService;
private final Validator validator;
public HubApiController(HubService hubService, Validator validator) {
this.hubService = hubService;
this.validator = validator;
}
@Override
public ResponseEntity<Response<HubResponseBean>> createHub(HttpServletRequest request, @Valid HubReq hubReq) {
validator.validateUser(request);
HubResponseBean hubResponse = hubService.createHub(hubReq);
return ResponseEntity.status(HttpStatus.CREATED)
.body(new Response<>(hubResponse, Status.SUCCESS, Translator.toLocale(GepafinConstant.HUB_CREATE_SUCCESS)));
}
@Override
public ResponseEntity<Response<HubResponseBean>> updateHub(HttpServletRequest request, Long hubId, @Valid HubReq hubReq) {
validator.validateUser(request);
HubResponseBean hubResponse = hubService.updateHub(hubId, hubReq);
if (hubResponse != null) {
return ResponseEntity.status(HttpStatus.OK)
.body(new Response<>(hubResponse, Status.SUCCESS, Translator.toLocale(GepafinConstant.HUB_UPDATE_SUCCESS)));
} else {
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.body(new Response<>(null, Status.NOT_FOUND, Translator.toLocale(GepafinConstant.HUB_NOT_FOUND)));
}
}
@Override
public ResponseEntity<Response<HubResponseBean>> getHubById(HttpServletRequest request, Long hubId) {
validator.validateUser(request);
HubResponseBean hubResponse = hubService.getHubById(hubId);
if (hubResponse != null) {
return ResponseEntity.status(HttpStatus.OK)
.body(new Response<>(hubResponse, Status.SUCCESS, Translator.toLocale(GepafinConstant.HUB_GET_SUCCESS)));
} else {
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.body(new Response<>(null, Status.NOT_FOUND, Translator.toLocale(GepafinConstant.HUB_NOT_FOUND)));
}
}
@Override
public ResponseEntity<Response<List<HubResponseBean>>> getAllHubs(HttpServletRequest request) {
validator.validateUser(request);
List<HubResponseBean> hubs = hubService.getAllHubs();
return ResponseEntity.status(HttpStatus.OK)
.body(new Response<>(hubs, Status.SUCCESS, Translator.toLocale(GepafinConstant.HUB_GET_ALL_SUCCESS)));
}
@Override
public ResponseEntity<Response<Void>> deleteHub(HttpServletRequest request, Long hubId) {
validator.validateUser(request);
hubService.deleteHub(hubId);
return ResponseEntity.status(HttpStatus.OK)
.body(new Response<>(null, Status.SUCCESS, Translator.toLocale(GepafinConstant.HUB_DELETE_SUCCESS)));
}
}

View File

@@ -21,6 +21,8 @@ import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("${openapi.gepafin.base-path:/v1/user}")
@@ -124,4 +126,20 @@ public class UserApiController implements UserApi {
.body(new Response<>(user, Status.SUCCESS, Translator.toLocale(GepafinConstant.GET_USER_SUCCESS_MSG)));
}
@Override
public ResponseEntity<Response<UserResponseBean>> createUserByHubId(
@PathVariable("hubId") String hubId,
@Valid @RequestBody UserReq userReq) {
log.info("Create User by Hub ID - Hub ID: {}, Request Body: {}", hubId, userReq);
UserResponseBean createdUser = userService.createUserByHubId(hubId, userReq);
return ResponseEntity.status(HttpStatus.CREATED)
.body(new Response<>(createdUser, Status.SUCCESS, Translator.toLocale(GepafinConstant.USER_CREATED_SUCCESS_MSG)));
}
@Override
public ResponseEntity<Response<List<UserResponseBean>>> getUserByHubId(String hubId) {
log.info("Get User by Hub ID - Hub ID: {}", hubId);
List<UserResponseBean> user = userService.getUserByHubId(hubId);
return ResponseEntity.status(HttpStatus.OK)
.body(new Response<>(user, Status.SUCCESS, Translator.toLocale(GepafinConstant.GET_USER_SUCCESS_MSG)));
}
}