Add basic module and redis data with jpa and example
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2022-04-11 12:35:47 +07:00
parent bc81cb0988
commit 8c999a9e7c
14 changed files with 368 additions and 2 deletions

View File

@@ -0,0 +1,20 @@
package com.cubetiqs.web.modules.redis
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.data.redis.connection.RedisConnectionFactory
import org.springframework.data.redis.core.RedisTemplate
@RedisModule
@Configuration
class RedisConfig @Autowired constructor(
private val connectionFactory: RedisConnectionFactory,
) {
@Bean
fun redisTemplate(): RedisTemplate<String, RedisKVModel> {
val template = RedisTemplate<String, RedisKVModel>()
template.setConnectionFactory(connectionFactory)
return template
}
}

View File

@@ -0,0 +1,31 @@
package com.cubetiqs.web.modules.redis
import com.cubetiqs.web.util.RouteConstants
import io.swagger.v3.oas.annotations.tags.Tag
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.redis.core.RedisTemplate
import org.springframework.web.bind.annotation.*
@RedisModule
@Tag(name = "Redis Controller")
@RestController
@RequestMapping(RouteConstants.INDEX + "redis")
class RedisController @Autowired constructor(
private val redisTemplate: RedisTemplate<String, RedisKVModel>,
) {
@GetMapping("/{key}")
fun getAll(
@PathVariable("key") key: String,
): Collection<RedisKVModel?> {
return redisTemplate.opsForValue().multiGet(listOf(key)) ?: listOf()
}
@PostMapping("/{key}")
fun set(
@PathVariable("key") key: String,
@RequestBody body: RedisKVModel
): RedisKVModel {
redisTemplate.opsForValue().set(key, body)
return body
}
}

View File

@@ -0,0 +1,14 @@
package com.cubetiqs.web.modules.redis
import java.io.Serializable
data class RedisKVModel(
var key: String? = null,
var value: Any? = null,
) : Serializable {
companion object {
fun create(key: String, value: Any): RedisKVModel {
return RedisKVModel(key, value)
}
}
}

View File

@@ -0,0 +1,6 @@
package com.cubetiqs.web.modules.redis
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
@ConditionalOnProperty(name = ["spring.redis.enabled"], havingValue = "true")
annotation class RedisModule

View File

@@ -0,0 +1,60 @@
package com.cubetiqs.web.modules.user
import com.cubetiqs.web.util.RouteConstants
import io.swagger.v3.oas.annotations.Parameter
import io.swagger.v3.oas.annotations.tags.Tag
import org.springdoc.core.converters.models.PageableAsQueryParam
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.web.bind.annotation.*
import java.util.*
@UserModule
@Tag(name = "User Controller")
@RestController
@RequestMapping(RouteConstants.INDEX + "user")
class UserController @Autowired constructor(
private val userRepository: UserRepository,
) {
@GetMapping
@PageableAsQueryParam
fun getAll(
@Parameter(hidden = true)
pageable: Pageable?,
): Page<UserEntity> {
return userRepository.findAll(pageable ?: Pageable.unpaged())
}
@ResponseStatus(value = org.springframework.http.HttpStatus.CREATED)
@PostMapping
fun create(
@RequestBody body: UserEntity
): UserEntity {
return userRepository.save(body)
}
@ResponseStatus(value = org.springframework.http.HttpStatus.OK)
@PutMapping("/{id}")
fun update(
@PathVariable id: String,
@RequestBody body: UserEntity
): UserEntity {
val user = userRepository.findById(UUID.fromString(id)).orElseThrow {
throw IllegalArgumentException("User not found")
}
body.id = user.id
return userRepository.save(body)
}
@ResponseStatus(value = org.springframework.http.HttpStatus.NO_CONTENT)
@DeleteMapping("/{id}")
fun delete(
@PathVariable id: String,
) {
val user = userRepository.findById(UUID.fromString(id)).orElseThrow {
throw IllegalArgumentException("User not found")
}
userRepository.delete(user)
}
}

View File

@@ -0,0 +1,30 @@
package com.cubetiqs.web.modules.user
import org.hibernate.Hibernate
import java.io.Serializable
import java.util.*
import javax.persistence.*
@Entity
@Table(name = "user")
open class UserEntity(
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
open var id: UUID? = null,
@Column(name = "name", length = 50)
open var name: String? = null,
@Column(name = "username", length = 50, unique = true)
open var username: String? = null,
) : Serializable {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other == null || Hibernate.getClass(this) != Hibernate.getClass(other)) return false
other as UserEntity
return id != null && id == other.id
}
override fun hashCode(): Int = javaClass.hashCode()
}

View File

@@ -0,0 +1,6 @@
package com.cubetiqs.web.modules.user
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
@ConditionalOnProperty(name = ["module.user.enabled", "spring.datasource.enabled"], havingValue = "true")
annotation class UserModule

View File

@@ -0,0 +1,9 @@
package com.cubetiqs.web.modules.user
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.stereotype.Repository
import java.util.*
@UserModule
@Repository
interface UserRepository : JpaRepository<UserEntity, UUID>

View File

@@ -1,8 +1,22 @@
spring:
profiles:
active: ${APP_PROFILE:dev}
active: ${APP_PROFILE:demo}
application:
name: cubetiq-api-service
redis:
enabled: ${REDIS_ENABLED:false}
host: ${REDIS_HOST:localhost}
password: ${REDIS_PASSWORD:null}
datasource:
enabled: ${DATASOURCE_ENABLED:false}
driverClassName: ${DATA_SOURCE_DRIVER_CLASS_NAME:org.h2.Driver}
url: jdbc:h2:file:${H2_DB_PATH:${cubetiq.app.data-dir}/data/db};DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
username: ${H2_USERNAME:sa}
password: ${H2_PASSWORD:password}
module:
user:
enabled: ${MODULE_USER_ENABLED:false}
cubetiq:
app:

View File

@@ -0,0 +1,8 @@
----------------------- CUBEIQ @sombochea ---------------------------
________ _____ ___ __ ______
__ ___/________ ___________(_)_______ _______ _ __ | / /_____ ___ /_
_____ \ ___ __ \__ ___/__ / __ __ \__ __ `/ __ | /| / / _ _ \__ __ \
____/ / __ /_/ /_ / _ / _ / / /_ /_/ / __ |/ |/ / / __/_ /_/ /
/____/ _ .___/ /_/ /_/ /_/ /_/ _\__, / ____/|__/ \___/ /_.___/
/_/ /____/
Spring Boot Version: ${spring-boot.formatted-version}