From a7b93557391f864fe58b3740307adca153c57a82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=D0=B8=D0=BC=20=D0=AF=D0=BA=D0=BE?= =?UTF-8?q?=D0=B2=D0=BB=D0=B5=D0=B2?= Date: Sat, 13 Apr 2024 21:44:13 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=BB=D0=BE=D1=85=D0=BE=20=D1=80=D0=B0?= =?UTF-8?q?=D0=B1=D0=BE=D1=82=D0=B0=D1=8E=D1=82=20=D0=B7=D0=B0=D0=BF=D1=80?= =?UTF-8?q?=D0=BE=D1=81=D1=8B=20=D0=A2-=D0=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data.mv.db | Bin 0 -> 49152 bytes data.trace.db | 135 ++++++++++++++++++ .../com/example/demo/DemoApplication.java | 44 +++--- .../com/example/demo/core/api/PageDto.java | 97 +++++++++++++ .../example/demo/core/api/PageDtoMapper.java | 25 ++++ .../demo/core/configuration/Constants.java | 2 + .../demo/core/error/NotFoundException.java | 4 +- .../example/demo/core/model/BaseEntity.java | 16 ++- .../core/repository/CommonRepository.java | 17 --- .../demo/core/repository/MapRepository.java | 57 -------- .../demo/games/api/GameController.java | 11 +- .../com/example/demo/games/api/GameDto.java | 2 +- .../example/demo/games/model/GameEntity.java | 19 ++- .../demo/games/repository/GameRepository.java | 16 ++- .../demo/games/service/GameService.java | 65 ++++++--- .../com/example/demo/genres/api/GenreDto.java | 19 +-- .../demo/genres/model/GenreEntity.java | 11 +- .../genres/repository/GenreRepository.java | 9 +- .../demo/genres/service/GenreService.java | 23 ++- .../demo/orders/api/OrderController.java | 39 ++--- .../com/example/demo/orders/api/OrderDto.java | 13 +- .../demo/orders/model/OrderEntity.java | 62 ++++---- .../orders/repository/OrderRepository.java | 19 ++- .../demo/orders/service/OrderService.java | 59 +++++--- .../com/example/demo/types/api/TypeDto.java | 4 +- .../example/demo/types/model/TypeEntity.java | 11 +- .../demo/types/repository/TypeRepository.java | 10 +- .../demo/types/service/TypeService.java | 23 ++- .../demo/users/api/UserController.java | 21 ++- .../com/example/demo/users/api/UserDto.java | 6 +- .../example/demo/users/model/UserEntity.java | 36 ++++- .../demo/users/repository/UserRepository.java | 11 +- .../demo/users/service/UserService.java | 31 +++- src/main/resources/application.properties | 18 +++ .../com/example/demo/GameServiceTests.java | 62 -------- .../com/example/demo/OrderServiceTest.java | 52 ------- .../com/example/demo/TypeServiceTests.java | 58 -------- .../com/example/demo/UserServiceTest.java | 48 ------- src/test/resources/application.properties | 14 ++ 39 files changed, 674 insertions(+), 495 deletions(-) create mode 100644 data.mv.db create mode 100644 data.trace.db create mode 100644 src/main/java/com/example/demo/core/api/PageDto.java create mode 100644 src/main/java/com/example/demo/core/api/PageDtoMapper.java delete mode 100644 src/main/java/com/example/demo/core/repository/CommonRepository.java delete mode 100644 src/main/java/com/example/demo/core/repository/MapRepository.java delete mode 100644 src/test/java/com/example/demo/GameServiceTests.java delete mode 100644 src/test/java/com/example/demo/OrderServiceTest.java delete mode 100644 src/test/java/com/example/demo/TypeServiceTests.java delete mode 100644 src/test/java/com/example/demo/UserServiceTest.java create mode 100644 src/test/resources/application.properties diff --git a/data.mv.db b/data.mv.db new file mode 100644 index 0000000000000000000000000000000000000000..6868d4ad2d8c4c256fbccc03cb25e0567bb5556d GIT binary patch literal 49152 zcmeHQOOPAKc^+V~yAYqUVmpdLTOjzBR><_c2X+!-cd=Y>p8~KVnTm^W7jck_3HCX!`&#(Vqj);mpP8#CTc z;Oqu|6Ip?8siEfC!A!WlbIaLnsx$8fJ9lnwZ;=zj&0yDiJJ@L=O$#mE^bSeIzd{3r z1_})n8YnbSXrRzQp@BjJg$4=@6dEWrP-x((YhbaM|3CFMCH{xB_lFH-bA&Yv%UNt-0;HTf0p(bIZBcL|!1n z!R@AI%03J5CXEUnYuEwd-E0`1}xXLtr!e|X10QRyKtx7ZLf(;%d9=wKMW)| zC;dR0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D#4 zl4ti`<1M;+a^Mr8ut295U_Z?5o%?4}Cxh_a?XPjM`B^|?VE^{3A_pL3Ws=G`iiqiI z?}ADiv-fv{*3OP|pI83?ge+s+vUC79o}Aj}78*s3qnwjgb#@Oc!#}As`njbM&#l0H z$2qq=#(gJ7Ja-aTZp3q^#*i1EJE{!0-%UuR(pWujl;*9aULfR2THfBe@mLShC?)QD z6^a`+hK>=VG|DLF6ncJgJu9il4c?}(Id2b!a@a3*#kqZY7PhK|9chD|V9N_mk7|3o zG0JUMAXGx``0O_%UJ?4(zn8Lz@!4^p2uTIY=oAs5L_}zW01J%>WidB-NGL+n5K38M z#w1{VASC2x?*Jk~!Lzy2>hs|Nd=7#w5q%$ahoPahXK+)R0Z-~vi3lZjQlWGJMq-p6 zfG73eh)rJp`RDPXYsLD18?)3+6)ly9J%nUaw!}(`#rl8!ZmW}6Sae~jSpTmzKVOU8 zzXEq*VJUabzGD49wOIcTsP4Jzixuntxtl?wSpP4v7GK7Cj>YR9gKM7ha1F%ji}nAaRi%pc{|K160U(JdbcPreparedStatement.java:93) + at org.h2.jdbc.JdbcConnection.prepareStatement(JdbcConnection.java:316) + at com.zaxxer.hikari.pool.ProxyConnection.prepareStatement(ProxyConnection.java:327) + at com.zaxxer.hikari.pool.HikariProxyConnection.prepareStatement(HikariProxyConnection.java) + at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$5.doPrepare(StatementPreparerImpl.java:153) + at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:183) + at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareQueryStatement(StatementPreparerImpl.java:155) + at org.hibernate.sql.exec.spi.JdbcSelectExecutor.lambda$list$0(JdbcSelectExecutor.java:85) + at org.hibernate.sql.results.jdbc.internal.DeferredResultSetAccess.executeQuery(DeferredResultSetAccess.java:231) + at org.hibernate.sql.results.jdbc.internal.DeferredResultSetAccess.getResultSet(DeferredResultSetAccess.java:167) + at org.hibernate.sql.results.jdbc.internal.JdbcValuesResultSetImpl.advanceNext(JdbcValuesResultSetImpl.java:218) + at org.hibernate.sql.results.jdbc.internal.JdbcValuesResultSetImpl.processNext(JdbcValuesResultSetImpl.java:98) + at org.hibernate.sql.results.jdbc.internal.AbstractJdbcValues.next(AbstractJdbcValues.java:19) + at org.hibernate.sql.results.internal.RowProcessingStateStandardImpl.next(RowProcessingStateStandardImpl.java:66) + at org.hibernate.sql.results.spi.ListResultsConsumer.consume(ListResultsConsumer.java:180) + at org.hibernate.sql.results.spi.ListResultsConsumer.consume(ListResultsConsumer.java:33) + at org.hibernate.sql.exec.internal.JdbcSelectExecutorStandardImpl.doExecuteQuery(JdbcSelectExecutorStandardImpl.java:209) + at org.hibernate.sql.exec.internal.JdbcSelectExecutorStandardImpl.executeQuery(JdbcSelectExecutorStandardImpl.java:83) + at org.hibernate.sql.exec.spi.JdbcSelectExecutor.list(JdbcSelectExecutor.java:76) + at org.hibernate.sql.exec.spi.JdbcSelectExecutor.list(JdbcSelectExecutor.java:65) + at org.hibernate.query.sqm.internal.ConcreteSqmSelectQueryPlan.lambda$new$2(ConcreteSqmSelectQueryPlan.java:137) + at org.hibernate.query.sqm.internal.ConcreteSqmSelectQueryPlan.withCacheableSqmInterpretation(ConcreteSqmSelectQueryPlan.java:362) + at org.hibernate.query.sqm.internal.ConcreteSqmSelectQueryPlan.performList(ConcreteSqmSelectQueryPlan.java:303) + at org.hibernate.query.sqm.internal.QuerySqmImpl.doList(QuerySqmImpl.java:509) + at org.hibernate.query.spi.AbstractSelectionQuery.list(AbstractSelectionQuery.java:427) + at org.hibernate.query.Query.getResultList(Query.java:120) + at org.springframework.data.jpa.repository.query.JpaQueryExecution$PagedExecution.doExecute(JpaQueryExecution.java:204) + at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:92) + at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:149) + at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:137) + at org.springframework.data.repository.core.support.RepositoryMethodInvoker.doInvoke(RepositoryMethodInvoker.java:170) + at org.springframework.data.repository.core.support.RepositoryMethodInvoker.invoke(RepositoryMethodInvoker.java:158) + at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.doInvoke(QueryExecutorMethodInterceptor.java:164) + at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.invoke(QueryExecutorMethodInterceptor.java:143) + at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184) + at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:123) + at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:392) + at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:119) + at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184) + at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:137) + at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184) + at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:135) + at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184) + at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97) + at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184) + at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:220) + at jdk.proxy2/jdk.proxy2.$Proxy124.findByTypeIdAndGenres(Unknown Source) + at com.example.demo.games.service.GameService.getAll(GameService.java:29) + at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104) + at java.base/java.lang.reflect.Method.invoke(Method.java:578) + at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:351) + at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:196) + at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) + at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:765) + at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:123) + at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:392) + at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:119) + at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184) + at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:765) + at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:717) + at com.example.demo.games.service.GameService$$SpringCGLIB$$0.getAll() + at com.example.demo.games.api.GameController.getAll(GameController.java:70) + at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104) + at java.base/java.lang.reflect.Method.invoke(Method.java:578) + at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:255) + at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:188) + at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:118) + at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:925) + at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:830) + at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) + at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1089) + at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:979) + at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1014) + at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:903) + at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:564) + at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:885) + at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:658) + at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:205) + at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) + at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51) + at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) + at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) + at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) + at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) + at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) + at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) + at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) + at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) + at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) + at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) + at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) + at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) + at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) + at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) + at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:167) + at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:90) + at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:482) + at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:115) + at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:93) + at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) + at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:344) + at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:391) + at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:63) + at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:896) + at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1744) + at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52) + at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) + at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) + at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:63) + at java.base/java.lang.Thread.run(Thread.java:1623) diff --git a/src/main/java/com/example/demo/DemoApplication.java b/src/main/java/com/example/demo/DemoApplication.java index 5f863c5..97358da 100644 --- a/src/main/java/com/example/demo/DemoApplication.java +++ b/src/main/java/com/example/demo/DemoApplication.java @@ -3,11 +3,12 @@ package com.example.demo; import java.util.ArrayList; import java.util.List; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; import com.example.demo.games.model.GameEntity; import com.example.demo.games.service.GameService; @@ -22,9 +23,9 @@ import com.example.demo.users.service.UserService; @SpringBootApplication -@RestController -@RequestMapping("/api") public class DemoApplication implements CommandLineRunner { + private final Logger log = LoggerFactory.getLogger(DemoApplication.class); + private final TypeService typeService; private final GenreService genreService; private final GameService gameService; @@ -39,13 +40,18 @@ public class DemoApplication implements CommandLineRunner { this.userService = userService; } + public static void main(String[] args) { + SpringApplication.run(DemoApplication.class, args); + } + @Override public void run(String... args) throws Exception{ - final var type1 = typeService.create(new TypeEntity(null,"ААА")); - final var type2 = typeService.create(new TypeEntity(null,"АА")); + log.info("start"); + final var type1 = typeService.create(new TypeEntity("ААА")); + final var type2 = typeService.create(new TypeEntity("АА")); - final var genre1 = genreService.create(new GenreEntity(null, "Приключения")); - final var genre2 = genreService.create(new GenreEntity(null, "Симулятор")); + final var genre1 = genreService.create(new GenreEntity("Приключения")); + final var genre2 = genreService.create(new GenreEntity("Симулятор")); final List genres1 = new ArrayList(); genres1.add(genre1); @@ -54,20 +60,16 @@ public class DemoApplication implements CommandLineRunner { final List genres2 = new ArrayList(); genres2.add(genre2); - final var game1 = gameService.create(new GameEntity(null,type1,"Game1",2100.0,"good game", genres1)); - final var game2 = gameService.create(new GameEntity(null, type2, "Game2", 1200.0,"bad game", genres2)); - final List games = new ArrayList(); - games.add(game1); - games.add(game2); + final var game1 = gameService.create(new GameEntity(type1,"Game1",2100.0,"good game", genres1)); + // final var game2 = gameService.create(new GameEntity( type2, "Game2", 1200.0,"bad game", genres2)); + // final List games = new ArrayList(); + // games.add(game1); + // games.add(game2); - userService.create(new UserEntity(null, "login1", "email@mail.com", "qwerty123")); - userService.create(new UserEntity(null, "login2", "email@gmail.com", "qwerty1234")); + // var user1 = userService.create(new UserEntity( "login1", "email@mail.com", "qwerty123")); + // var user2 = userService.create(new UserEntity( "login2", "email@gmail.com", "qwerty1234")); - orderService.create(new OrderEntity(null, 1L,games)); - orderService.create(new OrderEntity(null, 2L,games)); - } - - public static void main(String[] args) { - SpringApplication.run(DemoApplication.class, args); + // orderService.create(1, new OrderEntity(user1,games)); + // orderService.create(1, new OrderEntity(user1,games)); } } diff --git a/src/main/java/com/example/demo/core/api/PageDto.java b/src/main/java/com/example/demo/core/api/PageDto.java new file mode 100644 index 0000000..4cae429 --- /dev/null +++ b/src/main/java/com/example/demo/core/api/PageDto.java @@ -0,0 +1,97 @@ +package com.example.demo.core.api; + +import java.util.ArrayList; +import java.util.List; + +public class PageDto { + private List items = new ArrayList<>(); + private int itemsCount; + private int currentPage; + private int currentSize; + private int totalPages; + private long totalItems; + private boolean isFirst; + private boolean isLast; + private boolean hasNext; + private boolean hasPrevious; + + public List getItems() { + return items; + } + + public void setItems(List items) { + this.items = items; + } + + public int getItemsCount() { + return itemsCount; + } + + public void setItemsCount(int itemsCount) { + this.itemsCount = itemsCount; + } + + public int getCurrentPage() { + return currentPage; + } + + public void setCurrentPage(int currentPage) { + this.currentPage = currentPage; + } + + public int getCurrentSize() { + return currentSize; + } + + public void setCurrentSize(int currentSize) { + this.currentSize = currentSize; + } + + public int getTotalPages() { + return totalPages; + } + + public void setTotalPages(int totalPages) { + this.totalPages = totalPages; + } + + public long getTotalItems() { + return totalItems; + } + + public void setTotalItems(long totalItems) { + this.totalItems = totalItems; + } + + public boolean isFirst() { + return isFirst; + } + + public void setFirst(boolean isFirst) { + this.isFirst = isFirst; + } + + public boolean isLast() { + return isLast; + } + + public void setLast(boolean isLast) { + this.isLast = isLast; + } + + public boolean isHasNext() { + return hasNext; + } + + public void setHasNext(boolean hasNext) { + this.hasNext = hasNext; + } + + public boolean isHasPrevious() { + return hasPrevious; + } + + public void setHasPrevious(boolean hasPrevious) { + this.hasPrevious = hasPrevious; + } +} diff --git a/src/main/java/com/example/demo/core/api/PageDtoMapper.java b/src/main/java/com/example/demo/core/api/PageDtoMapper.java new file mode 100644 index 0000000..e8d3dd0 --- /dev/null +++ b/src/main/java/com/example/demo/core/api/PageDtoMapper.java @@ -0,0 +1,25 @@ +package com.example.demo.core.api; + +import java.util.function.Function; + +import org.springframework.data.domain.Page; + +public class PageDtoMapper { + private PageDtoMapper() { + } + + public static PageDto toDto(Page page, Function mapper) { + final PageDto dto = new PageDto<>(); + dto.setItems(page.getContent().stream().map(mapper::apply).toList()); + dto.setItemsCount(page.getNumberOfElements()); + dto.setCurrentPage(page.getNumber()); + dto.setCurrentSize(page.getSize()); + dto.setTotalPages(page.getTotalPages()); + dto.setTotalItems(page.getTotalElements()); + dto.setFirst(page.isFirst()); + dto.setLast(page.isLast()); + dto.setHasNext(page.hasNext()); + dto.setHasPrevious(page.hasPrevious()); + return dto; + } +} diff --git a/src/main/java/com/example/demo/core/configuration/Constants.java b/src/main/java/com/example/demo/core/configuration/Constants.java index d9c6b7c..42de69f 100644 --- a/src/main/java/com/example/demo/core/configuration/Constants.java +++ b/src/main/java/com/example/demo/core/configuration/Constants.java @@ -1,7 +1,9 @@ package com.example.demo.core.configuration; public class Constants { + public static final String SEQUENCE_NAME = "hibernate_sequence"; public static final String API_URL = "/api/1.0"; + public static final String DEFAULT_PAGE_SIZE = "5"; private Constants() { } diff --git a/src/main/java/com/example/demo/core/error/NotFoundException.java b/src/main/java/com/example/demo/core/error/NotFoundException.java index 586af3c..a61d118 100644 --- a/src/main/java/com/example/demo/core/error/NotFoundException.java +++ b/src/main/java/com/example/demo/core/error/NotFoundException.java @@ -1,7 +1,7 @@ package com.example.demo.core.error; public class NotFoundException extends RuntimeException { - public NotFoundException(Long id) { - super(String.format("Entity with id [%s] is not found or not exists", id)); + public NotFoundException(Class clazz, Long id) { + super(String.format("%s with id [%s] is not found or not exists", clazz.getSimpleName(), id)); } } diff --git a/src/main/java/com/example/demo/core/model/BaseEntity.java b/src/main/java/com/example/demo/core/model/BaseEntity.java index 674ddfb..eba74ad 100644 --- a/src/main/java/com/example/demo/core/model/BaseEntity.java +++ b/src/main/java/com/example/demo/core/model/BaseEntity.java @@ -1,15 +1,23 @@ package com.example.demo.core.model; +import com.example.demo.core.configuration.Constants; + +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.MappedSuperclass; +import jakarta.persistence.SequenceGenerator; + +@MappedSuperclass public abstract class BaseEntity { + @Id + @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = Constants.SEQUENCE_NAME) + @SequenceGenerator(name = Constants.SEQUENCE_NAME, sequenceName = Constants.SEQUENCE_NAME, allocationSize = 1) protected Long id; protected BaseEntity() { } - protected BaseEntity(Long id) { - this.id = id; - } - public Long getId() { return id; } diff --git a/src/main/java/com/example/demo/core/repository/CommonRepository.java b/src/main/java/com/example/demo/core/repository/CommonRepository.java deleted file mode 100644 index 85e1e6d..0000000 --- a/src/main/java/com/example/demo/core/repository/CommonRepository.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.example.demo.core.repository; - -import java.util.List; - -public interface CommonRepository { - List getAll(); - - E get(T id); - - E create(E entity); - - E update(E entity); - - E delete(E entity); - - void deleteAll(); -} diff --git a/src/main/java/com/example/demo/core/repository/MapRepository.java b/src/main/java/com/example/demo/core/repository/MapRepository.java deleted file mode 100644 index 6809ac2..0000000 --- a/src/main/java/com/example/demo/core/repository/MapRepository.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.example.demo.core.repository; - -import java.util.List; -import java.util.Map; -import java.util.TreeMap; - -import com.example.demo.core.model.BaseEntity; - -public abstract class MapRepository implements CommonRepository { - private final Map entities = new TreeMap<>(); - private Long lastId = 0L; - - protected MapRepository() { - } - - @Override - public List getAll() { - return entities.values().stream().toList(); - } - - @Override - public E get(Long id) { - return entities.get(id); - } - - @Override - public E create(E entity) { - lastId++; - entity.setId(lastId); - entities.put(lastId, entity); - return entity; - } - - @Override - public E update(E entity) { - if (get(entity.getId()) == null) { - return null; - } - entities.put(entity.getId(), entity); - return entity; - } - - @Override - public E delete(E entity) { - if (get(entity.getId()) == null) { - return null; - } - entities.remove(entity.getId()); - return entity; - } - - @Override - public void deleteAll() { - lastId = 0L; - entities.clear(); - } -} diff --git a/src/main/java/com/example/demo/games/api/GameController.java b/src/main/java/com/example/demo/games/api/GameController.java index cdecdc9..b372b40 100644 --- a/src/main/java/com/example/demo/games/api/GameController.java +++ b/src/main/java/com/example/demo/games/api/GameController.java @@ -13,6 +13,8 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; +import com.example.demo.core.api.PageDto; +import com.example.demo.core.api.PageDtoMapper; import com.example.demo.core.configuration.Constants; import com.example.demo.games.model.GameEntity; import com.example.demo.games.service.GameService; @@ -60,8 +62,13 @@ public class GameController { } @GetMapping - public List getAll(@RequestParam(name = "typeId", defaultValue = "0") Long typeId, @RequestParam(name = "genres", defaultValue = "") List genres){ - return gameService.getAll(typeId, genres).stream().map(this::toDto).toList(); + public PageDto getAll( + @RequestParam(name = "typeId", defaultValue = "0") Long typeId, + //@RequestParam(name = "genres", defaultValue = "") List genres, + @RequestParam(name = "genres", defaultValue = "") Long genre, + @RequestParam(name = "page", defaultValue = "0") int page, + @RequestParam(name = "size", defaultValue = Constants.DEFAULT_PAGE_SIZE) int size){ + return PageDtoMapper.toDto(gameService.getAll(typeId, genre, page, size), this::toDto); } @GetMapping("/{id}") diff --git a/src/main/java/com/example/demo/games/api/GameDto.java b/src/main/java/com/example/demo/games/api/GameDto.java index 1703389..c7ef495 100644 --- a/src/main/java/com/example/demo/games/api/GameDto.java +++ b/src/main/java/com/example/demo/games/api/GameDto.java @@ -10,6 +10,7 @@ import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotNull; public class GameDto { + @JsonProperty(access = JsonProperty.Access.READ_ONLY) private Long id; @NotNull @Min(1) @@ -24,7 +25,6 @@ public class GameDto { @NotBlank private String description; - @JsonProperty(access = JsonProperty.Access.READ_ONLY) public Long getId(){ return id; } diff --git a/src/main/java/com/example/demo/games/model/GameEntity.java b/src/main/java/com/example/demo/games/model/GameEntity.java index 63c0a1b..464373f 100644 --- a/src/main/java/com/example/demo/games/model/GameEntity.java +++ b/src/main/java/com/example/demo/games/model/GameEntity.java @@ -8,19 +8,32 @@ import com.example.demo.core.model.BaseEntity; import com.example.demo.genres.model.GenreEntity; import com.example.demo.types.model.TypeEntity; +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.OneToMany; +import jakarta.persistence.OneToOne; +import jakarta.persistence.Table; + +@Entity +@Table(name = "games") public class GameEntity extends BaseEntity{ + @Column(nullable = false) private String name; + @Column(nullable = false) private Double price; + @Column(nullable = false) private String description; + @OneToMany private final List genres = new ArrayList<>(); + @OneToOne + @JoinColumn(name = "typeId", nullable = false) private TypeEntity type; public GameEntity(){ - super(); } - public GameEntity(Long id, TypeEntity type, String name, Double price, String description, List genres){ - super(id); + public GameEntity(TypeEntity type, String name, Double price, String description, List genres){ this.type = type; this.name = name; this.price = price; diff --git a/src/main/java/com/example/demo/games/repository/GameRepository.java b/src/main/java/com/example/demo/games/repository/GameRepository.java index 961dad3..96a9834 100644 --- a/src/main/java/com/example/demo/games/repository/GameRepository.java +++ b/src/main/java/com/example/demo/games/repository/GameRepository.java @@ -1,11 +1,17 @@ package com.example.demo.games.repository; -import org.springframework.stereotype.Repository; +import java.util.Optional; + +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.repository.CrudRepository; +import org.springframework.data.repository.PagingAndSortingRepository; -import com.example.demo.core.repository.MapRepository; import com.example.demo.games.model.GameEntity; -@Repository -public class GameRepository extends MapRepository{ - +public interface GameRepository extends CrudRepository, PagingAndSortingRepository { + Optional findOneById(Long id); + Page findByTypeIdAndGenres(long typeId, Long genres, Pageable pageable); + Page findByTypeId(long typeId, Pageable pageable); + Page findByGenres(Long genres, Pageable pageable); } diff --git a/src/main/java/com/example/demo/games/service/GameService.java b/src/main/java/com/example/demo/games/service/GameService.java index 5f801d8..eefbca3 100644 --- a/src/main/java/com/example/demo/games/service/GameService.java +++ b/src/main/java/com/example/demo/games/service/GameService.java @@ -1,11 +1,12 @@ package com.example.demo.games.service; -import java.util.ArrayList; -import java.util.List; +//import java.util.List; import java.util.Objects; -import java.util.Optional; - +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.data.domain.Pageable; import com.example.demo.core.error.NotFoundException; import com.example.demo.games.model.GameEntity; @@ -15,40 +16,58 @@ import com.example.demo.genres.service.GenreService; @Service public class GameService { private final GameRepository repository; - private final GenreService genreService; public GameService(GameRepository repository, GenreService genreService){ this.repository = repository; - this.genreService = genreService; } - public List getAll(Long typeId, List genres){ + // @Transactional(readOnly = true) + // public Page getAll(Long typeId, List genres, int page, int size){ + // final Pageable pageRequest = PageRequest.of(page, size); - var genresEnt = new ArrayList<>(); - for(var genreid : genres){ - genresEnt.add(genreService.get(genreid)); - } + // if(!Objects.equals(typeId, 0L) && !Objects.equals(genres.size(), 0)){ + // return repository.findByTypeIdAndGenres(typeId, genres, pageRequest); + // } + // if(Objects.equals(typeId, 0L) && !Objects.equals(genres.size(), 0)){ + // return repository.findByGenres(genres, pageRequest); + // } + // if(!Objects.equals(typeId, 0L) && Objects.equals(genres.size(), 0)){ + // return repository.findByTypeId(typeId, pageRequest); + // } + // return repository.findAll(PageRequest.of(page, size)); + // } + //getAll.stream().filter(game -> game.getType().getId().equals(typeId)).toList(); - if(!Objects.equals(typeId, 0L) && !Objects.equals(genresEnt.size(), 0)){ - return repository.getAll().stream().filter(game -> game.getType().getId().equals(typeId) && game.getGenres().containsAll(genresEnt)).toList(); + @Transactional(readOnly = true) + public Page getAll(Long typeId, Long genre, int page, int size){ + final Pageable pageRequest = PageRequest.of(page, size); + + if(!Objects.equals(typeId, 0L) && !Objects.equals(genre, 0L)){ + return repository.findByTypeIdAndGenres(typeId, genre, pageRequest); } - if(Objects.equals(typeId, 0L) && !Objects.equals(genresEnt.size(), 0)){ - return repository.getAll().stream().filter(game -> game.getGenres().containsAll(genresEnt)).toList(); + if(Objects.equals(typeId, 0L) && !Objects.equals(genre, 0L)){ + return repository.findByGenres(genre, pageRequest); } - if(!Objects.equals(typeId, 0L) && Objects.equals(genresEnt.size(), 0)){ - return repository.getAll().stream().filter(game -> game.getType().getId().equals(typeId)).toList(); + if(!Objects.equals(typeId, 0L) && Objects.equals(genre, 0L)){ + return repository.findByTypeId(typeId, pageRequest); } - return repository.getAll(); + return repository.findAll(pageRequest); } + @Transactional(readOnly = true) public GameEntity get(Long id){ - return Optional.ofNullable(repository.get(id)).orElseThrow(() -> new NotFoundException(id)); + return repository.findOneById(id).orElseThrow(() -> new NotFoundException(GameEntity.class, id)); } + @Transactional public GameEntity create(GameEntity entity){ - return repository.create(entity); + if (entity == null) { + throw new IllegalArgumentException("Entity is null"); + } + return repository.save(entity); } + @Transactional public GameEntity update(Long id, GameEntity entity){ final GameEntity existEntity = get(id); existEntity.setName(entity.getName()); @@ -59,11 +78,13 @@ public class GameService { for(var genre : genres){ existEntity.setGenres(genre); } - return repository.update(existEntity); + return repository.save(existEntity); } + @Transactional public GameEntity delete(Long id){ final GameEntity existEntity = get(id); - return repository.delete(existEntity); + repository.delete(existEntity); + return existEntity; } } diff --git a/src/main/java/com/example/demo/genres/api/GenreDto.java b/src/main/java/com/example/demo/genres/api/GenreDto.java index 97a9778..5f3c6e0 100644 --- a/src/main/java/com/example/demo/genres/api/GenreDto.java +++ b/src/main/java/com/example/demo/genres/api/GenreDto.java @@ -2,20 +2,20 @@ package com.example.demo.genres.api; import com.fasterxml.jackson.annotation.JsonProperty; -import jakarta.validation.constraints.Min; -import jakarta.validation.constraints.NotNull; +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.Size; public class GenreDto { - private Long id; - @NotNull - @Min(1) - private String name; - @NotNull - @JsonProperty(access = JsonProperty.Access.READ_ONLY) - public Long getId() { + private Long id; + @NotBlank + @Size(min = 1, max = 50) + private String name; + + public Long getId(){ return id; } + public void setId(Long id){ this.id = id; } @@ -23,6 +23,7 @@ public class GenreDto { public String getName(){ return name; } + public void setName(String name){ this.name = name; } diff --git a/src/main/java/com/example/demo/genres/model/GenreEntity.java b/src/main/java/com/example/demo/genres/model/GenreEntity.java index 7213eab..a97eeb3 100644 --- a/src/main/java/com/example/demo/genres/model/GenreEntity.java +++ b/src/main/java/com/example/demo/genres/model/GenreEntity.java @@ -4,15 +4,20 @@ import java.util.Objects; import com.example.demo.core.model.BaseEntity; +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.Table; + +@Entity +@Table(name = "genres") public class GenreEntity extends BaseEntity { + @Column(nullable = false, unique = true, length = 50) private String name; public GenreEntity(){ - super(); } - public GenreEntity(Long id, String name){ - super(id); + public GenreEntity(String name){ this.name = name; } diff --git a/src/main/java/com/example/demo/genres/repository/GenreRepository.java b/src/main/java/com/example/demo/genres/repository/GenreRepository.java index bd86082..13d6932 100644 --- a/src/main/java/com/example/demo/genres/repository/GenreRepository.java +++ b/src/main/java/com/example/demo/genres/repository/GenreRepository.java @@ -1,10 +1,11 @@ package com.example.demo.genres.repository; -import org.springframework.stereotype.Repository; +import java.util.Optional; + +import org.springframework.data.repository.CrudRepository; -import com.example.demo.core.repository.MapRepository; import com.example.demo.genres.model.GenreEntity; -@Repository -public class GenreRepository extends MapRepository { +public interface GenreRepository extends CrudRepository { + Optional findByNameIgnoreCase(String name); } diff --git a/src/main/java/com/example/demo/genres/service/GenreService.java b/src/main/java/com/example/demo/genres/service/GenreService.java index 08e6b46..aea07a9 100644 --- a/src/main/java/com/example/demo/genres/service/GenreService.java +++ b/src/main/java/com/example/demo/genres/service/GenreService.java @@ -1,7 +1,9 @@ package com.example.demo.genres.service; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + import java.util.List; -import java.util.Optional; +import java.util.stream.StreamSupport; import com.example.demo.core.error.NotFoundException; import com.example.demo.genres.model.GenreEntity; @@ -17,26 +19,35 @@ public class GenreService { this.repository = repository; } + @Transactional(readOnly = true) public List getAll(){ - return repository.getAll(); + return StreamSupport.stream(repository.findAll().spliterator(), false).toList(); } + @Transactional(readOnly = true) public GenreEntity get(Long id){ - return Optional.ofNullable(repository.get(id)).orElseThrow(() -> new NotFoundException(id)); + return repository.findById(id).orElseThrow(() -> new NotFoundException(GenreEntity.class, id)); } + @Transactional public GenreEntity create(GenreEntity entity){ - return repository.create(entity); + if (entity == null) { + throw new IllegalArgumentException("Entity is null"); + } + return repository.save(entity); } + @Transactional public GenreEntity update(Long id, GenreEntity entity){ final GenreEntity exisEntity = get(id); exisEntity.setName(entity.getName()); - return repository.update(exisEntity); + return repository.save(exisEntity); } + @Transactional public GenreEntity delete(Long id){ final GenreEntity existEntity = get(id); - return repository.delete(existEntity); + repository.delete(existEntity); + return existEntity; } } diff --git a/src/main/java/com/example/demo/orders/api/OrderController.java b/src/main/java/com/example/demo/orders/api/OrderController.java index b0c12be..7e2c55b 100644 --- a/src/main/java/com/example/demo/orders/api/OrderController.java +++ b/src/main/java/com/example/demo/orders/api/OrderController.java @@ -10,7 +10,6 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; -import java.util.List; import com.example.demo.core.configuration.Constants; import com.example.demo.games.model.GameEntity; @@ -18,33 +17,27 @@ import com.example.demo.games.service.GameService; import com.example.demo.orders.model.OrderEntity; import com.example.demo.orders.service.OrderService; import com.example.demo.users.service.UserService; +import com.example.demo.core.api.PageDtoMapper; import jakarta.validation.Valid; @RestController -@RequestMapping(Constants.API_URL + "/order") +@RequestMapping(Constants.API_URL + "/user/{user}/order") public class OrderController { private final GameService gameService; private final ModelMapper modelMapper; private final OrderService orderService; - //private final UserService userService; public OrderController(GameService gameService, ModelMapper modelMapper, OrderService orderService, UserService userService){ this.gameService = gameService; this.modelMapper = modelMapper; this.orderService = orderService; - //this.userService = userService; } private OrderDto toDto(OrderEntity entity){ var dto = new OrderDto(); dto.setId(entity.getId()); - double sum = 0; - for(var game : entity.getGames()){ - sum += game.getPrice(); - } - dto.setUserId(entity.getUserId()); - dto.setSum(sum); + dto.setUserId(entity.getUser().getId()); dto.setGames(entity.getGames().stream().map(GameEntity::getId).toList()); return dto; } @@ -59,22 +52,32 @@ public class OrderController { } @GetMapping - public List getAll(@RequestParam(name = "userId", defaultValue = "") Long userId){ - return orderService.getAll(userId).stream().map(this::toDto).toList(); + public com.example.demo.core.api.PageDto getAll( + @RequestParam(name = "userId", defaultValue = "") Long userId, + @RequestParam(name = "page", defaultValue = "0") int page, + @RequestParam(name = "size", defaultValue = Constants.DEFAULT_PAGE_SIZE)int size){ + return PageDtoMapper.toDto(orderService.getAll(userId, page, size), this::toDto); } + @GetMapping("/{id}") - public OrderDto get(@PathVariable(name = "id")Long id){ - return toDto(orderService.get(id)); + public OrderDto get( + @PathVariable(name = "user") Long userId, + @PathVariable(name = "id")Long id) { + return toDto(orderService.get(userId, id)); } @PostMapping - public OrderDto create(@RequestBody @Valid OrderDto dto){ - return toDto(orderService.create(toEntity(dto))); + public OrderDto create( + @PathVariable(name = "user") Long userId, + @RequestBody @Valid OrderDto dto){ + return toDto(orderService.create(userId, toEntity(dto))); } @DeleteMapping("/{id}") - public OrderDto delete(@PathVariable(name = "id") Long id){ - return toDto(orderService.delete(id)); + public OrderDto delete( + @PathVariable(name = "user") Long userId, + @PathVariable(name = "id") Long id){ + return toDto(orderService.delete(userId, id)); } } diff --git a/src/main/java/com/example/demo/orders/api/OrderDto.java b/src/main/java/com/example/demo/orders/api/OrderDto.java index 57c444f..abf1d68 100644 --- a/src/main/java/com/example/demo/orders/api/OrderDto.java +++ b/src/main/java/com/example/demo/orders/api/OrderDto.java @@ -9,17 +9,14 @@ import jakarta.validation.constraints.Min; import jakarta.validation.constraints.NotNull; public class OrderDto { + @JsonProperty(access = JsonProperty.Access.READ_ONLY) private Long id; @NotNull @Min(1) - private double sum; - @NotNull - @Min(1) private Long userId; @NotNull private final List games = new ArrayList<>(); - @JsonProperty(access = JsonProperty.Access.READ_ONLY) public Long getId(){ return id; } @@ -27,14 +24,6 @@ public class OrderDto { this.id = id; } - @JsonProperty(access = JsonProperty.Access.READ_ONLY) - public double getSum(){ - return sum; - } - public void setSum(double sum){ - this.sum = sum; - } - public List getGames(){ return games; } diff --git a/src/main/java/com/example/demo/orders/model/OrderEntity.java b/src/main/java/com/example/demo/orders/model/OrderEntity.java index 407e0fe..ade2829 100644 --- a/src/main/java/com/example/demo/orders/model/OrderEntity.java +++ b/src/main/java/com/example/demo/orders/model/OrderEntity.java @@ -2,46 +2,60 @@ package com.example.demo.orders.model; import java.util.List; +import java.util.HashSet; import java.util.Objects; - -import java.util.ArrayList; +import java.util.Set; import com.example.demo.core.model.BaseEntity; import com.example.demo.games.model.GameEntity; +import com.example.demo.users.model.UserEntity; +import jakarta.persistence.Entity; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.OneToMany; +import jakarta.persistence.Table; + +@Entity +@Table(name = "orders") public class OrderEntity extends BaseEntity{ - private double sum; - private Long userId; - private final List games = new ArrayList<>(); + // @Column(nullable = false) + // private double sum; + @ManyToOne + @JoinColumn(name = "userId", nullable = false) + private UserEntity user; + @OneToMany + private Set games = new HashSet<>(); public OrderEntity(){ - super(); } - public OrderEntity(Long id, Long userId, List games){ - super(id); - this.userId = userId; + public OrderEntity(UserEntity user, List games){ + this.user = user; this.games.clear(); this.games.addAll(games); } - public double getSum(){ + // public double getSum(){ - for(var game : games){ - sum += game.getPrice(); + // for(var game : games){ + // sum += game.getPrice(); + // } + + // return sum; + // } + + public UserEntity getUser(){ + return user; + } + public void setUser(UserEntity user){ + this.user = user; + if(!user.getOrders().contains(this)){ + user.getOrders().add(this); } - - return sum; } - public Long getUserId(){ - return userId; - } - public void setUserId(Long userId){ - this.userId = userId; - } - - public List getGames(){ + public Set getGames(){ return games; } public void setGames(GameEntity game){ @@ -50,7 +64,7 @@ public class OrderEntity extends BaseEntity{ @Override public int hashCode(){ - return Objects.hash(id, sum, games); + return Objects.hash(id, games); } @Override @@ -60,7 +74,7 @@ public class OrderEntity extends BaseEntity{ final OrderEntity other = (OrderEntity) obj; return Objects.equals(other.getId(), id) - && Objects.equals(other.getSum(), sum) + //&& Objects.equals(other.getSum(), sum) && Objects.equals(other.getGames(), games); } } diff --git a/src/main/java/com/example/demo/orders/repository/OrderRepository.java b/src/main/java/com/example/demo/orders/repository/OrderRepository.java index 18272ed..14049fd 100644 --- a/src/main/java/com/example/demo/orders/repository/OrderRepository.java +++ b/src/main/java/com/example/demo/orders/repository/OrderRepository.java @@ -1,11 +1,22 @@ package com.example.demo.orders.repository; -import org.springframework.stereotype.Repository; -import com.example.demo.core.repository.MapRepository; +import java.util.List; +import java.util.Optional; + +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; + +import org.springframework.data.repository.CrudRepository; +import org.springframework.data.repository.PagingAndSortingRepository; + import com.example.demo.orders.model.OrderEntity; -@Repository +public interface OrderRepository extends CrudRepository, PagingAndSortingRepository { + Optional findOneByUserIdAndId(long userId, long id); -public class OrderRepository extends MapRepository{ + List findByUserId(long userId); + Page findByUserId(long userId, Pageable pageable); + + //Можно сделать запрос на сумму JPQL } diff --git a/src/main/java/com/example/demo/orders/service/OrderService.java b/src/main/java/com/example/demo/orders/service/OrderService.java index c58b75e..8fd2ed9 100644 --- a/src/main/java/com/example/demo/orders/service/OrderService.java +++ b/src/main/java/com/example/demo/orders/service/OrderService.java @@ -1,40 +1,63 @@ package com.example.demo.orders.service; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import java.util.List; -import java.util.Objects; -import java.util.Optional; import com.example.demo.core.error.NotFoundException; import com.example.demo.orders.model.OrderEntity; import com.example.demo.orders.repository.OrderRepository; +import com.example.demo.users.model.UserEntity; +import com.example.demo.users.service.UserService; @Service public class OrderService { private final OrderRepository repository; + private final UserService userService; - public OrderService(OrderRepository repository){ + public OrderService(OrderRepository repository, UserService userService){ this.repository = repository; + this.userService = userService; } - public List getAll(Long userId){ - if(!Objects.equals(userId, null)){ - return repository.getAll().stream().filter(user -> user.getId().equals(userId)).toList(); + @Transactional(readOnly = true) + public List getAll(long userId){ + userService.get(userId); + return repository.findByUserId(userId); + } + + @Transactional(readOnly = true) + public Page getAll(long userId, int page, int size){ + final Pageable pageRequest = PageRequest.of(page, size); + userService.get(userId); + return repository.findByUserId(userId, pageRequest); + } + + @Transactional(readOnly = true) + public OrderEntity get(long userId, long id){ + userService.get(userId); + return repository.findOneByUserIdAndId(userId, id).orElseThrow(() -> new NotFoundException(OrderEntity.class, id)); + } + + @Transactional + public OrderEntity create(long userId, OrderEntity entity){ + if(entity == null){ + throw new IllegalArgumentException("Entity is null"); } - return repository.getAll(); + final UserEntity existsUser = userService.get(userId); + entity.setUser(existsUser); + return repository.save(entity); } - public OrderEntity get(Long id){ - return Optional.ofNullable(repository.get(id)).orElseThrow(() -> new NotFoundException(id)); - } - - public OrderEntity create(OrderEntity entity){ - return repository.create(entity); - } - - public OrderEntity delete(Long id){ - final OrderEntity exisEntity = get(id); - return repository.delete(exisEntity); + @Transactional + public OrderEntity delete(long userId, long id){ + userService.get(userId); + final OrderEntity existsEntity = get(userId, id); + repository.delete(existsEntity); + return existsEntity; } } diff --git a/src/main/java/com/example/demo/types/api/TypeDto.java b/src/main/java/com/example/demo/types/api/TypeDto.java index f0ae6ec..e451a22 100644 --- a/src/main/java/com/example/demo/types/api/TypeDto.java +++ b/src/main/java/com/example/demo/types/api/TypeDto.java @@ -3,13 +3,15 @@ package com.example.demo.types.api; import com.fasterxml.jackson.annotation.JsonProperty; import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.Size; public class TypeDto { + @JsonProperty(access = JsonProperty.Access.READ_ONLY) private Long id; @NotBlank + @Size(min = 1, max = 50) private String name; - @JsonProperty(access = JsonProperty.Access.READ_ONLY) public Long getId(){ return id; } diff --git a/src/main/java/com/example/demo/types/model/TypeEntity.java b/src/main/java/com/example/demo/types/model/TypeEntity.java index c60fcef..f58ffeb 100644 --- a/src/main/java/com/example/demo/types/model/TypeEntity.java +++ b/src/main/java/com/example/demo/types/model/TypeEntity.java @@ -4,15 +4,20 @@ import java.util.Objects; import com.example.demo.core.model.BaseEntity; +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.Table; + +@Entity +@Table(name = "types") public class TypeEntity extends BaseEntity{ + @Column(nullable = false, unique = true, length = 50) private String name; public TypeEntity(){ - super(); } - public TypeEntity(Long id, String name){ - super(id); + public TypeEntity(String name){ this.name = name; } diff --git a/src/main/java/com/example/demo/types/repository/TypeRepository.java b/src/main/java/com/example/demo/types/repository/TypeRepository.java index 4474581..9fea284 100644 --- a/src/main/java/com/example/demo/types/repository/TypeRepository.java +++ b/src/main/java/com/example/demo/types/repository/TypeRepository.java @@ -1,11 +1,11 @@ package com.example.demo.types.repository; -import org.springframework.stereotype.Repository; +import java.util.Optional; + +import org.springframework.data.repository.CrudRepository; -import com.example.demo.core.repository.MapRepository; import com.example.demo.types.model.TypeEntity; -@Repository -public class TypeRepository extends MapRepository { - +public interface TypeRepository extends CrudRepository { + Optional findByNameIgnoreCase(String name); } diff --git a/src/main/java/com/example/demo/types/service/TypeService.java b/src/main/java/com/example/demo/types/service/TypeService.java index 095ba44..7c1fd46 100644 --- a/src/main/java/com/example/demo/types/service/TypeService.java +++ b/src/main/java/com/example/demo/types/service/TypeService.java @@ -1,9 +1,11 @@ package com.example.demo.types.service; import java.util.List; -import java.util.Optional; +import java.util.stream.StreamSupport; + import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import com.example.demo.core.error.NotFoundException; import com.example.demo.types.model.TypeEntity; @@ -17,26 +19,35 @@ public class TypeService { this.repository = repository; } + @Transactional(readOnly = true) public List getAll(){ - return repository.getAll(); + return StreamSupport.stream(repository.findAll().spliterator(), false).toList(); } + @Transactional(readOnly = true) public TypeEntity get(Long id){ - return Optional.ofNullable(repository.get(id)).orElseThrow(() -> new NotFoundException(id)); + return repository.findById(id).orElseThrow(() -> new NotFoundException(TypeEntity.class, id)); } + @Transactional public TypeEntity create(TypeEntity entity){ - return repository.create(entity); + if(entity == null){ + throw new IllegalArgumentException("Entity is null"); + } + return repository.save(entity); } + @Transactional public TypeEntity update(Long id, TypeEntity entity){ final TypeEntity existsEntity = get(id); existsEntity.setName(entity.getName()); - return repository.update(existsEntity); + return repository.save(existsEntity); } + @Transactional public TypeEntity delete(Long id){ final TypeEntity existsEntity = get(id); - return repository.delete(existsEntity); + repository.delete(existsEntity); + return existsEntity; } } diff --git a/src/main/java/com/example/demo/users/api/UserController.java b/src/main/java/com/example/demo/users/api/UserController.java index 9b9c8e2..102cf11 100644 --- a/src/main/java/com/example/demo/users/api/UserController.java +++ b/src/main/java/com/example/demo/users/api/UserController.java @@ -8,10 +8,11 @@ import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; -import java.util.List; - +import com.example.demo.core.api.PageDto; +import com.example.demo.core.api.PageDtoMapper; import com.example.demo.core.configuration.Constants; import com.example.demo.orders.service.OrderService; import com.example.demo.users.model.UserEntity; @@ -31,22 +32,18 @@ public class UserController { } private UserDto toDto(UserEntity entity){ - var dto = new UserDto(); - dto.setId(entity.getId()); - dto.setLogin(entity.getLogin()); - dto.setEmail(entity.getEmail()); - dto.setPassword(entity.getPassword()); - return dto; + return modelMapper.map(entity, UserDto.class); } private UserEntity toEntity(UserDto dto){ - final UserEntity entity = modelMapper.map(dto, UserEntity.class); - return entity; + return modelMapper.map(dto, UserEntity.class); } @GetMapping - public List getAll(){ - return userService.getAll().stream().map(this::toDto).toList(); + public PageDto getAll( + @RequestParam(name = "page", defaultValue = "0") int page, + @RequestParam(name = "size", defaultValue = Constants.DEFAULT_PAGE_SIZE) int size) { + return PageDtoMapper.toDto(userService.getAll(page, size), this::toDto); } @GetMapping("/{id}") diff --git a/src/main/java/com/example/demo/users/api/UserDto.java b/src/main/java/com/example/demo/users/api/UserDto.java index 4de59c1..a0e0584 100644 --- a/src/main/java/com/example/demo/users/api/UserDto.java +++ b/src/main/java/com/example/demo/users/api/UserDto.java @@ -3,17 +3,21 @@ package com.example.demo.users.api; import com.fasterxml.jackson.annotation.JsonProperty; import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.Size; public class UserDto { + @JsonProperty(access = JsonProperty.Access.READ_ONLY) private Long id; @NotBlank + @Size(min = 2, max = 20) private String login; @NotBlank + @Size(min = 2, max = 20) private String email; @NotBlank + @Size(min = 2, max = 20) private String password; - @JsonProperty(access = JsonProperty.Access.READ_ONLY) public Long getId(){ return id; } diff --git a/src/main/java/com/example/demo/users/model/UserEntity.java b/src/main/java/com/example/demo/users/model/UserEntity.java index b9e2135..e093a4a 100644 --- a/src/main/java/com/example/demo/users/model/UserEntity.java +++ b/src/main/java/com/example/demo/users/model/UserEntity.java @@ -1,18 +1,35 @@ package com.example.demo.users.model; -import com.example.demo.core.model.BaseEntity; +import java.util.ArrayList; +import java.util.List; +import com.example.demo.core.model.BaseEntity; +import com.example.demo.orders.model.OrderEntity; + +import jakarta.persistence.CascadeType; +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.OneToMany; +import jakarta.persistence.OrderBy; +import jakarta.persistence.Table; + +@Entity +@Table(name = "users") public class UserEntity extends BaseEntity{ + @Column(nullable = false, unique = true, length = 20) private String login; + @Column(nullable = false, unique = true, length = 20) private String email; + @Column(nullable = false, unique = true, length = 20) private String password; + @OneToMany(mappedBy = "user", cascade = CascadeType.ALL) + @OrderBy("id ASC") + private List orders = new ArrayList<>(); public UserEntity(){ - super(); } - public UserEntity(Long id, String login, String email, String password){ - super(id); + public UserEntity(String login, String email, String password){ this.login = login; this.email = email; this.password = password; @@ -38,4 +55,15 @@ public class UserEntity extends BaseEntity{ public void setPassword(String password){ this.password = password; } + + public List getOrders(){ + return orders; + } + + public void addOrder(OrderEntity order){ + if(order.getUser() != this){ + order.setUser(this); + } + orders.add(order); + } } diff --git a/src/main/java/com/example/demo/users/repository/UserRepository.java b/src/main/java/com/example/demo/users/repository/UserRepository.java index dbc0e52..16d30c7 100644 --- a/src/main/java/com/example/demo/users/repository/UserRepository.java +++ b/src/main/java/com/example/demo/users/repository/UserRepository.java @@ -1,11 +1,12 @@ package com.example.demo.users.repository; -import org.springframework.stereotype.Repository; +import java.util.Optional; + +import org.springframework.data.repository.CrudRepository; +import org.springframework.data.repository.PagingAndSortingRepository; -import com.example.demo.core.repository.MapRepository; import com.example.demo.users.model.UserEntity; -@Repository -public class UserRepository extends MapRepository { - +public interface UserRepository extends CrudRepository, PagingAndSortingRepository { + Optional findByLoginIgnoreCase(String login); } diff --git a/src/main/java/com/example/demo/users/service/UserService.java b/src/main/java/com/example/demo/users/service/UserService.java index 749fc6d..040b5d4 100644 --- a/src/main/java/com/example/demo/users/service/UserService.java +++ b/src/main/java/com/example/demo/users/service/UserService.java @@ -1,9 +1,13 @@ package com.example.demo.users.service; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import java.util.List; -import java.util.Optional; +import java.util.stream.StreamSupport; + +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; import com.example.demo.core.error.NotFoundException; import com.example.demo.users.model.UserEntity; @@ -17,28 +21,43 @@ public class UserService { this.repository = repository; } + @Transactional(readOnly = true) public List getAll(){ - return repository.getAll().stream().toList(); + return StreamSupport.stream(repository.findAll().spliterator(), false).toList(); } + @Transactional(readOnly = true) + public Page getAll(int page, int size) { + return repository.findAll(PageRequest.of(page, size)); + } + + @Transactional(readOnly = true) public UserEntity get(Long id){ - return Optional.ofNullable(repository.get(id)).orElseThrow(()-> new NotFoundException(id)); + return repository.findById(id).orElseThrow(() -> new NotFoundException(UserEntity.class, id)); } + @Transactional public UserEntity create(UserEntity entity){ - return repository.create(entity); + if (entity == null) { + throw new IllegalArgumentException("Entity is null"); + } + return repository.save(entity); } + @Transactional public UserEntity update(Long id, UserEntity entity){ final UserEntity existEntity = get(id); existEntity.setLogin(entity.getLogin()); existEntity.setEmail(entity.getEmail()); existEntity.setPassword(entity.getPassword()); - return repository.update(existEntity); + repository.save(existEntity); + return existEntity; } + @Transactional public UserEntity delete(Long id){ final UserEntity existEntity = get(id); - return repository.delete(existEntity); + repository.delete(existEntity); + return existEntity; } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 8b13789..f803d58 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1 +1,19 @@ +spring.main.banner-mode=off +server.port=8080 +# Logger settings +# Available levels are: TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF +logging.level.com.example.demo=DEBUG + +# JPA Settings +spring.datasource.url=jdbc:h2:file:./data +spring.datasource.username=sa +spring.datasource.password=password +spring.datasource.driver-class-name=org.h2.Driver +spring.jpa.hibernate.ddl-auto=create +spring.jpa.open-in-view=false +spring.jpa.show-sql=true +# spring.jpa.properties.hibernate.format_sql=true + +# H2 console +spring.h2.console.enabled=true \ No newline at end of file diff --git a/src/test/java/com/example/demo/GameServiceTests.java b/src/test/java/com/example/demo/GameServiceTests.java deleted file mode 100644 index a31f472..0000000 --- a/src/test/java/com/example/demo/GameServiceTests.java +++ /dev/null @@ -1,62 +0,0 @@ -package com.example.demo; - -import java.util.ArrayList; -import java.util.List; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; -import org.junit.jupiter.api.Order; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestMethodOrder; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; - -import com.example.demo.core.error.NotFoundException; -import com.example.demo.games.model.GameEntity; -import com.example.demo.games.service.GameService; -import com.example.demo.genres.model.GenreEntity; - - -@SpringBootTest -@TestMethodOrder(OrderAnnotation.class) -class GameServiceTests { - @Autowired - private GameService gameService; - List list = new ArrayList(); - List list2 = new ArrayList(); - - @Test - void getTest(){ - Assertions.assertThrows(NotFoundException.class, () -> gameService.get(0L)); - } - - @Test - @Order(1) - void createTest(){ - - list.add(new GenreEntity(null, "приключения")); - list.add(new GenreEntity(null, "Симулятор")); - gameService.create(new GameEntity(null, null, "Game", 210.0, "cool game", list)); - gameService.create(new GameEntity(null, null, "VideoGame", 2100.0, "very cool game", list)); - Assertions.assertEquals(4, gameService.getAll(0L,list2).size()); - } - - @Test - @Order(2) - void updateTest(){ - final GameEntity newGame = new GameEntity(null, null, "Programm", 1500.0, "cool not game", list); - final GameEntity updGame = gameService.update(2L, newGame); - Assertions.assertEquals(4, gameService.getAll(0L, list2).size()); - Assertions.assertEquals(updGame, gameService.get(2L)); - Assertions.assertEquals(newGame.getName(), updGame.getName()); - Assertions.assertEquals(newGame.getDescription(), updGame.getDescription()); - Assertions.assertEquals(newGame.getPrice(), updGame.getPrice()); - } - - @Test - @Order(3) - void deleteTest(){ - gameService.delete(2L); - Assertions.assertEquals(3L, gameService.getAll(0L,list2).size()); - } -} diff --git a/src/test/java/com/example/demo/OrderServiceTest.java b/src/test/java/com/example/demo/OrderServiceTest.java deleted file mode 100644 index 3d7fda5..0000000 --- a/src/test/java/com/example/demo/OrderServiceTest.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.example.demo; - -import java.util.ArrayList; -import java.util.List; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Order; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestMethodOrder; -import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; - -import com.example.demo.core.error.NotFoundException; -import com.example.demo.games.model.GameEntity; -import com.example.demo.games.service.GameService; -import com.example.demo.orders.model.OrderEntity; -import com.example.demo.orders.service.OrderService; - -@SpringBootTest -@TestMethodOrder(OrderAnnotation.class) -public class OrderServiceTest { - @Autowired - private OrderService orderService; - @Autowired - private GameService gameService; - - @Test - void getTest(){ - Assertions.assertThrows(NotFoundException.class, () -> orderService.get(0L)); - } - - @Test - @Order(1) - void createTest(){ - final List games = new ArrayList(); - games.add(gameService.get(1L)); - orderService.create(new OrderEntity(null, 1L,games)); - final var last = orderService.create(new OrderEntity(null, 2L,games)); - Assertions.assertEquals(4, orderService.getAll(null).size()); - Assertions.assertEquals(last, orderService.get(4L)); - } - - @Test - @Order(2) - void deleteTest(){ - orderService.delete(2L); - Assertions.assertEquals(3, orderService.getAll(null).size()); - final OrderEntity last = orderService.get(1L); - Assertions.assertEquals(1L, last.getId()); - } -} diff --git a/src/test/java/com/example/demo/TypeServiceTests.java b/src/test/java/com/example/demo/TypeServiceTests.java deleted file mode 100644 index e0a3e39..0000000 --- a/src/test/java/com/example/demo/TypeServiceTests.java +++ /dev/null @@ -1,58 +0,0 @@ -package com.example.demo; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Order; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestMethodOrder; -import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; - -import com.example.demo.core.error.NotFoundException; -import com.example.demo.types.model.TypeEntity; -import com.example.demo.types.service.TypeService; - -@SpringBootTest -@TestMethodOrder(OrderAnnotation.class) -class TypeServiceTests { - @Autowired - private TypeService typeService; - - @Test - void getTest(){ - Assertions.assertThrows(NotFoundException.class, () -> typeService.get(0L)); - } - - @Test - @Order(1) - void createTest(){ - typeService.create(new TypeEntity(null, "Игра")); - typeService.create(new TypeEntity(null, "Программа")); - final TypeEntity last = typeService.create(new TypeEntity(null, "Игра2")); - Assertions.assertEquals(5, typeService.getAll().size()); - Assertions.assertEquals(last, typeService.get(5L)); - } - - @Test - @Order(2) - void updateTest(){ - final String test = "TEST"; - final TypeEntity newEntity = typeService.update(5L, new TypeEntity(1L, test)); - Assertions.assertEquals(5, typeService.getAll().size()); - Assertions.assertEquals(newEntity, typeService.get(5L)); - Assertions.assertEquals(test, newEntity.getName()); - } - - @Test - @Order(3) - void deleteTest(){ - typeService.delete(5L); - Assertions.assertEquals(4, typeService.getAll().size()); - final TypeEntity last = typeService.get(4L); - Assertions.assertEquals(4L, last.getId()); - - final TypeEntity newEntity = typeService.create(new TypeEntity(null, "Игра")); - Assertions.assertEquals(5, typeService.getAll().size()); - Assertions.assertEquals(6L, newEntity.getId()); - } -} diff --git a/src/test/java/com/example/demo/UserServiceTest.java b/src/test/java/com/example/demo/UserServiceTest.java deleted file mode 100644 index c58f269..0000000 --- a/src/test/java/com/example/demo/UserServiceTest.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.example.demo; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Order; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestMethodOrder; -import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; - -import com.example.demo.core.error.NotFoundException; -import com.example.demo.users.model.UserEntity; -import com.example.demo.users.service.UserService; - -@SpringBootTest -@TestMethodOrder(OrderAnnotation.class) -public class UserServiceTest { - @Autowired - UserService userService; - - @Test - void getTest(){ - Assertions.assertThrows(NotFoundException.class, () -> userService.get(0L)); - } - - @Test - @Order(1) - void createTest(){ - userService.create(new UserEntity(null, "login5", "gmail", "qwert123")); - final UserEntity last = userService.create(new UserEntity(null, "login6", "mail", "qwer123")); - Assertions.assertEquals(4, userService.getAll().size()); - Assertions.assertEquals(last, userService.get(4L)); - } - - @Test - @Order(2) - void updateTest(){ - userService.update(4L, new UserEntity(null,"login","email", "qwe123")); - Assertions.assertEquals(4, userService.getAll().size()); - } - - @Test - @Order(3) - void deleteTest(){ - userService.delete(2L); - Assertions.assertEquals(3, userService.getAll().size()); - } -} diff --git a/src/test/resources/application.properties b/src/test/resources/application.properties new file mode 100644 index 0000000..d5f355c --- /dev/null +++ b/src/test/resources/application.properties @@ -0,0 +1,14 @@ +# Server +spring.main.banner-mode=off + +# Logger settings +# Available levels are: TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF +logging.level.com.example.demo=DEBUG + +# JPA Settings +spring.datasource.url=jdbc:h2:mem:testdb +spring.datasource.username=sa +spring.datasource.password=password +spring.datasource.driver-class-name=org.h2.Driver +spring.jpa.hibernate.ddl-auto=create +spring.jpa.open-in-view=false \ No newline at end of file