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
| func cretedStudent(c *gin.Context) { | |
| var std transformedStudent | |
| var model student | |
| c.Bind(&std) | |
| validasi := validatorCreated(std) | |
| model = transferVoToModel(std) | |
| if validasi != "" { | |
| c.JSON(http.StatusOK, gin.H{"message": http.StatusOK, "result": validasi}) | |
| } else { | |
| db.Create(&model) |
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
| func fetchAllStudent(c *gin.Context) { | |
| var model [] student | |
| var vo [] transformedStudent | |
| db.Find(&model) | |
| if len(model) <= 0 { | |
| c.JSON(http.StatusNotFound, gin.H{"message": http.StatusNotFound, "result": "Data Tidak Ada"}) | |
| } |
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
| func fetchSingleStuden(c *gin.Context) { | |
| var model student | |
| var vo transformedStudent | |
| modelID := c.Param("id") | |
| db.Find(&model, modelID) | |
| if model.ID == 0 { | |
| c.JSON(http.StatusNotFound, gin.H{"message": http.StatusNotFound, "result": "Data Tidak Ada"}) | |
| } |
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
| func updateStudent(c *gin.Context) { | |
| var model student | |
| var vo transformedStudent | |
| modelID := c.Param("id") | |
| db.First(&model, modelID) | |
| if model.ID == 0 { | |
| c.JSON(http.StatusNotFound, gin.H{"message": http.StatusNotFound, "result": "Data Tidak Ada"}) | |
| } | |
| c.Bind(&vo) |
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
| func deleteStudent(c *gin.Context) { | |
| var model student | |
| modelID := c.Param("id") | |
| db.First(&model, modelID) | |
| if model.ID == 0 { | |
| c.JSON(http.StatusNotFound, gin.H{"message": http.StatusNotFound, "result": "Datat Tidak di Temukan"}) | |
| } | |
| db.Delete(model) | |
| c.JSON(http.StatusOK, gin.H{"message": http.StatusOK, "result": "Data Telah berhasil di hapus"}) |
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
| func main() { | |
| router := gin.Default() | |
| v1 := router.Group("/api/student") | |
| { | |
| v1.POST("", cretedStudent) | |
| v1.GET("", fetchAllStudent) | |
| v1.GET("/:id", fetchSingleStuden) | |
| v1.PUT("/:id", updateStudent) | |
| v1.DELETE("/:id", deleteStudent) |
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 com.codecika.kotlindemo.model.domain | |
| import org.hibernate.annotations.DynamicUpdate | |
| import javax.persistence.* | |
| import javax.validation.constraints.NotBlank | |
| @Entity | |
| @Table(name = "STUDENT") | |
| @DynamicUpdate | |
| class Student { |
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 com.codecika.kotlindemo.model.repository | |
| import com.codecika.kotlindemo.model.domain.Student | |
| import org.springframework.data.jpa.repository.JpaRepository | |
| import org.springframework.stereotype.Repository | |
| @Repository | |
| interface StudentRepository : JpaRepository<Student,Long> { | |
| } |
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 com.codecika.kotlindemo.vo | |
| class StudentVO( | |
| var name: String = "", | |
| var address: String = "", | |
| val birthday: String = "" | |
| ) |
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 com.codecika.kotlindemo.converter | |
| import com.codecika.kotlindemo.model.domain.Student | |
| import com.codecika.kotlindemo.vo.StudentVO | |
| fun Student.toVO(): StudentVO { | |
| return StudentVO( | |
| name = name, | |
| address = address, | |
| birthday = birthday |