package com.example.elasticsearch.elasticsearchdemo.controller | package com.example.elasticsearch.elasticsearchdemo.controller | ||||
import com.example.elasticsearch.elasticsearchdemo.service.BankService | |||||
import com.example.elasticsearch.elasticsearchdemo.model.Matching | |||||
import com.example.elasticsearch.elasticsearchdemo.service.MatchingService | |||||
import org.springframework.beans.factory.annotation.Autowired | import org.springframework.beans.factory.annotation.Autowired | ||||
import org.springframework.web.bind.annotation.* | import org.springframework.web.bind.annotation.* | ||||
class APIController { | class APIController { | ||||
@Autowired | @Autowired | ||||
lateinit var bankService:BankService | |||||
lateinit var matchingService: MatchingService | |||||
@GetMapping("hello") | @GetMapping("hello") | ||||
fun hello(): String{ | fun hello(): String{ | ||||
""" | """ | ||||
} | } | ||||
@GetMapping("/search/{id}") | |||||
fun search(@PathVariable("id") id:String): Matching { | |||||
lateinit var matching: Matching | |||||
matching = matchingService.getMatching(id) | |||||
return matching | |||||
} | |||||
@GetMapping("/insert/{name}") | |||||
fun save(@PathVariable("name") name:String):String { | |||||
matchingService.save(name) | |||||
return "ねねっち" | |||||
} | |||||
} | } |
package com.example.elasticsearch.elasticsearchdemo.model | |||||
import org.springframework.data.annotation.Id | |||||
import org.springframework.data.elasticsearch.annotations.Document | |||||
@Document(indexName = "bank",type = "_doc") | |||||
data class Bank ( | |||||
@Id | |||||
val accountNumber: Long | |||||
,val balance: Long | |||||
,val firstName: String | |||||
,val lastName: String | |||||
,val age: Int | |||||
,val gender: String | |||||
,val address: String | |||||
,val employer: String | |||||
,val email: String | |||||
,val city: String | |||||
,val state: String | |||||
) |
package com.example.elasticsearch.elasticsearchdemo.model | |||||
import org.springframework.data.annotation.Id | |||||
import org.springframework.data.elasticsearch.annotations.Document | |||||
@Document(indexName = "matching") | |||||
data class Matching( | |||||
@Id | |||||
val id: String ? = null , | |||||
val name: String | |||||
) |
package com.example.elasticsearch.elasticsearchdemo.repository | |||||
import com.example.elasticsearch.elasticsearchdemo.model.Bank | |||||
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository | |||||
import org.springframework.stereotype.Repository | |||||
@Repository | |||||
interface BankRepository : ElasticsearchRepository<Bank,String> { | |||||
fun findByName(name: String): List<Bank> | |||||
} |
package com.example.elasticsearch.elasticsearchdemo.repository | |||||
import com.example.elasticsearch.elasticsearchdemo.model.Matching | |||||
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository | |||||
interface matchingRepository: ElasticsearchRepository<Matching, String> { | |||||
fun findByName(name:String): List<Matching> | |||||
} |
package com.example.elasticsearch.elasticsearchdemo.service | |||||
import com.example.elasticsearch.elasticsearchdemo.model.Bank | |||||
interface BankService { | |||||
fun save(bank:Bank): Bank | |||||
fun delete(bank:Bank) | |||||
fun findByName(name:String):List<Bank> | |||||
} |
package com.example.elasticsearch.elasticsearchdemo.service | |||||
import com.example.elasticsearch.elasticsearchdemo.model.Bank | |||||
import com.example.elasticsearch.elasticsearchdemo.repository.BankRepository | |||||
import org.springframework.beans.factory.annotation.Autowired | |||||
import org.springframework.stereotype.Service | |||||
@Service | |||||
class BankServiceImpl : BankService{ | |||||
lateinit var repository: BankRepository | |||||
@Autowired | |||||
fun setBankRepository(repository: BankRepository){ | |||||
this.repository = repository | |||||
} | |||||
override fun save(bank: Bank): Bank { | |||||
return repository.save(bank) | |||||
} | |||||
override fun delete(bank: Bank) { | |||||
return repository.delete(bank) | |||||
} | |||||
override fun findByName(name: String): List<Bank> { | |||||
return repository.findByName(name) | |||||
} | |||||
} |
package com.example.elasticsearch.elasticsearchdemo.service | |||||
import com.example.elasticsearch.elasticsearchdemo.model.Matching | |||||
import com.example.elasticsearch.elasticsearchdemo.repository.matchingRepository | |||||
import org.springframework.beans.factory.annotation.Autowired | |||||
import org.springframework.stereotype.Service | |||||
@Service | |||||
class MatchingService { | |||||
@Autowired | |||||
lateinit var matchingRepository: matchingRepository | |||||
fun save(name: String) { | |||||
val matching = Matching(null ,name) | |||||
matchingRepository.save(matching) | |||||
} | |||||
fun getMatching(id: String): Matching { | |||||
val matching = matchingRepository.findById(id) | |||||
return matching.get() | |||||
} | |||||
} |