Skip to content

Instantly share code, notes, and snippets.

@mohashari
Last active June 30, 2018 02:27
Show Gist options
  • Select an option

  • Save mohashari/57f52dda8e0cf5ef2c4eccc9f2732bba to your computer and use it in GitHub Desktop.

Select an option

Save mohashari/57f52dda8e0cf5ef2c4eccc9f2732bba to your computer and use it in GitHub Desktop.
package com.codecika.kotlindemo.service
import com.codecika.kotlindemo.converter.toVO
import com.codecika.kotlindemo.model.domain.Student
import com.codecika.kotlindemo.model.repository.StudentRepository
import com.codecika.kotlindemo.vo.StudentVO
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import java.util.*
@Service
class StudentService {
@Autowired
lateinit var studentRepository: StudentRepository
fun saveData(student: Student): StudentVO {
return studentRepository.save(student).toVO()
}
fun getAll(): List<StudentVO> {
return studentRepository.findAll().map { it.toVO() }
}
fun getDetail(id: Long): Optional<StudentVO> {
return studentRepository.findById(id).map { it.toVO() }
}
fun editData(student: Student): StudentVO {
val model = studentRepository.getOne(student.id)
model.apply {
address = student.address
birthday = student.birthday
name = student.name
}
return studentRepository.saveAndFlush(model).toVO()
}
fun deleteStudent(id: Long): String {
val model: Student = studentRepository.getOne(id)
if (model == null) {
return "Data Tidak Ada";
} else {
studentRepository.delete(model)
return "Data Sudah Di hapus"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment