Skip to content

Commit

Permalink
add new endpointa for deletions, introduce service class for dockerma…
Browse files Browse the repository at this point in the history
…chine
  • Loading branch information
NiHaiden committed Oct 24, 2023
1 parent ea666ed commit 019f806
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ data class DockerMachine(
if (other == null || Hibernate.getClass(this) != Hibernate.getClass(other)) return false
other as DockerMachine

return machineID != null && machineID == other.machineID
return machineID == other.machineID
}

override fun hashCode(): Int = javaClass.hashCode()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package tech.niklas.ariesbackend.service

import tech.niklas.ariesbackend.db.DockerMachineRepository
import tech.niklas.ariesbackend.exception.MachineAlreadyExistsException
import tech.niklas.ariesbackend.model.DockerMachine
import kotlin.jvm.optionals.getOrElse

class DockerMachineService(private val dockerMachineRepository: DockerMachineRepository,
private val dockerMachineService: DockerMachineService) {


fun registerNew(dockerMachine: DockerMachine): DockerMachine {
if(dockerMachineRepository.existsByMachineName(dockerMachine.machineName) ) {
throw MachineAlreadyExistsException("The machine ${dockerMachine.machineName} already exists in the database!")
}
return dockerMachineRepository.save(dockerMachine)
}

fun getAll(): List<DockerMachine> {
return dockerMachineRepository.findAll()
}

fun getSpecificMachine(id: String): DockerMachine {
return dockerMachineRepository.findById(id).getOrElse {
throw MachineAlreadyExistsException("not found lol")
}
}

fun deleteMachine(id: String): String {
dockerMachineRepository.deleteById(id)
return "Deleted machine with id $id"
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import kotlin.jvm.optionals.getOrElse
@RestController
@RequestMapping("/machine")
class MachineController(private val dockerMachineRepository: DockerMachineRepository) {
@PostMapping("/registernew")
@PostMapping("/new")
fun registerNew(@RequestBody dockerMachine: DockerMachine): DockerMachine {
if(dockerMachineRepository.existsByMachineName(dockerMachine.machineName) ) {
throw MachineAlreadyExistsException("The machine ${dockerMachine.machineName} already exists in the database!")
Expand All @@ -29,5 +29,11 @@ class MachineController(private val dockerMachineRepository: DockerMachineReposi
}
}

@DeleteMapping("/delete/{id}")
fun deleteMachine(@PathVariable id: String): String {
dockerMachineRepository.deleteById(id)
return "Deleted machine with id $id"
}


}

0 comments on commit 019f806

Please sign in to comment.