/
java56
/
Hotels
Обзор
Документация
Войти
/
java56
/
Hotels
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/main/java/org/mikhail/controller/HotelController.java
104 строки
6 KB
Mikhail Gorbatenkov
42 - edit the page-by-page display of the list of hotels
02 фев 2025, 17:05
02 фев 2025, 17:05
a20ca2f
Код
Авторство
О чём код?
package org.mikhail.controller; import jakarta.enterprise.context.ApplicationScoped; import jakarta.validation.Valid; import jakarta.ws.rs.*; import jakarta.ws.rs.core.MediaType; import jakarta.ws.rs.core.Response; import org.eclipse.microprofile.openapi.annotations.Operation; import org.eclipse.microprofile.openapi.annotations.media.Content; import org.eclipse.microprofile.openapi.annotations.media.Schema; import org.eclipse.microprofile.openapi.annotations.parameters.Parameter; import org.eclipse.microprofile.openapi.annotations.responses.APIResponse; import org.eclipse.microprofile.openapi.annotations.responses.APIResponses; import org.eclipse.microprofile.openapi.annotations.tags.Tag; import org.mikhail.dto.HotelDTO; import org.mikhail.dto.response.HotelResponseDTO; import org.mikhail.entity.SortOrder; import org.mikhail.exceptions.responses.ErrorResponse; import org.mikhail.service.HotelService; import java.util.List; import java.util.Map; @Tag(name = "Отель", description = "Выполнение различных операций с отелем") @Path("/api/v1/hotels") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) @ApplicationScoped public class HotelController { private final HotelService hotelService; public HotelController(HotelService hotelService) { this.hotelService = hotelService; } @POST @Operation(summary = "Создание отеля", description = "Создает отель и записывает в базу данных.") @APIResponses(value = { @APIResponse(responseCode = "201", description = "Отель успешно создан", content = @Content(mediaType = MediaType.APPLICATION_JSON, schema = @Schema(implementation = HotelResponseDTO.class)) ), @APIResponse(responseCode = "404", description = "Адрес отеля не найден в БД, поэтому отель не создан.", content = @Content(mediaType = MediaType.APPLICATION_JSON, schema = @Schema(implementation = HotelResponseDTO.class))), @APIResponse(responseCode = "400", description = "Отправлены некорректные данные, отель не создан", content = @Content(mediaType = MediaType.APPLICATION_JSON, schema = @Schema(implementation = Map.class))) }) public Response createHotel(@Valid HotelDTO hotelDTO) { HotelResponseDTO response = hotelService.addHotel(hotelDTO); return Response.status(Response.Status.CREATED).entity(response).build(); } @DELETE @Path("/{id}") @Operation(summary = "Удаление отеля", description = "Удаляет отель по идентификатору.") @APIResponses(value = { @APIResponse(responseCode = "200", description = "Отель успешно удален", content = @Content(mediaType = MediaType.APPLICATION_JSON, schema = @Schema(implementation = String.class)) ), @APIResponse(responseCode = "404", description = "Отель с указанным ID не найден, удаление не выполнено.", content = @Content(mediaType = MediaType.APPLICATION_JSON, schema = @Schema(implementation = ErrorResponse.class)) ) }) public Response deleteHotel(@PathParam(value = "id") Long id) { String result = hotelService.deleteHotel(id); return Response.ok(result).build(); } @GET @Operation(summary = "Получение списка отелей", description = "Отображает список отелей, имеющихся в БД.") @APIResponses(value = { @APIResponse(responseCode = "200", description = "Список отелей успешно получен", content = @Content(schema = @Schema(implementation = HotelDTO.class)) ) }) public List<HotelDTO> getAllHotels() { return hotelService.getAllHotels(); } @GET @Path("/paged") @Operation(summary = "Отображение информации об отелях в постраничном списке", description = "Выводит информацию по отелям в постраничном списке, " + "с возможностью сортировки по полю \"Название отеля\". Если порядок сортировки указан неверно, " + "то сортировка будет происходить в порядке возрастания (ASCENDING)") @APIResponses(value = { @APIResponse(responseCode = "200", description = "Постраничный список отелей успешно получен", content = @Content(mediaType = MediaType.APPLICATION_JSON, schema = @Schema(implementation = HotelDTO.class))) }) public List<HotelDTO> getPagedHotels( @Parameter(description = "Номер страницы") @QueryParam("page") @DefaultValue("0") int page, @Parameter(description = "Количество элементов на странице") @QueryParam("size") @DefaultValue("10") int size, @Parameter(description = "Порядок сортировки (ASCENDING или DESCENDING)") @QueryParam("sort") @DefaultValue("ASCENDING") String sortOrder) { return hotelService.getHotelsPaged(page, size, SortOrder.fromString(sortOrder)); } }