Skip to content

Commit df00cdb

Browse files
committed
Add SchedulingInfo Dto
1 parent e7582fd commit df00cdb

File tree

6 files changed

+66
-9
lines changed

6 files changed

+66
-9
lines changed

app/dtos/SchedulingInfoDto.scala

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package dtos
2+
3+
import play.api.db.DB
4+
import play.api.Play.current
5+
import models.SchedulingInfo
6+
7+
import scala.collection.mutable.ListBuffer
8+
9+
object SchedulingInfoDto {
10+
11+
def getAll: List[SchedulingInfo] = {
12+
val infos = new ListBuffer[SchedulingInfo]()
13+
DB.withConnection { conn =>
14+
val statement = conn.prepareStatement("SELECT * FROM SchedulingInfo")
15+
val resultSet = statement.executeQuery()
16+
while (resultSet.next()) {
17+
infos += new SchedulingInfo(resultSet.getLong("id"), resultSet.getLong("eFactor"),
18+
resultSet.getInt("repetition"), resultSet.getLong("interval"), resultSet.getDate("nextDate"))
19+
}
20+
}
21+
infos.toList
22+
}
23+
24+
def save(info: SchedulingInfo) = {
25+
DB.withConnection { conn =>
26+
val statement = conn.prepareStatement("INSERT INTO SchedulingInfo (eFactor, repetition, interval, nextDate) VALUES (?, ?, ?, ?)")
27+
statement.setDouble(1, info.eFactor)
28+
statement.setInt(2, info.repetition)
29+
statement.setLong(3, info.interval)
30+
statement.setDate(4, new java.sql.Date(info.nextDate.getTime))
31+
statement.executeUpdate()
32+
}
33+
}
34+
}

app/models/SchedulingInfo.scala

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package models
22

3-
import java.time.LocalDate
3+
import java.util.Date
44

5-
class SchedulingInfo(val id: Long, val eFactor: Double, val repetition: Int, val interval: Long, val nextDate: LocalDate)
5+
class SchedulingInfo(val id: Long, val eFactor: Double, val repetition: Int, val interval: Long, val nextDate: Date) {
6+
7+
def this(eFactor: Double, repetition: Int, interval: Long, nextDate: Date) = this(0, eFactor, repetition, interval, nextDate)
8+
}

app/schedulers/SM2.scala

+14-6
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
package schedulers
22

3-
import java.time.LocalDate
3+
import java.util.Date
44

55
import models.SchedulingInfo
66

77
class SM2 {
8-
def init(id: Long, dateAdded: LocalDate) =
8+
def init(id: Long, dateAdded: Date) =
99
new SchedulingInfo(
1010
id = id,
1111
eFactor = 2.5,
1212
repetition = 1,
1313
interval = 1,
14-
nextDate = dateAdded.plusDays(1)
14+
nextDate = addDays(dateAdded, 1)
1515
)
1616

17-
def schedule(item: SchedulingInfo, date: LocalDate, quality: Int): SchedulingInfo = {
17+
def schedule(item: SchedulingInfo, date: Date, quality: Int): SchedulingInfo = {
1818
if (quality < 3) {
1919
new SchedulingInfo(
2020
id = item.id,
2121
eFactor = item.eFactor,
2222
repetition = 1,
2323
interval = 1,
24-
nextDate = date.plusDays(1)
24+
nextDate = addDays(date, 1)
2525
)
2626
} else {
2727
val eFactor = easiness(item.eFactor, quality)
@@ -31,7 +31,7 @@ class SM2 {
3131
eFactor = eFactor,
3232
repetition = item.repetition + 1,
3333
interval = nextInterval,
34-
nextDate = date.plusDays(nextInterval)
34+
nextDate = addDays(date, nextInterval)
3535
)
3636
}
3737
}
@@ -43,4 +43,12 @@ class SM2 {
4343
else if (n == 2) 6
4444
else Math.round(interval * eFactor)
4545

46+
private def addDays(date: Date, days: Long): Date = {
47+
new Date(date.getTime + days * SM2.MILLIS_IN_DAY)
48+
}
49+
}
50+
51+
object SM2 {
52+
53+
private val MILLIS_IN_DAY = 24 * 60 * 60 * 1000
4654
}

build.sbt

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ scalaVersion := "2.11.7"
88

99
libraryDependencies ++= Seq( jdbc , cache , ws , specs2 % Test )
1010

11+
libraryDependencies += "org.xerial" % "sqlite-jdbc" % "3.8.10.1"
12+
1113
unmanagedResourceDirectories in Test <+= baseDirectory ( _ /"target/web/public/test" )
1214

13-
resolvers += "scalaz-bintray" at "https://dl.bintray.com/scalaz/releases"
15+
resolvers += "scalaz-bintray" at "https://dl.bintray.com/scalaz/releases"

conf/application.conf

+3
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ application.langs="en"
3737
# db.default.url="jdbc:h2:mem:play"
3838
# db.default.user=sa
3939
# db.default.password=""
40+
# Default database configuration using SQLite database engine
41+
db.default.driver=org.sqlite.JDBC
42+
db.default.url="jdbc:sqlite:database.db3"
4043

4144
# Evolutions
4245
# ~~~~~

db/evolutions/1.sql

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CREATE TABLE SchedulingInfo (
2+
id INTEGER PRIMARY KEY AUTOINCREMENT,
3+
eFactor DOUBLE NOT NULL,
4+
repetition INTEGER NOT NULL,
5+
interval bigint NOT NULL,
6+
nextDate DATE NOT NULL
7+
);

0 commit comments

Comments
 (0)