Initial commit
This commit is contained in:
commit
0686dd7c93
83 changed files with 2178 additions and 0 deletions
50
src/main/kotlin/ru/sicamp/sicamphelper/ApplicationConfig.kt
Normal file
50
src/main/kotlin/ru/sicamp/sicamphelper/ApplicationConfig.kt
Normal file
|
@ -0,0 +1,50 @@
|
|||
package ru.sicamp.sicamphelper
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper
|
||||
import com.fasterxml.jackson.module.kotlin.KotlinFeature
|
||||
import com.fasterxml.jackson.module.kotlin.KotlinModule
|
||||
import org.jetbrains.exposed.sql.DatabaseConfig
|
||||
import org.postgresql.PGProperty
|
||||
import org.springframework.context.annotation.Bean
|
||||
import org.springframework.context.annotation.Configuration
|
||||
import javax.sql.DataSource
|
||||
import org.postgresql.ds.PGSimpleDataSource
|
||||
import org.springframework.beans.factory.annotation.Value
|
||||
|
||||
@Configuration
|
||||
class ApplicationConfig {
|
||||
@Bean
|
||||
fun datasource(
|
||||
@Value("\${spring.datasource.url}")
|
||||
url: String,
|
||||
@Value("\${spring.datasource.username}")
|
||||
username: String,
|
||||
@Value("\${spring.datasource.password}")
|
||||
password: String,
|
||||
): DataSource {
|
||||
val dataSource = PGSimpleDataSource()
|
||||
dataSource.setURL(url)
|
||||
dataSource.user = username
|
||||
dataSource.password = password
|
||||
dataSource.setProperty(PGProperty.REWRITE_BATCHED_INSERTS, "true")
|
||||
|
||||
return dataSource
|
||||
}
|
||||
|
||||
@Bean
|
||||
fun objectMapper(): ObjectMapper = ObjectMapper().registerModule(
|
||||
KotlinModule.Builder()
|
||||
.withReflectionCacheSize(512)
|
||||
.configure(KotlinFeature.NullToEmptyCollection, true)
|
||||
.configure(KotlinFeature.NullToEmptyMap, true)
|
||||
.configure(KotlinFeature.NullIsSameAsDefault, true)
|
||||
.configure(KotlinFeature.SingletonSupport, false)
|
||||
.configure(KotlinFeature.StrictNullChecks, true)
|
||||
.build()
|
||||
)
|
||||
|
||||
@Bean
|
||||
fun databaseConfig(): DatabaseConfig = DatabaseConfig {
|
||||
keepLoadedReferencesOutOfTransaction = true
|
||||
}
|
||||
}
|
8
src/main/kotlin/ru/sicamp/sicamphelper/Constants.kt
Normal file
8
src/main/kotlin/ru/sicamp/sicamphelper/Constants.kt
Normal file
|
@ -0,0 +1,8 @@
|
|||
package ru.sicamp.sicamphelper
|
||||
|
||||
const val CUSTOM_TEXT_SIZE = 500
|
||||
|
||||
object ConfigNames {
|
||||
const val CAMP_START = "CAMP_START"
|
||||
const val CAMP_END = "CAMP_END"
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
package ru.sicamp.sicamphelper
|
||||
|
||||
import org.jetbrains.exposed.sql.*
|
||||
import org.jetbrains.exposed.sql.transactions.transaction
|
||||
import org.springframework.beans.factory.InitializingBean
|
||||
import org.springframework.security.crypto.password.PasswordEncoder
|
||||
import org.springframework.stereotype.Component
|
||||
import ru.sicamp.sicamphelper.db.entity.Group
|
||||
import ru.sicamp.sicamphelper.db.entity.User
|
||||
import ru.sicamp.sicamphelper.db.table.Groups
|
||||
import ru.sicamp.sicamphelper.db.table.Tables
|
||||
import ru.sicamp.sicamphelper.db.table.Users
|
||||
import java.time.Duration
|
||||
import java.time.LocalDateTime
|
||||
import javax.sql.DataSource
|
||||
|
||||
@Component
|
||||
class InitializingBeanImpl(
|
||||
private val datasource: DataSource,
|
||||
private val passwordEncoder: PasswordEncoder,
|
||||
private val databaseConfig: DatabaseConfig,
|
||||
) : InitializingBean {
|
||||
override fun afterPropertiesSet() {
|
||||
val db = Database.connect(
|
||||
datasource = datasource,
|
||||
databaseConfig = databaseConfig
|
||||
)
|
||||
|
||||
transaction(db) {
|
||||
addLogger(StdOutSqlLogger)
|
||||
|
||||
SchemaUtils.create(*Tables.values().map { it.table }.toTypedArray())
|
||||
}
|
||||
|
||||
transaction {
|
||||
val now = LocalDateTime.now()
|
||||
val adminGroup = Group.find {
|
||||
Groups.name eq "admins"
|
||||
}.firstOrNull() ?: Group.new {
|
||||
name = "admins"
|
||||
tgLink = ""
|
||||
}
|
||||
|
||||
User.find {
|
||||
Users.login eq "root"
|
||||
}.firstOrNull() ?: User.new {
|
||||
login = "root"
|
||||
_password = passwordEncoder.encode("123456")
|
||||
role = Users.Role.ADMIN
|
||||
group = adminGroup
|
||||
name = "Рут Рутович"
|
||||
sex = Users.Sex.OTHER
|
||||
age = 12
|
||||
enabled = true
|
||||
locked = false
|
||||
accountExpiration = now + Duration.ofDays(60)
|
||||
credentialsExpiration = now + Duration.ofDays(60)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package ru.sicamp.sicamphelper
|
||||
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication
|
||||
import org.springframework.boot.runApplication
|
||||
|
||||
@SpringBootApplication
|
||||
class SicampHelperApplication
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
runApplication<SicampHelperApplication>(*args)
|
||||
}
|
15
src/main/kotlin/ru/sicamp/sicamphelper/api/bot/BotConfig.kt
Normal file
15
src/main/kotlin/ru/sicamp/sicamphelper/api/bot/BotConfig.kt
Normal file
|
@ -0,0 +1,15 @@
|
|||
package ru.sicamp.sicamphelper.api.bot
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value
|
||||
import org.springframework.context.annotation.Bean
|
||||
import org.springframework.context.annotation.Configuration
|
||||
import org.telegram.telegrambots.meta.api.methods.updates.SetWebhook
|
||||
|
||||
@Configuration
|
||||
class BotConfig {
|
||||
@Bean
|
||||
fun setWebhook(
|
||||
@Value("bot.token")
|
||||
token: String
|
||||
) = SetWebhook.builder().secretToken(token).build()
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
package ru.sicamp.sicamphelper.api.bot
|
||||
|
||||
import mu.KLogging
|
||||
import org.springframework.core.env.Environment
|
||||
import org.springframework.stereotype.Component
|
||||
import org.telegram.telegrambots.extensions.bots.commandbot.TelegramLongPollingCommandBot
|
||||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage
|
||||
import org.telegram.telegrambots.meta.api.objects.Update
|
||||
import ru.sicamp.sicamphelper.api.command.Command
|
||||
|
||||
/*
|
||||
@Component
|
||||
class TelegramPrivateBot(
|
||||
commands: List<Command<*, *>>,
|
||||
env: Environment
|
||||
) : TelegramLongPollingCommandBot() {
|
||||
private val botName = env.getProperty("bot.username", "SicampHelper")
|
||||
|
||||
init {
|
||||
commands.forEach {
|
||||
register(it)
|
||||
}
|
||||
}
|
||||
|
||||
override fun getBotUsername(): String {
|
||||
return botName
|
||||
}
|
||||
|
||||
override fun processNonCommandUpdate(update: Update?) {
|
||||
logger.warn("Received non command update. Message: ${update?.message}")
|
||||
}
|
||||
|
||||
fun send() {
|
||||
val answer = SendMessage.builder().chatId(10L).text("ssasa")
|
||||
}
|
||||
|
||||
companion object : KLogging()
|
||||
}*/
|
|
@ -0,0 +1,34 @@
|
|||
package ru.sicamp.sicamphelper.api.bot
|
||||
|
||||
import mu.KLogging
|
||||
import org.springframework.beans.factory.annotation.Value
|
||||
import org.springframework.core.env.Environment
|
||||
import org.springframework.stereotype.Component
|
||||
import org.telegram.telegrambots.extensions.bots.commandbot.TelegramLongPollingCommandBot
|
||||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage
|
||||
import org.telegram.telegrambots.meta.api.objects.Update
|
||||
import org.telegram.telegrambots.starter.SpringWebhookBot
|
||||
import ru.sicamp.sicamphelper.api.command.PublicCommand
|
||||
|
||||
@Component
|
||||
class TelegramPublicBot(
|
||||
commands: List<PublicCommand<*, *>>,
|
||||
@Value("\${bot.username}")
|
||||
private val botName: String,
|
||||
) : TelegramLongPollingCommandBot() { // TODO: switch to webHook?
|
||||
init {
|
||||
commands.forEach {
|
||||
register(it)
|
||||
}
|
||||
}
|
||||
|
||||
override fun getBotUsername(): String {
|
||||
return botName
|
||||
}
|
||||
|
||||
override fun processNonCommandUpdate(update: Update?) {
|
||||
logger.warn("Received non command update. Message: ${update?.message}")
|
||||
}
|
||||
|
||||
companion object : KLogging()
|
||||
}
|
|
@ -0,0 +1,67 @@
|
|||
package ru.sicamp.sicamphelper.api.command
|
||||
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import mu.KLogging
|
||||
import org.telegram.telegrambots.extensions.bots.commandbot.commands.BotCommand
|
||||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage
|
||||
import org.telegram.telegrambots.meta.api.objects.Chat
|
||||
import org.telegram.telegrambots.meta.api.objects.User
|
||||
import org.telegram.telegrambots.meta.bots.AbsSender
|
||||
import ru.sicamp.sicamphelper.model.metadata.RequestSource
|
||||
import ru.sicamp.sicamphelper.db.table.Users
|
||||
import ru.sicamp.sicamphelper.model.request.Request
|
||||
import ru.sicamp.sicamphelper.model.request.RequestWrapper
|
||||
import ru.sicamp.sicamphelper.model.response.Response
|
||||
import ru.sicamp.sicamphelper.model.response.ResponseWrapper
|
||||
import ru.sicamp.sicamphelper.model.response.TelegramResponse
|
||||
import ru.sicamp.sicamphelper.service.UserService
|
||||
import ru.sicamp.sicamphelper.util.Extractor
|
||||
|
||||
abstract class Command<REQ : Request, RES : TelegramResponse>(
|
||||
private val command: Commands
|
||||
) : BotCommand(command.name, command.description), Extractor<REQ> {
|
||||
protected abstract val userService: UserService
|
||||
|
||||
protected abstract suspend fun executeRequest(request: RequestWrapper<REQ>): ResponseWrapper<RES>
|
||||
|
||||
override fun execute(absSender: AbsSender?, user: User?, chat: Chat?, arguments: Array<out String>?) {
|
||||
val internalUser = authorizeCommand(user) ?: TODO("Respond unauthorized request")
|
||||
val request = try {
|
||||
RequestWrapper(
|
||||
issuer = internalUser,
|
||||
source = RequestSource.TELEGRAM,
|
||||
body = extract(arguments)
|
||||
)
|
||||
} catch (e: Exception) {
|
||||
TODO("Respond error")
|
||||
}
|
||||
try {
|
||||
val response = runBlocking {
|
||||
executeRequest(request)
|
||||
}
|
||||
response.getTelegramMessage()?.let {
|
||||
absSender?.execute(it)
|
||||
TODO("Respond success")
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
logger.error { e }
|
||||
TODO("Respond internal error")
|
||||
}
|
||||
}
|
||||
|
||||
private fun authorizeCommand(user: User?): ru.sicamp.sicamphelper.db.entity.User? = userService.findUserByTgUser(
|
||||
user ?: error("No user provided for command $commandIdentifier")
|
||||
).let {
|
||||
if (it == null) {
|
||||
logger.warn("Not found user with tgUsername: ${user.userName}, tgId: ${user.id}")
|
||||
return@let null
|
||||
}
|
||||
if (it.role !in Users.Role.upperRoles(command.minimalRole)) {
|
||||
logger.warn("User $it is not authorized to perform command $commandIdentifier")
|
||||
return@let null
|
||||
}
|
||||
it
|
||||
}
|
||||
|
||||
companion object : KLogging()
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
package ru.sicamp.sicamphelper.api.command
|
||||
|
||||
import ru.sicamp.sicamphelper.db.table.Users
|
||||
|
||||
enum class Commands(
|
||||
val commandName: String,
|
||||
val description: String,
|
||||
val minimalRole: Users.Role,
|
||||
) {
|
||||
INFO("info", "Получить информацию по пользователю", Users.Role.STUDENT),
|
||||
SCHEDULE("schedule", "Просмотреть расписание", Users.Role.STUDENT),
|
||||
SCHEDULE_GROUP("schedule_group", "Просмотреть расписание группы", Users.Role.STUDENT)
|
||||
;
|
||||
|
||||
companion object {
|
||||
const val PREFIX_URL = "command"
|
||||
const val SCHEDULE_URL = "schedule"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package ru.sicamp.sicamphelper.api.command
|
||||
|
||||
import ru.sicamp.sicamphelper.model.request.Request
|
||||
import ru.sicamp.sicamphelper.model.response.Response
|
||||
import ru.sicamp.sicamphelper.model.response.TelegramResponse
|
||||
|
||||
abstract class PrivateCommand<REQ : Request, RES : TelegramResponse>(command: Commands) : Command<REQ, RES>(command)
|
|
@ -0,0 +1,7 @@
|
|||
package ru.sicamp.sicamphelper.api.command
|
||||
|
||||
import ru.sicamp.sicamphelper.model.request.Request
|
||||
import ru.sicamp.sicamphelper.model.response.Response
|
||||
import ru.sicamp.sicamphelper.model.response.TelegramResponse
|
||||
|
||||
abstract class PublicCommand<REQ : Request, RES : TelegramResponse>(command: Commands) : Command<REQ, RES>(command)
|
14
src/main/kotlin/ru/sicamp/sicamphelper/api/command/Utils.kt
Normal file
14
src/main/kotlin/ru/sicamp/sicamphelper/api/command/Utils.kt
Normal file
|
@ -0,0 +1,14 @@
|
|||
package ru.sicamp.sicamphelper.api.command
|
||||
|
||||
import java.time.LocalDate
|
||||
import java.time.format.DateTimeParseException
|
||||
|
||||
fun String?.toLocalDate(): LocalDate? = try {
|
||||
LocalDate.parse(this)
|
||||
} catch (e: DateTimeParseException) {
|
||||
null
|
||||
}
|
||||
|
||||
const val EMPTY_TOKEN = "_"
|
||||
|
||||
fun Array<out String>.getValueOrNull(index: Int) = getOrNull(index)?.let { if (it == EMPTY_TOKEN) null else it }
|
|
@ -0,0 +1,28 @@
|
|||
package ru.sicamp.sicamphelper.api.command.impl
|
||||
|
||||
import org.springframework.stereotype.Component
|
||||
import ru.sicamp.sicamphelper.api.command.Commands
|
||||
import ru.sicamp.sicamphelper.api.command.PublicCommand
|
||||
import ru.sicamp.sicamphelper.api.command.getValueOrNull
|
||||
import ru.sicamp.sicamphelper.model.request.RequestWrapper
|
||||
import ru.sicamp.sicamphelper.model.data.UserInput
|
||||
import ru.sicamp.sicamphelper.model.request.info.InfoRequest
|
||||
import ru.sicamp.sicamphelper.model.response.info.InfoResponse
|
||||
import ru.sicamp.sicamphelper.service.InfoService
|
||||
import ru.sicamp.sicamphelper.service.UserService
|
||||
|
||||
@Component
|
||||
class InfoCommand(
|
||||
override val userService: UserService,
|
||||
private val infoService: InfoService
|
||||
) : PublicCommand<InfoRequest, InfoResponse>(Commands.INFO) {
|
||||
override suspend fun executeRequest(request: RequestWrapper<InfoRequest>) = infoService.getInfo(request)
|
||||
|
||||
override fun extract(
|
||||
arguments: Array<out String>?
|
||||
) = InfoRequest(
|
||||
userInput = arguments?.getValueOrNull(0)?.let {
|
||||
UserInput(it)
|
||||
}
|
||||
)
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
package ru.sicamp.sicamphelper.api.command.impl
|
||||
|
||||
import mu.KLogging
|
||||
import org.springframework.stereotype.Controller
|
||||
import ru.sicamp.sicamphelper.api.command.Commands
|
||||
import ru.sicamp.sicamphelper.api.command.PublicCommand
|
||||
import ru.sicamp.sicamphelper.api.command.getValueOrNull
|
||||
import ru.sicamp.sicamphelper.api.command.toLocalDate
|
||||
import ru.sicamp.sicamphelper.model.request.RequestWrapper
|
||||
import ru.sicamp.sicamphelper.model.data.UserInput
|
||||
import ru.sicamp.sicamphelper.model.request.schedule.ScheduleRequest
|
||||
import ru.sicamp.sicamphelper.model.response.ResponseWrapper
|
||||
import ru.sicamp.sicamphelper.model.response.schedule.ScheduleResponse
|
||||
import ru.sicamp.sicamphelper.service.ScheduleService
|
||||
import ru.sicamp.sicamphelper.service.UserService
|
||||
|
||||
@Controller
|
||||
class ScheduleCommand(
|
||||
override val userService: UserService,
|
||||
private val scheduleService: ScheduleService
|
||||
) : PublicCommand<ScheduleRequest, ScheduleResponse>(Commands.SCHEDULE) {
|
||||
override suspend fun executeRequest(
|
||||
request: RequestWrapper<ScheduleRequest>
|
||||
): ResponseWrapper<ScheduleResponse> = scheduleService.getUserSchedule(request)
|
||||
|
||||
override fun extract(
|
||||
arguments: Array<out String>?
|
||||
) = ScheduleRequest(
|
||||
date = arguments?.getValueOrNull(0).toLocalDate(),
|
||||
userInput = arguments?.getValueOrNull(1)?.let { UserInput(it) }
|
||||
)
|
||||
|
||||
|
||||
companion object : KLogging()
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package ru.sicamp.sicamphelper.api.command.impl
|
||||
|
||||
import org.springframework.stereotype.Component
|
||||
import ru.sicamp.sicamphelper.api.command.Commands
|
||||
import ru.sicamp.sicamphelper.api.command.PublicCommand
|
||||
import ru.sicamp.sicamphelper.api.command.getValueOrNull
|
||||
import ru.sicamp.sicamphelper.api.command.toLocalDate
|
||||
import ru.sicamp.sicamphelper.model.data.GroupInput
|
||||
import ru.sicamp.sicamphelper.model.request.RequestWrapper
|
||||
import ru.sicamp.sicamphelper.model.request.schedule.ScheduleGroupRequest
|
||||
import ru.sicamp.sicamphelper.model.response.ResponseWrapper
|
||||
import ru.sicamp.sicamphelper.model.response.schedule.ScheduleResponse
|
||||
import ru.sicamp.sicamphelper.service.ScheduleService
|
||||
import ru.sicamp.sicamphelper.service.UserService
|
||||
|
||||
@Component
|
||||
class ScheduleGroupCommand(
|
||||
override val userService: UserService,
|
||||
private val scheduleService: ScheduleService,
|
||||
) : PublicCommand<ScheduleGroupRequest, ScheduleResponse>(Commands.SCHEDULE_GROUP) {
|
||||
override suspend fun executeRequest(request: RequestWrapper<ScheduleGroupRequest>): ResponseWrapper<ScheduleResponse> {
|
||||
return scheduleService.getSchedulesByGroup(request)
|
||||
}
|
||||
|
||||
override fun extract(arguments: Array<out String>?): ScheduleGroupRequest =
|
||||
ScheduleGroupRequest(
|
||||
date = arguments?.getValueOrNull(0).toLocalDate(),
|
||||
group = arguments?.getValueOrNull(1)?.let { GroupInput(it) }
|
||||
)
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package ru.sicamp.sicamphelper.api.controller
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper
|
||||
import com.fasterxml.jackson.module.kotlin.convertValue
|
||||
import mu.KLogging
|
||||
import org.springframework.web.bind.annotation.*
|
||||
import ru.sicamp.sicamphelper.model.dto.UserDto
|
||||
import ru.sicamp.sicamphelper.service.UserService
|
||||
|
||||
@RestController
|
||||
class UsersController(
|
||||
private val userService: UserService,
|
||||
private val objectMapper: ObjectMapper,
|
||||
) {
|
||||
@PostMapping("/user/create")
|
||||
fun create(@RequestParam dto: UserDto): String {
|
||||
val user = userService.createUser(dto)
|
||||
return user.toString()
|
||||
}
|
||||
|
||||
@GetMapping("/custom/users/find/{login}")
|
||||
fun getUser(@PathVariable(name = "login", required = true) login: String): String {
|
||||
logger.info("Getting user with username: $login!!!!!")
|
||||
return objectMapper.convertValue(userService.loadUserByUsername(login))
|
||||
}
|
||||
|
||||
@GetMapping("/test")
|
||||
fun test() = "test"
|
||||
|
||||
companion object : KLogging()
|
||||
}
|
11
src/main/kotlin/ru/sicamp/sicamphelper/db/entity/Config.kt
Normal file
11
src/main/kotlin/ru/sicamp/sicamphelper/db/entity/Config.kt
Normal file
|
@ -0,0 +1,11 @@
|
|||
package ru.sicamp.sicamphelper.db.entity
|
||||
|
||||
import org.jetbrains.exposed.dao.Entity
|
||||
import org.jetbrains.exposed.dao.EntityClass
|
||||
import org.jetbrains.exposed.dao.id.EntityID
|
||||
import ru.sicamp.sicamphelper.db.table.Configs
|
||||
|
||||
class Config(id: EntityID<String>): Entity<String>(id) {
|
||||
companion object : EntityClass<String, Config>(Configs)
|
||||
val value by Configs.value
|
||||
}
|
25
src/main/kotlin/ru/sicamp/sicamphelper/db/entity/Event.kt
Normal file
25
src/main/kotlin/ru/sicamp/sicamphelper/db/entity/Event.kt
Normal file
|
@ -0,0 +1,25 @@
|
|||
package ru.sicamp.sicamphelper.db.entity
|
||||
|
||||
import org.jetbrains.exposed.dao.LongEntity
|
||||
import org.jetbrains.exposed.dao.LongEntityClass
|
||||
import org.jetbrains.exposed.dao.id.EntityID
|
||||
import ru.sicamp.sicamphelper.db.table.Events
|
||||
import ru.sicamp.sicamphelper.db.table.Schedules
|
||||
|
||||
class Event(id: EntityID<Long>): LongEntity(id) {
|
||||
companion object : LongEntityClass<Event>(Events)
|
||||
|
||||
var name by Events.name
|
||||
var type by Events.type
|
||||
var registrationType by Events.registrationType
|
||||
var group by Group optionalReferencedOn Events.group
|
||||
var eventDate by Events.eventDate
|
||||
var eventTime by Events.eventTime
|
||||
var repeat by Events.repeat
|
||||
var assigneeCount by Events.assigneeCount
|
||||
var status by Events.status
|
||||
var description by Events.description
|
||||
var responsible by User referencedOn Events.responsible
|
||||
var defaultRoom by Room optionalReferencedOn Events.defaultRoom
|
||||
val schedules by Schedule referrersOn Schedules.event
|
||||
}
|
13
src/main/kotlin/ru/sicamp/sicamphelper/db/entity/Group.kt
Normal file
13
src/main/kotlin/ru/sicamp/sicamphelper/db/entity/Group.kt
Normal file
|
@ -0,0 +1,13 @@
|
|||
package ru.sicamp.sicamphelper.db.entity
|
||||
|
||||
import org.jetbrains.exposed.dao.LongEntity
|
||||
import org.jetbrains.exposed.dao.LongEntityClass
|
||||
import org.jetbrains.exposed.dao.id.EntityID
|
||||
import ru.sicamp.sicamphelper.db.table.Groups
|
||||
|
||||
class Group(id: EntityID<Long>) : LongEntity(id) {
|
||||
companion object : LongEntityClass<Group>(Groups)
|
||||
|
||||
var name by Groups.name
|
||||
var tgLink by Groups.tgLink
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package ru.sicamp.sicamphelper.db.entity
|
||||
|
||||
import org.jetbrains.exposed.dao.LongEntity
|
||||
import org.jetbrains.exposed.dao.LongEntityClass
|
||||
import org.jetbrains.exposed.dao.id.EntityID
|
||||
import ru.sicamp.sicamphelper.db.table.GroupRegistrations
|
||||
|
||||
class GroupRegistration(id: EntityID<Long>): LongEntity(id) {
|
||||
companion object : LongEntityClass<GroupRegistration>(GroupRegistrations)
|
||||
|
||||
var schedule by Schedule referencedOn GroupRegistrations.schedule
|
||||
var group by Group referencedOn GroupRegistrations.group
|
||||
}
|
14
src/main/kotlin/ru/sicamp/sicamphelper/db/entity/Room.kt
Normal file
14
src/main/kotlin/ru/sicamp/sicamphelper/db/entity/Room.kt
Normal file
|
@ -0,0 +1,14 @@
|
|||
package ru.sicamp.sicamphelper.db.entity
|
||||
|
||||
import org.jetbrains.exposed.dao.LongEntity
|
||||
import org.jetbrains.exposed.dao.LongEntityClass
|
||||
import org.jetbrains.exposed.dao.id.EntityID
|
||||
import ru.sicamp.sicamphelper.db.table.Rooms
|
||||
|
||||
class Room(id: EntityID<Long>) : LongEntity(id) {
|
||||
companion object : LongEntityClass<Room>(Rooms)
|
||||
|
||||
var name by Rooms.name
|
||||
var type by Rooms.type
|
||||
var description by Rooms.description
|
||||
}
|
21
src/main/kotlin/ru/sicamp/sicamphelper/db/entity/Schedule.kt
Normal file
21
src/main/kotlin/ru/sicamp/sicamphelper/db/entity/Schedule.kt
Normal file
|
@ -0,0 +1,21 @@
|
|||
package ru.sicamp.sicamphelper.db.entity
|
||||
|
||||
import org.jetbrains.exposed.dao.LongEntity
|
||||
import org.jetbrains.exposed.dao.LongEntityClass
|
||||
import org.jetbrains.exposed.dao.id.EntityID
|
||||
import ru.sicamp.sicamphelper.db.table.GroupRegistrations
|
||||
import ru.sicamp.sicamphelper.db.table.Schedules
|
||||
import ru.sicamp.sicamphelper.db.table.UserRegistrations
|
||||
|
||||
class Schedule(id: EntityID<Long>) : LongEntity(id) {
|
||||
companion object : LongEntityClass<Schedule>(Schedules)
|
||||
|
||||
var dateTime by Schedules.start
|
||||
var description by Schedules.description
|
||||
var responsible by User referencedOn Schedules.responsible
|
||||
var event by Event referencedOn Schedules.event
|
||||
var room by Room optionalReferencedOn Schedules.room
|
||||
|
||||
val userRegistrations by UserRegistration referrersOn UserRegistrations.schedule
|
||||
val groupRegistrations by GroupRegistration referrersOn GroupRegistrations.schedule
|
||||
}
|
48
src/main/kotlin/ru/sicamp/sicamphelper/db/entity/User.kt
Normal file
48
src/main/kotlin/ru/sicamp/sicamphelper/db/entity/User.kt
Normal file
|
@ -0,0 +1,48 @@
|
|||
package ru.sicamp.sicamphelper.db.entity
|
||||
|
||||
import org.jetbrains.exposed.dao.LongEntity
|
||||
import org.jetbrains.exposed.dao.LongEntityClass
|
||||
import org.jetbrains.exposed.dao.id.EntityID
|
||||
import org.springframework.security.core.GrantedAuthority
|
||||
import org.springframework.security.core.userdetails.UserDetails
|
||||
import ru.sicamp.sicamphelper.db.table.Users
|
||||
import java.time.LocalDateTime
|
||||
|
||||
class User(id: EntityID<Long>) : LongEntity(id), UserDetails {
|
||||
companion object : LongEntityClass<User>(Users)
|
||||
|
||||
var login by Users.login
|
||||
var name by Users.name
|
||||
var supervisor by User optionalReferencedOn Users.supervisor
|
||||
var _password by Users.password
|
||||
var tgId by Users.tgId
|
||||
var tgUsername by Users.tgUsername
|
||||
var role by Users.role
|
||||
var group by Group referencedOn Users.group
|
||||
var sex by Users.sex
|
||||
var age by Users.age
|
||||
var locked by Users.locked
|
||||
var enabled by Users.enabled
|
||||
var accountExpiration by Users.accountExpiration
|
||||
var credentialsExpiration by Users.credentialsExpiration
|
||||
|
||||
override fun getAuthorities(): MutableCollection<out GrantedAuthority> = Users.Role.lowerRoles(role).map {
|
||||
GrantedAuthority { role.name }
|
||||
}.toMutableList()
|
||||
|
||||
override fun getPassword(): String = _password
|
||||
|
||||
override fun getUsername(): String = login
|
||||
|
||||
override fun isAccountNonExpired(): Boolean = LocalDateTime.now() < accountExpiration
|
||||
|
||||
override fun isAccountNonLocked(): Boolean = !locked
|
||||
|
||||
override fun isCredentialsNonExpired(): Boolean = LocalDateTime.now() < credentialsExpiration
|
||||
|
||||
override fun isEnabled(): Boolean = enabled
|
||||
|
||||
override fun toString(): String {
|
||||
return "User: {login:$login, tgUsername:$tgUsername, tgId:$tgId, role:$role}"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package ru.sicamp.sicamphelper.db.entity
|
||||
|
||||
import org.jetbrains.exposed.dao.LongEntity
|
||||
import org.jetbrains.exposed.dao.LongEntityClass
|
||||
import org.jetbrains.exposed.dao.id.EntityID
|
||||
import ru.sicamp.sicamphelper.db.table.UserRegistrations
|
||||
|
||||
class UserRegistration(id: EntityID<Long>): LongEntity(id) {
|
||||
companion object : LongEntityClass<UserRegistration>(UserRegistrations)
|
||||
|
||||
var schedule by Schedule referencedOn UserRegistrations.schedule
|
||||
var user by User referencedOn UserRegistrations.user
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package ru.sicamp.sicamphelper.db.table
|
||||
|
||||
import org.jetbrains.exposed.dao.id.LongIdTable
|
||||
|
||||
object Accommodations : LongIdTable(Tables.ACCOMMODATIONS.name) {
|
||||
val user = reference("user", Users).uniqueIndex("users_index")
|
||||
val placeNumber = integer("place_number")
|
||||
val room = reference("room", Rooms)
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package ru.sicamp.sicamphelper.db.table
|
||||
|
||||
import org.jetbrains.exposed.dao.id.LongIdTable
|
||||
|
||||
object Assignments : LongIdTable("assignments") {
|
||||
val schedule = reference("schedule", Schedules)
|
||||
val assignee = reference("assignee", Users)
|
||||
}
|
10
src/main/kotlin/ru/sicamp/sicamphelper/db/table/Configs.kt
Normal file
10
src/main/kotlin/ru/sicamp/sicamphelper/db/table/Configs.kt
Normal file
|
@ -0,0 +1,10 @@
|
|||
package ru.sicamp.sicamphelper.db.table
|
||||
|
||||
import org.jetbrains.exposed.dao.id.IdTable
|
||||
|
||||
object Configs : IdTable<String>("configs") {
|
||||
override val id = varchar("id", 255).entityId()
|
||||
val value = varchar("value", 255)
|
||||
|
||||
override val primaryKey: PrimaryKey = PrimaryKey(id)
|
||||
}
|
27
src/main/kotlin/ru/sicamp/sicamphelper/db/table/Events.kt
Normal file
27
src/main/kotlin/ru/sicamp/sicamphelper/db/table/Events.kt
Normal file
|
@ -0,0 +1,27 @@
|
|||
package ru.sicamp.sicamphelper.db.table
|
||||
|
||||
import org.jetbrains.exposed.dao.id.LongIdTable
|
||||
import org.jetbrains.exposed.sql.javatime.date
|
||||
import org.jetbrains.exposed.sql.javatime.duration
|
||||
import org.jetbrains.exposed.sql.javatime.time
|
||||
import ru.sicamp.sicamphelper.CUSTOM_TEXT_SIZE
|
||||
|
||||
object Events : LongIdTable("events") {
|
||||
val name = varchar("name", 255)
|
||||
val type = enumerationByName<Type>("type", 255)
|
||||
val registrationType = enumerationByName<RegistrationType>("registration_type", 255)
|
||||
val group = reference("group", Groups).nullable()
|
||||
val eventDate = date("event_date").nullable()
|
||||
val eventTime = time("event_time")
|
||||
/**Should be in days*/
|
||||
val repeat = duration("repeat").nullable()
|
||||
val assigneeCount = integer("assignee_count").nullable()
|
||||
val status = enumerationByName<Status>("status", 255)
|
||||
val description = varchar("description", CUSTOM_TEXT_SIZE)
|
||||
val responsible = reference("responsible", Users)
|
||||
val defaultRoom = reference("default_room", Rooms).nullable()
|
||||
|
||||
enum class Type { ONCE, REGULAR }
|
||||
enum class RegistrationType { ALL, GROUP, USER }
|
||||
enum class Status { SCHEDULE, SKIP_ONCE, DISABLED }
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package ru.sicamp.sicamphelper.db.table
|
||||
|
||||
import org.jetbrains.exposed.sql.Table
|
||||
|
||||
object GateLogins : Table("gate_logins") {
|
||||
val gateLogin = varchar("gate_login", 255)
|
||||
val user = reference("user", Users)
|
||||
|
||||
override val primaryKey: PrimaryKey = PrimaryKey(gateLogin)
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package ru.sicamp.sicamphelper.db.table
|
||||
|
||||
import org.jetbrains.exposed.dao.id.LongIdTable
|
||||
|
||||
object GroupRegistrations : LongIdTable("group_registrations") {
|
||||
val group = reference("group", Groups)
|
||||
val schedule = reference("schedule", Schedules)
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package ru.sicamp.sicamphelper.db.table
|
||||
|
||||
import org.jetbrains.exposed.dao.id.LongIdTable
|
||||
|
||||
object Groups : LongIdTable("groups") {
|
||||
val name = varchar("name", 255)
|
||||
val tgLink = varchar("tg_link", 255)
|
||||
}
|
11
src/main/kotlin/ru/sicamp/sicamphelper/db/table/Rooms.kt
Normal file
11
src/main/kotlin/ru/sicamp/sicamphelper/db/table/Rooms.kt
Normal file
|
@ -0,0 +1,11 @@
|
|||
package ru.sicamp.sicamphelper.db.table
|
||||
|
||||
import org.jetbrains.exposed.dao.id.LongIdTable
|
||||
|
||||
object Rooms : LongIdTable("rooms") {
|
||||
val name = varchar("name", 255)
|
||||
val type = enumerationByName<Type>("type", 255)
|
||||
val description = varchar("description", 255).nullable()
|
||||
|
||||
enum class Type { DORMITORY, AUDIENCE, CANTEEN, COMMON_SPACE }
|
||||
}
|
16
src/main/kotlin/ru/sicamp/sicamphelper/db/table/Schedules.kt
Normal file
16
src/main/kotlin/ru/sicamp/sicamphelper/db/table/Schedules.kt
Normal file
|
@ -0,0 +1,16 @@
|
|||
package ru.sicamp.sicamphelper.db.table
|
||||
|
||||
import org.jetbrains.exposed.dao.id.LongIdTable
|
||||
import org.jetbrains.exposed.sql.javatime.datetime
|
||||
import ru.sicamp.sicamphelper.CUSTOM_TEXT_SIZE
|
||||
|
||||
object Schedules : LongIdTable("schedules") {
|
||||
val start = datetime("start").index("datetime_index")
|
||||
val end = datetime("end")
|
||||
val description = varchar("description", CUSTOM_TEXT_SIZE).nullable()
|
||||
val responsible = reference("responsible", Users)
|
||||
val event = reference("event", Events).index("event_index")
|
||||
val room = reference("room", Rooms).nullable()
|
||||
|
||||
val eventStartIdx = index("event_start_idx", isUnique = true, event, start)
|
||||
}
|
22
src/main/kotlin/ru/sicamp/sicamphelper/db/table/Tables.kt
Normal file
22
src/main/kotlin/ru/sicamp/sicamphelper/db/table/Tables.kt
Normal file
|
@ -0,0 +1,22 @@
|
|||
package ru.sicamp.sicamphelper.db.table
|
||||
|
||||
import org.jetbrains.exposed.sql.Table
|
||||
|
||||
enum class Tables(val tableName: String, val table: Table) {
|
||||
ACCOMMODATIONS("accommodations", Accommodations),
|
||||
ASSIGNMENTS("assignments", Assignments),
|
||||
CONFIGS("configs", Configs),
|
||||
EVENTS("events", Events),
|
||||
GATE_LOGINS("gate_logins", GateLogins),
|
||||
GROUP_REGISTRATIONS("group_registrations", GroupRegistrations),
|
||||
GROUPS("groups", Groups),
|
||||
ROOMS("rooms", Rooms),
|
||||
SCHEDULES("schedules", Schedules),
|
||||
USER_REGISTRATIONS("user_registrations", UserRegistrations),
|
||||
USERS("users", Users),
|
||||
WARNINGS("warnings", Warnings);
|
||||
|
||||
companion object {
|
||||
fun tableByName(name: String): Tables? = values().find { it.tableName == name }
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package ru.sicamp.sicamphelper.db.table
|
||||
|
||||
import org.jetbrains.exposed.dao.id.LongIdTable
|
||||
|
||||
object UserRegistrations : LongIdTable("user_registrations") {
|
||||
val user = reference("user", Users)
|
||||
val schedule = reference("schedule", Schedules)
|
||||
}
|
31
src/main/kotlin/ru/sicamp/sicamphelper/db/table/Users.kt
Normal file
31
src/main/kotlin/ru/sicamp/sicamphelper/db/table/Users.kt
Normal file
|
@ -0,0 +1,31 @@
|
|||
package ru.sicamp.sicamphelper.db.table
|
||||
|
||||
import org.jetbrains.exposed.dao.id.LongIdTable
|
||||
import org.jetbrains.exposed.sql.javatime.datetime
|
||||
|
||||
object Users : LongIdTable("users") {
|
||||
val name = varchar("name", 255)
|
||||
val supervisor = reference("supervisor", Users).nullable()
|
||||
val role = enumerationByName<Role>("role", 255)
|
||||
val group = reference("group", Groups)
|
||||
val tgId = long("tg_id").uniqueIndex("tg_id_index").nullable()
|
||||
val tgUsername = varchar("tg_username", 255).nullable()
|
||||
val sex = enumerationByName<Sex>("sex", 255)
|
||||
val age = integer("age")
|
||||
|
||||
val login = varchar("login", 255)
|
||||
val password = varchar("password", 255)
|
||||
val enabled = bool("enabled")
|
||||
val locked = bool("locked")
|
||||
val accountExpiration = datetime("account_expiration")
|
||||
val credentialsExpiration = datetime("credentials_expiration")
|
||||
//TODO add photos
|
||||
|
||||
enum class Role(val level: Int) { STUDENT(0), TEACHER(1), SENIOR_TEACHER(2), PRINCIPAL(3), ADMIN(4);
|
||||
companion object {
|
||||
fun upperRoles(role: Role): List<Role> = values().filter { it.level <= role.level }
|
||||
fun lowerRoles(role: Role): List<Role> = values().filter { it.level >= role.level }
|
||||
}
|
||||
}
|
||||
enum class Sex { MALE, FEMALE, OTHER }
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package ru.sicamp.sicamphelper.db.table
|
||||
|
||||
import org.jetbrains.exposed.dao.id.LongIdTable
|
||||
|
||||
object Warnings : LongIdTable("warnings") {
|
||||
val user = reference("user", Users)
|
||||
val issuer = reference("issuer", Users)
|
||||
val description = varchar("description", 255)
|
||||
}
|
|
@ -0,0 +1,64 @@
|
|||
package ru.sicamp.sicamphelper.helper
|
||||
|
||||
import org.jetbrains.exposed.dao.with
|
||||
import org.springframework.stereotype.Component
|
||||
import ru.sicamp.sicamphelper.ConfigNames
|
||||
import ru.sicamp.sicamphelper.db.entity.Event
|
||||
import ru.sicamp.sicamphelper.db.entity.Room
|
||||
import ru.sicamp.sicamphelper.db.entity.Schedule
|
||||
import ru.sicamp.sicamphelper.db.entity.User
|
||||
import ru.sicamp.sicamphelper.service.ConfigService
|
||||
import ru.sicamp.sicamphelper.util.InTransaction
|
||||
|
||||
@Component
|
||||
class ScheduleHelper(
|
||||
private val configService: ConfigService
|
||||
) {
|
||||
@InTransaction
|
||||
fun updateSchedulesByEvent(newEvent: Event, oldEvent: Event? = null): List<Schedule> {
|
||||
val oldScheduleUpdates = oldEvent?.let { old ->
|
||||
old.schedules.notForUpdate().mapIndexedNotNull { index, schedule ->
|
||||
ScheduleUpdate(
|
||||
description = schedule.description,
|
||||
responsible = schedule.responsible.takeIf { schedule.responsible != old.responsible },
|
||||
room = schedule.room.takeIf { schedule.room != old.defaultRoom }
|
||||
).takeIf { !it.isEmpty() }?.let {
|
||||
index to it
|
||||
}
|
||||
}.toMap()
|
||||
}
|
||||
|
||||
val startDate = newEvent.eventDate ?: configService.getDate(ConfigNames.CAMP_START)
|
||||
val dates = generateSequence(seed = startDate) { prevDate ->
|
||||
val nextDate = prevDate + newEvent.repeat
|
||||
if (nextDate <= configService.getDate(ConfigNames.CAMP_END)) {
|
||||
nextDate
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
return dates.mapIndexed { index, date ->
|
||||
val scheduleUpdate = oldScheduleUpdates?.get(index)
|
||||
Schedule.new {
|
||||
dateTime = date.atTime(newEvent.eventTime)
|
||||
responsible = scheduleUpdate?.responsible ?: newEvent.responsible
|
||||
event = newEvent
|
||||
room = scheduleUpdate?.room ?: newEvent.defaultRoom
|
||||
}
|
||||
}
|
||||
.toList()
|
||||
.with(
|
||||
Schedule::event,
|
||||
Schedule::responsible,
|
||||
Schedule::room
|
||||
)
|
||||
}
|
||||
|
||||
private data class ScheduleUpdate(
|
||||
val description: String?,
|
||||
val responsible: User?,
|
||||
val room: Room?
|
||||
) {
|
||||
fun isEmpty() = description != null || responsible != null || room != null
|
||||
}
|
||||
}
|
49
src/main/kotlin/ru/sicamp/sicamphelper/model/data/Change.kt
Normal file
49
src/main/kotlin/ru/sicamp/sicamphelper/model/data/Change.kt
Normal file
|
@ -0,0 +1,49 @@
|
|||
/*
|
||||
package ru.sicamp.sicamphelper.model.data
|
||||
|
||||
import kotlinx.serialization.DeserializationStrategy
|
||||
import kotlinx.serialization.json.Json
|
||||
import mu.KLogging
|
||||
import org.jetbrains.exposed.sql.Column
|
||||
import ru.sicamp.sicamphelper.db.table.Tables
|
||||
|
||||
class Change<T : Any>(
|
||||
change: String,
|
||||
private val deserializationStrategy: DeserializationStrategy<T>
|
||||
) {
|
||||
val column: Column<T> = extractColumn(change)
|
||||
val newValue: T = extractValue(change)
|
||||
|
||||
*/
|
||||
/**Extract change from string
|
||||
* @param change format: "table_name.column_name=newValue" *//*
|
||||
|
||||
@Suppress("", "UNCHECKED_CAST")
|
||||
private fun extractColumn(change: String): Column<T> {
|
||||
if (!format.matches(change)) {
|
||||
throw BadFormatException(change)
|
||||
}
|
||||
|
||||
val (columnAndTable, _) = change.split("=")
|
||||
val (tableName, columnName) = columnAndTable.split(".")
|
||||
|
||||
val table = Tables.tableByName(tableName)?.table ?: throw BadFormatException(change)
|
||||
return table.columns.find { it.name == columnName } as Column<T>? ?: throw BadFormatException(change)
|
||||
}
|
||||
|
||||
class BadFormatException(change: String) : Exception("Bad format exception for change: $change")
|
||||
|
||||
private fun extractValue(change: String): T {
|
||||
if (!format.matches(change)) {
|
||||
throw BadFormatException(change)
|
||||
}
|
||||
|
||||
val (_, valueString) = change.split("=")
|
||||
|
||||
return Json.decodeFromString(deserializationStrategy, valueString)
|
||||
}
|
||||
|
||||
private val format = "[\\w_]+\\.[\\w_]+=.+".toRegex()
|
||||
|
||||
companion object : KLogging()
|
||||
}*/
|
|
@ -0,0 +1,18 @@
|
|||
package ru.sicamp.sicamphelper.model.data
|
||||
|
||||
class GroupInput(
|
||||
input: String
|
||||
) {
|
||||
val type = Type.typeByPrefix(input.firstOrNull())
|
||||
|
||||
val id: Long? = if (type == Type.ID) input.tail().toString().toLong() else null
|
||||
val name: String? = if (type == Type.NAME) input.tail().toString() else null
|
||||
|
||||
enum class Type(val prefix: Char) {
|
||||
NAME('@'), ID('#');
|
||||
|
||||
companion object {
|
||||
fun typeByPrefix(char: Char?) = values().find { it.prefix == char }
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package ru.sicamp.sicamphelper.model.data
|
||||
|
||||
/**Class extracts user from user input. Also used to represent user in views. Format: @tgUsername|$gateLogin|#userId */
|
||||
class UserInput(
|
||||
input: String
|
||||
) {
|
||||
val type: Type? = Type.typeByPrefix(input.firstOrNull())
|
||||
|
||||
val tgUsername = if (type == Type.TG_USERNAME) input.tail().toString() else null
|
||||
val gateLogin = if (type == Type.GATE_LOGIN) input.tail().toString() else null
|
||||
val userId = if (type == Type.USER_ID) input.tail().toString().toLong() else null
|
||||
|
||||
enum class Type(val prefix: Char) {
|
||||
TG_USERNAME('@'), GATE_LOGIN('$'), USER_ID('#');
|
||||
|
||||
companion object {
|
||||
fun typeByPrefix(char: Char?) = values().find { it.prefix == char }
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun fromTgUser(tgUser: org.telegram.telegrambots.meta.api.objects.User) = UserInput(Type.TG_USERNAME.prefix + tgUser.userName)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
package ru.sicamp.sicamphelper.model.data
|
||||
|
||||
fun String.tail() = subSequence(1..lastIndex)
|
20
src/main/kotlin/ru/sicamp/sicamphelper/model/dto/EventDto.kt
Normal file
20
src/main/kotlin/ru/sicamp/sicamphelper/model/dto/EventDto.kt
Normal file
|
@ -0,0 +1,20 @@
|
|||
package ru.sicamp.sicamphelper.model.dto
|
||||
|
||||
import ru.sicamp.sicamphelper.db.table.Events
|
||||
import java.time.Duration
|
||||
import java.time.LocalDate
|
||||
import java.time.LocalTime
|
||||
|
||||
data class EventDto(
|
||||
val name: String,
|
||||
val registrationType: Events.RegistrationType,
|
||||
val group: Long?,
|
||||
val eventDate: LocalDate?,
|
||||
val eventTime: LocalTime,
|
||||
val repeat: Duration?,
|
||||
val assigneeCount: Int?,
|
||||
val status: Events.Status,
|
||||
val description: String,
|
||||
val responsible: Long,
|
||||
val defaultRoom: Long?,
|
||||
)
|
|
@ -0,0 +1,11 @@
|
|||
package ru.sicamp.sicamphelper.model.dto
|
||||
|
||||
import java.time.LocalDateTime
|
||||
|
||||
data class ScheduleDto(
|
||||
val id: Long?,
|
||||
val dateTime: LocalDateTime?,
|
||||
val roomId: Long?,
|
||||
val scheduleDescription: String?,
|
||||
val responsibleId: Long?,
|
||||
)
|
15
src/main/kotlin/ru/sicamp/sicamphelper/model/dto/UserDto.kt
Normal file
15
src/main/kotlin/ru/sicamp/sicamphelper/model/dto/UserDto.kt
Normal file
|
@ -0,0 +1,15 @@
|
|||
package ru.sicamp.sicamphelper.model.dto
|
||||
|
||||
import ru.sicamp.sicamphelper.db.table.Users
|
||||
|
||||
data class UserDto(
|
||||
val login: String,
|
||||
val password: String?,
|
||||
val role: Users.Role,
|
||||
val name: String,
|
||||
val sex: Users.Sex,
|
||||
val age: Int,
|
||||
val tgId: Long? = null,
|
||||
val tgUsername: String? = null,
|
||||
val enabled: Boolean = false,
|
||||
)
|
|
@ -0,0 +1,5 @@
|
|||
package ru.sicamp.sicamphelper.model.metadata
|
||||
|
||||
data class Cursor(
|
||||
val data: String
|
||||
)
|
|
@ -0,0 +1,6 @@
|
|||
package ru.sicamp.sicamphelper.model.metadata
|
||||
|
||||
data class Error(
|
||||
val code: Int,
|
||||
val message: String
|
||||
)
|
|
@ -0,0 +1,6 @@
|
|||
package ru.sicamp.sicamphelper.model.metadata
|
||||
|
||||
data class MetaData(
|
||||
val error: Error?,
|
||||
val cursor: Cursor?
|
||||
)
|
|
@ -0,0 +1,3 @@
|
|||
package ru.sicamp.sicamphelper.model.metadata
|
||||
|
||||
enum class RequestSource { TELEGRAM, WEB }
|
|
@ -0,0 +1,3 @@
|
|||
package ru.sicamp.sicamphelper.model.request
|
||||
|
||||
interface Request
|
|
@ -0,0 +1,38 @@
|
|||
package ru.sicamp.sicamphelper.model.request
|
||||
|
||||
import ru.sicamp.sicamphelper.model.metadata.Cursor
|
||||
import ru.sicamp.sicamphelper.model.metadata.Error
|
||||
import ru.sicamp.sicamphelper.model.metadata.MetaData
|
||||
import ru.sicamp.sicamphelper.model.metadata.RequestSource
|
||||
import ru.sicamp.sicamphelper.model.response.Response
|
||||
import ru.sicamp.sicamphelper.model.response.ResponseWrapper
|
||||
import ru.sicamp.sicamphelper.db.entity.User
|
||||
|
||||
class RequestWrapper<REQ : Request>(
|
||||
val issuer: User,
|
||||
val source: RequestSource,
|
||||
val cursor: Cursor? = null,
|
||||
val body: REQ
|
||||
) {
|
||||
fun <RES : Response> success(response: RES, newCursor: Cursor? = null): ResponseWrapper<RES> =
|
||||
ResponseWrapper(
|
||||
issuer = issuer,
|
||||
source = source,
|
||||
metaData = MetaData(
|
||||
cursor = newCursor,
|
||||
error = null
|
||||
),
|
||||
response = response
|
||||
)
|
||||
|
||||
fun <RES : Response> fail(statusCode: Int = 500, message: String = "Unknown Error"): ResponseWrapper<RES> =
|
||||
ResponseWrapper(
|
||||
issuer = issuer,
|
||||
source = source,
|
||||
metaData = MetaData(
|
||||
error = Error(statusCode, message),
|
||||
cursor = cursor,
|
||||
),
|
||||
response = null
|
||||
)
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package ru.sicamp.sicamphelper.model.request.info
|
||||
|
||||
import ru.sicamp.sicamphelper.model.data.UserInput
|
||||
import ru.sicamp.sicamphelper.model.request.Request
|
||||
|
||||
data class InfoRequest(
|
||||
val userInput: UserInput?
|
||||
) : Request
|
|
@ -0,0 +1,10 @@
|
|||
package ru.sicamp.sicamphelper.model.request.schedule
|
||||
|
||||
import ru.sicamp.sicamphelper.model.data.GroupInput
|
||||
import ru.sicamp.sicamphelper.model.request.Request
|
||||
import java.time.LocalDate
|
||||
|
||||
data class ScheduleGroupRequest(
|
||||
val group: GroupInput?,
|
||||
val date: LocalDate?
|
||||
) : Request
|
|
@ -0,0 +1,10 @@
|
|||
package ru.sicamp.sicamphelper.model.request.schedule
|
||||
|
||||
import ru.sicamp.sicamphelper.model.data.UserInput
|
||||
import ru.sicamp.sicamphelper.model.request.Request
|
||||
import java.time.LocalDate
|
||||
|
||||
data class ScheduleRequest(
|
||||
val userInput: UserInput?,
|
||||
val date: LocalDate?
|
||||
) : Request
|
|
@ -0,0 +1,3 @@
|
|||
package ru.sicamp.sicamphelper.model.response
|
||||
|
||||
interface Response
|
|
@ -0,0 +1,23 @@
|
|||
package ru.sicamp.sicamphelper.model.response
|
||||
|
||||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage
|
||||
import ru.sicamp.sicamphelper.model.metadata.MetaData
|
||||
import ru.sicamp.sicamphelper.model.metadata.RequestSource
|
||||
import ru.sicamp.sicamphelper.db.entity.User
|
||||
|
||||
data class ResponseWrapper<RES: Response>(
|
||||
val issuer: User,
|
||||
val source: RequestSource,
|
||||
val metaData: MetaData?,
|
||||
val response: RES?
|
||||
) {
|
||||
fun getTelegramMessage(): SendMessage? {
|
||||
return if (response is TelegramResponse && source == RequestSource.TELEGRAM) {
|
||||
issuer.tgId?.let {
|
||||
response.buildMessage().chatId(it).build()
|
||||
} ?: error("Not found chatId for user with id=${issuer.id.value}")
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package ru.sicamp.sicamphelper.model.response
|
||||
|
||||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage
|
||||
import org.telegram.telegrambots.meta.api.objects.MessageEntity
|
||||
import org.telegram.telegrambots.meta.api.objects.replykeyboard.ReplyKeyboard
|
||||
import ru.sicamp.sicamphelper.util.ifNotEmpty
|
||||
import ru.sicamp.sicamphelper.util.ifNotNull
|
||||
|
||||
abstract class TelegramResponse : Response {
|
||||
protected abstract val text: String
|
||||
protected open val parseMode: String? = null
|
||||
protected open val disableWebPagePreview: Boolean? = null
|
||||
protected open val disableNotification: Boolean? = null
|
||||
protected open val replyToMessageId: Int? = null
|
||||
protected open val replyMarkup: ReplyKeyboard? = null
|
||||
protected open val entities: List<MessageEntity> = emptyList()
|
||||
protected open val allowSendingWithoutReply: Boolean? = null
|
||||
protected open val protectContent: Boolean = true
|
||||
|
||||
fun buildMessage() = SendMessage.builder()
|
||||
.text(text)
|
||||
.ifNotNull(parseMode) { parseMode(parseMode) }
|
||||
.ifNotNull(disableWebPagePreview) { disableWebPagePreview(disableWebPagePreview) }
|
||||
.ifNotNull(disableNotification) { disableNotification(disableNotification) }
|
||||
.ifNotNull(replyToMessageId) { replyToMessageId(replyToMessageId) }
|
||||
.ifNotNull(replyMarkup) { replyMarkup(replyMarkup) }
|
||||
.ifNotEmpty(entities) { entities(entities) }
|
||||
.ifNotNull(allowSendingWithoutReply) { allowSendingWithoutReply(allowSendingWithoutReply) }
|
||||
.protectContent(protectContent)!!
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package ru.sicamp.sicamphelper.model.response.info
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore
|
||||
import ru.sicamp.sicamphelper.db.entity.User
|
||||
import ru.sicamp.sicamphelper.model.response.TelegramResponse
|
||||
|
||||
data class InfoResponse(
|
||||
val user: User
|
||||
) : TelegramResponse() {
|
||||
override val text: String
|
||||
@JsonIgnore
|
||||
get() = user.toString()
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package ru.sicamp.sicamphelper.model.response.schedule
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore
|
||||
import ru.sicamp.sicamphelper.db.entity.Schedule
|
||||
import ru.sicamp.sicamphelper.model.response.TelegramResponse
|
||||
|
||||
data class ScheduleResponse(
|
||||
val schedules: List<Schedule>,
|
||||
) : TelegramResponse() {
|
||||
override val text: String
|
||||
@JsonIgnore
|
||||
get() = schedules.toString()
|
||||
}
|
|
@ -0,0 +1,88 @@
|
|||
package ru.sicamp.sicamphelper.repository
|
||||
|
||||
import mu.KLogging
|
||||
import org.jetbrains.exposed.dao.with
|
||||
import org.jetbrains.exposed.sql.batchInsert
|
||||
import org.jetbrains.exposed.sql.transactions.transaction
|
||||
import org.springframework.beans.factory.annotation.Value
|
||||
import org.springframework.stereotype.Repository
|
||||
import ru.sicamp.sicamphelper.db.entity.*
|
||||
import ru.sicamp.sicamphelper.db.table.Events
|
||||
import ru.sicamp.sicamphelper.db.table.Groups
|
||||
import ru.sicamp.sicamphelper.db.table.Rooms
|
||||
import ru.sicamp.sicamphelper.db.table.Users
|
||||
import ru.sicamp.sicamphelper.helper.ScheduleHelper
|
||||
import ru.sicamp.sicamphelper.model.dto.EventDto
|
||||
import ru.sicamp.sicamphelper.util.InTransaction
|
||||
import ru.sicamp.sicamphelper.util.checkIds
|
||||
import java.time.LocalDate
|
||||
|
||||
@Repository
|
||||
class EventRepository(
|
||||
@Value("\${exposed.batch-size}")
|
||||
private val batchSize: Int,
|
||||
private val scheduleHelper: ScheduleHelper,
|
||||
) {
|
||||
suspend fun insertEvent(new: EventDto) = transaction {
|
||||
val event = insertSingleEvent(new)
|
||||
scheduleHelper.updateSchedulesByEvent(event)
|
||||
}
|
||||
|
||||
suspend fun insertEventsBatch(batch: List<EventDto>) = transaction {
|
||||
batch.chunked(batchSize).flatMap { insertEvents(it) }
|
||||
}
|
||||
|
||||
@InTransaction
|
||||
private fun insertEvents(batch: List<EventDto>): List<Schedule> {
|
||||
val userIds = batch.map { it.responsible }
|
||||
checkIds(Users, userIds)
|
||||
val groupIds = batch.mapNotNull { it.group }
|
||||
checkIds(Groups, groupIds)
|
||||
val roomIds = batch.mapNotNull { it.defaultRoom }
|
||||
checkIds(Rooms, roomIds)
|
||||
val events = Events.batchInsert(batch) { eventDto ->
|
||||
this[Events.name] = eventDto.name
|
||||
this[Events.registrationType] = eventDto.registrationType
|
||||
this[Events.group] = eventDto.group
|
||||
this[Events.eventDate] = eventDto.eventDate
|
||||
this[Events.eventTime] = eventDto.eventTime
|
||||
this[Events.repeat] = eventDto.repeat
|
||||
this[Events.assigneeCount] = eventDto.assigneeCount
|
||||
this[Events.status] = eventDto.status
|
||||
this[Events.description] = eventDto.description
|
||||
this[Events.responsible] = eventDto.responsible
|
||||
this[Events.defaultRoom] = eventDto.defaultRoom
|
||||
}
|
||||
.map { Event.wrapRow(it) }
|
||||
.with(
|
||||
Event::responsible,
|
||||
Event::group,
|
||||
Event::defaultRoom
|
||||
)
|
||||
|
||||
return events.flatMap {
|
||||
scheduleHelper.updateSchedulesByEvent(it)
|
||||
}
|
||||
}
|
||||
|
||||
@InTransaction
|
||||
private fun insertSingleEvent(new: EventDto) = Event.new {
|
||||
name = new.name
|
||||
registrationType = new.registrationType
|
||||
group = new.group?.let {
|
||||
Group.findById(it) ?: error("Not found group with id=$it")
|
||||
}
|
||||
eventDate = new.eventDate
|
||||
eventTime = new.eventTime
|
||||
repeat = new.repeat
|
||||
assigneeCount = new.assigneeCount
|
||||
status = new.status
|
||||
description = new.description
|
||||
responsible = User.findById(new.responsible) ?: error("Not found user with id=${new.responsible}")
|
||||
defaultRoom = new.defaultRoom?.let {
|
||||
Room.findById(it) ?: error("Not found room with id=${new.defaultRoom}")
|
||||
}
|
||||
}
|
||||
|
||||
companion object : KLogging()
|
||||
}
|
|
@ -0,0 +1,164 @@
|
|||
package ru.sicamp.sicamphelper.repository
|
||||
|
||||
import org.jetbrains.exposed.dao.with
|
||||
import org.jetbrains.exposed.sql.*
|
||||
import org.jetbrains.exposed.sql.javatime.date
|
||||
import org.jetbrains.exposed.sql.transactions.transaction
|
||||
import org.springframework.stereotype.Repository
|
||||
import ru.sicamp.sicamphelper.db.entity.*
|
||||
import ru.sicamp.sicamphelper.db.table.*
|
||||
import ru.sicamp.sicamphelper.model.dto.ScheduleDto
|
||||
import ru.sicamp.sicamphelper.util.InTransaction
|
||||
import java.time.LocalDate
|
||||
|
||||
@Repository
|
||||
class ScheduleRepository {
|
||||
suspend fun updateScheduleById(new: ScheduleDto) = transaction {
|
||||
if (new.id == null) {
|
||||
error("Id is required for update, but was no no id was provided in scheduleDto: $new")
|
||||
}
|
||||
val schedule = Schedule.findById(new.id) ?: error("Not found schedule by id=${new.id}")
|
||||
|
||||
schedule.apply {
|
||||
new.dateTime?.let { dateTime = it }
|
||||
new.roomId?.let { room = Room.findById(it) }
|
||||
new.scheduleDescription?.let { description = it }
|
||||
new.responsibleId?.let { responsible = User.findById(it) ?: error("Not found user with id=$it") }
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun scheduleByGroupId(groupId: Long, date: LocalDate) = transaction {
|
||||
val groupSelector = Op.build { GroupRegistrations.group eq groupId }
|
||||
|
||||
return@transaction schedulesByGroupSelector(groupSelector, date)
|
||||
}
|
||||
|
||||
suspend fun scheduleByGroupName(name: String, date: LocalDate) = transaction {
|
||||
val groupId = Group.find {
|
||||
Groups.name eq name
|
||||
}.firstOrNull()?.id ?: return@transaction null
|
||||
|
||||
val groupSelector = Op.build { GroupRegistrations.id eq groupId }
|
||||
|
||||
return@transaction schedulesByGroupSelector(groupSelector, date)
|
||||
}
|
||||
|
||||
suspend fun scheduleByGateLogin(gateLogin: String, date: LocalDate) = transaction {
|
||||
val user = User.find {
|
||||
Users.login eq gateLogin
|
||||
}.firstOrNull() ?: return@transaction null
|
||||
return@transaction internalScheduleByUser(user, date)
|
||||
}
|
||||
|
||||
suspend fun scheduleByTgUsername(tgUsername: String, date: LocalDate) = transaction {
|
||||
val user = User.find {
|
||||
Users.tgUsername eq tgUsername
|
||||
}.firstOrNull() ?: return@transaction null
|
||||
return@transaction internalScheduleByUser(user, date)
|
||||
}
|
||||
|
||||
suspend fun scheduleByUserId(userId: Long, date: LocalDate) = transaction {
|
||||
val user = User.findById(userId) ?: return@transaction null
|
||||
return@transaction internalScheduleByUser(user, date)
|
||||
}
|
||||
|
||||
suspend fun scheduleByUser(user: User, date: LocalDate) = transaction {
|
||||
return@transaction internalScheduleByUser(user, date)
|
||||
}
|
||||
|
||||
@InTransaction
|
||||
private fun internalScheduleByUser(user: User, date: LocalDate): List<Schedule> {
|
||||
val userSelector = Op.build { UserRegistrations.id eq user.id }
|
||||
val userSchedules = schedulesByUserSelector(userSelector, date)
|
||||
|
||||
val groupSelector = Op.build { GroupRegistrations.group eq user.group.id }
|
||||
val groupSchedules = schedulesByGroupSelector(groupSelector, date)
|
||||
|
||||
return userSchedules.plus(groupSchedules)
|
||||
}
|
||||
|
||||
@InTransaction
|
||||
private fun schedulesByUserSelector(
|
||||
userSelector: Op<Boolean>,
|
||||
date: LocalDate
|
||||
) = UserRegistrations
|
||||
.join(
|
||||
Schedules,
|
||||
joinType = JoinType.INNER,
|
||||
additionalConstraint = {
|
||||
UserRegistrations.schedule eq Schedules.id
|
||||
}
|
||||
)
|
||||
.joinEvents()
|
||||
.joinRooms()
|
||||
.slice(selectedColumns)
|
||||
.select {
|
||||
userSelector and
|
||||
(Schedules.start.date().date() eq date)
|
||||
}
|
||||
.map {
|
||||
Schedule.wrapRow(it)
|
||||
}
|
||||
.with(
|
||||
Schedule::room,
|
||||
Schedule::event,
|
||||
Schedule::responsible
|
||||
)
|
||||
|
||||
/*.map {
|
||||
Room.wrapRow(it)
|
||||
Event.wrapRow(it)
|
||||
Schedule.wrapRow(it)
|
||||
}*/
|
||||
|
||||
private fun schedulesByGroupSelector(
|
||||
groupSelector: Op<Boolean>,
|
||||
date: LocalDate
|
||||
) = GroupRegistrations
|
||||
.join(
|
||||
Schedules,
|
||||
joinType = JoinType.INNER,
|
||||
additionalConstraint = {
|
||||
GroupRegistrations.schedule eq Schedules.id
|
||||
}
|
||||
)
|
||||
.joinEvents()
|
||||
.joinRooms()
|
||||
.select {
|
||||
groupSelector and (Schedules.start.date().date() eq date)
|
||||
}
|
||||
.map {
|
||||
Event.wrapRow(it)
|
||||
Room.wrapRow(it)
|
||||
Schedule.wrapRow(it)
|
||||
}
|
||||
|
||||
private fun ColumnSet.joinEvents() = join(
|
||||
Events,
|
||||
joinType = JoinType.INNER,
|
||||
additionalConstraint = {
|
||||
Schedules.event eq Events.id
|
||||
}
|
||||
)
|
||||
|
||||
private fun ColumnSet.joinRooms() = join(
|
||||
Rooms,
|
||||
joinType = JoinType.INNER,
|
||||
additionalConstraint = {
|
||||
Schedules.room eq Rooms.id
|
||||
}
|
||||
)
|
||||
|
||||
private val selectedColumns = listOf(
|
||||
Schedules.id,
|
||||
Schedules.start,
|
||||
Schedules.room,
|
||||
Events.id,
|
||||
Events.description,
|
||||
Events.responsible,
|
||||
Rooms.id,
|
||||
Rooms.type,
|
||||
Rooms.name,
|
||||
Rooms.description
|
||||
)
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
package ru.sicamp.sicamphelper.security
|
||||
|
||||
import org.springframework.context.annotation.Bean
|
||||
import org.springframework.context.annotation.Configuration
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity
|
||||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder
|
||||
import org.springframework.security.crypto.password.PasswordEncoder
|
||||
import org.springframework.security.web.SecurityFilterChain
|
||||
|
||||
@Configuration
|
||||
@EnableWebSecurity
|
||||
class WebSecurityConfig {
|
||||
@Bean
|
||||
fun passwordEncoder(): PasswordEncoder = BCryptPasswordEncoder()
|
||||
|
||||
@Bean
|
||||
fun securityFilterChain(httpSecurity: HttpSecurity): SecurityFilterChain = httpSecurity
|
||||
.authorizeHttpRequests { requests ->
|
||||
requests.requestMatchers("/**").permitAll()
|
||||
}
|
||||
.formLogin { configurer ->
|
||||
configurer.loginPage("/login")
|
||||
}
|
||||
.logout {
|
||||
it.permitAll()
|
||||
}
|
||||
.build()
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package ru.sicamp.sicamphelper.service
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper
|
||||
import org.jetbrains.exposed.sql.transactions.transaction
|
||||
import org.springframework.stereotype.Service
|
||||
import ru.sicamp.sicamphelper.db.entity.Config
|
||||
import java.time.LocalDate
|
||||
|
||||
@Service
|
||||
class ConfigService(
|
||||
private val objectMapper: ObjectMapper
|
||||
) {
|
||||
fun getDate(name: String): LocalDate? = transaction {
|
||||
return@transaction objectMapper.readValue(Config.findById(name)?.value, LocalDate::class.java)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package ru.sicamp.sicamphelper.service
|
||||
|
||||
import org.springframework.stereotype.Service
|
||||
|
||||
@Service
|
||||
class EventService {
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
package ru.sicamp.sicamphelper.service
|
||||
|
||||
import org.jetbrains.exposed.sql.transactions.transaction
|
||||
import org.springframework.stereotype.Service
|
||||
import ru.sicamp.sicamphelper.db.entity.Group
|
||||
import ru.sicamp.sicamphelper.db.table.Groups
|
||||
|
||||
@Service
|
||||
class GroupService(
|
||||
|
||||
) {
|
||||
fun findByName(groupName: String) = transaction {
|
||||
Group.find { Groups.name eq groupName }.firstOrNull()
|
||||
}
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
package ru.sicamp.sicamphelper.service
|
||||
|
||||
import org.jetbrains.exposed.sql.transactions.transaction
|
||||
import org.springframework.stereotype.Service
|
||||
import ru.sicamp.sicamphelper.db.entity.User
|
||||
import ru.sicamp.sicamphelper.db.table.Users
|
||||
import ru.sicamp.sicamphelper.model.data.UserInput
|
||||
import ru.sicamp.sicamphelper.model.request.RequestWrapper
|
||||
import ru.sicamp.sicamphelper.model.request.info.InfoRequest
|
||||
import ru.sicamp.sicamphelper.model.response.ResponseWrapper
|
||||
import ru.sicamp.sicamphelper.model.response.info.InfoResponse
|
||||
|
||||
@Service
|
||||
class InfoService {
|
||||
suspend fun getInfo(requestWrapper: RequestWrapper<InfoRequest>): ResponseWrapper<InfoResponse> {
|
||||
val request = requestWrapper.body
|
||||
val userInput = request.userInput
|
||||
val type = userInput?.type
|
||||
val user = transaction {
|
||||
when (type) {
|
||||
UserInput.Type.USER_ID -> User.findById(userInput.userId ?: return@transaction null)
|
||||
UserInput.Type.TG_USERNAME -> User.find { Users.tgUsername eq userInput.tgUsername }.firstOrNull()
|
||||
UserInput.Type.GATE_LOGIN -> userInput.gateLogin?.let { User.find { Users.login eq it }.firstOrNull() }
|
||||
null -> requestWrapper.issuer
|
||||
}
|
||||
}
|
||||
|
||||
return if (user != null) {
|
||||
requestWrapper.success(InfoResponse(user))
|
||||
} else {
|
||||
requestWrapper.fail(statusCode = 404, message = "Not found user")
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,69 @@
|
|||
package ru.sicamp.sicamphelper.service
|
||||
|
||||
import mu.KLogging
|
||||
import org.springframework.stereotype.Service
|
||||
import ru.sicamp.sicamphelper.model.data.GroupInput
|
||||
import ru.sicamp.sicamphelper.model.data.UserInput
|
||||
import ru.sicamp.sicamphelper.model.request.RequestWrapper
|
||||
import ru.sicamp.sicamphelper.model.request.schedule.ScheduleGroupRequest
|
||||
import ru.sicamp.sicamphelper.model.request.schedule.ScheduleRequest
|
||||
import ru.sicamp.sicamphelper.model.response.ResponseWrapper
|
||||
import ru.sicamp.sicamphelper.model.response.schedule.ScheduleResponse
|
||||
import ru.sicamp.sicamphelper.repository.ScheduleRepository
|
||||
import java.time.LocalDate
|
||||
|
||||
@Service
|
||||
class ScheduleService(
|
||||
private val scheduleRepository: ScheduleRepository,
|
||||
) {
|
||||
suspend fun getUserSchedule(requestWrapper: RequestWrapper<ScheduleRequest>): ResponseWrapper<ScheduleResponse> {
|
||||
val request = requestWrapper.body
|
||||
val userInput = request.userInput
|
||||
val date = request.date ?: LocalDate.now()
|
||||
val schedules = try {
|
||||
when (userInput?.type) {
|
||||
UserInput.Type.TG_USERNAME -> userInput.tgUsername?.let {
|
||||
scheduleRepository.scheduleByTgUsername(it, date)
|
||||
}
|
||||
UserInput.Type.GATE_LOGIN -> userInput.gateLogin?.let {
|
||||
scheduleRepository.scheduleByGateLogin(it, date)
|
||||
}
|
||||
UserInput.Type.USER_ID -> userInput.userId?.let {
|
||||
scheduleRepository.scheduleByUserId(it, date)
|
||||
}
|
||||
null -> scheduleRepository.scheduleByUser(requestWrapper.issuer, date)
|
||||
} ?: return requestWrapper.fail(404, message = "Not found schedules. Bad input parameters")
|
||||
} catch (e: Exception) {
|
||||
logger.error { e }
|
||||
return requestWrapper.fail(500, message = "Error while trying to fetch schedules for user $userInput")
|
||||
}
|
||||
|
||||
return requestWrapper.success(ScheduleResponse(schedules))
|
||||
}
|
||||
|
||||
suspend fun getSchedulesByGroup(requestWrapper: RequestWrapper<ScheduleGroupRequest>): ResponseWrapper<ScheduleResponse> {
|
||||
val request = requestWrapper.body
|
||||
val date = request.date ?: LocalDate.now()
|
||||
val type = request.group?.type
|
||||
val schedules = try {
|
||||
when (type) {
|
||||
GroupInput.Type.ID -> request.group.id?.let {
|
||||
scheduleRepository.scheduleByGroupId(it, date)
|
||||
}
|
||||
GroupInput.Type.NAME -> request.group.name?.let {
|
||||
scheduleRepository.scheduleByGroupName(it, date)
|
||||
}
|
||||
null -> requestWrapper.issuer.group.id.value.let {
|
||||
scheduleRepository.scheduleByGroupId(it, date)
|
||||
}
|
||||
} ?: return requestWrapper.fail(404, message = "Not found schedules. Bad input parameters")
|
||||
} catch (e: Exception) {
|
||||
logger.error { e }
|
||||
return requestWrapper.fail(500, "Error while trying to fetch schedules for group ${request.group}")
|
||||
}
|
||||
|
||||
return requestWrapper.success(ScheduleResponse(schedules))
|
||||
}
|
||||
|
||||
companion object : KLogging()
|
||||
}
|
|
@ -0,0 +1,53 @@
|
|||
package ru.sicamp.sicamphelper.service
|
||||
|
||||
import org.jetbrains.exposed.sql.and
|
||||
import org.jetbrains.exposed.sql.transactions.transaction
|
||||
import org.springframework.security.core.userdetails.UserDetailsService
|
||||
import org.springframework.security.crypto.password.PasswordEncoder
|
||||
import org.springframework.stereotype.Service
|
||||
import ru.sicamp.sicamphelper.api.controller.UsersController
|
||||
import ru.sicamp.sicamphelper.db.entity.User
|
||||
import ru.sicamp.sicamphelper.db.table.Users
|
||||
import ru.sicamp.sicamphelper.model.dto.UserDto
|
||||
import java.time.Duration
|
||||
import java.time.LocalDateTime
|
||||
|
||||
@Service
|
||||
class UserService(
|
||||
private val passwordEncoder: PasswordEncoder
|
||||
) : UserDetailsService {
|
||||
private val userExpirationTime = Duration.ofDays(60)
|
||||
private val credentialsExpirationTime = Duration.ofDays(30)
|
||||
|
||||
fun findUserByTgUser(user: org.telegram.telegrambots.meta.api.objects.User): User? = transaction {
|
||||
User.find {
|
||||
(Users.tgId eq user.id) and
|
||||
(Users.tgUsername eq user.userName)
|
||||
}.firstOrNull()
|
||||
}
|
||||
|
||||
override fun loadUserByUsername(username: String?): User = transaction {
|
||||
username?.let {
|
||||
User.find { Users.login eq it }.firstOrNull()
|
||||
} ?: error("Not found user with username $username")
|
||||
}
|
||||
|
||||
fun createUser(userDto: UserDto): User {
|
||||
val now = LocalDateTime.now()
|
||||
UsersController.logger.info("Creating new user at $now")
|
||||
return User.new {
|
||||
login = userDto.login
|
||||
_password = passwordEncoder.encode(userDto.password)
|
||||
role = userDto.role
|
||||
name = userDto.name
|
||||
sex = userDto.sex
|
||||
age = userDto.age
|
||||
tgId = userDto.tgId
|
||||
tgUsername = userDto.tgUsername
|
||||
locked = false
|
||||
enabled = userDto.enabled
|
||||
accountExpiration = now + userExpirationTime
|
||||
credentialsExpiration = now + credentialsExpirationTime
|
||||
}
|
||||
}
|
||||
}
|
4
src/main/kotlin/ru/sicamp/sicamphelper/service/Utils.kt
Normal file
4
src/main/kotlin/ru/sicamp/sicamphelper/service/Utils.kt
Normal file
|
@ -0,0 +1,4 @@
|
|||
package ru.sicamp.sicamphelper.service
|
||||
|
||||
import mu.KLogger
|
||||
|
12
src/main/kotlin/ru/sicamp/sicamphelper/util/ExposedUtils.kt
Normal file
12
src/main/kotlin/ru/sicamp/sicamphelper/util/ExposedUtils.kt
Normal file
|
@ -0,0 +1,12 @@
|
|||
package ru.sicamp.sicamphelper.util
|
||||
|
||||
import org.jetbrains.exposed.dao.id.LongIdTable
|
||||
import org.jetbrains.exposed.sql.select
|
||||
|
||||
@InTransaction
|
||||
fun checkIds(table: LongIdTable, ids: List<Long>) {
|
||||
val entityCount = table.select { table.id inList ids }.count()
|
||||
if (entityCount != ids.size.toLong()) {
|
||||
error("Not found some users from id list. Supposed to find ${ids.size}, found: $entityCount. Ids: $ids")
|
||||
}
|
||||
}
|
7
src/main/kotlin/ru/sicamp/sicamphelper/util/Extractor.kt
Normal file
7
src/main/kotlin/ru/sicamp/sicamphelper/util/Extractor.kt
Normal file
|
@ -0,0 +1,7 @@
|
|||
package ru.sicamp.sicamphelper.util
|
||||
|
||||
import ru.sicamp.sicamphelper.model.request.Request
|
||||
|
||||
interface Extractor<E : Request> {
|
||||
fun extract(arguments: Array<out String>?): E
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
package ru.sicamp.sicamphelper.util
|
||||
|
||||
/**This function should only be called inside exposed transaction*/
|
||||
@Target(AnnotationTarget.FUNCTION)
|
||||
@Retention(AnnotationRetention.SOURCE)
|
||||
annotation class InTransaction()
|
5
src/main/kotlin/ru/sicamp/sicamphelper/util/Utils.kt
Normal file
5
src/main/kotlin/ru/sicamp/sicamphelper/util/Utils.kt
Normal file
|
@ -0,0 +1,5 @@
|
|||
package ru.sicamp.sicamphelper.util
|
||||
|
||||
fun <R, T> R.ifNotNull(obj: T?, block: R.(T) -> R): R = if (obj != null) block(obj) else this
|
||||
|
||||
fun <R, T> R.ifNotEmpty(list: List<T>, block: R.(List<T>) -> R): R = if (list.isNotEmpty()) block(list) else this
|
Loading…
Add table
Add a link
Reference in a new issue