1.1.48.1. fejezet, Kotlin

HelloResource.kt

package hu.infokristaly.homework4jee
 
import hu.infokristaly.homework4jee.entity.Person
import jakarta.enterprise.context.ApplicationScoped
import jakarta.persistence.EntityManager
import jakarta.persistence.PersistenceContext
import jakarta.transaction.Transactional
import jakarta.ws.rs.*
import jakarta.ws.rs.core.Context
import jakarta.ws.rs.core.Response
import jakarta.ws.rs.core.UriInfo
 
@Path("hello/{username}")
@ApplicationScoped
open class HelloResource {
    @Context
    private val context: UriInfo? = null
 
    @PersistenceContext
    open val em: EntityManager? = null
 
    @GET()
    @Produces("text/plain")
    open fun hello(@PathParam("username") userName: String) : String {
        val result = "Hello ${userName}, how are you?"
 
        return result
    }
 
    @POST()
    @Produces("text/plain")
    @Consumes("application/json")
    @Transactional
    open fun savePersonData(@PathParam("username") username: String, person: Person) : Response {
        em?.persist(person)
        return Response.accepted("User created").build()
    }
 
    @PUT()
    @Consumes("application/json")
    @Produces("text/plain")
    open fun updatePersonData(@PathParam("username") username: String, person: Person): Response {
        em?.merge(person)
        return Response.accepted("User modified").build()
    }
 
}

Person.kt

package hu.infokristaly.homework4jee.entity
 
import jakarta.persistence.*
 
@Entity
@Table(name = "person", schema = "public")
open class Person (
    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    open var id: Long?,
    @Basic
    @Column(name = "first_name")
    open var firstName: String?,
    @Basic
    @Column(name = "last_name")
    open var lastName: String?,
    @Basic
    @Column(name = "email")
    open var email: String?,
) {
 
    // Szükséges default konstruktor
    constructor() : this(null, null, null, null) {
 
    }
}