/
Ozzus
/
SciTech
Обзор
Документация
Войти
/
Ozzus
/
SciTech
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/main/java/org/example/scitech/controller/UniversalController.java
330 строк
16 KB
Ozzus
добавлены CRUD,тесты написаны,swagger работает,НАЙК!
05 апр 2025, 15:14
05 апр 2025, 15:14
67d8148
Код
Авторство
О чём код?
package org.example.scitech.controller; 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.Schema; import io.swagger.v3.oas.annotations.responses.ApiResponse; import io.swagger.v3.oas.annotations.responses.ApiResponses; import io.swagger.v3.oas.annotations.tags.Tag; import org.example.scitech.models.Client; import org.example.scitech.models.ClientAddresses; import org.example.scitech.models.Role; import org.example.scitech.models.Status; import org.example.scitech.repositories.ClientAddressesRepository; import org.example.scitech.repositories.ClientRepository; import org.example.scitech.repositories.RoleRepository; import org.example.scitech.repositories.StatusRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import java.util.List; import java.util.Optional; @RestController @RequestMapping("/api") @Tag(name = "Universal API", description = "CRUD operations for Clients, Addresses, Roles and Statuses") public class UniversalController { private final ClientRepository clientRepository; private final ClientAddressesRepository clientAddressesRepository; private final RoleRepository roleRepository; private final StatusRepository statusRepository; @Autowired public UniversalController(ClientRepository clientRepository, ClientAddressesRepository clientAddressesRepository, RoleRepository roleRepository, StatusRepository statusRepository) { this.clientRepository = clientRepository; this.clientAddressesRepository = clientAddressesRepository; this.roleRepository = roleRepository; this.statusRepository = statusRepository; } // Client CRUD operations @Operation(summary = "Create a new client", description = "Creates a new client with the provided details") @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Client created successfully", content = @Content(schema = @Schema(implementation = Client.class))), @ApiResponse(responseCode = "400", description = "Invalid input") }) @PostMapping("/clients") public Client createClient(@RequestBody Client client) { return clientRepository.save(client); } @Operation(summary = "Get all clients", description = "Returns a list of all clients") @ApiResponse(responseCode = "200", description = "Successfully retrieved list of clients", content = @Content(schema = @Schema(implementation = Client.class))) @GetMapping("/clients") public List<Client> getAllClients() { return clientRepository.findAll(); } @Operation(summary = "Get client by ID", description = "Returns a single client by their ID") @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Client found", content = @Content(schema = @Schema(implementation = Client.class))), @ApiResponse(responseCode = "404", description = "Client not found") }) @GetMapping("/clients/{id}") public ResponseEntity<Client> getClientById( @Parameter(description = "ID of the client to be retrieved", required = true) @PathVariable Long id) { Optional<Client> client = clientRepository.findById(id); return client.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build()); } @Operation(summary = "Update client", description = "Updates an existing client's details") @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Client updated successfully", content = @Content(schema = @Schema(implementation = Client.class))), @ApiResponse(responseCode = "404", description = "Client not found"), @ApiResponse(responseCode = "400", description = "Invalid input") }) @PutMapping("/clients/{id}") public ResponseEntity<Client> updateClient( @Parameter(description = "ID of the client to be updated", required = true) @PathVariable Long id, @RequestBody Client clientDetails) { return clientRepository.findById(id).map(client -> { client.setClientName(clientDetails.getClientName()); client.setClientEmail(clientDetails.getClientEmail()); client.setPassword(clientDetails.getPassword()); client.setPhone(clientDetails.getPhone()); client.setFirstName(clientDetails.getFirstName()); client.setLastName(clientDetails.getLastName()); client.setStatus(clientDetails.getStatus()); client.setRole(clientDetails.getRole()); Client updatedClient = clientRepository.save(client); return ResponseEntity.ok(updatedClient); }).orElseGet(() -> ResponseEntity.notFound().build()); } @Operation(summary = "Delete client", description = "Deletes a client by their ID") @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Client deleted successfully"), @ApiResponse(responseCode = "404", description = "Client not found") }) @DeleteMapping("/clients/{id}") public ResponseEntity<Void> deleteClient( @Parameter(description = "ID of the client to be deleted", required = true) @PathVariable Long id) { return clientRepository.findById(id).map(client -> { clientRepository.delete(client); return ResponseEntity.ok().<Void>build(); }).orElseGet(() -> ResponseEntity.notFound().build()); } // ClientAddresses CRUD operations @Operation(summary = "Create client address", description = "Creates a new address for a client") @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Address created successfully", content = @Content(schema = @Schema(implementation = ClientAddresses.class))), @ApiResponse(responseCode = "400", description = "Invalid input") }) @PostMapping("/client-addresses") public ClientAddresses createClientAddress(@RequestBody ClientAddresses clientAddress) { return clientAddressesRepository.save(clientAddress); } @Operation(summary = "Get all client addresses", description = "Returns a list of all client addresses") @ApiResponse(responseCode = "200", description = "Successfully retrieved list of addresses", content = @Content(schema = @Schema(implementation = ClientAddresses.class))) @GetMapping("/client-addresses") public List<ClientAddresses> getAllClientAddresses() { return clientAddressesRepository.findAll(); } @Operation(summary = "Get client address by ID", description = "Returns a single address by its ID") @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Address found", content = @Content(schema = @Schema(implementation = ClientAddresses.class))), @ApiResponse(responseCode = "404", description = "Address not found") }) @GetMapping("/client-addresses/{id}") public ResponseEntity<ClientAddresses> getClientAddressById( @Parameter(description = "ID of the address to be retrieved", required = true) @PathVariable Long id) { Optional<ClientAddresses> clientAddress = clientAddressesRepository.findById(id); return clientAddress.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build()); } @Operation(summary = "Update client address", description = "Updates an existing client address") @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Address updated successfully", content = @Content(schema = @Schema(implementation = ClientAddresses.class))), @ApiResponse(responseCode = "404", description = "Address not found"), @ApiResponse(responseCode = "400", description = "Invalid input") }) @PutMapping("/client-addresses/{id}") public ResponseEntity<ClientAddresses> updateClientAddress( @Parameter(description = "ID of the address to be updated", required = true) @PathVariable Long id, @RequestBody ClientAddresses clientAddressDetails) { return clientAddressesRepository.findById(id).map(clientAddress -> { clientAddress.setCity(clientAddressDetails.getCity()); clientAddress.setCountry(clientAddressDetails.getCountry()); clientAddress.setEducationalInstruction(clientAddressDetails.getEducationalInstruction()); ClientAddresses updatedClientAddress = clientAddressesRepository.save(clientAddress); return ResponseEntity.ok(updatedClientAddress); }).orElseGet(() -> ResponseEntity.notFound().build()); } @Operation(summary = "Delete client address", description = "Deletes a client address by its ID") @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Address deleted successfully"), @ApiResponse(responseCode = "404", description = "Address not found") }) @DeleteMapping("/client-addresses/{id}") public ResponseEntity<Void> deleteClientAddress( @Parameter(description = "ID of the address to be deleted", required = true) @PathVariable Long id) { return clientAddressesRepository.findById(id).map(clientAddress -> { clientAddressesRepository.delete(clientAddress); return ResponseEntity.ok().<Void>build(); }).orElseGet(() -> ResponseEntity.notFound().build()); } // Role CRUD operations @Operation(summary = "Create role", description = "Creates a new role") @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Role created successfully", content = @Content(schema = @Schema(implementation = Role.class))), @ApiResponse(responseCode = "400", description = "Invalid input") }) @PostMapping("/roles") public Role createRole(@RequestBody Role role) { return roleRepository.save(role); } @Operation(summary = "Get all roles", description = "Returns a list of all roles") @ApiResponse(responseCode = "200", description = "Successfully retrieved list of roles", content = @Content(schema = @Schema(implementation = Role.class))) @GetMapping("/roles") public List<Role> getAllRoles() { return roleRepository.findAll(); } @Operation(summary = "Get role by ID", description = "Returns a single role by its ID") @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Role found", content = @Content(schema = @Schema(implementation = Role.class))), @ApiResponse(responseCode = "404", description = "Role not found") }) @GetMapping("/roles/{id}") public ResponseEntity<Role> getRoleById( @Parameter(description = "ID of the role to be retrieved", required = true) @PathVariable Long id) { Optional<Role> role = roleRepository.findById(id); return role.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build()); } @Operation(summary = "Update role", description = "Updates an existing role") @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Role updated successfully", content = @Content(schema = @Schema(implementation = Role.class))), @ApiResponse(responseCode = "404", description = "Role not found"), @ApiResponse(responseCode = "400", description = "Invalid input") }) @PutMapping("/roles/{id}") public ResponseEntity<Role> updateRole( @Parameter(description = "ID of the role to be updated", required = true) @PathVariable Long id, @RequestBody Role roleDetails) { return roleRepository.findById(id).map(role -> { role.setName(roleDetails.getName()); Role updatedRole = roleRepository.save(role); return ResponseEntity.ok(updatedRole); }).orElseGet(() -> ResponseEntity.notFound().build()); } @Operation(summary = "Delete role", description = "Deletes a role by its ID") @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Role deleted successfully"), @ApiResponse(responseCode = "404", description = "Role not found") }) @DeleteMapping("/roles/{id}") public ResponseEntity<Void> deleteRole( @Parameter(description = "ID of the role to be deleted", required = true) @PathVariable Long id) { return roleRepository.findById(id).map(role -> { roleRepository.delete(role); return ResponseEntity.ok().<Void>build(); }).orElseGet(() -> ResponseEntity.notFound().build()); } // Status CRUD operations @Operation(summary = "Create status", description = "Creates a new status") @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Status created successfully", content = @Content(schema = @Schema(implementation = Status.class))), @ApiResponse(responseCode = "400", description = "Invalid input") }) @PostMapping("/statuses") public Status createStatus(@RequestBody Status status) { return statusRepository.save(status); } @Operation(summary = "Get all statuses", description = "Returns a list of all statuses") @ApiResponse(responseCode = "200", description = "Successfully retrieved list of statuses", content = @Content(schema = @Schema(implementation = Status.class))) @GetMapping("/statuses") public List<Status> getAllStatuses() { return statusRepository.findAll(); } @Operation(summary = "Get status by ID", description = "Returns a single status by its ID") @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Status found", content = @Content(schema = @Schema(implementation = Status.class))), @ApiResponse(responseCode = "404", description = "Status not found") }) @GetMapping("/statuses/{id}") public ResponseEntity<Status> getStatusById( @Parameter(description = "ID of the status to be retrieved", required = true) @PathVariable Long id) { Optional<Status> status = statusRepository.findById(id); return status.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build()); } @Operation(summary = "Update status", description = "Updates an existing status") @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Status updated successfully", content = @Content(schema = @Schema(implementation = Status.class))), @ApiResponse(responseCode = "404", description = "Status not found"), @ApiResponse(responseCode = "400", description = "Invalid input") }) @PutMapping("/statuses/{id}") public ResponseEntity<Status> updateStatus( @Parameter(description = "ID of the status to be updated", required = true) @PathVariable Long id, @RequestBody Status statusDetails) { return statusRepository.findById(id).map(status -> { status.setName(statusDetails.getName()); Status updatedStatus = statusRepository.save(status); return ResponseEntity.ok(updatedStatus); }).orElseGet(() -> ResponseEntity.notFound().build()); } @Operation(summary = "Delete status", description = "Deletes a status by its ID") @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Status deleted successfully"), @ApiResponse(responseCode = "404", description = "Status not found") }) @DeleteMapping("/statuses/{id}") public ResponseEntity<Void> deleteStatus( @Parameter(description = "ID of the status to be deleted", required = true) @PathVariable Long id) { return statusRepository.findById(id).map(status -> { statusRepository.delete(status); return ResponseEntity.ok().<Void>build(); }).orElseGet(() -> ResponseEntity.notFound().build()); } }