From a4325412c0b786db99fc3bf78752d6fc9f5e0578 Mon Sep 17 00:00:00 2001 From: Faraphel Date: Sun, 9 Jun 2024 22:50:59 +0200 Subject: [PATCH] generalised database API --- .../database/api/entities/ClassApi.kt | 70 ++---------- .../database/api/entities/PersonApi.kt | 70 ++---------- .../api/entities/RelationClassPersonApi.kt | 72 ++----------- .../RelationPersonSessionSubjectApi.kt | 72 ++----------- .../database/api/entities/SessionApi.kt | 72 ++----------- .../database/api/entities/SubjectApi.kt | 72 ++----------- .../database/api/entities/TaskApi.kt | 72 ++----------- .../database/api/entities/ValidationApi.kt | 74 ++----------- .../database/api/entities/base/BaseTaskApi.kt | 101 ++++++++++++++++++ .../database/dao/base/BaseCronDao.kt | 19 ++++ .../screen/communication/internet/server.kt | 4 +- 11 files changed, 188 insertions(+), 510 deletions(-) create mode 100644 app/src/main/java/com/faraphel/tasks_valider/database/api/entities/base/BaseTaskApi.kt diff --git a/app/src/main/java/com/faraphel/tasks_valider/database/api/entities/ClassApi.kt b/app/src/main/java/com/faraphel/tasks_valider/database/api/entities/ClassApi.kt index 5512313..3c52d54 100644 --- a/app/src/main/java/com/faraphel/tasks_valider/database/api/entities/ClassApi.kt +++ b/app/src/main/java/com/faraphel/tasks_valider/database/api/entities/ClassApi.kt @@ -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, 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" - ) - } - - 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() - ) - } -} \ No newline at end of file +class ClassApi( + dao: BaseTaskDao, + session: SessionEntity +) : BaseTaskApi( + dao, + session, + ClassEntity::class.java +) diff --git a/app/src/main/java/com/faraphel/tasks_valider/database/api/entities/PersonApi.kt b/app/src/main/java/com/faraphel/tasks_valider/database/api/entities/PersonApi.kt index a6e04e2..5e8f65e 100644 --- a/app/src/main/java/com/faraphel/tasks_valider/database/api/entities/PersonApi.kt +++ b/app/src/main/java/com/faraphel/tasks_valider/database/api/entities/PersonApi.kt @@ -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, 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" - ) - } - - 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() - ) - } -} \ No newline at end of file +class PersonApi( + dao: BaseTaskDao, + session: SessionEntity +) : BaseTaskApi( + dao, + session, + PersonEntity::class.java +) diff --git a/app/src/main/java/com/faraphel/tasks_valider/database/api/entities/RelationClassPersonApi.kt b/app/src/main/java/com/faraphel/tasks_valider/database/api/entities/RelationClassPersonApi.kt index 9a4ef5a..5a90a28 100644 --- a/app/src/main/java/com/faraphel/tasks_valider/database/api/entities/RelationClassPersonApi.kt +++ b/app/src/main/java/com/faraphel/tasks_valider/database/api/entities/RelationClassPersonApi.kt @@ -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, - 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" - ) - } - - 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() - ) - } -} \ No newline at end of file + dao: BaseTaskDao, + session: SessionEntity +) : BaseTaskApi( + dao, + session, + RelationClassPersonEntity::class.java +) diff --git a/app/src/main/java/com/faraphel/tasks_valider/database/api/entities/RelationPersonSessionSubjectApi.kt b/app/src/main/java/com/faraphel/tasks_valider/database/api/entities/RelationPersonSessionSubjectApi.kt index feb1a79..ac6fd2d 100644 --- a/app/src/main/java/com/faraphel/tasks_valider/database/api/entities/RelationPersonSessionSubjectApi.kt +++ b/app/src/main/java/com/faraphel/tasks_valider/database/api/entities/RelationPersonSessionSubjectApi.kt @@ -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, - 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" - ) - } - - 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() - ) - } -} \ No newline at end of file + dao: BaseTaskDao, + session: SessionEntity +) : BaseTaskApi( + dao, + session, + RelationPersonSessionSubjectEntity::class.java +) diff --git a/app/src/main/java/com/faraphel/tasks_valider/database/api/entities/SessionApi.kt b/app/src/main/java/com/faraphel/tasks_valider/database/api/entities/SessionApi.kt index 4c65413..4f2182a 100644 --- a/app/src/main/java/com/faraphel/tasks_valider/database/api/entities/SessionApi.kt +++ b/app/src/main/java/com/faraphel/tasks_valider/database/api/entities/SessionApi.kt @@ -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, - 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" - ) - } - - 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() - ) - } -} \ No newline at end of file + dao: BaseTaskDao, + session: SessionEntity +) : BaseTaskApi( + dao, + session, + SessionEntity::class.java +) diff --git a/app/src/main/java/com/faraphel/tasks_valider/database/api/entities/SubjectApi.kt b/app/src/main/java/com/faraphel/tasks_valider/database/api/entities/SubjectApi.kt index 252cd7f..b0b2057 100644 --- a/app/src/main/java/com/faraphel/tasks_valider/database/api/entities/SubjectApi.kt +++ b/app/src/main/java/com/faraphel/tasks_valider/database/api/entities/SubjectApi.kt @@ -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, - 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" - ) - } - - 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() - ) - } -} \ No newline at end of file + dao: BaseTaskDao, + session: SessionEntity +) : BaseTaskApi( + dao, + session, + SubjectEntity::class.java +) diff --git a/app/src/main/java/com/faraphel/tasks_valider/database/api/entities/TaskApi.kt b/app/src/main/java/com/faraphel/tasks_valider/database/api/entities/TaskApi.kt index 2292dfd..78585fb 100644 --- a/app/src/main/java/com/faraphel/tasks_valider/database/api/entities/TaskApi.kt +++ b/app/src/main/java/com/faraphel/tasks_valider/database/api/entities/TaskApi.kt @@ -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, - 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" - ) - } - - 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() - ) - } -} \ No newline at end of file + dao: BaseTaskDao, + session: SessionEntity +) : BaseTaskApi( + dao, + session, + TaskEntity::class.java +) diff --git a/app/src/main/java/com/faraphel/tasks_valider/database/api/entities/ValidationApi.kt b/app/src/main/java/com/faraphel/tasks_valider/database/api/entities/ValidationApi.kt index 266e1d0..5549501 100644 --- a/app/src/main/java/com/faraphel/tasks_valider/database/api/entities/ValidationApi.kt +++ b/app/src/main/java/com/faraphel/tasks_valider/database/api/entities/ValidationApi.kt @@ -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, - 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" - ) - } - - 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() - ) - } -} \ No newline at end of file + dao: BaseTaskDao, + session: SessionEntity +) : BaseTaskApi( + dao, + session, + ValidationEntity::class.java +) diff --git a/app/src/main/java/com/faraphel/tasks_valider/database/api/entities/base/BaseTaskApi.kt b/app/src/main/java/com/faraphel/tasks_valider/database/api/entities/base/BaseTaskApi.kt new file mode 100644 index 0000000..8875921 --- /dev/null +++ b/app/src/main/java/com/faraphel/tasks_valider/database/api/entities/base/BaseTaskApi.kt @@ -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 ( + private val dao: BaseTaskDao, + private val session: SessionEntity, + private val entityType: Class, +) : 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() + ) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/faraphel/tasks_valider/database/dao/base/BaseCronDao.kt b/app/src/main/java/com/faraphel/tasks_valider/database/dao/base/BaseCronDao.kt index dfd824d..483bfad 100644 --- a/app/src/main/java/com/faraphel/tasks_valider/database/dao/base/BaseCronDao.kt +++ b/app/src/main/java/com/faraphel/tasks_valider/database/dao/base/BaseCronDao.kt @@ -10,6 +10,13 @@ import androidx.room.OnConflictStrategy * @param Entity the entity to handle */ interface BaseCronDao { + /** + * 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 { 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 + /** + * Delete the entity from the database. + */ + @Delete + fun delete(entity: Entity): Int + /** * Delete the entities from the database. */ diff --git a/app/src/main/java/com/faraphel/tasks_valider/ui/screen/communication/internet/server.kt b/app/src/main/java/com/faraphel/tasks_valider/ui/screen/communication/internet/server.kt index 2f81efa..d5138ba 100644 --- a/app/src/main/java/com/faraphel/tasks_valider/ui/screen/communication/internet/server.kt +++ b/app/src/main/java/com/faraphel/tasks_valider/ui/screen/communication/internet/server.kt @@ -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(),