mirror of
https://github.com/dalbodeule/sh0rt.kr-pdns.git
synced 2025-12-17 05:41:59 +09:00
add domain service, auth service
This commit is contained in:
@@ -2,29 +2,56 @@ 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.db.UserRepository
|
||||
import space.mori.dnsapi.dto.DomainRequestDTO
|
||||
import space.mori.dnsapi.filter.getCurrentUser
|
||||
import java.util.*
|
||||
|
||||
|
||||
@Service
|
||||
class DomainService {
|
||||
class DomainService(
|
||||
@Autowired
|
||||
private val domainRepository: DomainRepository? = null
|
||||
private val userRepository: UserRepository,
|
||||
@Autowired
|
||||
private val domainRepository: DomainRepository,
|
||||
@Autowired
|
||||
private val powerDNSApiClient: PowerDNSApiClient
|
||||
) {
|
||||
fun getAllDomains(): List<Domain> {
|
||||
val user = getCurrentUser()
|
||||
val domain = domainRepository.findAllByUser(user)
|
||||
if(domain.isEmpty()) throw RuntimeException("Unauthorized")
|
||||
|
||||
fun getAllDomains(): List<Domain?> {
|
||||
return domainRepository!!.findAll()
|
||||
return domain
|
||||
}
|
||||
|
||||
fun getDomainById(cfid: String): Optional<Domain> {
|
||||
return domainRepository!!.findByCfid(cfid)
|
||||
fun getDomainById(domain_id: String): Domain {
|
||||
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
|
||||
}
|
||||
|
||||
fun createDomain(domain: Domain): Domain {
|
||||
return domainRepository!!.save<Domain>(domain)
|
||||
fun createDomain(domain: DomainRequestDTO): Domain {
|
||||
val user = getCurrentUser()
|
||||
|
||||
powerDNSApiClient.createDomain(domain.name)
|
||||
val saved_domain = domainRepository.save(Domain(name=domain.name, user=user))
|
||||
|
||||
return saved_domain
|
||||
}
|
||||
|
||||
fun deleteDomain(cfid: String) {
|
||||
domainRepository!!.deleteByCfid(cfid)
|
||||
fun deleteDomain(domain_id: String): String {
|
||||
val count = domainRepository.deleteByCfid(domain_id)
|
||||
|
||||
if(count > 0) throw RuntimeException("Domain with CFID $domain_id not found")
|
||||
return domain_id
|
||||
}
|
||||
}
|
||||
@@ -7,9 +7,11 @@ import space.mori.dnsapi.PowerDNSApiClient
|
||||
import space.mori.dnsapi.db.DomainRepository
|
||||
import space.mori.dnsapi.db.Record as DomainRecord
|
||||
import space.mori.dnsapi.db.RecordRepository
|
||||
import space.mori.dnsapi.db.UserRepository
|
||||
import space.mori.dnsapi.dto.DomainRequestDTO
|
||||
import space.mori.dnsapi.dto.RecordRequestDTO
|
||||
import space.mori.dnsapi.dto.RecordResponseDTO
|
||||
import space.mori.dnsapi.filter.getCurrentUser
|
||||
import space.mori.dnsapi.getISOFormat
|
||||
import java.util.*
|
||||
|
||||
@@ -21,18 +23,25 @@ class RecordService(
|
||||
@Autowired
|
||||
private val domainRepository: DomainRepository,
|
||||
@Autowired
|
||||
private val recordRepository: RecordRepository
|
||||
private val recordRepository: RecordRepository,
|
||||
@Autowired
|
||||
private val userRepository: UserRepository,
|
||||
) {
|
||||
fun createRecord(domain_id: String, recordRequest: RecordRequestDTO): RecordResponseDTO {
|
||||
val domain = domainRepository.findByCfid(domain_id)
|
||||
if(domain.isEmpty) throw RuntimeException("Failed to find domain in API: $domain_id")
|
||||
val domain = domainRepository.findByCfid(domain_id).orElseThrow {
|
||||
throw RuntimeException("Failed to find domain in API: $domain_id")
|
||||
}
|
||||
|
||||
val response = powerDNSApiClient.createRecord(domain.get().name, recordRequest)
|
||||
val user = getCurrentUser()
|
||||
if(domain.user.id != user.id)
|
||||
throw RuntimeException("Unauthorized to create record in API: $domain_id")
|
||||
|
||||
val response = powerDNSApiClient.createRecord(domain.name, recordRequest)
|
||||
if (!response.statusCode.is2xxSuccessful) {
|
||||
throw RuntimeException("Failed to create record in PowerDNS: ${response.body}")
|
||||
}
|
||||
val record = DomainRecord(
|
||||
domain = domain.get(),
|
||||
domain = domain,
|
||||
name = recordRequest.name,
|
||||
type = recordRequest.type,
|
||||
content = recordRequest.content,
|
||||
@@ -55,7 +64,7 @@ class RecordService(
|
||||
ttl = record.ttl,
|
||||
locked = false,
|
||||
zoneId = record.cfid,
|
||||
zoneName = domain.get().name,
|
||||
zoneName = domain.name,
|
||||
createdOn = record.createdOn.getISOFormat(),
|
||||
modifiedOn = record.modifiedOn.getISOFormat(),
|
||||
priority = record.prio,
|
||||
@@ -64,10 +73,15 @@ class RecordService(
|
||||
}
|
||||
|
||||
fun getRecord(domain_id: String, record_id: String): RecordResponseDTO {
|
||||
val domain = domainRepository.findByCfid(domain_id)
|
||||
if(domain.isEmpty) throw RuntimeException("Failed to find domain in API: $domain_id")
|
||||
val domain = domainRepository.findByCfid(domain_id).orElseThrow {
|
||||
RuntimeException("Failed to find domain in API: $domain_id")
|
||||
}
|
||||
|
||||
val record = domain.get().records.find { it.cfid == record_id }
|
||||
val user = getCurrentUser()
|
||||
if(domain.user.id != user.id)
|
||||
throw RuntimeException("Unauthorized to get record in API: $domain_id")
|
||||
|
||||
val record = domain.records.find { it.cfid == record_id }
|
||||
if(record == null) throw RuntimeException("Failed to find record in API: $record_id")
|
||||
|
||||
return RecordResponseDTO(
|
||||
@@ -85,7 +99,14 @@ class RecordService(
|
||||
}
|
||||
|
||||
fun getRecordsByDomain(domain_id: String): List<RecordResponseDTO>? {
|
||||
val domain = domainRepository.findByCfid(domain_id).orElseThrow { RuntimeException("Failed to find domain in API: $domain_id") }
|
||||
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?.records?.map { RecordResponseDTO(
|
||||
id = it.cfid,
|
||||
type = it.type,
|
||||
@@ -102,10 +123,15 @@ class RecordService(
|
||||
}
|
||||
|
||||
@Transactional
|
||||
fun updateRecord(domainId: String, cfid: String, updatedRecord: RecordRequestDTO): RecordResponseDTO {
|
||||
fun updateRecord(domain_id: String, cfid: String, updatedRecord: RecordRequestDTO): RecordResponseDTO {
|
||||
// 도메인 조회
|
||||
val domain = domainRepository.findByCfid(domainId)
|
||||
.orElseThrow { RuntimeException("Domain not found") }
|
||||
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")
|
||||
|
||||
// 레코드 조회
|
||||
val record = recordRepository.findByDomainIdAndCfid(domain.id!!, cfid)
|
||||
@@ -144,18 +170,18 @@ class RecordService(
|
||||
)
|
||||
}
|
||||
|
||||
fun deleteRecord(domain_id: String, record_id: String) {
|
||||
val domain = domainRepository.findByCfid(domain_id).orElseThrow { RuntimeException("Failed to find domain in API: $domain_id") }
|
||||
fun deleteRecord(domain_id: String, record_id: String): String {
|
||||
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")
|
||||
|
||||
val deletedCount = recordRepository.deleteByDomainIdAndCfid(domain.id!!, record_id)
|
||||
|
||||
if(deletedCount == 0) throw RuntimeException("Failed to find record in API: $record_id")
|
||||
}
|
||||
|
||||
fun deleteDomain(name: String) {
|
||||
val response = powerDNSApiClient.deleteDomain(name)
|
||||
if (!response.statusCode.is2xxSuccessful) {
|
||||
throw RuntimeException("Failed to delete domain in PowerDNS: ${response.body}")
|
||||
}
|
||||
else return record_id
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user