cards #8
11 changed files with 188 additions and 510 deletions
|
@ -1,68 +1,16 @@
|
|||
package com.faraphel.tasks_valider.database.api.entities
|
||||
|
||||
import com.faraphel.tasks_valider.database.api.entities.base.BaseApi
|
||||
import com.faraphel.tasks_valider.database.api.entities.base.BaseTaskApi
|
||||
import com.faraphel.tasks_valider.database.dao.base.BaseTaskDao
|
||||
import com.faraphel.tasks_valider.database.entities.ClassEntity
|
||||
import com.faraphel.tasks_valider.database.entities.SessionEntity
|
||||
import com.faraphel.tasks_valider.utils.getBody
|
||||
import com.google.gson.Gson
|
||||
import fi.iki.elonen.NanoHTTPD
|
||||
|
||||
class ClassApi(private val dao: BaseTaskDao<ClassEntity>, private val session: SessionEntity) : BaseApi {
|
||||
companion object {
|
||||
private val parser = Gson() ///< The JSON parser
|
||||
}
|
||||
|
||||
// Requests
|
||||
|
||||
override fun head(httpSession: NanoHTTPD.IHTTPSession): NanoHTTPD.Response {
|
||||
// get the content of the request
|
||||
val data = httpSession.getBody()
|
||||
// parse the object
|
||||
val obj = parser.fromJson(data, ClassEntity::class.java)
|
||||
// check if the object is in the object accessible from the session
|
||||
val exists = this.dao.getAllBySession(session.id).contains(obj)
|
||||
|
||||
return NanoHTTPD.newFixedLengthResponse(
|
||||
if (exists) NanoHTTPD.Response.Status.OK else NanoHTTPD.Response.Status.NOT_FOUND,
|
||||
"text/plain",
|
||||
if (exists) "Exists" else "Not found"
|
||||
class ClassApi(
|
||||
dao: BaseTaskDao<ClassEntity>,
|
||||
session: SessionEntity
|
||||
) : BaseTaskApi<ClassEntity>(
|
||||
dao,
|
||||
session,
|
||||
ClassEntity::class.java
|
||||
)
|
||||
}
|
||||
|
||||
override fun get(httpSession: NanoHTTPD.IHTTPSession): NanoHTTPD.Response {
|
||||
return NanoHTTPD.newFixedLengthResponse(
|
||||
NanoHTTPD.Response.Status.OK,
|
||||
"application/json",
|
||||
parser.toJson(this.dao.getAllBySession(session.id))
|
||||
)
|
||||
}
|
||||
|
||||
override fun post(httpSession: NanoHTTPD.IHTTPSession): NanoHTTPD.Response {
|
||||
// get the content of the request
|
||||
val data = httpSession.getBody()
|
||||
// parse the object
|
||||
val obj = parser.fromJson(data, ClassEntity::class.java)
|
||||
val id = this.dao.insert(obj)
|
||||
|
||||
return NanoHTTPD.newFixedLengthResponse(
|
||||
NanoHTTPD.Response.Status.CREATED,
|
||||
"text/plain",
|
||||
id.toString()
|
||||
)
|
||||
}
|
||||
|
||||
override fun delete(httpSession: NanoHTTPD.IHTTPSession): NanoHTTPD.Response {
|
||||
// get the content of the request
|
||||
val data = httpSession.getBody()
|
||||
// parse the object
|
||||
val obj = parser.fromJson(data, ClassEntity::class.java)
|
||||
val count = this.dao.delete(obj)
|
||||
|
||||
return NanoHTTPD.newFixedLengthResponse(
|
||||
if (count > 0) NanoHTTPD.Response.Status.OK else NanoHTTPD.Response.Status.NOT_FOUND,
|
||||
"text/plain",
|
||||
count.toString()
|
||||
)
|
||||
}
|
||||
}
|
|
@ -1,68 +1,16 @@
|
|||
package com.faraphel.tasks_valider.database.api.entities
|
||||
|
||||
import com.faraphel.tasks_valider.database.api.entities.base.BaseApi
|
||||
import com.faraphel.tasks_valider.database.api.entities.base.BaseTaskApi
|
||||
import com.faraphel.tasks_valider.database.dao.base.BaseTaskDao
|
||||
import com.faraphel.tasks_valider.database.entities.PersonEntity
|
||||
import com.faraphel.tasks_valider.database.entities.SessionEntity
|
||||
import com.faraphel.tasks_valider.utils.getBody
|
||||
import com.google.gson.Gson
|
||||
import fi.iki.elonen.NanoHTTPD
|
||||
|
||||
class PersonApi(private val dao: BaseTaskDao<PersonEntity>, private val session: SessionEntity) : BaseApi {
|
||||
companion object {
|
||||
private val parser = Gson() ///< The JSON parser
|
||||
}
|
||||
|
||||
// Requests
|
||||
|
||||
override fun head(httpSession: NanoHTTPD.IHTTPSession): NanoHTTPD.Response {
|
||||
// get the content of the request
|
||||
val data = httpSession.getBody()
|
||||
// parse the object
|
||||
val obj = parser.fromJson(data, PersonEntity::class.java)
|
||||
// check if the object is in the object accessible from the session
|
||||
val exists = this.dao.getAllBySession(session.id).contains(obj)
|
||||
|
||||
return NanoHTTPD.newFixedLengthResponse(
|
||||
if (exists) NanoHTTPD.Response.Status.OK else NanoHTTPD.Response.Status.NOT_FOUND,
|
||||
"text/plain",
|
||||
if (exists) "Exists" else "Not found"
|
||||
class PersonApi(
|
||||
dao: BaseTaskDao<PersonEntity>,
|
||||
session: SessionEntity
|
||||
) : BaseTaskApi<PersonEntity>(
|
||||
dao,
|
||||
session,
|
||||
PersonEntity::class.java
|
||||
)
|
||||
}
|
||||
|
||||
override fun get(httpSession: NanoHTTPD.IHTTPSession): NanoHTTPD.Response {
|
||||
return NanoHTTPD.newFixedLengthResponse(
|
||||
NanoHTTPD.Response.Status.OK,
|
||||
"application/json",
|
||||
parser.toJson(this.dao.getAllBySession(session.id))
|
||||
)
|
||||
}
|
||||
|
||||
override fun post(httpSession: NanoHTTPD.IHTTPSession): NanoHTTPD.Response {
|
||||
// get the content of the request
|
||||
val data = httpSession.getBody()
|
||||
// parse the object
|
||||
val obj = parser.fromJson(data, PersonEntity::class.java)
|
||||
val id = this.dao.insert(obj)
|
||||
|
||||
return NanoHTTPD.newFixedLengthResponse(
|
||||
NanoHTTPD.Response.Status.CREATED,
|
||||
"text/plain",
|
||||
id.toString()
|
||||
)
|
||||
}
|
||||
|
||||
override fun delete(httpSession: NanoHTTPD.IHTTPSession): NanoHTTPD.Response {
|
||||
// get the content of the request
|
||||
val data = httpSession.getBody()
|
||||
// parse the object
|
||||
val obj = parser.fromJson(data, PersonEntity::class.java)
|
||||
val count = this.dao.delete(obj)
|
||||
|
||||
return NanoHTTPD.newFixedLengthResponse(
|
||||
if (count > 0) NanoHTTPD.Response.Status.OK else NanoHTTPD.Response.Status.NOT_FOUND,
|
||||
"text/plain",
|
||||
count.toString()
|
||||
)
|
||||
}
|
||||
}
|
|
@ -1,72 +1,16 @@
|
|||
package com.faraphel.tasks_valider.database.api.entities
|
||||
|
||||
import com.faraphel.tasks_valider.database.api.entities.base.BaseApi
|
||||
import com.faraphel.tasks_valider.database.api.entities.base.BaseTaskApi
|
||||
import com.faraphel.tasks_valider.database.dao.base.BaseTaskDao
|
||||
import com.faraphel.tasks_valider.database.entities.RelationClassPersonEntity
|
||||
import com.faraphel.tasks_valider.database.entities.SessionEntity
|
||||
import com.faraphel.tasks_valider.utils.getBody
|
||||
import com.google.gson.Gson
|
||||
import fi.iki.elonen.NanoHTTPD
|
||||
|
||||
|
||||
class RelationClassPersonApi(
|
||||
private val dao: BaseTaskDao<RelationClassPersonEntity>,
|
||||
private val session: SessionEntity
|
||||
) : BaseApi {
|
||||
companion object {
|
||||
private val parser = Gson() ///< The JSON parser
|
||||
}
|
||||
|
||||
// Requests
|
||||
|
||||
override fun head(httpSession: NanoHTTPD.IHTTPSession): NanoHTTPD.Response {
|
||||
// get the content of the request
|
||||
val data = httpSession.getBody()
|
||||
// parse the object
|
||||
val obj = parser.fromJson(data, RelationClassPersonEntity::class.java)
|
||||
// check if the object is in the object accessible from the session
|
||||
val exists = this.dao.getAllBySession(session.id).contains(obj)
|
||||
|
||||
return NanoHTTPD.newFixedLengthResponse(
|
||||
if (exists) NanoHTTPD.Response.Status.OK else NanoHTTPD.Response.Status.NOT_FOUND,
|
||||
"text/plain",
|
||||
if (exists) "Exists" else "Not found"
|
||||
dao: BaseTaskDao<RelationClassPersonEntity>,
|
||||
session: SessionEntity
|
||||
) : BaseTaskApi<RelationClassPersonEntity>(
|
||||
dao,
|
||||
session,
|
||||
RelationClassPersonEntity::class.java
|
||||
)
|
||||
}
|
||||
|
||||
override fun get(httpSession: NanoHTTPD.IHTTPSession): NanoHTTPD.Response {
|
||||
return NanoHTTPD.newFixedLengthResponse(
|
||||
NanoHTTPD.Response.Status.OK,
|
||||
"application/json",
|
||||
parser.toJson(this.dao.getAllBySession(session.id))
|
||||
)
|
||||
}
|
||||
|
||||
override fun post(httpSession: NanoHTTPD.IHTTPSession): NanoHTTPD.Response {
|
||||
// get the content of the request
|
||||
val data = httpSession.getBody()
|
||||
// parse the object
|
||||
val obj = parser.fromJson(data, RelationClassPersonEntity::class.java)
|
||||
val id = this.dao.insert(obj)
|
||||
|
||||
return NanoHTTPD.newFixedLengthResponse(
|
||||
NanoHTTPD.Response.Status.CREATED,
|
||||
"text/plain",
|
||||
id.toString()
|
||||
)
|
||||
}
|
||||
|
||||
override fun delete(httpSession: NanoHTTPD.IHTTPSession): NanoHTTPD.Response {
|
||||
// get the content of the request
|
||||
val data = httpSession.getBody()
|
||||
// parse the object
|
||||
val obj = parser.fromJson(data, RelationClassPersonEntity::class.java)
|
||||
val count = this.dao.delete(obj)
|
||||
|
||||
return NanoHTTPD.newFixedLengthResponse(
|
||||
if (count > 0) NanoHTTPD.Response.Status.OK else NanoHTTPD.Response.Status.NOT_FOUND,
|
||||
"text/plain",
|
||||
count.toString()
|
||||
)
|
||||
}
|
||||
}
|
|
@ -1,72 +1,16 @@
|
|||
package com.faraphel.tasks_valider.database.api.entities
|
||||
|
||||
import com.faraphel.tasks_valider.database.api.entities.base.BaseApi
|
||||
import com.faraphel.tasks_valider.database.api.entities.base.BaseTaskApi
|
||||
import com.faraphel.tasks_valider.database.dao.base.BaseTaskDao
|
||||
import com.faraphel.tasks_valider.database.entities.RelationPersonSessionSubjectEntity
|
||||
import com.faraphel.tasks_valider.database.entities.SessionEntity
|
||||
import com.faraphel.tasks_valider.utils.getBody
|
||||
import com.google.gson.Gson
|
||||
import fi.iki.elonen.NanoHTTPD
|
||||
|
||||
|
||||
class RelationPersonSessionSubjectApi(
|
||||
private val dao: BaseTaskDao<RelationPersonSessionSubjectEntity>,
|
||||
private val session: SessionEntity
|
||||
) : BaseApi {
|
||||
companion object {
|
||||
private val parser = Gson() ///< The JSON parser
|
||||
}
|
||||
|
||||
// Requests
|
||||
|
||||
override fun head(httpSession: NanoHTTPD.IHTTPSession): NanoHTTPD.Response {
|
||||
// get the content of the request
|
||||
val data = httpSession.getBody()
|
||||
// parse the object
|
||||
val obj = parser.fromJson(data, RelationPersonSessionSubjectEntity::class.java)
|
||||
// check if the object is in the object accessible from the session
|
||||
val exists = this.dao.getAllBySession(session.id).contains(obj)
|
||||
|
||||
return NanoHTTPD.newFixedLengthResponse(
|
||||
if (exists) NanoHTTPD.Response.Status.OK else NanoHTTPD.Response.Status.NOT_FOUND,
|
||||
"text/plain",
|
||||
if (exists) "Exists" else "Not found"
|
||||
dao: BaseTaskDao<RelationPersonSessionSubjectEntity>,
|
||||
session: SessionEntity
|
||||
) : BaseTaskApi<RelationPersonSessionSubjectEntity>(
|
||||
dao,
|
||||
session,
|
||||
RelationPersonSessionSubjectEntity::class.java
|
||||
)
|
||||
}
|
||||
|
||||
override fun get(httpSession: NanoHTTPD.IHTTPSession): NanoHTTPD.Response {
|
||||
return NanoHTTPD.newFixedLengthResponse(
|
||||
NanoHTTPD.Response.Status.OK,
|
||||
"application/json",
|
||||
parser.toJson(this.dao.getAllBySession(session.id))
|
||||
)
|
||||
}
|
||||
|
||||
override fun post(httpSession: NanoHTTPD.IHTTPSession): NanoHTTPD.Response {
|
||||
// get the content of the request
|
||||
val data = httpSession.getBody()
|
||||
// parse the object
|
||||
val obj = parser.fromJson(data, RelationPersonSessionSubjectEntity::class.java)
|
||||
val id = this.dao.insert(obj)
|
||||
|
||||
return NanoHTTPD.newFixedLengthResponse(
|
||||
NanoHTTPD.Response.Status.CREATED,
|
||||
"text/plain",
|
||||
id.toString()
|
||||
)
|
||||
}
|
||||
|
||||
override fun delete(httpSession: NanoHTTPD.IHTTPSession): NanoHTTPD.Response {
|
||||
// get the content of the request
|
||||
val data = httpSession.getBody()
|
||||
// parse the object
|
||||
val obj = parser.fromJson(data, RelationPersonSessionSubjectEntity::class.java)
|
||||
val count = this.dao.delete(obj)
|
||||
|
||||
return NanoHTTPD.newFixedLengthResponse(
|
||||
if (count > 0) NanoHTTPD.Response.Status.OK else NanoHTTPD.Response.Status.NOT_FOUND,
|
||||
"text/plain",
|
||||
count.toString()
|
||||
)
|
||||
}
|
||||
}
|
|
@ -1,71 +1,15 @@
|
|||
package com.faraphel.tasks_valider.database.api.entities
|
||||
|
||||
import com.faraphel.tasks_valider.database.api.entities.base.BaseApi
|
||||
import com.faraphel.tasks_valider.database.api.entities.base.BaseTaskApi
|
||||
import com.faraphel.tasks_valider.database.dao.base.BaseTaskDao
|
||||
import com.faraphel.tasks_valider.database.entities.SessionEntity
|
||||
import com.faraphel.tasks_valider.utils.getBody
|
||||
import com.google.gson.Gson
|
||||
import fi.iki.elonen.NanoHTTPD
|
||||
|
||||
|
||||
class SessionApi(
|
||||
private val dao: BaseTaskDao<SessionEntity>,
|
||||
private val session: SessionEntity
|
||||
) : BaseApi {
|
||||
companion object {
|
||||
private val parser = Gson() ///< The JSON parser
|
||||
}
|
||||
|
||||
// Requests
|
||||
|
||||
override fun head(httpSession: NanoHTTPD.IHTTPSession): NanoHTTPD.Response {
|
||||
// get the content of the request
|
||||
val data = httpSession.getBody()
|
||||
// parse the object
|
||||
val obj = parser.fromJson(data, SessionEntity::class.java)
|
||||
// check if the object is in the object accessible from the session
|
||||
val exists = this.dao.getAllBySession(session.id).contains(obj)
|
||||
|
||||
return NanoHTTPD.newFixedLengthResponse(
|
||||
if (exists) NanoHTTPD.Response.Status.OK else NanoHTTPD.Response.Status.NOT_FOUND,
|
||||
"text/plain",
|
||||
if (exists) "Exists" else "Not found"
|
||||
dao: BaseTaskDao<SessionEntity>,
|
||||
session: SessionEntity
|
||||
) : BaseTaskApi<SessionEntity>(
|
||||
dao,
|
||||
session,
|
||||
SessionEntity::class.java
|
||||
)
|
||||
}
|
||||
|
||||
override fun get(httpSession: NanoHTTPD.IHTTPSession): NanoHTTPD.Response {
|
||||
return NanoHTTPD.newFixedLengthResponse(
|
||||
NanoHTTPD.Response.Status.OK,
|
||||
"application/json",
|
||||
parser.toJson(this.dao.getAllBySession(session.id))
|
||||
)
|
||||
}
|
||||
|
||||
override fun post(httpSession: NanoHTTPD.IHTTPSession): NanoHTTPD.Response {
|
||||
// get the content of the request
|
||||
val data = httpSession.getBody()
|
||||
// parse the object
|
||||
val obj = parser.fromJson(data, SessionEntity::class.java)
|
||||
val id = this.dao.insert(obj)
|
||||
|
||||
return NanoHTTPD.newFixedLengthResponse(
|
||||
NanoHTTPD.Response.Status.CREATED,
|
||||
"text/plain",
|
||||
id.toString()
|
||||
)
|
||||
}
|
||||
|
||||
override fun delete(httpSession: NanoHTTPD.IHTTPSession): NanoHTTPD.Response {
|
||||
// get the content of the request
|
||||
val data = httpSession.getBody()
|
||||
// parse the object
|
||||
val obj = parser.fromJson(data, SessionEntity::class.java)
|
||||
val count = this.dao.delete(obj)
|
||||
|
||||
return NanoHTTPD.newFixedLengthResponse(
|
||||
if (count > 0) NanoHTTPD.Response.Status.OK else NanoHTTPD.Response.Status.NOT_FOUND,
|
||||
"text/plain",
|
||||
count.toString()
|
||||
)
|
||||
}
|
||||
}
|
|
@ -1,72 +1,16 @@
|
|||
package com.faraphel.tasks_valider.database.api.entities
|
||||
|
||||
import com.faraphel.tasks_valider.database.api.entities.base.BaseApi
|
||||
import com.faraphel.tasks_valider.database.api.entities.base.BaseTaskApi
|
||||
import com.faraphel.tasks_valider.database.dao.base.BaseTaskDao
|
||||
import com.faraphel.tasks_valider.database.entities.SessionEntity
|
||||
import com.faraphel.tasks_valider.database.entities.SubjectEntity
|
||||
import com.faraphel.tasks_valider.utils.getBody
|
||||
import com.google.gson.Gson
|
||||
import fi.iki.elonen.NanoHTTPD
|
||||
|
||||
|
||||
class SubjectApi(
|
||||
private val dao: BaseTaskDao<SubjectEntity>,
|
||||
private val session: SessionEntity
|
||||
) : BaseApi {
|
||||
companion object {
|
||||
private val parser = Gson() ///< The JSON parser
|
||||
}
|
||||
|
||||
// Requests
|
||||
|
||||
override fun head(httpSession: NanoHTTPD.IHTTPSession): NanoHTTPD.Response {
|
||||
// get the content of the request
|
||||
val data = httpSession.getBody()
|
||||
// parse the object
|
||||
val obj = parser.fromJson(data, SubjectEntity::class.java)
|
||||
// check if the object is in the object accessible from the session
|
||||
val exists = this.dao.getAllBySession(session.id).contains(obj)
|
||||
|
||||
return NanoHTTPD.newFixedLengthResponse(
|
||||
if (exists) NanoHTTPD.Response.Status.OK else NanoHTTPD.Response.Status.NOT_FOUND,
|
||||
"text/plain",
|
||||
if (exists) "Exists" else "Not found"
|
||||
dao: BaseTaskDao<SubjectEntity>,
|
||||
session: SessionEntity
|
||||
) : BaseTaskApi<SubjectEntity>(
|
||||
dao,
|
||||
session,
|
||||
SubjectEntity::class.java
|
||||
)
|
||||
}
|
||||
|
||||
override fun get(httpSession: NanoHTTPD.IHTTPSession): NanoHTTPD.Response {
|
||||
return NanoHTTPD.newFixedLengthResponse(
|
||||
NanoHTTPD.Response.Status.OK,
|
||||
"application/json",
|
||||
parser.toJson(this.dao.getAllBySession(session.id))
|
||||
)
|
||||
}
|
||||
|
||||
override fun post(httpSession: NanoHTTPD.IHTTPSession): NanoHTTPD.Response {
|
||||
// get the content of the request
|
||||
val data = httpSession.getBody()
|
||||
// parse the object
|
||||
val obj = parser.fromJson(data, SubjectEntity::class.java)
|
||||
val id = this.dao.insert(obj)
|
||||
|
||||
return NanoHTTPD.newFixedLengthResponse(
|
||||
NanoHTTPD.Response.Status.CREATED,
|
||||
"text/plain",
|
||||
id.toString()
|
||||
)
|
||||
}
|
||||
|
||||
override fun delete(httpSession: NanoHTTPD.IHTTPSession): NanoHTTPD.Response {
|
||||
// get the content of the request
|
||||
val data = httpSession.getBody()
|
||||
// parse the object
|
||||
val obj = parser.fromJson(data, SubjectEntity::class.java)
|
||||
val count = this.dao.delete(obj)
|
||||
|
||||
return NanoHTTPD.newFixedLengthResponse(
|
||||
if (count > 0) NanoHTTPD.Response.Status.OK else NanoHTTPD.Response.Status.NOT_FOUND,
|
||||
"text/plain",
|
||||
count.toString()
|
||||
)
|
||||
}
|
||||
}
|
|
@ -1,72 +1,16 @@
|
|||
package com.faraphel.tasks_valider.database.api.entities
|
||||
|
||||
import com.faraphel.tasks_valider.database.api.entities.base.BaseApi
|
||||
import com.faraphel.tasks_valider.database.api.entities.base.BaseTaskApi
|
||||
import com.faraphel.tasks_valider.database.dao.base.BaseTaskDao
|
||||
import com.faraphel.tasks_valider.database.entities.SessionEntity
|
||||
import com.faraphel.tasks_valider.database.entities.TaskEntity
|
||||
import com.faraphel.tasks_valider.utils.getBody
|
||||
import com.google.gson.Gson
|
||||
import fi.iki.elonen.NanoHTTPD
|
||||
|
||||
|
||||
class TaskApi(
|
||||
private val dao: BaseTaskDao<TaskEntity>,
|
||||
private val session: SessionEntity
|
||||
) : BaseApi {
|
||||
companion object {
|
||||
private val parser = Gson() ///< The JSON parser
|
||||
}
|
||||
|
||||
// Requests
|
||||
|
||||
override fun head(httpSession: NanoHTTPD.IHTTPSession): NanoHTTPD.Response {
|
||||
// get the content of the request
|
||||
val data = httpSession.getBody()
|
||||
// parse the object
|
||||
val obj = parser.fromJson(data, TaskEntity::class.java)
|
||||
// check if the object is in the object accessible from the session
|
||||
val exists = this.dao.getAllBySession(session.id).contains(obj)
|
||||
|
||||
return NanoHTTPD.newFixedLengthResponse(
|
||||
if (exists) NanoHTTPD.Response.Status.OK else NanoHTTPD.Response.Status.NOT_FOUND,
|
||||
"text/plain",
|
||||
if (exists) "Exists" else "Not found"
|
||||
dao: BaseTaskDao<TaskEntity>,
|
||||
session: SessionEntity
|
||||
) : BaseTaskApi<TaskEntity>(
|
||||
dao,
|
||||
session,
|
||||
TaskEntity::class.java
|
||||
)
|
||||
}
|
||||
|
||||
override fun get(httpSession: NanoHTTPD.IHTTPSession): NanoHTTPD.Response {
|
||||
return NanoHTTPD.newFixedLengthResponse(
|
||||
NanoHTTPD.Response.Status.OK,
|
||||
"application/json",
|
||||
parser.toJson(this.dao.getAllBySession(session.id))
|
||||
)
|
||||
}
|
||||
|
||||
override fun post(httpSession: NanoHTTPD.IHTTPSession): NanoHTTPD.Response {
|
||||
// get the content of the request
|
||||
val data = httpSession.getBody()
|
||||
// parse the object
|
||||
val obj = parser.fromJson(data, TaskEntity::class.java)
|
||||
val id = this.dao.insert(obj)
|
||||
|
||||
return NanoHTTPD.newFixedLengthResponse(
|
||||
NanoHTTPD.Response.Status.CREATED,
|
||||
"text/plain",
|
||||
id.toString()
|
||||
)
|
||||
}
|
||||
|
||||
override fun delete(httpSession: NanoHTTPD.IHTTPSession): NanoHTTPD.Response {
|
||||
// get the content of the request
|
||||
val data = httpSession.getBody()
|
||||
// parse the object
|
||||
val obj = parser.fromJson(data, TaskEntity::class.java)
|
||||
val count = this.dao.delete(obj)
|
||||
|
||||
return NanoHTTPD.newFixedLengthResponse(
|
||||
if (count > 0) NanoHTTPD.Response.Status.OK else NanoHTTPD.Response.Status.NOT_FOUND,
|
||||
"text/plain",
|
||||
count.toString()
|
||||
)
|
||||
}
|
||||
}
|
|
@ -1,74 +1,16 @@
|
|||
package com.faraphel.tasks_valider.database.api.entities
|
||||
|
||||
import com.faraphel.tasks_valider.database.api.entities.base.BaseApi
|
||||
import com.faraphel.tasks_valider.database.api.entities.base.BaseTaskApi
|
||||
import com.faraphel.tasks_valider.database.dao.base.BaseTaskDao
|
||||
import com.faraphel.tasks_valider.database.entities.SessionEntity
|
||||
import com.faraphel.tasks_valider.database.entities.ValidationEntity
|
||||
import com.faraphel.tasks_valider.utils.getBody
|
||||
import com.google.gson.Gson
|
||||
import fi.iki.elonen.NanoHTTPD
|
||||
|
||||
|
||||
class ValidationApi(
|
||||
private val dao: BaseTaskDao<ValidationEntity>,
|
||||
private val session: SessionEntity
|
||||
) : BaseApi {
|
||||
companion object {
|
||||
private val parser = Gson() ///< The JSON parser
|
||||
}
|
||||
|
||||
// Requests
|
||||
|
||||
override fun head(httpSession: NanoHTTPD.IHTTPSession): NanoHTTPD.Response {
|
||||
// get the content of the request
|
||||
val data = httpSession.getBody()
|
||||
// parse the object
|
||||
val obj = parser.fromJson(data, ValidationEntity::class.java)
|
||||
// check if the object is in the object accessible from the session
|
||||
val exists = this.dao.getAllBySession(session.id).contains(obj)
|
||||
|
||||
return NanoHTTPD.newFixedLengthResponse(
|
||||
if (exists) NanoHTTPD.Response.Status.OK else NanoHTTPD.Response.Status.NOT_FOUND,
|
||||
"text/plain",
|
||||
if (exists) "Exists" else "Not found"
|
||||
dao: BaseTaskDao<ValidationEntity>,
|
||||
session: SessionEntity
|
||||
) : BaseTaskApi<ValidationEntity>(
|
||||
dao,
|
||||
session,
|
||||
ValidationEntity::class.java
|
||||
)
|
||||
}
|
||||
|
||||
override fun get(httpSession: NanoHTTPD.IHTTPSession): NanoHTTPD.Response {
|
||||
return NanoHTTPD.newFixedLengthResponse(
|
||||
NanoHTTPD.Response.Status.OK,
|
||||
"application/json",
|
||||
parser.toJson(this.dao.getAllBySession(session.id))
|
||||
)
|
||||
}
|
||||
|
||||
override fun post(httpSession: NanoHTTPD.IHTTPSession): NanoHTTPD.Response {
|
||||
// get the content of the request
|
||||
val data = httpSession.getBody()
|
||||
// parse the object
|
||||
val obj = parser.fromJson(data, ValidationEntity::class.java)
|
||||
// save the data into the database
|
||||
val id = this.dao.insert(obj)
|
||||
|
||||
return NanoHTTPD.newFixedLengthResponse(
|
||||
NanoHTTPD.Response.Status.CREATED,
|
||||
"text/plain",
|
||||
id.toString()
|
||||
)
|
||||
}
|
||||
|
||||
override fun delete(httpSession: NanoHTTPD.IHTTPSession): NanoHTTPD.Response {
|
||||
// get the content of the request
|
||||
val data = httpSession.getBody()
|
||||
// parse the object
|
||||
val obj = parser.fromJson(data, ValidationEntity::class.java)
|
||||
// delete the object from the database
|
||||
val count = this.dao.delete(obj)
|
||||
|
||||
return NanoHTTPD.newFixedLengthResponse(
|
||||
if (count > 0) NanoHTTPD.Response.Status.OK else NanoHTTPD.Response.Status.NOT_FOUND,
|
||||
"text/plain",
|
||||
count.toString()
|
||||
)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,101 @@
|
|||
package com.faraphel.tasks_valider.database.api.entities.base
|
||||
|
||||
import com.faraphel.tasks_valider.database.dao.base.BaseTaskDao
|
||||
import com.faraphel.tasks_valider.database.entities.SessionEntity
|
||||
import com.faraphel.tasks_valider.utils.getBody
|
||||
import com.google.gson.Gson
|
||||
import fi.iki.elonen.NanoHTTPD
|
||||
|
||||
abstract class BaseTaskApi<Entity> (
|
||||
private val dao: BaseTaskDao<Entity>,
|
||||
private val session: SessionEntity,
|
||||
private val entityType: Class<Entity>,
|
||||
) : BaseApi {
|
||||
companion object {
|
||||
private val parser = Gson() ///< The JSON parser
|
||||
}
|
||||
|
||||
private fun parseJson(data: String): Entity =
|
||||
parser.fromJson(data, entityType)
|
||||
|
||||
/**
|
||||
* Handle an HTTP HEAD request.
|
||||
* Indicate if an object exist in the database.
|
||||
* @param httpSession the current http session to handle.
|
||||
* @return a response indicating if the object does exist.
|
||||
*/
|
||||
override fun head(httpSession: NanoHTTPD.IHTTPSession): NanoHTTPD.Response {
|
||||
// get the content of the request
|
||||
val data = httpSession.getBody()
|
||||
// parse the object
|
||||
val obj = this.parseJson(data)
|
||||
// check if the object is in the object accessible from the session
|
||||
val exists = this.dao.getAllBySession(session.id).contains(obj)
|
||||
|
||||
// return the response
|
||||
return NanoHTTPD.newFixedLengthResponse(
|
||||
if (exists) NanoHTTPD.Response.Status.OK else NanoHTTPD.Response.Status.NOT_FOUND,
|
||||
"text/plain",
|
||||
if (exists) "Exists" else "Not found"
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle an HTTP GET request.
|
||||
* Indicate the content of a table in the database.
|
||||
* @param httpSession the current http session to handle.
|
||||
* @return a response indicating the content of all the objects.
|
||||
*/
|
||||
override fun get(httpSession: NanoHTTPD.IHTTPSession): NanoHTTPD.Response {
|
||||
// return the content of all the objects in the database about this session
|
||||
return NanoHTTPD.newFixedLengthResponse(
|
||||
NanoHTTPD.Response.Status.OK,
|
||||
"application/json",
|
||||
parser.toJson(this.dao.getAllBySession(session.id))
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle an HTTP POST request.
|
||||
* Create a new object in the database.
|
||||
* @param httpSession the current http session to handle.
|
||||
* @return the id of the newly created object
|
||||
*/
|
||||
override fun post(httpSession: NanoHTTPD.IHTTPSession): NanoHTTPD.Response {
|
||||
// get the content of the request
|
||||
val data = httpSession.getBody()
|
||||
// parse the object
|
||||
val obj = parser.fromJson(data, entityType)
|
||||
// insert it into the database
|
||||
val id = this.dao.insert(obj)
|
||||
|
||||
// return the id of the object created
|
||||
return NanoHTTPD.newFixedLengthResponse(
|
||||
NanoHTTPD.Response.Status.CREATED,
|
||||
"text/plain",
|
||||
id.toString()
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle an HTTP DELETE request.
|
||||
* Delete an item from the database.
|
||||
* @param httpSession the current http session to handle.
|
||||
* @return the number of object deleted.
|
||||
*/
|
||||
override fun delete(httpSession: NanoHTTPD.IHTTPSession): NanoHTTPD.Response {
|
||||
// get the content of the request
|
||||
val data = httpSession.getBody()
|
||||
// parse the object
|
||||
val obj = parser.fromJson(data, entityType)
|
||||
// delete all the instance of this object in the database
|
||||
val count = this.dao.delete(obj)
|
||||
|
||||
// return the number of corresponding element deleted
|
||||
return NanoHTTPD.newFixedLengthResponse(
|
||||
if (count > 0) NanoHTTPD.Response.Status.OK else NanoHTTPD.Response.Status.NOT_FOUND,
|
||||
"text/plain",
|
||||
count.toString()
|
||||
)
|
||||
}
|
||||
}
|
|
@ -10,6 +10,13 @@ import androidx.room.OnConflictStrategy
|
|||
* @param Entity the entity to handle
|
||||
*/
|
||||
interface BaseCronDao<Entity> {
|
||||
/**
|
||||
* Check if the entity exists in the database.
|
||||
*/
|
||||
fun exists(entity: Entity): Boolean {
|
||||
return this.getAll().contains(entity)
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the entities exists in the database.
|
||||
*/
|
||||
|
@ -17,12 +24,24 @@ interface BaseCronDao<Entity> {
|
|||
return this.getAll().containsAll(entities.toList())
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert the entities into the database.
|
||||
*/
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
fun insert(entity: Entity): Long
|
||||
|
||||
/**
|
||||
* Insert the entities into the database.
|
||||
*/
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
fun insert(vararg entities: Entity): List<Long>
|
||||
|
||||
/**
|
||||
* Delete the entity from the database.
|
||||
*/
|
||||
@Delete
|
||||
fun delete(entity: Entity): Int
|
||||
|
||||
/**
|
||||
* Delete the entities from the database.
|
||||
*/
|
||||
|
|
|
@ -172,12 +172,12 @@ fun CommunicationInternetServerContent(
|
|||
Thread { // a thread is used for networking
|
||||
|
||||
// Insert the admin in the database and get its id
|
||||
val (adminPersonEntityId) = database.personDao().insert(adminPersonEntityRaw.value!!)
|
||||
val adminPersonEntityId = database.personDao().insert(adminPersonEntityRaw.value!!)
|
||||
adminPersonEntity.value = database.personDao().getById(adminPersonEntityId)!!
|
||||
|
||||
// Create a new session
|
||||
// TODO(Faraphel): name
|
||||
val (sessionId) = database.sessionDao().insert(
|
||||
val sessionId = database.sessionDao().insert(
|
||||
SessionEntity(
|
||||
name="NOM",
|
||||
start=Instant.now(),
|
||||
|
|
Loading…
Reference in a new issue