/** * */ package ru.app.demo.rs.rs; import io.swagger.v3.oas.annotations.info.Info; import io.swagger.v3.oas.annotations.media.ArraySchema; import io.swagger.v3.oas.annotations.media.Content; import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.parameters.RequestBody; import io.swagger.v3.oas.annotations.OpenAPIDefinition; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.responses.ApiResponse; import io.swagger.v3.oas.annotations.servers.Server; import io.swagger.v3.oas.annotations.tags.Tag; import javax.inject.Singleton; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.MediaType; import ru.funsys.avalanche.rs.RestService; import ru.app.demo.rs.app.WebApp; import ru.app.demo.rs.model.Smartphone; /** * @author Валерий Лиховских * */ @OpenAPIDefinition(servers = {@Server(url = ".")}, info = @Info(title = "REST API", description = "REST API моделей смартфонов", version = "1.0")) @Path("/smartphone") @Consumes(MediaType.APPLICATION_JSON + ";charset=UTF-8") @Produces({MediaType.APPLICATION_JSON + ";charset=UTF-8"}) @Tag(name = "Smartphone", description = "Модели смартфонов") @Singleton public class Service extends RestService { /** * Поле устанавливается автоматически */ private WebApp app; @Operation(summary = "Получить список смартфонов", responses = { @ApiResponse(responseCode = "200", description = "Ok", content = @Content(array = @ArraySchema(schema = @Schema(implementation = Smartphone.class)))), @ApiResponse(responseCode = "401", description = "Unauthorized"), @ApiResponse(responseCode = "403", description = "Forbidden"), @ApiResponse(responseCode = "500", description = "Internal server error") }) @GET @Path("/list") public Smartphone[] list() throws WebApplicationException { try { return app.list(); } catch (Exception e) { throw new WebApplicationException(e, 500); } } @Operation(summary = "Получить список смартфонов заданного бренда", responses = { @ApiResponse(responseCode = "200", description = "Ok", content = @Content(array = @ArraySchema(schema = @Schema(implementation = Smartphone.class)))), @ApiResponse(responseCode = "401", description = "Unauthorized"), @ApiResponse(responseCode = "403", description = "Forbidden"), @ApiResponse(responseCode = "500", description = "Internal server error") }) @GET @Path("/findBrend") public Smartphone[] findBrend(@QueryParam("brend") @Parameter(description = "Имя бренда") String brend) throws WebApplicationException { try { return app.findBrend(brend); } catch (Exception e) { throw new WebApplicationException(e, 500); } } @Operation(summary = "Получить список смартфонов c заданным количеством ядер", responses = { @ApiResponse(responseCode = "200", description = "Ok", content = @Content(array = @ArraySchema(schema = @Schema(implementation = Smartphone.class)))), @ApiResponse(responseCode = "401", description = "Unauthorized"), @ApiResponse(responseCode = "403", description = "Forbidden"), @ApiResponse(responseCode = "500", description = "Internal server error") }) @GET @Path("/findCore") public Smartphone[] findCore(@QueryParam("core") @Parameter(description = "Количество ядер") Integer core) throws WebApplicationException { try { return app.findCore(core); } catch (Exception e) { throw new WebApplicationException(e, 500); } } @Operation(summary = "Инициировать виртуальную таблицу", responses = { @ApiResponse(responseCode = "200", description = "Ok"), @ApiResponse(responseCode = "401", description = "Unauthorized"), @ApiResponse(responseCode = "403", description = "Forbidden"), @ApiResponse(responseCode = "500", description = "Internal server error") }) @GET @Path("/initTable") public void initTable() { app.initVirtualTable(); } @DELETE @Path("/delete") public void delete(@QueryParam("brend") @Parameter(description = "Бренд") String brend, @QueryParam("model") @Parameter(description = "Модель") String model) throws WebApplicationException { try { app.delete(brend, model); } catch (Exception e) { throw new WebApplicationException(e, 500); } } @POST @Path("/insert") public void insert( @RequestBody(description = "Добавить модель смартфона", required = true, content = @Content(schema=@Schema(implementation = Smartphone.class))) Smartphone smartphone) throws WebApplicationException { try { app.addSmartphone(smartphone); } catch (Exception e) { throw new WebApplicationException(e, 500); } } @PUT @Path("/update") public void update( @RequestBody(description = "Изменить данные о модели смартфона", required = true, content = @Content(schema=@Schema(implementation = Smartphone.class))) Smartphone smartphone) throws WebApplicationException { try { app.update(smartphone); } catch (Exception e) { throw new WebApplicationException(e, 500); } } }