Last active
July 28, 2022 11:11
-
-
Save 85degree/bc67a8209bbfee35a4232c18e09c55c1 to your computer and use it in GitHub Desktop.
Latihan Rest API sederhana untuk teman2 UPJ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package id.ac.upj.entity; | |
import javax.persistence.Entity; | |
import javax.persistence.Table; | |
import io.quarkus.hibernate.orm.panache.PanacheEntity; | |
@Entity | |
@Table(name = "orang") | |
public class OrangEntity extends PanacheEntity { | |
public String nama; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package id.ac.upj.resource; | |
import java.util.List; | |
import javax.transaction.Transactional; | |
import javax.ws.rs.DELETE; | |
import javax.ws.rs.GET; | |
import javax.ws.rs.POST; | |
import javax.ws.rs.Path; | |
import javax.ws.rs.core.Response; | |
import org.jboss.resteasy.reactive.RestPath; | |
import id.ac.upj.entity.OrangEntity; | |
@Path("/orang") | |
public class OrangResource { | |
@GET | |
public List<OrangEntity> getOrangAll() { | |
return OrangEntity.listAll(); | |
} | |
@POST | |
@Transactional // semua selain select | |
public OrangEntity bikinDataOrangBaru(OrangEntity dataYangDikirimOlehAPI) { | |
// perhatikan dataYangDikirimOlehAPI | |
// method ini akan mengambil data dengan tipe OrangEntity | |
// sekarang kita akan coba manipulasi datanya | |
// simpan ke dalam entity: OrangEntity | |
OrangEntity.persist(dataYangDikirimOlehAPI); | |
// sekarang kita kembalikan lagi ke return | |
return dataYangDikirimOlehAPI; | |
} | |
@DELETE | |
@Path("{id}") | |
public Response hapusOrang(@RestPath long id) { | |
OrangEntity.deleteById(id); | |
return Response.ok().build(); // <-- ini returnnya berupa raw response HTTP | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment