/
java56
/
Hotels
Обзор
Документация
Войти
/
java56
/
Hotels
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/main/java/org/mikhail/controller/AddressController.java
81 строка
4 KB
Mikhail Gorbatenkov
38 - edited the address controller
01 фев 2025, 19:16
01 фев 2025, 19:16
5907fde
Код
Авторство
О чём код?
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.responses.APIResponse; import org.eclipse.microprofile.openapi.annotations.responses.APIResponses; import org.eclipse.microprofile.openapi.annotations.tags.Tag; import org.mikhail.dto.AddressDTO; import org.mikhail.exceptions.responses.ErrorResponse; import org.mikhail.service.AddressService; import java.util.List; import java.util.Map; @Tag(name = "Адрес отеля", description = "Выполнение различных операций с адресом") @Path("/api/v1/addresses") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) @ApplicationScoped public class AddressController { private final AddressService addressService; public AddressController(AddressService addressService) { this.addressService = addressService; } @POST @Operation(summary = "Создание адреса отеля", description = "Создает адрес и записывает в базу данных.") @APIResponses( value = { @APIResponse(responseCode = "201", description = "Адрес отеля успешно создан.", content = @Content(mediaType = MediaType.APPLICATION_JSON, schema = @Schema(implementation = String.class))), @APIResponse(responseCode = "400", description = "Отправлены некорректные данные, поэтому адрес не создан.", content = @Content(mediaType = MediaType.APPLICATION_JSON, schema = @Schema(implementation = Map.class)) ) } ) public Response createAddress(@Valid AddressDTO addressDTO) { Long addressId = addressService.addAddress(addressDTO); return Response.status(Response.Status.CREATED) .entity("Адрес успешно создан, id: " + addressId).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 = "Адрес отеля не найден", content = @Content(mediaType = MediaType.APPLICATION_JSON, schema = @Schema(implementation = ErrorResponse.class))) }) public Response deleteAddress(@PathParam(value = "id") Long addressId) { String result = addressService.deleteAddress(addressId); return Response.ok(result).build(); } @GET @Operation(summary = "Получение списка адресов", description = "Отображает список всех доступных адресов.") @APIResponses(value = { @APIResponse(responseCode = "200", description = "Список адресов успешно получен", content = @Content(mediaType = MediaType.APPLICATION_JSON, schema = @Schema(implementation = AddressDTO.class))) }) public List<AddressDTO> getAddresses() { return addressService.getAllAddresses(); } }