Commit aadfdaad authored by Ghitha Dinan's avatar Ghitha Dinan

delete add check data application

parent 785624a5
...@@ -7,6 +7,7 @@ import id.co.sangkuriang.inovasi.daerah.master.service.TagService ...@@ -7,6 +7,7 @@ import id.co.sangkuriang.inovasi.daerah.master.service.TagService
import org.springframework.beans.factory.annotation.Autowired import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.ResponseEntity import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.* import org.springframework.web.bind.annotation.*
import javax.servlet.http.HttpServletRequest
import javax.validation.Valid import javax.validation.Valid
@RestController @RestController
...@@ -37,7 +38,10 @@ class TagController { ...@@ -37,7 +38,10 @@ class TagController {
} }
@DeleteMapping(value = [""], produces = ["application/json"]) @DeleteMapping(value = [""], produces = ["application/json"])
fun deleteData(@Valid @RequestBody request: DeleteDataRequest): ResponseEntity<*>? { fun deleteData(
return service.deleteData(request) @Valid @RequestBody request: DeleteDataRequest,
httpServletRequest: HttpServletRequest
): ResponseEntity<*>? {
return service.deleteData(request, httpServletRequest)
} }
} }
...@@ -72,5 +72,25 @@ class RequestHelpers { ...@@ -72,5 +72,25 @@ class RequestHelpers {
null null
} }
} }
fun applicationTagCountByParam(bearer: String?, param: String?, paramId: String?): ReturnData? {
return try {
configHttpClient()
val objectMapper = ObjectMapper()
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
val reqUrl =
"$APPLICATION_URL/application-tag/count-by-param?param=$param&paramId=$paramId"
val response = Unirest.get(reqUrl)
.header("Content-Type", "application/json")
.header(HEADER_STRING, bearer)
.asString()
objectMapper.readValue(response.body, ReturnData::class.java)
} catch (e: Exception) {
e.printStackTrace()
null
}
}
} }
} }
...@@ -21,4 +21,7 @@ interface RegencyRepository : JpaRepository<Regency, String> { ...@@ -21,4 +21,7 @@ interface RegencyRepository : JpaRepository<Regency, String> {
@Query("FROM Regency WHERE LOWER(name) = LOWER(:name) AND provinceId = :provinceId AND active = true") @Query("FROM Regency WHERE LOWER(name) = LOWER(:name) AND provinceId = :provinceId AND active = true")
fun findByNameProvinceId(name: String?, provinceId: String?): Optional<Regency> fun findByNameProvinceId(name: String?, provinceId: String?): Optional<Regency>
@Query("SELECT COUNT(u) FROM Regency u WHERE u.active = true AND u.provinceId = :provinceId")
fun countByProvinceId(provinceId: String?): Long?
} }
...@@ -4,6 +4,7 @@ import id.co.sangkuriang.inovasi.daerah.master.model.request.DeleteDataRequest ...@@ -4,6 +4,7 @@ import id.co.sangkuriang.inovasi.daerah.master.model.request.DeleteDataRequest
import id.co.sangkuriang.inovasi.daerah.master.model.request.PaginationRequest import id.co.sangkuriang.inovasi.daerah.master.model.request.PaginationRequest
import id.co.sangkuriang.inovasi.daerah.master.pojo.Tag import id.co.sangkuriang.inovasi.daerah.master.pojo.Tag
import org.springframework.http.ResponseEntity import org.springframework.http.ResponseEntity
import javax.servlet.http.HttpServletRequest
interface TagService { interface TagService {
...@@ -15,5 +16,5 @@ interface TagService { ...@@ -15,5 +16,5 @@ interface TagService {
fun updateData(id: String, request: Tag): ResponseEntity<*>? fun updateData(id: String, request: Tag): ResponseEntity<*>?
fun deleteData(request: DeleteDataRequest): ResponseEntity<*>? fun deleteData(request: DeleteDataRequest, httpServletRequest: HttpServletRequest): ResponseEntity<*>?
} }
...@@ -12,6 +12,7 @@ import id.co.sangkuriang.inovasi.daerah.master.model.request.PaginationRequest ...@@ -12,6 +12,7 @@ import id.co.sangkuriang.inovasi.daerah.master.model.request.PaginationRequest
import id.co.sangkuriang.inovasi.daerah.master.model.response.ReturnData import id.co.sangkuriang.inovasi.daerah.master.model.response.ReturnData
import id.co.sangkuriang.inovasi.daerah.master.pojo.Province import id.co.sangkuriang.inovasi.daerah.master.pojo.Province
import id.co.sangkuriang.inovasi.daerah.master.repository.ProvinceRepository import id.co.sangkuriang.inovasi.daerah.master.repository.ProvinceRepository
import id.co.sangkuriang.inovasi.daerah.master.repository.RegencyRepository
import id.co.sangkuriang.inovasi.daerah.master.repository.native.ProvinceRepositoryNative import id.co.sangkuriang.inovasi.daerah.master.repository.native.ProvinceRepositoryNative
import id.co.sangkuriang.inovasi.daerah.master.service.ProvinceService import id.co.sangkuriang.inovasi.daerah.master.service.ProvinceService
import org.springframework.beans.factory.annotation.Autowired import org.springframework.beans.factory.annotation.Autowired
...@@ -33,6 +34,9 @@ class ProvinceServiceImpl : ProvinceService { ...@@ -33,6 +34,9 @@ class ProvinceServiceImpl : ProvinceService {
@Autowired @Autowired
private lateinit var repo: ProvinceRepository private lateinit var repo: ProvinceRepository
@Autowired
private lateinit var repoRegency: RegencyRepository
@Autowired @Autowired
private lateinit var repoNative: ProvinceRepositoryNative private lateinit var repoNative: ProvinceRepositoryNative
...@@ -140,19 +144,24 @@ class ProvinceServiceImpl : ProvinceService { ...@@ -140,19 +144,24 @@ class ProvinceServiceImpl : ProvinceService {
) as ReturnData ) as ReturnData
if (checkApplication.success == true) { if (checkApplication.success == true) {
if (checkApplication.data != null) { if (checkApplication.data != null) {
val countUserApplication: Long = (checkApplication.data as Int).toLong() val countApplication: Long = (checkApplication.data as Int).toLong()
if (countUserApplication > 0) { if (countApplication > 0) {
return responseBadRequest("$VALIDATOR_MSG_DELETE_RELATION $title ${data.get().name} mempunyai $countUserApplication data aplikasi") return responseBadRequest("$VALIDATOR_MSG_DELETE_RELATION $title ${data.get().name} mempunyai $countApplication data Aplikasi")
} }
} }
} else { } else {
return responseBadRequest("$VALIDATOR_MSG_REQUEST_FAILED Aplikasi") return responseBadRequest("$VALIDATOR_MSG_REQUEST_FAILED Aplikasi")
} }
val countRegency = repoRegency.countByProvinceId(data.get().id) ?: 0
if (countRegency > 0) {
return responseBadRequest("$VALIDATOR_MSG_DELETE_RELATION $title ${data.get().name} mempunyai $countRegency data Kabupaten / Kota")
}
val update = data.get() val update = data.get()
update.active = false update.active = false
update.updatedAt = Date() update.updatedAt = Date()
repo.save(update) // repo.save(update)
return responseSuccess() return responseSuccess()
} }
return responseNotFound() return responseNotFound()
......
...@@ -108,9 +108,9 @@ class RegencyServiceImpl : RegencyService { ...@@ -108,9 +108,9 @@ class RegencyServiceImpl : RegencyService {
) as ReturnData ) as ReturnData
if (checkApplication.success == true) { if (checkApplication.success == true) {
if (checkApplication.data != null) { if (checkApplication.data != null) {
val countUserApplication: Long = (checkApplication.data as Int).toLong() val countApplication: Long = (checkApplication.data as Int).toLong()
if (countUserApplication > 0) { if (countApplication > 0) {
return responseBadRequest("$VALIDATOR_MSG_DELETE_RELATION $title ${data.get().name} mempunyai $countUserApplication data aplikasi") return responseBadRequest("$VALIDATOR_MSG_DELETE_RELATION $title ${data.get().name} mempunyai $countApplication data Aplikasi")
} }
} }
} else { } else {
......
package id.co.sangkuriang.inovasi.daerah.master.repository.native.impl package id.co.sangkuriang.inovasi.daerah.master.service.impl
import id.co.sangkuriang.inovasi.daerah.master.constant.HEADER_STRING
import id.co.sangkuriang.inovasi.daerah.master.constant.VALIDATOR_MSG_DELETE_RELATION
import id.co.sangkuriang.inovasi.daerah.master.constant.VALIDATOR_MSG_HAS_USED import id.co.sangkuriang.inovasi.daerah.master.constant.VALIDATOR_MSG_HAS_USED
import id.co.sangkuriang.inovasi.daerah.master.constant.VALIDATOR_MSG_REQUEST_FAILED
import id.co.sangkuriang.inovasi.daerah.master.exception.BadRequestException import id.co.sangkuriang.inovasi.daerah.master.exception.BadRequestException
import id.co.sangkuriang.inovasi.daerah.master.helpers.responseCreated import id.co.sangkuriang.inovasi.daerah.master.helpers.*
import id.co.sangkuriang.inovasi.daerah.master.helpers.responseNotFound
import id.co.sangkuriang.inovasi.daerah.master.helpers.responseSuccess
import id.co.sangkuriang.inovasi.daerah.master.model.request.DeleteDataRequest import id.co.sangkuriang.inovasi.daerah.master.model.request.DeleteDataRequest
import id.co.sangkuriang.inovasi.daerah.master.model.request.PaginationRequest import id.co.sangkuriang.inovasi.daerah.master.model.request.PaginationRequest
import id.co.sangkuriang.inovasi.daerah.master.model.response.ReturnData
import id.co.sangkuriang.inovasi.daerah.master.pojo.Tag import id.co.sangkuriang.inovasi.daerah.master.pojo.Tag
import id.co.sangkuriang.inovasi.daerah.master.repository.TagRepository import id.co.sangkuriang.inovasi.daerah.master.repository.TagRepository
import id.co.sangkuriang.inovasi.daerah.master.service.TagService import id.co.sangkuriang.inovasi.daerah.master.service.TagService
...@@ -18,11 +20,14 @@ import org.springframework.data.domain.Sort ...@@ -18,11 +20,14 @@ import org.springframework.data.domain.Sort
import org.springframework.http.ResponseEntity import org.springframework.http.ResponseEntity
import org.springframework.stereotype.Service import org.springframework.stereotype.Service
import java.util.* import java.util.*
import javax.servlet.http.HttpServletRequest
@Suppress("NAME_SHADOWING") @Suppress("NAME_SHADOWING")
@Service @Service
class TagServiceImpl : TagService { class TagServiceImpl : TagService {
private val title = "Tag"
@Autowired @Autowired
private lateinit var repo: TagRepository private lateinit var repo: TagRepository
...@@ -90,10 +95,27 @@ class TagServiceImpl : TagService { ...@@ -90,10 +95,27 @@ class TagServiceImpl : TagService {
} }
} }
override fun deleteData(request: DeleteDataRequest): ResponseEntity<*>? { override fun deleteData(request: DeleteDataRequest, httpServletRequest: HttpServletRequest): ResponseEntity<*>? {
try { try {
val data = repo.findByIdAndActive(request.id) val data = repo.findByIdAndActive(request.id)
if (data.isPresent) { if (data.isPresent) {
val checkApplicationTag: ReturnData =
RequestHelpers.applicationTagCountByParam(
httpServletRequest.getHeader(HEADER_STRING),
"tagId",
data.get().id
) as ReturnData
if (checkApplicationTag.success == true) {
if (checkApplicationTag.data != null) {
val countUserApplicationTag: Long = (checkApplicationTag.data as Int).toLong()
if (countUserApplicationTag > 0) {
return responseBadRequest("$VALIDATOR_MSG_DELETE_RELATION $title ${data.get().name} mempunyai $countUserApplicationTag data Tag Aplikasi")
}
}
} else {
return responseBadRequest("$VALIDATOR_MSG_REQUEST_FAILED Tag Aplikasi")
}
val update = data.get() val update = data.get()
update.active = false update.active = false
update.updatedAt = Date() update.updatedAt = Date()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment