cards #8
11 changed files with 188 additions and 510 deletions
|
@ -1,68 +1,16 @@
|
||||||
package com.faraphel.tasks_valider.database.api.entities
|
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.dao.base.BaseTaskDao
|
||||||
import com.faraphel.tasks_valider.database.entities.ClassEntity
|
import com.faraphel.tasks_valider.database.entities.ClassEntity
|
||||||
import com.faraphel.tasks_valider.database.entities.SessionEntity
|
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
|
class ClassApi(
|
||||||
|
dao: BaseTaskDao<ClassEntity>,
|
||||||
override fun head(httpSession: NanoHTTPD.IHTTPSession): NanoHTTPD.Response {
|
session: SessionEntity
|
||||||
// get the content of the request
|
) : BaseTaskApi<ClassEntity>(
|
||||||
val data = httpSession.getBody()
|
dao,
|
||||||
// parse the object
|
session,
|
||||||
val obj = parser.fromJson(data, ClassEntity::class.java)
|
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"
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
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.dao.base.BaseTaskDao
|
||||||
import com.faraphel.tasks_valider.database.entities.PersonEntity
|
import com.faraphel.tasks_valider.database.entities.PersonEntity
|
||||||
import com.faraphel.tasks_valider.database.entities.SessionEntity
|
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
|
class PersonApi(
|
||||||
|
dao: BaseTaskDao<PersonEntity>,
|
||||||
override fun head(httpSession: NanoHTTPD.IHTTPSession): NanoHTTPD.Response {
|
session: SessionEntity
|
||||||
// get the content of the request
|
) : BaseTaskApi<PersonEntity>(
|
||||||
val data = httpSession.getBody()
|
dao,
|
||||||
// parse the object
|
session,
|
||||||
val obj = parser.fromJson(data, PersonEntity::class.java)
|
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"
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
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.dao.base.BaseTaskDao
|
||||||
import com.faraphel.tasks_valider.database.entities.RelationClassPersonEntity
|
import com.faraphel.tasks_valider.database.entities.RelationClassPersonEntity
|
||||||
import com.faraphel.tasks_valider.database.entities.SessionEntity
|
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(
|
class RelationClassPersonApi(
|
||||||
private val dao: BaseTaskDao<RelationClassPersonEntity>,
|
dao: BaseTaskDao<RelationClassPersonEntity>,
|
||||||
private val session: SessionEntity
|
session: SessionEntity
|
||||||
) : BaseApi {
|
) : BaseTaskApi<RelationClassPersonEntity>(
|
||||||
companion object {
|
dao,
|
||||||
private val parser = Gson() ///< The JSON parser
|
session,
|
||||||
}
|
RelationClassPersonEntity::class.java
|
||||||
|
)
|
||||||
// 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"
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
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.dao.base.BaseTaskDao
|
||||||
import com.faraphel.tasks_valider.database.entities.RelationPersonSessionSubjectEntity
|
import com.faraphel.tasks_valider.database.entities.RelationPersonSessionSubjectEntity
|
||||||
import com.faraphel.tasks_valider.database.entities.SessionEntity
|
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(
|
class RelationPersonSessionSubjectApi(
|
||||||
private val dao: BaseTaskDao<RelationPersonSessionSubjectEntity>,
|
dao: BaseTaskDao<RelationPersonSessionSubjectEntity>,
|
||||||
private val session: SessionEntity
|
session: SessionEntity
|
||||||
) : BaseApi {
|
) : BaseTaskApi<RelationPersonSessionSubjectEntity>(
|
||||||
companion object {
|
dao,
|
||||||
private val parser = Gson() ///< The JSON parser
|
session,
|
||||||
}
|
RelationPersonSessionSubjectEntity::class.java
|
||||||
|
)
|
||||||
// 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"
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
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.dao.base.BaseTaskDao
|
||||||
import com.faraphel.tasks_valider.database.entities.SessionEntity
|
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(
|
class SessionApi(
|
||||||
private val dao: BaseTaskDao<SessionEntity>,
|
dao: BaseTaskDao<SessionEntity>,
|
||||||
private val session: SessionEntity
|
session: SessionEntity
|
||||||
) : BaseApi {
|
) : BaseTaskApi<SessionEntity>(
|
||||||
companion object {
|
dao,
|
||||||
private val parser = Gson() ///< The JSON parser
|
session,
|
||||||
}
|
SessionEntity::class.java
|
||||||
|
)
|
||||||
// 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"
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
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.dao.base.BaseTaskDao
|
||||||
import com.faraphel.tasks_valider.database.entities.SessionEntity
|
import com.faraphel.tasks_valider.database.entities.SessionEntity
|
||||||
import com.faraphel.tasks_valider.database.entities.SubjectEntity
|
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(
|
class SubjectApi(
|
||||||
private val dao: BaseTaskDao<SubjectEntity>,
|
dao: BaseTaskDao<SubjectEntity>,
|
||||||
private val session: SessionEntity
|
session: SessionEntity
|
||||||
) : BaseApi {
|
) : BaseTaskApi<SubjectEntity>(
|
||||||
companion object {
|
dao,
|
||||||
private val parser = Gson() ///< The JSON parser
|
session,
|
||||||
}
|
SubjectEntity::class.java
|
||||||
|
)
|
||||||
// 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"
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
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.dao.base.BaseTaskDao
|
||||||
import com.faraphel.tasks_valider.database.entities.SessionEntity
|
import com.faraphel.tasks_valider.database.entities.SessionEntity
|
||||||
import com.faraphel.tasks_valider.database.entities.TaskEntity
|
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(
|
class TaskApi(
|
||||||
private val dao: BaseTaskDao<TaskEntity>,
|
dao: BaseTaskDao<TaskEntity>,
|
||||||
private val session: SessionEntity
|
session: SessionEntity
|
||||||
) : BaseApi {
|
) : BaseTaskApi<TaskEntity>(
|
||||||
companion object {
|
dao,
|
||||||
private val parser = Gson() ///< The JSON parser
|
session,
|
||||||
}
|
TaskEntity::class.java
|
||||||
|
)
|
||||||
// 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"
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
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.dao.base.BaseTaskDao
|
||||||
import com.faraphel.tasks_valider.database.entities.SessionEntity
|
import com.faraphel.tasks_valider.database.entities.SessionEntity
|
||||||
import com.faraphel.tasks_valider.database.entities.ValidationEntity
|
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(
|
class ValidationApi(
|
||||||
private val dao: BaseTaskDao<ValidationEntity>,
|
dao: BaseTaskDao<ValidationEntity>,
|
||||||
private val session: SessionEntity
|
session: SessionEntity
|
||||||
) : BaseApi {
|
) : BaseTaskApi<ValidationEntity>(
|
||||||
companion object {
|
dao,
|
||||||
private val parser = Gson() ///< The JSON parser
|
session,
|
||||||
}
|
ValidationEntity::class.java
|
||||||
|
)
|
||||||
// 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"
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
* @param Entity the entity to handle
|
||||||
*/
|
*/
|
||||||
interface BaseCronDao<Entity> {
|
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.
|
* Check if the entities exists in the database.
|
||||||
*/
|
*/
|
||||||
|
@ -17,12 +24,24 @@ interface BaseCronDao<Entity> {
|
||||||
return this.getAll().containsAll(entities.toList())
|
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 the entities into the database.
|
||||||
*/
|
*/
|
||||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||||
fun insert(vararg entities: Entity): List<Long>
|
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.
|
* Delete the entities from the database.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -172,12 +172,12 @@ fun CommunicationInternetServerContent(
|
||||||
Thread { // a thread is used for networking
|
Thread { // a thread is used for networking
|
||||||
|
|
||||||
// Insert the admin in the database and get its id
|
// 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)!!
|
adminPersonEntity.value = database.personDao().getById(adminPersonEntityId)!!
|
||||||
|
|
||||||
// Create a new session
|
// Create a new session
|
||||||
// TODO(Faraphel): name
|
// TODO(Faraphel): name
|
||||||
val (sessionId) = database.sessionDao().insert(
|
val sessionId = database.sessionDao().insert(
|
||||||
SessionEntity(
|
SessionEntity(
|
||||||
name="NOM",
|
name="NOM",
|
||||||
start=Instant.now(),
|
start=Instant.now(),
|
||||||
|
|
Loading…
Reference in a new issue