Files
sh0rt.kr-pdns/src/main/kotlin/space/mori/dnsapi/service/DomainService.kt
T
dalbodeule 47461c505f feat: Add script to refresh README from OpenAPI document
refactor: Improve error handling in PowerDNSAPIClient and streamline response processing

fix: Update RecordController to directly return records without unnecessary mapping

refactor: Update DomainRepository and RecordRepository to remove nullable types for IDs

fix: Change User domains fetch strategy to lazy loading for performance improvement

fix: Enhance user authentication handling in UserFilter to ensure authenticated user presence

refactor: Improve DomainService to enforce ownership checks before domain deletion

refactor: Update RecordService to streamline record retrieval and ensure proper domain association

test: Add OpenAPI generation test to validate API documentation endpoint

test: Implement PowerDNSAPIClient tests to verify HTTP method usage and error handling

test: Create DomainService tests to validate domain ownership and deletion logic

test: Add application-test.properties for in-memory database configuration during tests
2026-10-29 23:11:21 +09:00

78 lines
2.3 KiB
Kotlin

package space.mori.dnsapi.service
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import space.mori.dnsapi.PowerDNSAPIClient
import space.mori.dnsapi.db.Domain
import space.mori.dnsapi.db.DomainRepository
import space.mori.dnsapi.dto.DomainRequestDTO
import space.mori.dnsapi.filter.getCurrentUser
@Service
class DomainService(
@Autowired
private val domainRepository: DomainRepository,
@Autowired
private val powerDNSApiClient: PowerDNSAPIClient
) {
fun getAllDomains(): List<Domain> {
try {
val user = getCurrentUser()
val domain = domainRepository.findAllByUser(user)
return domain
} catch(ex: Error) {
throw ex
}
}
fun getDomainById(domain_id: String): Domain {
try {
val domain = domainRepository.findByCfid(domain_id).orElseThrow {
RuntimeException("Failed to find domain in API: $domain_id")
}
val user = getCurrentUser()
if (domain.user.id != user.id)
throw RuntimeException("Unauthorized to create record in API: $domain_id")
return domain
} catch(ex: Error) {
throw ex
}
}
@Throws(Exception::class)
fun createDomain(domain: DomainRequestDTO): Domain {
try {
val user = getCurrentUser()
powerDNSApiClient.createZone(domain.name)
val saved_domain = domainRepository.save(Domain(name = domain.name, user = user))
return saved_domain
} catch(ex: Error) {
throw ex
}
}
fun deleteDomain(domain_id: String): String {
try {
val domain = domainRepository.findByCfid(domain_id).orElseThrow {
throw RuntimeException("Domain with CFID $domain_id not found")
}
val user = getCurrentUser()
if (domain.user.id != user.id)
throw RuntimeException("Unauthorized to delete domain in API: $domain_id")
powerDNSApiClient.deleteZone(domain.name)
val count = domainRepository.deleteByCfid(domain_id)
if (count == 0) throw RuntimeException("Domain with CFID $domain_id not found")
return domain_id
} catch(ex: Error) {
throw ex
}
}
}