소스 검색

:construction: create ,get one , get many

visuddhinanda 4 년 전
부모
커밋
e68b68b833
2개의 변경된 파일134개의 추가작업 그리고 1개의 파일을 삭제
  1. 15 1
      api/main.go
  2. 119 0
      api/mint/course.go

+ 15 - 1
api/main.go

@@ -9,7 +9,7 @@ import (
 
 func main() {
 
-	opt, err := pg.ParseURL("postgres://postgres:@127.0.0.1:5433/mint?sslmode=disable")
+	opt, err := pg.ParseURL("postgres://postgres:@127.0.0.1:5432/mint?sslmode=disable")
 	if err != nil {
 		panic(err)
 	}
@@ -27,5 +27,19 @@ func main() {
 	rt.PUT("/demo/user",mint.PutDemo(db))
 	rt.PATCH("/demo/user/:id",mint.PatchDemo(db))
 	rt.DELETE("/demo/user/:id",mint.DeleteDemo(db))
+
+
+	rt.GET("/api/course/:cid",mint.GetCourse(db))
+	rt.GET("/api/course/title/:ctitle",mint.GetCourseByTitle(db))
+	rt.PUT("/api/course",mint.PutCourse(db)) 
+	//rt.POST /api/course/:cid/?data=data
+	//rt.DELETE /api/course/:cid
+/*
+	rt.GET /api/lesson/:lid
+	rt.GET /api/lessons/:cid
+	rt.PUT /api/lesson/?data=data
+	rt.POST /api/lesson/:lid/?data=data
+	rt.DELETE /api/lesson/:lid
+*/
 	rt.Run()
 }

+ 119 - 0
api/mint/course.go

@@ -0,0 +1,119 @@
+package mint
+
+import (
+	"net/http"
+	"strconv"
+	"fmt"
+	"github.com/gin-gonic/gin"
+	"github.com/go-pg/pg/v10"
+)
+
+type Course struct{
+    Id int64
+    Cover string
+    Title string
+    Subtitle string
+    Summary string
+    Teacher int64
+    Tag string
+    Lang string
+    Speech_lang string
+    Status int64
+    Lesson_num int
+    Creator int64
+    Create_time int64
+    Update_time int64
+    Delete_time int64
+    Content string
+}
+
+//查询
+func GetCourse(db *pg.DB) gin.HandlerFunc {
+	return func(c *gin.Context) {
+		cid,err := strconv.ParseInt(c.Param("cid"),10,64)
+		if err != nil {
+			panic(err)
+		}
+		fmt.Println("get course")
+		// TODO 在这里进行db操作
+		// Select user by primary key.
+		course := &Course{Id: cid}
+		err = db.Model(course).WherePK().Select()
+		if err != nil {
+			panic(err)
+		}
+		
+		c.JSON(http.StatusOK, gin.H{
+			"message": "course-"+course.Title,
+		})
+	}
+}
+
+//查询
+func GetCourseByTitle(db *pg.DB) gin.HandlerFunc {
+	return func(c *gin.Context) {
+		ctitle:= c.Param("ctitle")
+
+		// TODO 在这里进行db操作
+		// Select user by primary key.
+		var courses []Course
+		err := db.Model(&courses).Column("id","title","subtitle").Where("title like ?",ctitle+"%").Select()
+		if err != nil {
+			panic(err)
+		}
+		
+		c.JSON(http.StatusOK, gin.H{
+			"message": courses,
+		})
+	}
+}
+
+//增加
+func PutCourse(db *pg.DB) gin.HandlerFunc{
+	return func(c *gin.Context){
+		title := c.Query("title")
+		status1,err := strconv.ParseInt(c.Query("status"),10,64)
+		if err != nil {
+			panic(err)
+		}
+		fmt.Println("title:"+title)
+
+		newCouse := &Course{
+			Title:   title,
+			Status: status1,
+			Teacher:1,
+			Creator:1,
+			Create_time:1,
+		}
+		_, err = db.Model(newCouse).Insert()
+		if err != nil {
+			panic(err)
+		}
+		c.JSON(http.StatusOK,gin.H{
+			"message":"new-Ok- hello "+title,
+		})
+	}
+}
+
+//改
+func PostCourse(db *pg.DB) gin.HandlerFunc{
+	return func(c *gin.Context){
+		userid,err := strconv.ParseInt(c.Param("id"),10,64)
+		if err != nil {
+			panic(err)
+		}
+		email := c.Query("emails")
+		user1 := &User{
+			Id:   userid,
+			Emails: []string{email},
+		}
+		//_, err = db.Model(user1).WherePK().Update()
+		_, err = db.Model(user1).Set("emails = ?emails").Where("id = ?id").Update()
+		if err != nil {
+			panic(err)
+		}
+		c.JSON(http.StatusOK,gin.H{
+			"message":"-patch="+email,
+		})
+	}
+}