Skip to content
This repository has been archived by the owner on May 5, 2022. It is now read-only.

Commit

Permalink
Merge pull request #63 from echo-webkom/develop
Browse files Browse the repository at this point in the history
Bedpres answers
  • Loading branch information
bakseter authored May 19, 2021
2 parents c42bd26 + 3ce809e commit 9fcba1a
Show file tree
Hide file tree
Showing 16 changed files with 362 additions and 217 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
uses: actions/checkout@v2

- name: Set up JDK 8
uses: actions/checkout@v2
uses: actions/setup-java@v2
with:
java-version: '8'
distribution: 'adopt'
Expand Down
9 changes: 4 additions & 5 deletions .github/workflows/release_tag.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ jobs:
- name: Create & tag release
run: |
git fetch --tags;
gh config set prompt disabled;
git fetch --tags
gh config set prompt disabled
VERSION=$(git tag --sort=-committerdate | head -n1 | cut -c 2-)
MAJOR=$(echo $VERSION | cut -d. -f1);
MAJOR=$(echo $VERSION | cut -d. -f1)
MINOR=$(echo $VERSION | cut -d. -f2)
PATCH=$(echo $VERSION | cut -d. -f3)
LABELS=$(echo "${{ toJson(github.event.pull_request.labels.*.name) }}")
Expand All @@ -47,10 +47,9 @@ jobs:
PATCH=0
fi
}
COMMITS=$(git log origin/master..origin/develop --no-merges --reverse --format=format:%h\ %s\<br\>\<br\>)
gh release create "v$MAJOR.$MINOR.$PATCH" \
-t "$MAJOR.$MINOR.$PATCH" \
-n "<h1>Release for version $MAJOR.$MINOR.$PATCH</h1><h2>Commits since last release</h2><b>$COMMITS</b>" \
-n "<h1>Release for version $MAJOR.$MINOR.$PATCH</h1><h2>Commits since last release</h2><b>$(git log origin/master..origin/develop --no-merges --reverse --format=format:%h\ %s\<br\>\<br\>)</b>" \
--target master
shell: bash
env:
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
2 changes: 1 addition & 1 deletion renovate.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"prHourlyLimit": 0,
"stabilityDays": 3,
"prCreation": "not-pending",
"ignoreDeps": ["openjdk"],
"ignoreDeps": ["openjdk", "postgres"],
"packageRules": [
{
"matchPackagePatterns": [
Expand Down
9 changes: 6 additions & 3 deletions src/main/kotlin/no/uib/echo/Database.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package no.uib.echo

import com.zaxxer.hikari.*
import com.zaxxer.hikari.HikariConfig
import com.zaxxer.hikari.HikariDataSource
import no.uib.echo.schema.Answer
import no.uib.echo.schema.Bedpres
import no.uib.echo.schema.Registration
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.Database
import org.jetbrains.exposed.sql.SchemaUtils
import org.jetbrains.exposed.sql.transactions.transaction
import java.net.URI

Expand Down Expand Up @@ -45,7 +48,7 @@ object Db {

fun init() {
transaction(conn) {
SchemaUtils.create(Bedpres, Registration)
SchemaUtils.create(Bedpres, Registration, Answer)
}
}
}
27 changes: 27 additions & 0 deletions src/main/kotlin/no/uib/echo/schema/Answer.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package no.uib.echo.schema

import no.uib.echo.schema.Answer.answer
import no.uib.echo.schema.Answer.question
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.transactions.transaction


data class AnswerJson(
val question: String,
val answer: String
)

object Answer : Table() {
val question: Column<String> = varchar("question", 50)
val answer: Column<String> = varchar("answer", 50)
val bedpresSlug: Column<String> = varchar("bedpres_slug", 50) references Bedpres.slug
val registrationEmail: Column<String> = varchar("registration_email", 50)
}

fun selectQuestionsByEmailAndSlug(email: String, slug: String): List<AnswerJson> {
val result = transaction {
Answer.select { Answer.registrationEmail eq email and (Answer.bedpresSlug eq slug)}.toList()
}

return result.map { q -> AnswerJson(q[question], q[answer]) }
}
2 changes: 1 addition & 1 deletion src/main/kotlin/no/uib/echo/schema/Bedpres.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ data class BedpresJson(val slug: String, val spots: Int, val registrationDate: S
data class BedpresSlugJson(val slug: String)

object Bedpres : Table() {
val slug: Column<String> = varchar("slug", 40).uniqueIndex()
val slug: Column<String> = varchar("slug", 50).uniqueIndex()
val spots: Column<Int> = integer("spots")
val registrationDate: Column<DateTime> = datetime("registrationDate")

Expand Down
19 changes: 19 additions & 0 deletions src/main/kotlin/no/uib/echo/schema/Degree.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package no.uib.echo.schema

enum class Degree {
DTEK,
DSIK,
DVIT,
BINF,
IMO,
IKT,
KOGNI,
INF,
PROG,
ARMNINF,
POST,
MISC
}

val bachelors: List<Degree> = listOf(Degree.DTEK, Degree.DSIK, Degree.DVIT, Degree.BINF, Degree.IMO, Degree.IKT)
val masters: List<Degree> = listOf(Degree.INF, Degree.PROG)
62 changes: 28 additions & 34 deletions src/main/kotlin/no/uib/echo/schema/Registration.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
package no.uib.echo.schema

import org.jetbrains.exposed.sql.Column
import org.jetbrains.exposed.sql.StdOutSqlLogger
import org.jetbrains.exposed.sql.Table
import org.jetbrains.exposed.sql.addLogger
import org.jetbrains.exposed.sql.and
import org.jetbrains.exposed.sql.deleteWhere
import org.jetbrains.exposed.sql.insert
import org.jetbrains.exposed.sql.jodatime.CurrentDateTime
import org.jetbrains.exposed.sql.jodatime.datetime
import org.jetbrains.exposed.sql.select
import no.uib.echo.schema.Answer.answer
import no.uib.echo.schema.Answer.bedpresSlug
import no.uib.echo.schema.Answer.question
import no.uib.echo.schema.Answer.registrationEmail
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.jodatime.*
import org.jetbrains.exposed.sql.transactions.transaction
import org.joda.time.DateTime

Expand All @@ -30,39 +26,26 @@ data class RegistrationJson(
val slug: String,
val terms: Boolean,
val submitDate: String?,
val waitList: Boolean
val waitList: Boolean,
val answers: List<AnswerJson>
)

data class ShortRegistrationJson(val slug: String, val email: String)

object Registration : Table() {
val email: Column<String> = varchar("email", 40)
val firstName: Column<String> = varchar("firstName", 40)
val lastName: Column<String> = varchar("lastName", 40)
val email: Column<String> = varchar("email", 50)
val firstName: Column<String> = varchar("first_name", 50)
val lastName: Column<String> = varchar("last_name", 50)
val degree: Column<String> = varchar("degree", 50)
val degreeYear: Column<Int> = integer("degreeYear")
val bedpresSlug: Column<String> = varchar("bedpresSlug", 40) references Bedpres.slug
val degreeYear: Column<Int> = integer("degree_year")
val bedpresSlug: Column<String> = varchar("bedpres_slug", 50) references Bedpres.slug
val terms: Column<Boolean> = bool("terms")
val submitDate: Column<DateTime> = datetime("submitDate").defaultExpression(CurrentDateTime())
val waitList: Column<Boolean> = bool("waitList")
val submitDate: Column<DateTime> = datetime("submit_date").defaultExpression(CurrentDateTime())
val waitList: Column<Boolean> = bool("wait_list")

override val primaryKey: PrimaryKey = PrimaryKey(email, bedpresSlug)
}

enum class Degree {
DTEK,
DSIK,
DVIT,
BINF,
IMO,
IKT,
KOGNI,
INF,
PROG,
ARMNINF,
POST,
MISC,
}

fun selectRegistrations(
emailParam: String?,
Expand All @@ -85,6 +68,7 @@ fun selectRegistrations(
}

return (result?.map { reg ->

RegistrationJson(
reg[Registration.email],
reg[Registration.firstName],
Expand All @@ -94,7 +78,8 @@ fun selectRegistrations(
reg[Registration.bedpresSlug],
reg[Registration.terms],
reg[Registration.submitDate].toString(),
reg[Registration.waitList]
reg[Registration.waitList],
selectQuestionsByEmailAndSlug(reg[Registration.email], reg[Registration.bedpresSlug])
)
})
}
Expand All @@ -112,7 +97,7 @@ fun insertRegistration(reg: RegistrationJson): Pair<String?, RegistrationStatus>
val countRegs = Registration.select { Registration.bedpresSlug eq reg.slug }.toList()
val waitList = countRegs.size >= bedpres.spots

val oldReg = Registration.select { Registration.email eq reg.email }.firstOrNull()
val oldReg = Registration.select { Registration.email eq reg.email and (Registration.bedpresSlug eq bedpres.slug)}.firstOrNull()
if (oldReg != null)
return@transaction Pair(null, RegistrationStatus.ALREADY_EXISTS)

Expand All @@ -127,6 +112,15 @@ fun insertRegistration(reg: RegistrationJson): Pair<String?, RegistrationStatus>
it[Registration.waitList] = waitList
}

if (reg.answers.isNotEmpty()) {
Answer.batchInsert(reg.answers) { a ->
this[registrationEmail] = reg.email
this[bedpresSlug] = reg.slug
this[question] = a.question
this[answer] = a.answer
}
}

return@transaction Pair(null, if (waitList) RegistrationStatus.WAITLIST else RegistrationStatus.ACCEPTED)
}
}
Expand Down
21 changes: 9 additions & 12 deletions src/test/kotlin/no/uib/echo/BedpresTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ import io.ktor.http.HttpStatusCode
import no.uib.echo.plugins.Routing

import no.uib.echo.plugins.configureRouting
import no.uib.echo.schema.Bedpres
import no.uib.echo.schema.BedpresJson
import no.uib.echo.schema.BedpresSlugJson
import no.uib.echo.schema.Registration
import no.uib.echo.schema.*
import org.jetbrains.exposed.sql.SchemaUtils
import org.jetbrains.exposed.sql.StdOutSqlLogger
import org.jetbrains.exposed.sql.addLogger
Expand All @@ -30,13 +27,13 @@ class BedpresTest : StringSpec({
transaction {
addLogger(StdOutSqlLogger)

SchemaUtils.drop(Registration, Bedpres)
SchemaUtils.create(Registration, Bedpres)
SchemaUtils.drop(Registration, Answer, Bedpres)
SchemaUtils.create(Registration, Answer, Bedpres)
}
}


"PUT request on /${Routing.bedpresRoute} with correct payload should return OK" {
"When trying to submit a bedpres, server should respond with OK." {
withTestApplication({
configureRouting("secret")
}) {
Expand All @@ -51,7 +48,7 @@ class BedpresTest : StringSpec({
}
}

"PUT request on /${Routing.bedpresRoute} with correct payload should return OK, when the slug already exists but the value for spots is different" {
"Whe trying to update bedpres spots, server should respond with OK." {
withTestApplication({
configureRouting("secret")
}) {
Expand All @@ -75,7 +72,7 @@ class BedpresTest : StringSpec({
}
}

"PUT request on /${Routing.bedpresRoute} with correct payload should return ACCEPTED, when the slug already exists and spots and registrationDate both have the same values" {
"When trying to update a bedpres with the excact same values, server should respond with ACCEPTED." {
withTestApplication({
configureRouting("secret")
}) {
Expand All @@ -99,7 +96,7 @@ class BedpresTest : StringSpec({
}
}

"PUT request on /${Routing.bedpresRoute} with incorrect payload should return INTERNAL_SERVER_ERROR" {
"When trying to submit a bedpres with bad data, server should respond with INTERNAL_SERVER_ERROR." {
withTestApplication({
configureRouting("secret")
}) {
Expand All @@ -114,7 +111,7 @@ class BedpresTest : StringSpec({
}
}

"PUT request on /${Routing.bedpresRoute} with wrong Authorization header should return UNAUTHORIZED" {
"When trying to submit or update a bedpres with wrong Authorization header, server should respond with UNAUTHORIZED." {
withTestApplication({
configureRouting("secret")
}) {
Expand All @@ -127,7 +124,7 @@ class BedpresTest : StringSpec({
}
}

"DELETE request on /${Routing.bedpresRoute} with wrong Authorization header should return UNAUTHORIZED" {
"When trying to delete a bedpres with wrong Authorization header, server should respond with UNAUTHORIZED." {
withTestApplication({
configureRouting("secret")
}) {
Expand Down
Loading

0 comments on commit 9fcba1a

Please sign in to comment.