Skip to content

Commit 413bc3e

Browse files
committed
first working version
1 parent 0fcfa71 commit 413bc3e

File tree

8 files changed

+31
-35
lines changed

8 files changed

+31
-35
lines changed

src/main/kotlin/com/ferum_bot/quotesapi/application/QuotesApiApplication.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import org.springframework.transaction.annotation.EnableTransactionManagement
3232
AuthorsDataSource::class,
3333
TagsDataSource::class,
3434
])
35-
@EnableTransactionManagement
3635
class QuotesApiApplication
3736

3837
fun main(args: Array<String>) {

src/main/kotlin/com/ferum_bot/quotesapi/configurations/RepositoryConfig.kt

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -41,31 +41,6 @@ class RepositoryConfig {
4141
@Autowired
4242
private lateinit var tagsDataSource: TagsDataSource
4343

44-
@Bean
45-
fun provideDataSource(): DataSource {
46-
val builder = EmbeddedDatabaseBuilder()
47-
return builder.setType(EmbeddedDatabaseType.HSQL).build()
48-
}
49-
50-
@Bean
51-
fun provideLocalContainerManager(): LocalContainerEntityManagerFactoryBean {
52-
val vendorAdapter = HibernateJpaVendorAdapter()
53-
vendorAdapter.setGenerateDdl(true)
54-
55-
val factory = LocalContainerEntityManagerFactoryBean()
56-
factory.jpaVendorAdapter = vendorAdapter
57-
factory.dataSource = provideDataSource()
58-
factory.setPackagesToScan("com.ferum_bot.quotesapi.repository.jpa")
59-
return factory
60-
}
61-
62-
@Bean
63-
fun provideTransactionManager(factory: EntityManagerFactory): PlatformTransactionManager {
64-
val txManager = JpaTransactionManager()
65-
txManager.entityManagerFactory = factory
66-
return txManager
67-
}
68-
6944
@Bean
7045
fun provideQuotesRepository(adapter: QuotesDataSourceAdapter): QuotesRepository {
7146
return QuotesRepositoryImpl(
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
package com.ferum_bot.quotesapi.configurations
22

3+
import com.ferum_bot.quotesapi.controllers.QuoteAuthorsController
4+
import com.ferum_bot.quotesapi.controllers.QuotesController
5+
import com.ferum_bot.quotesapi.controllers.TagsController
36
import org.springframework.context.annotation.ComponentScan
47
import org.springframework.context.annotation.Configuration
58

69
@Configuration
7-
@ComponentScan("com.ferum_bot.quotesapi.controllers")
10+
@ComponentScan(basePackageClasses = [
11+
QuoteAuthorsController::class,
12+
QuotesController::class,
13+
TagsController::class,
14+
])
815
class ScanConfig

src/main/kotlin/com/ferum_bot/quotesapi/models/entity/QuoteEntity.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,10 @@ data class QuoteEntity(
1515

1616
var text: String,
1717

18-
@Column(name = "author")
1918
@ManyToOne
2019
@JoinColumn(name = "author_entity_id", referencedColumnName = "id")
2120
var author: AuthorEntity? = null,
2221

23-
@Column(name = "tag")
2422
@ManyToOne
2523
@JoinColumn(name = "tag_entity_id", referencedColumnName = "id")
2624
var tag: TagEntity? = null,

src/main/kotlin/com/ferum_bot/quotesapi/repositories/jpa/AuthorsDataSource.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ import org.springframework.data.jpa.repository.Query
88

99
interface AuthorsDataSource: JpaRepository<AuthorEntity, Long> {
1010

11-
@Query("SELECT AuthorEntity FROM AuthorEntity WHERE authorFullName LIKE ?1")
11+
@Query(
12+
value = "SELECT ALL FROM author_entity WHERE author_full_name=?1",
13+
nativeQuery = true,
14+
)
1215
fun getAllAuthorsWhereNameContains(text: String, pageable: Pageable): Page<AuthorEntity>
1316

1417
}

src/main/kotlin/com/ferum_bot/quotesapi/repositories/jpa/QuotesDataSource.kt

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,21 @@ import org.springframework.data.repository.CrudRepository
77

88
interface QuotesDataSource: JpaRepository<QuoteEntity, Long> {
99

10-
@Query("SELECT id FROM QuoteEntity")
10+
@Query(
11+
value = "SELECT id FROM quote_entity",
12+
nativeQuery = true
13+
)
1114
fun getAllAvailableIds(): List<Long>
1215

13-
@Query("SELECT QuoteEntity FROM QuoteEntity WHERE author.authorFullName = ?1")
16+
@Query(
17+
value = "SELECT ALL FROM QuoteEntity WHERE author.authorFullName = ?1",
18+
nativeQuery = true
19+
)
1420
fun getAllWhereAuthorIs(authorName: String): List<QuoteEntity>
1521

16-
@Query("SELECT QuoteEntity FROM QuoteEntity WHERE tag.tagName = ?1")
22+
@Query(
23+
value = "SELECT ALL FROM QuoteEntity WHERE tag.tagName = ?1",
24+
nativeQuery = true,
25+
)
1726
fun getAllWhereTagIs(tag: String): List<QuoteEntity>
1827
}

src/main/kotlin/com/ferum_bot/quotesapi/util/RandomUtil.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ object RandomUtil {
99
* Calculates random Ints between start and end inclusive.
1010
*/
1111
fun getRandomIntsBetween(start: Int, end: Int, count: Int): List<Int> {
12+
val segmentLength = end - start + 1
13+
if (segmentLength <= count) {
14+
return List(segmentLength) { it }
15+
}
16+
1217
val availableInts = mutableSetOf<Int>()
1318
val seed = System.currentTimeMillis()
1419
val random = Random(seed)
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
spring.jpa.generate-ddl=true
22

3-
spring.datasource.username=Matvey
3+
spring.datasource.username=matvejpopov
44
spring.datasource.password=1234567890
5+
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
56
spring.sql.init.platform=postgres
6-
spring.sql.init.mode=always

0 commit comments

Comments
 (0)