Skip to content

Commit

Permalink
Code api updated. #5
Browse files Browse the repository at this point in the history
  • Loading branch information
afonsoamachado committed Apr 19, 2022
1 parent e8498d2 commit e5909ab
Show file tree
Hide file tree
Showing 10 changed files with 206 additions and 4 deletions.
25 changes: 25 additions & 0 deletions code/jvm/src/main/kotlin/pt/isel/ion/teams/common/Uris.kt
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,29 @@ object Uris {
fun make(id: Int) = TEMPLATE.expand(mapOf("id" to id))
}
}

object Tags {
const val PATH = "/api/orgs/{orgId}/classrooms/{classroomId}/teams/{teamId}/repos/{repoId}/tags"

object Tag{
const val PATH = "/api/orgs/{orgId}/classrooms/{classroomId}/teams/{teamId}/repos/{repoId}/tags/{tagId}"
private val TEMPLATE = UriTemplate(PATH)
fun make(id: Int) = TEMPLATE.expand(mapOf("id" to id))
}
}

object Account {
const val PATH = "/api/account"
}

object Teachers{
const val PATH = "/api/orgs/{orgId}/classrooms/{classroomId}/teachers"

object Teacher{
const val PATH = "{number}"
private val TEMPLATE = UriTemplate(PATH)
fun make(id: Int) = TEMPLATE.expand(mapOf("id" to id))
}
}

}
7 changes: 3 additions & 4 deletions code/jvm/src/main/kotlin/pt/isel/ion/teams/tags/Tags.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,13 @@ data class TagInputModel(
data class TagUpdateModel(
val name: String?,
val date: Date?,
val delId: Int?,
val teamId: Int?
val delId: Int?
)

/**
* Functions to transition from external to internal, or vice-versa.
*/

fun TagInputModel.toDb() = TagDbWrite(this.name,this.date,this.delId,this.teamId)
fun TagUpdateModel.toDb(id: Int) = TagDbUpdate(id,this.name,this.date,this.delId,this.teamId)
fun TagInputModel.toDb(teamId: Int) = TagDbWrite(this.name,this.date,this.delId,teamId)
fun TagUpdateModel.toDb(id: Int,teamId: Int) = TagDbUpdate(id,this.name,this.date,this.delId,teamId)
fun TagDbRead.toOutput() = TagOutputModel(this.id, this.name,this.date,this.delId,this.teamId)
25 changes: 25 additions & 0 deletions code/jvm/src/main/kotlin/pt/isel/ion/teams/tags/TagsController.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package pt.isel.ion.teams.tags

import org.jdbi.v3.sqlobject.customizer.BindBean
import org.jdbi.v3.sqlobject.statement.GetGeneratedKeys
import org.springframework.web.bind.annotation.*
import pt.isel.ion.teams.common.Uris

@RestController
@RequestMapping(Uris.Tags.PATH)
class TagsController(val tagsService: TagsService) {

@GetMapping
fun getAllTags(@PathVariable teamId: Int): List<TagOutputModel> =
tagsService.getAllTags(teamId).map { it.toOutput() }

@PostMapping
@GetGeneratedKeys
fun createTag(@PathVariable teamId: Int, @BindBean tag: TagInputModel) =
tagsService.createTag(tag.toDb(teamId))

@PutMapping(Uris.Tags.Tag.PATH)
@GetGeneratedKeys
fun updateTag(@PathVariable tagId: Int,@PathVariable teamId: Int, @BindBean tag: TagUpdateModel) =
tagsService.updateTag(tag.toDb(tagId,teamId))
}
22 changes: 22 additions & 0 deletions code/jvm/src/main/kotlin/pt/isel/ion/teams/tags/TagsDAO.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package pt.isel.ion.teams.tags

import org.jdbi.v3.sqlobject.customizer.Bind
import org.jdbi.v3.sqlobject.customizer.BindBean
import org.jdbi.v3.sqlobject.statement.GetGeneratedKeys
import org.jdbi.v3.sqlobject.statement.SqlQuery
import org.jdbi.v3.sqlobject.statement.SqlUpdate
import pt.isel.ion.teams.organizations.OrganizationDbRead

interface TagsDAO {

@SqlQuery("SELECT * FROM tags WHERE teamid=:teamId")
fun getAllTags(@Bind("teamId") teamId: Int): List<TagDbRead>

@SqlUpdate("INSERT INTO tags (name,date,delId,teamId) VALUES (:name,:date,:delId,:teamId)")
@GetGeneratedKeys
fun createTag(@BindBean tag: TagDbWrite): OrganizationDbRead

@SqlUpdate("UPDATE tags SET name=:name,date=:date,delId=:delId,teamId=:teamId WHERE id=:id")
@GetGeneratedKeys
fun updateTag(@BindBean tag: TagDbUpdate): Int
}
14 changes: 14 additions & 0 deletions code/jvm/src/main/kotlin/pt/isel/ion/teams/tags/TagsService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package pt.isel.ion.teams.tags

import org.jdbi.v3.core.Jdbi
import org.springframework.stereotype.Component

@Component
class TagsService(val jdbi: Jdbi) {

fun getAllTags(teamId: Int) = jdbi.onDemand(TagsDAO::class.java).getAllTags(teamId)

fun createTag(tagDbWrite: TagDbWrite) = jdbi.onDemand(TagsDAO::class.java).createTag(tagDbWrite)

fun updateTag(tagDbUpdate: TagDbUpdate) = jdbi.onDemand(TagsDAO::class.java).updateTag(tagDbUpdate)
}
60 changes: 60 additions & 0 deletions code/jvm/src/main/kotlin/pt/isel/ion/teams/teacher/Teacher.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package pt.isel.ion.teams.teacher

import java.util.*


/**
* For internal use only.
*/
data class TeacherDbRead(
val number: Int,
val name: String,
val email: String,
val office: String
)

data class TeacherDbWrite(
val number: Int,
val name: String,
val email: String,
val office: String
)

data class TeacherDbUpdate(
val number: Int,
val name: String?,
val email: String?,
val office: String?
)

/**
* For external use only.
*/

data class TeacherOutputModel(
val number: Int,
val name: String,
val email: String,
val office: String
)

data class TeacherInputModel(
val number: Int,
val name: String,
val email: String,
val office: String
)

data class TeacherUpdateModel(
val name: String?,
val email: String?,
val office: String?
)

/**
* Functions to transition from external to internal, or vice-versa.
*/

fun TeacherInputModel.toDb() = TeacherDbWrite(this.number,this.name,this.email,this.office)
fun TeacherUpdateModel.toDb(number: Int) = TeacherDbUpdate(number,this.name,this.email,this.office)
fun TeacherDbRead.toOutput() = TeacherOutputModel(this.number,this.name,this.email,this.office)
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package pt.isel.ion.teams.teacher

import org.springframework.web.bind.annotation.*
import pt.isel.ion.teams.common.Uris

@RestController
@RequestMapping(Uris.Teachers.PATH)
class TeacherController(val service: TeacherService) {

@GetMapping()
fun getTeachers(@PathVariable classroomId: Int) = service.getTeachers(classroomId)

@GetMapping(Uris.Teachers.Teacher.PATH)
fun getTeacher(@PathVariable number: Int) = service.getTeacher(number)
}
24 changes: 24 additions & 0 deletions code/jvm/src/main/kotlin/pt/isel/ion/teams/teacher/TeacherDAO.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package pt.isel.ion.teams.teacher

import org.jdbi.v3.sqlobject.customizer.Bind
import org.jdbi.v3.sqlobject.customizer.BindBean
import org.jdbi.v3.sqlobject.statement.GetGeneratedKeys
import org.jdbi.v3.sqlobject.statement.SqlQuery
import org.jdbi.v3.sqlobject.statement.SqlUpdate

interface TeacherDAO {

@SqlQuery("SELECT * FROM teacher WHERE cId=:classroomId")
fun getTeachers(@Bind("classroomId") classroomId: Int): List<TeacherDbRead>

@SqlQuery("SELECT * FROM teacher WHERE number=:number")
fun getTeacher(@Bind("number")number: Int): TeacherDbRead

@SqlUpdate("INSERT INTO teacher (number,name,email,office) VALUES (:number,:name,:email,:office)")
@GetGeneratedKeys
fun createTeacher(@BindBean teacher: TeacherDbWrite): TeacherDbRead

@SqlUpdate("UPDATE teacher SET name=:name,email=:email,office=:office WHERE number=:number")
@GetGeneratedKeys
fun updateTeacher(@BindBean teacher: TeacherDbUpdate): Int
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package pt.isel.ion.teams.teacher

import org.jdbi.v3.core.Jdbi
import org.springframework.stereotype.Component

@Component
class TeacherService(val jdbi: Jdbi) {

fun getTeachers(classroomId: Int) = jdbi.onDemand(TeacherDAO::class.java).getTeachers(classroomId)

fun getTeacher(number: Int) = jdbi.onDemand(TeacherDAO::class.java).getTeacher(number)

fun createTeacher(teacher: TeacherDbWrite) = jdbi.onDemand(TeacherDAO::class.java).createTeacher(teacher)

fun updateTeacher(teacher: TeacherDbUpdate) = jdbi.onDemand(TeacherDAO::class.java).updateTeacher(teacher)
}
2 changes: 2 additions & 0 deletions code/sql/createTables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ CREATE TABLE TEACHER
name varchar(50),
email varchar,
office varchar(20), --X.X.XX (e.g G.1.16)
cId int,
PRIMARY KEY (number),
FOREIGN KEY (cId) REFERENCES CLASSROOMS (id),
CONSTRAINT email_check CHECK (email ~* '^[A-Za-z0-9._+%-]+@[A-Za-z0-9.-]+[.][A-Za-z]+$') --TODO: not sure what it is (CHECK)
);

Expand Down

0 comments on commit e5909ab

Please sign in to comment.