Compare commits

...
Sign in to create a new pull request.

4 commits
main ... 1-cli

Author SHA1 Message Date
6ad8e8f353 tests and sql fixes 2025-06-03 01:47:23 +02:00
07b3c3821f more sql and models 2025-06-02 07:53:13 +02:00
2a1d6ca031 outline models 2025-06-01 23:48:31 +02:00
f41d2c2b2c project init and sql outline 2025-06-01 06:58:09 +02:00
33 changed files with 538 additions and 1 deletions

36
.gitignore vendored Normal file
View file

@ -0,0 +1,36 @@
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
*.sqlite
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store

View file

@ -1,3 +1,14 @@
# klist
todos for your browser and terminal
todos for your browser and terminal
## disclaimer
work in progress, very far from production ready. use with caution and mind the [LICENSE](license) file
also, if you're getting mad at me because i used kotlin, i can assure you i will probably will be mad at myself
as well soon enough. there are languages way better for this.
## description
This project aims to provide a CLI as well as a BFF to organize your day-to-day life, even with friends and colleagues.

107
pom.xml Normal file
View file

@ -0,0 +1,107 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>dev.thebread</groupId>
<artifactId>klist</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<kotlin.code.style>official</kotlin.code.style>
<kotlin.compiler.jvmTarget>9</kotlin.compiler.jvmTarget>
</properties>
<repositories>
<repository>
<id>mavenCentral</id>
<url>https://repo1.maven.org/maven2/</url>
</repository>
</repositories>
<build>
<sourceDirectory>src/main/kotlin</sourceDirectory>
<testSourceDirectory>src/test/kotlin</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>2.1.20</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.5.3</version>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.5.3</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<configuration>
<mainClass>MainKt</mainClass>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test-junit5</artifactId>
<version>2.1.20</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>2.1.20</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jetbrains.kotlinx/dataframe
<dependency>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>dataframe</artifactId>
<version>0.8.0-dev-339-0.10.1.10</version>
<scope>runtime</scope>
</dependency>-->
<!-- https://mvnrepository.com/artifact/org.xerial/sqlite-jdbc -->
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.49.1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jetbrains.kotlinx/kotlinx-cli-jvm -->
<dependency>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kotlinx-cli-jvm</artifactId>
<version>0.3.6</version>
</dependency>
</dependencies>
</project>

53
src/main/kotlin/Main.kt Normal file
View file

@ -0,0 +1,53 @@
package dev.thebread
import java.sql.DriverManager
import java.sql.Connection
import java.io.File
import java.sql.PreparedStatement
import dev.thebread.Model.PreparedStatementCase
class KlistApp {
private var conn: Connection
constructor(path: String?) {
conn = DriverManager.getConnection(if (!path.isNullOrBlank()) path else "jdbc:sqlite::memory:")
dbSetup(conn)
Model.Common.stmts = dbPrepare(conn)
}
private fun dbSetup(conn: Connection) {
val initSql = javaClass.getResource("/init.sql")!!.readText()
conn.createStatement().executeUpdate(initSql)
}
private fun dbPrepare(conn: Connection): Map<PreparedStatementCase, PreparedStatement> {
return PreparedStatementCase.entries.associateWith { case ->
conn.prepareStatement(
javaClass.getResource("/prepare/${
case.toString().lowercase()
}.sql")!!.readText()
)
}
}
private fun assureUser() {}
private fun assureListForUser() {}
private fun assureStageForList() {}
public fun createTask() {}
public fun moveTask() {}
public fun deleteTask() {}
public fun createStage() {}
public fun moveStage() {}
public fun deleteStage() {}
public fun deleteUser() {}
}
fun main() {
val app = KlistApp(null);
println("Works yay")
}

210
src/main/kotlin/Models.kt Normal file
View file

@ -0,0 +1,210 @@
package dev.thebread
import java.sql.PreparedStatement
import java.sql.ResultSet
import java.util.Date
abstract class Model<T : Model<T>> {
enum class PreparedStatementCase {
GET_USER, GET_USERS, GET_LIST_FOR_USER, GET_LIST, GET_LISTS, GET_STAGE, GET_STAGES, GET_STAGES_FOR_LIST, GET_TASK, GET_TASKS,
ADD_LIST, ADD_USER, ADD_LIST_FOR_USER, ADD_STAGE_FOR_LIST, ADD_TASK_TO_LIST, ADD_STAGE, ADD_TASK,
MOVE_TASK_TO_STAGE,
REMOVE_TASK, REMOVE_USER, REMOVE_LIST_FOR_USER,
REG_LOCAL_FOR_USER, REG_DISCORD_FOR_USER, REG_GOOGLE_FOR_USER,
CLEANUP,
}
companion object Common {
var stmts: Map<PreparedStatementCase, PreparedStatement>? = null
}
constructor() {}
var id: Int? = null
fun assureStatements(): Map<PreparedStatementCase, PreparedStatement> {
return stmts!!
}
abstract fun get(id: Int): T?
abstract fun getAll(): Set<T>
abstract fun create(): T
}
class Models {
class User() : Model<User>() {
lateinit var name: String
var local: Int = -1
var discord: Int = -1
var google: Int = -1
constructor(result: ResultSet) : this() {
this.id = result.getInt("id")
this.name = result.getString("name")
this.local = result.getInt("local_id")
this.discord = result.getInt("discord_id")
this.google = result.getInt("google_id")
}
override fun get(id: Int): User? {
val stmt = assureStatements()[PreparedStatementCase.GET_USER]!!
stmt.setInt(1, id)
val result = stmt.executeQuery()
val user = User(result)
stmt.clearParameters()
return user
}
override fun getAll(): Set<User> {
val stmt = assureStatements()[PreparedStatementCase.GET_USERS]!!
val result = stmt.executeQuery()
val users = mutableSetOf<User>()
do { users.add(User(result)) } while (result.next())
return users
}
override fun create(): User {
val stmt = assureStatements()[PreparedStatementCase.ADD_USER]!!
stmt.setString(1, this.name)
if(this.local != -1) stmt.setInt(2, this.local)
if(this.discord != -1) stmt.setInt(3, this.discord)
if(this.google != -1) stmt.setInt(4, this.google)
val result = stmt.executeQuery()
val user = User(result)
stmt.clearParameters()
this.id = user.id
return user
}
}
class List() : Model<List>() {
lateinit var name: String
var description: String? = null
constructor(result: ResultSet) : this() {
this.id = result.getInt("id")
this.name = result.getString("name")
this.description = result.getString("description")
}
override fun get(id: Int): List? {
val stmt = assureStatements()[PreparedStatementCase.GET_LIST]!!
stmt.setInt(1, id)
val result = stmt.executeQuery()
val list = List(result)
stmt.clearParameters()
return list
}
override fun getAll(): Set<List> {
val stmt = assureStatements()[PreparedStatementCase.GET_LISTS]!!
val result = stmt.executeQuery()
val lists = mutableSetOf<List>()
do { lists.add(List(result)) } while (result.next())
return lists
}
override fun create(): List {
val stmt = assureStatements()[PreparedStatementCase.ADD_LIST]!!
stmt.setString(1, this.name)
if(this.description != null) stmt.setString(2, this.description)
val result = stmt.executeQuery()
val list = List(result)
stmt.clearParameters()
return list
}
}
class Stage() : Model<Stage>() {
lateinit var name: String
var description: String? = null
var list: List? = null
constructor(result: ResultSet) : this() {
this.id = result.getInt("id")
this.name = result.getString("name")
this.description = result.getString("description")
val listId = result.getInt("list_id")
this.list = List().get(listId)
}
override fun get(id: Int): Stage? {
val stmt = assureStatements()[PreparedStatementCase.GET_STAGE]!!
stmt.setInt(1, id)
val result = stmt.executeQuery()
val stage = Stage(result)
stmt.clearParameters()
return stage
}
override fun getAll(): Set<Stage> {
val stmt = assureStatements()[PreparedStatementCase.GET_STAGES]!!
val result = stmt.executeQuery()
val stages = mutableSetOf<Models.Stage>()
do {
stages.add(Stage(result))
} while (result.next())
return stages
}
override fun create(): Stage {
val stmt = assureStatements()[PreparedStatementCase.ADD_STAGE]!!
stmt.setString(1, this.name)
if (this.description != null) stmt.setString(2, this.description)
val result = stmt.executeQuery()
val stage = Stage(result)
stmt.clearParameters()
return stage
}
}
class Task() : Model<Task>() {
lateinit var name: String
var description: String? = null
var stage: Stage? = null
var due: Date? = null
var dueStage: Stage? = null
constructor(result: ResultSet) : this() {
this.id = result.getInt("id")
this.name = result.getString("name")
this.description = result.getString("description")
val stageId = result.getInt("stage_id")
this.stage = Stage().get(stageId)
this.due = result.getDate("due")
val dueStageId = result.getInt("due_stage_id")
this.dueStage = Stage().get(dueStageId)
}
override fun get(id: Int): Task? {
val stmt = assureStatements()[PreparedStatementCase.GET_TASK]!!
stmt.setInt(0, id)
val result = stmt.executeQuery()
val task = Task(result)
stmt.clearParameters()
return task
}
override fun getAll(): Set<Task> {
val stmt = assureStatements()[PreparedStatementCase.GET_TASKS]!!
val result = stmt.executeQuery()
val tasks = mutableSetOf<Task>()
do {
tasks.add(Task(result))
} while (result.next())
return tasks
}
override fun create(): Task {
val stmt = assureStatements()[PreparedStatementCase.ADD_TASK]!!
stmt.setString(1, this.name)
stmt.setString(2, this.description)
if (this.stage != null) stmt.setInt(3, this.stage!!.id!!)
if (this.due != null) stmt.setDate(4, this.due!! as java.sql.Date?)
if (this.dueStage != null) stmt.setInt(5, this.dueStage!!.id!!)
val result = stmt.executeQuery()
val task = Task(result)
stmt.clearParameters()
return task
}
}
}

View file

@ -0,0 +1,39 @@
create table if not exists users (
id integer primary key,
name text not null,
local_id integer, -- if the thing is running locally
discord_id integer, -- if logging in via web via discord
google_id integer -- if logging in via web via google
);
create table if not exists user_lists (
id integer primary key,
user_id integer not null,
list_id integer not null
);
create table if not exists lists (
id integer primary key,
name text not null,
desc text
);
create table if not exists task_stages (
id integer primary key,
name text not null,
desc text,
list_id integer not null,
foreign key (list_id) references lists(id)
);
-- this currently enables moving tasks _between lists_ (calculated of course)
create table if not exists tasks (
id integer primary key,
name text not null,
desc text,
stage integer not null,
due date,
due_stage integer, -- more like target stage
foreign key (stage) references task_stages(id),
foreign key (due_stage) references task_stages(id)
)

View file

@ -0,0 +1 @@
insert into lists values (?1, ?2);

View file

@ -0,0 +1 @@
insert or ignore into user_lists values (?1, ?2);

View file

@ -0,0 +1,2 @@
insert into task_stages
values (?1, ?2, ?3);

View file

@ -0,0 +1 @@
insert into task_stages values (?1, ?2, ?3);

View file

@ -0,0 +1,2 @@
insert into tasks
values (?1, ?2, ?3, ?4, ?5, ?6);

View file

@ -0,0 +1 @@
insert into tasks values (?1, ?2, ?3);

View file

@ -0,0 +1 @@
insert into users values (?1, ?2, ?3, ?4);

View file

@ -0,0 +1 @@
;

View file

@ -0,0 +1 @@
select * from lists where id = ?1;

View file

@ -0,0 +1 @@
;

View file

@ -0,0 +1 @@
select * from lists;

View file

@ -0,0 +1 @@
select * from task_stages where id = ?1;

View file

@ -0,0 +1 @@
select * from task_stages;

View file

@ -0,0 +1 @@
select * from task_stages where list_id = ?1;

View file

@ -0,0 +1,2 @@
select * from tasks
where id = ?1;

View file

@ -0,0 +1 @@
select * from tasks;

View file

@ -0,0 +1 @@
select * from users where id = ?1;

View file

@ -0,0 +1 @@
select * from users;

View file

@ -0,0 +1,3 @@
update tasks
set stage = ?1
where id = ?2;

View file

@ -0,0 +1,3 @@
update users
set discord_id = ?1
where id = ?2;

View file

@ -0,0 +1,3 @@
update users
set google_id = ?1
where id = ?2;

View file

@ -0,0 +1,3 @@
update users
set local_id = ?1
where id = ?2;

View file

@ -0,0 +1 @@
delete from user_lists where user_id = ?1 and list_id = ?2;

View file

@ -0,0 +1 @@
;

View file

@ -0,0 +1 @@
;

View file

@ -0,0 +1,3 @@
class CliTests {
}

View file

@ -0,0 +1,42 @@
import dev.thebread.Model
import dev.thebread.Model.PreparedStatementCase
import dev.thebread.Models
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.BeforeEach;
import java.sql.DriverManager
import kotlin.test.assertTrue
class DatabaseTests {
@BeforeEach
fun setup() {
val conn = DriverManager.getConnection("jdbc:sqlite::memory:")
val initSql = javaClass.getResource("/init.sql")!!.readText()
conn.createStatement().executeUpdate(initSql)
Model.Common.stmts = PreparedStatementCase.entries.associateWith { case ->
conn.prepareStatement(
javaClass.getResource("/prepare/${
case.toString().lowercase()
}.sql")!!.readText()
)
}
}
@Test
fun createUser() {
val user = Models.User()
user.name = "test"
user.create()
assertTrue { user.id != -1 && user.id != null }
val r = Models.User().get(user.id!!)
assertTrue { r != null && r.id != null && r.name == user.name }
}
@Test
fun getUser() {
val user = Models.User().getAll()
}
}