Explorar el Código

Merge pull request #453 from visuddhinanda/master

后端重构
visuddhinanda hace 4 años
padre
commit
25bb8b5625

+ 51 - 10
api/main.go

@@ -28,18 +28,59 @@ func main() {
 	rt.PATCH("/demo/user/:id",mint.PatchDemo(db))
 	rt.DELETE("/demo/user/:id",mint.DeleteDemo(db))
 
-
+	//课程
+	//根据id查询课程 
 	rt.GET("/api/course/:cid",mint.GetCourse(db))
+	//输入标题查询符合条件的课程 title% 
 	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.POST("/api/course",mint.PostCourse(db))//改
+	//删除
+	rt.DELETE("/api/course/:cid",mint.DeleteCourse(db)) 
+	//修改课程表里的课的数量
+	rt.PATCH("/api/course/lessonnum",mint.PatchLessonNumInCousrse(db))
+
+	//课
+	//根据id查询课程
+	rt.GET("/api/lesson/:lid",mint.GetLesson(db))
+	//输入标题查询符合条件的课程 title% 
+	rt.GET("/api/lesson/title/:ltitle",mint.GetLessonByTitle(db))
+	//新建课
+	rt.PUT("/api/lesson",mint.PutLesson(db)) 
+	//修改
+	rt.POST("/api/lesson",mint.PostLesson(db))//改
+	//删除
+	rt.DELETE("/api/lesson/:lid",mint.DeleteLesson(db)) 
+
+	//文章
+	//根据id查询
+	rt.GET("/api/article/:aid",mint.GetArticle(db))
+	//输入标题查询符合条件的 title% 
+	rt.GET("/api/article/title/:title",mint.GetArticleByTitle(db))
+	//新建课
+	rt.PUT("/api/article",mint.PutArticle(db))
+	//修改
+	rt.POST("/api/article",mint.PostAritcle(db))//改
+	//删除
+	rt.DELETE("/api/article/:aid",mint.DeleteArticle(db))
+
+	//文集
+	//根据id查询
+	rt.GET("/api/collection/:cid",mint.GetCollection(db))
+	//输入标题查询符合条件的 title% 
+	rt.GET("/api/collection/title/:title",mint.GetCollectionByTitle(db))
+	//新建课
+	rt.PUT("/api/collection",mint.PutCollection(db))
+	//修改
+	rt.POST("/api/collection",mint.PostCollection(db))//改
+	//删除
+	rt.DELETE("/api/collection/:cid",mint.DeleteCollection(db))
+
+	rt.GET("/api/article_list/:cid",mint.GetCollectionArticleList(db))//改
+	//修改
+	rt.POST("/api/article_list/article/:aid",mint.PostArticleListByArticle(db))//改
+
 	rt.Run()
 }

+ 153 - 0
api/mint/article.go

@@ -0,0 +1,153 @@
+package mint
+
+import (
+	"net/http"
+	"strconv"
+	"fmt"
+	"github.com/gin-gonic/gin"
+	"github.com/go-pg/pg/v10"
+	"time"
+)
+/*
+    id SERIAL PRIMARY KEY,
+    uuid         VARCHAR (36) ,
+    title        VARCHAR (32) NOT NULL,
+    subtitle     VARCHAR (32),
+    summary      VARCHAR (255),
+    content      TEXT,
+    owner_id     INTEGER  NOT NULL,
+    owner        VARCHAR (36),
+    setting      JSON,
+    status       INTEGER   NOT NULL DEFAULT (10),
+	version     INTEGER NOT NULL DEFAULT (1),
+    deleted_at  TIMESTAMP,
+    created_at  TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
+    updated_at  TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
+
+*/
+type Article struct {
+	Id     int `form:"id" json:"id" binding:"required"`
+	Title string `form:"title" json:"title" binding:"required"`
+	Subtitle string `form:"subtitle" json:"subtitle" binding:"required"`
+	Summary string `form:"summary" json:"summary" binding:"required"`
+	Content string `form:"content" json:"content" binding:"required"`
+	OwnerId int
+	Setting string `form:"setting" json:"setting" binding:"required"`
+	Status int `form:"status" json:"status" binding:"required"`
+	Version int
+    DeletedAt time.Time
+    CreatedAt time.Time
+    UpdatedAt time.Time
+}
+//查询
+func GetArticle(db *pg.DB) gin.HandlerFunc {
+	return func(c *gin.Context) {
+		lid,err := strconv.ParseInt(c.Param("aid"),10,64)
+		if err != nil {
+			panic(err)
+		}
+		fmt.Println("get lesson")
+		// TODO 在这里进行db操作
+		// Select user by primary key.
+		article := &Article{Id: int(lid)}
+		err = db.Model(article).WherePK().Select()
+		if err != nil {
+			panic(err)
+		}
+		
+		c.JSON(http.StatusOK, gin.H{
+			"message": "article-"+article.Title,
+		})
+	}
+}
+
+//查询
+func GetArticleByTitle(db *pg.DB) gin.HandlerFunc {
+	return func(c *gin.Context) {
+		title:= c.Param("ltitle")
+
+		// TODO 在这里进行db操作
+		// Select user by primary key.
+		var articles []Article
+		err := db.Model(&articles).Column("id","title","subtitle").Where("title like ?",title+"%").Select()
+		if err != nil {
+			panic(err)
+		}
+		
+		c.JSON(http.StatusOK, gin.H{
+			"message": articles,
+		})
+	}
+}
+
+//新建-
+//PUT http://127.0.0.1:8080/api/lesson?title=lesson-one&status=10
+func PutArticle(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)
+		}
+
+		newArticle := &Article{
+			Title:   title,
+			Status: int(status1),
+			OwnerId:1,
+		}
+		_, err = db.Model(newArticle).Insert()
+		if err != nil {
+			panic(err)
+		}
+
+		//修改完毕
+		c.JSON(http.StatusOK,gin.H{
+			"message":"",
+		})
+	}
+}
+
+
+//修改
+func PostAritcle(db *pg.DB) gin.HandlerFunc{
+	return func(c *gin.Context){
+		var form Article
+
+		if err := c.ShouldBindJSON(&form); err != nil {
+			c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
+			return
+		}
+
+		_,err := db.Model(&form).Column("title","subtitle","summary","status","content").WherePK().Update()
+		if err != nil {
+			panic(err)
+		}
+		c.JSON(http.StatusOK,gin.H{
+			"message":"update ok",
+		})
+	}
+}
+
+
+//删
+func DeleteArticle(db *pg.DB) gin.HandlerFunc{
+	return func(c *gin.Context){
+		id,err := strconv.ParseInt(c.Param("aid"),10,64)
+		if err != nil {
+			panic(err)
+		}
+		article := &Article{
+			Id:int(id),
+		}
+		//删之前获取 course_id
+		_, err = db.Model(article).WherePK().Delete()
+		if err != nil {
+			panic(err)
+		}
+		
+		c.JSON(http.StatusOK,gin.H{
+			"message":"delete "+c.Param("lid"),
+		})
+	}
+}

+ 94 - 0
api/mint/article_list.go

@@ -0,0 +1,94 @@
+package mint
+
+import (
+	"net/http"
+	"strconv"
+	"github.com/gin-gonic/gin"
+	"github.com/go-pg/pg/v10"
+	"time"
+	"encoding/json"
+
+)
+type ArticleList struct {
+	Id     int `form:"id" json:"id"`
+	CollectionId int `form:"collection_id" json:"collection_id" binding:"required"`
+	ArticleId int `form:"article_id" json:"article_id" binding:"required"`
+    CreatedAt time.Time
+}
+type ArticleListHolder struct{
+	Items []ArticleList
+}
+func (i *ArticleListHolder) UnmarshalJSON(b []byte) error{
+	return json.Unmarshal(b, &i.Items)
+}
+//查询
+func GetCollectionArticleList(db *pg.DB) gin.HandlerFunc {
+	return func(c *gin.Context) {
+		cid,err:= strconv.Atoi(c.Param("cid"))
+		if err != nil {
+			panic(err)
+		}
+
+		// TODO 在这里进行db操作
+		// Select user by primary key.
+		var articles []ArticleList
+		err = db.Model(&articles).Column("collection_id","article_id").Where("collection_id = ?",cid).Select()
+		if err != nil {
+			panic(err)
+		}
+		
+		c.JSON(http.StatusOK, gin.H{
+			"message": articles,
+		})
+	}
+}
+
+
+
+//修改
+func PostArticleListByArticle(db *pg.DB) gin.HandlerFunc{
+	return func(c *gin.Context){
+		aid,err:= strconv.Atoi(c.Param("aid"))
+		if err != nil {
+			panic(err)
+		}
+		//先删除
+		_, err = db.Model((*ArticleList)(nil)).Where("article_id = ?",aid).Delete()
+		if err != nil {
+			panic(err)
+		}
+
+		var form ArticleListHolder
+		if err := c.ShouldBindJSON(&form); err != nil {
+			c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
+			return
+		}
+
+		tx, err := db.Begin()
+		if err != nil {
+			panic(err)
+		}
+		defer tx.Rollback()
+		stmt, err := tx.Prepare("INSERT INTO article_lists( collection_id, article_id ) VALUES( $1, $2 )")
+		if err != nil {
+			panic(err)
+		}
+		defer stmt.Close()
+		for _, value := range form.Items{
+			_, err = stmt.Exec(value.CollectionId,aid)
+			if err != nil {
+				panic(err)
+			}
+	
+		}
+		err = tx.Commit()
+		if err != nil {
+			panic(err)
+		}
+		c.JSON(http.StatusOK,gin.H{
+			"message":"update ok",
+		})
+	}
+}
+
+

+ 153 - 0
api/mint/collection.go

@@ -0,0 +1,153 @@
+package mint
+
+import (
+	"net/http"
+	"strconv"
+	"fmt"
+	"github.com/gin-gonic/gin"
+	"github.com/go-pg/pg/v10"
+	"time"
+)
+/*
+    id SERIAL PRIMARY KEY,
+    uuid         VARCHAR (36) ,
+    title        VARCHAR (32) NOT NULL,
+    subtitle     VARCHAR (32),
+    summary      VARCHAR (255),
+    article_list TEXT,
+    status       INTEGER   NOT NULL DEFAULT (10),
+    creator_id   INTEGER,
+    owner        VARCHAR (36),
+    lang         CHAR (8),
+	version     INTEGER NOT NULL DEFAULT (1),
+    deleted_at  TIMESTAMP,
+    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
+    updated_at  TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
+
+*/
+type Collection struct {
+	Id     int `form:"id" json:"id" binding:"required"`
+	Title string `form:"title" json:"title" binding:"required"`
+	Subtitle string `form:"subtitle" json:"subtitle" binding:"required"`
+	Summary string `form:"summary" json:"summary" binding:"required"`
+	ArticleList string `form:"article_list" json:"article_list" binding:"required"`
+	CreatorId int
+	Lang string `form:"lang" json:"lang" binding:"required"`
+	Status int `form:"status" json:"status" binding:"required"`
+	Version int
+    DeletedAt time.Time
+    CreatedAt time.Time
+    UpdatedAt time.Time
+}
+//查询
+func GetCollection(db *pg.DB) gin.HandlerFunc {
+	return func(c *gin.Context) {
+		lid,err := strconv.Atoi(c.Param("cid"))
+		if err != nil {
+			panic(err)
+		}
+		fmt.Println("get lesson")
+		// TODO 在这里进行db操作
+		// Select user by primary key.
+		collection := &Collection{Id: int(lid)}
+		err = db.Model(collection).Column("title","subtitle","summary","status").WherePK().Select()
+		if err != nil {
+			panic(err)
+		}
+		
+		c.JSON(http.StatusOK, gin.H{
+			"message": collection,
+		})
+	}
+}
+
+//查询
+func GetCollectionByTitle(db *pg.DB) gin.HandlerFunc {
+	return func(c *gin.Context) {
+		title:= c.Param("ctitle")
+
+		// TODO 在这里进行db操作
+		// Select user by primary key.
+		var collections []Collection
+		err := db.Model(&collections).Column("id","title","subtitle").Where("title like ?",title+"%").Select()
+		if err != nil {
+			panic(err)
+		}
+		
+		c.JSON(http.StatusOK, gin.H{
+			"message": collections,
+		})
+	}
+}
+
+//新建-
+//PUT http://127.0.0.1:8080/api/lesson?title=lesson-one&course_id=1&status=10
+func PutCollection(db *pg.DB) gin.HandlerFunc{
+	return func(c *gin.Context){
+	
+		title := c.Query("title")
+		status1,err := strconv.Atoi(c.Query("status"))
+		if err != nil {
+			panic(err)
+		}
+
+		newOne := &Collection{
+			Title:   title,
+			Status: int(status1),
+			CreatorId:1,
+		}
+		_, err = db.Model(newOne).Insert()
+		if err != nil {
+			panic(err)
+		}
+
+		//修改完毕
+		c.JSON(http.StatusOK,gin.H{
+			"message":"insert ok",
+		})
+	}
+}
+
+
+//改
+func PostCollection(db *pg.DB) gin.HandlerFunc{
+	return func(c *gin.Context){
+		var form Collection
+
+		if err := c.ShouldBindJSON(&form); err != nil {
+			c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
+			return
+		}
+
+		_,err := db.Model(&form).Column("title","subtitle","summary","status","article_list").WherePK().Update()
+		if err != nil {
+			panic(err)
+		}
+		c.JSON(http.StatusOK,gin.H{
+			"message":"update ok",
+		})
+	}
+}
+
+
+//删
+func DeleteCollection(db *pg.DB) gin.HandlerFunc{
+	return func(c *gin.Context){
+		id,err := strconv.Atoi(c.Param("cid"))
+		if err != nil {
+			panic(err)
+		}
+		collection := &Collection{
+			Id:int(id),
+		}
+		//删之前获取 course_id
+		_, err = db.Model(collection).WherePK().Delete()
+		if err != nil {
+			panic(err)
+		}
+		
+		c.JSON(http.StatusOK,gin.H{
+			"message":"deleted "+c.Param("cid"),
+		})
+	}
+}

+ 66 - 30
api/mint/course.go

@@ -6,31 +6,31 @@ import (
 	"fmt"
 	"github.com/gin-gonic/gin"
 	"github.com/go-pg/pg/v10"
+	"time"
 )
 
-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
-}
 
+type Course struct {
+	Id     int `form:"id" json:"id" binding:"required"`
+	Cover string
+	Title string `form:"title" json:"title"`
+	Subtitle string `form:"subtitle" json:"subtitle"`
+	Summary string `form:"summary" json:"summary"`
+	Teacher int `form:"teacher" json:"teacher"`
+	Lang string `form:"lang" json:"lang"`
+	Speech_lang string `form:"speech_lang" json:"speech_lang"`
+	Status int `form:"status" json:"status"`
+	Content string `form:"content" json:"content"`
+	Creator int
+    LessonNum int `form:"lesson_num" json:"lesson_num"`
+    Version int
+    CreatedAt time.Time
+    UpdatedAt time.Time
+}
 //查询
 func GetCourse(db *pg.DB) gin.HandlerFunc {
 	return func(c *gin.Context) {
-		cid,err := strconv.ParseInt(c.Param("cid"),10,64)
+		cid,err := strconv.Atoi(c.Param("cid"))
 		if err != nil {
 			panic(err)
 		}
@@ -68,7 +68,7 @@ func GetCourseByTitle(db *pg.DB) gin.HandlerFunc {
 	}
 }
 
-//增加
+//新建
 func PutCourse(db *pg.DB) gin.HandlerFunc{
 	return func(c *gin.Context){
 		title := c.Query("title")
@@ -80,10 +80,9 @@ func PutCourse(db *pg.DB) gin.HandlerFunc{
 
 		newCouse := &Course{
 			Title:   title,
-			Status: status1,
+			Status: int(status1),
 			Teacher:1,
 			Creator:1,
-			Create_time:1,
 		}
 		_, err = db.Model(newCouse).Insert()
 		if err != nil {
@@ -98,22 +97,59 @@ func PutCourse(db *pg.DB) gin.HandlerFunc{
 //改
 func PostCourse(db *pg.DB) gin.HandlerFunc{
 	return func(c *gin.Context){
-		userid,err := strconv.ParseInt(c.Param("id"),10,64)
+		var form Course
+
+		if err := c.ShouldBindJSON(&form); err != nil {
+			c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
+			return
+		}
+
+		_,err := db.Model(&form).Table("courses").Column("title","subtitle","summary","teacher","lang","speech_lang","status","content").WherePK().Update()
+		if err != nil {
+			panic(err)
+		}
+		c.JSON(http.StatusOK,gin.H{
+			"message":"update ok",
+		})
+	}
+}
+
+
+//补
+func PatchLessonNumInCousrse(db *pg.DB) gin.HandlerFunc{
+	return func(c *gin.Context){
+		var form Course
+
+		if err := c.ShouldBindJSON(&form); err != nil {
+			c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
+			return
+		}
+
+		_, err := db.Model(&form).Column("lesson_num").WherePK().Update()
+		if err != nil {
+			panic(err)
+		}
+		c.JSON(http.StatusOK,gin.H{
+			"message":"patch ok",
+		})
+	}
+}
+//删
+func DeleteCourse(db *pg.DB) gin.HandlerFunc{
+	return func(c *gin.Context){
+		id,err := strconv.Atoi(c.Param("cid"))
 		if err != nil {
 			panic(err)
 		}
-		email := c.Query("emails")
-		user1 := &User{
-			Id:   userid,
-			Emails: []string{email},
+		course := &Course{
+			Id:   id,
 		}
-		//_, err = db.Model(user1).WherePK().Update()
-		_, err = db.Model(user1).Set("emails = ?emails").Where("id = ?id").Update()
+		_, err = db.Model(course).WherePK().Delete()
 		if err != nil {
 			panic(err)
 		}
 		c.JSON(http.StatusOK,gin.H{
-			"message":"-patch="+email,
+			"message":"delete "+c.Param("cid"),
 		})
 	}
 }

+ 200 - 0
api/mint/lesson.go

@@ -0,0 +1,200 @@
+package mint
+
+import (
+	"net/http"
+	"io/ioutil"
+	/*"strings"*/
+	"bytes"
+	"encoding/json"
+	"strconv"
+	"fmt"
+	"github.com/gin-gonic/gin"
+	"github.com/go-pg/pg/v10"
+	"time"
+)
+
+type Lesson struct {
+	Id     int `form:"id" json:"id" binding:"required"`
+	CourseId     int `form:"course_id" json:"course_id" binding:"required"`
+	Cover string
+	Title string `form:"title" json:"title" binding:"required"`
+	Subtitle string `form:"subtitle" json:"subtitle" binding:"required"`
+	Summary string `form:"summary" json:"summary" binding:"required"`
+	Teacher int `form:"teacher" json:"teacher" binding:"required"`
+	Lang string `form:"lang" json:"lang" binding:"required"`
+	Speech_lang string `form:"speech_lang" json:"speech_lang" binding:"required"`
+	Status int `form:"status" json:"status" binding:"required"`
+	Content string `form:"content" json:"content" binding:"required"`
+	Creator int
+	StartDate time.Time `form:"date" json:"date" binding:"required"`
+    Duration int64 `form:"duration" json:"duration" binding:"required"` 
+	Version int64
+    CreatedAt time.Time
+    UpdatedAt time.Time
+}
+//查询
+func GetLesson(db *pg.DB) gin.HandlerFunc {
+	return func(c *gin.Context) {
+		lid,err := strconv.ParseInt(c.Param("lid"),10,64)
+		if err != nil {
+			panic(err)
+		}
+		fmt.Println("get lesson")
+		// TODO 在这里进行db操作
+		// Select user by primary key.
+		lesson := &Lesson{Id: int(lid)}
+		err = db.Model(lesson).WherePK().Select()
+		if err != nil {
+			panic(err)
+		}
+		
+		c.JSON(http.StatusOK, gin.H{
+			"message": "lesson-"+lesson.Title,
+		})
+	}
+}
+
+//查询
+func GetLessonByTitle(db *pg.DB) gin.HandlerFunc {
+	return func(c *gin.Context) {
+		title:= c.Param("ltitle")
+
+		// TODO 在这里进行db操作
+		// Select user by primary key.
+		var lessons []Lesson
+		err := db.Model(&lessons).Column("id","title","subtitle").Where("title like ?",title+"%").Select()
+		if err != nil {
+			panic(err)
+		}
+		
+		c.JSON(http.StatusOK, gin.H{
+			"message": lessons,
+		})
+	}
+}
+
+/*新建
+新建课以后,查询这个course 里有几个lesson 然后更新 courese 的 lesson_num
+*/
+//PUT http://127.0.0.1:8080/api/lesson?title=lesson-one&course_id=1&status=10
+func PutLesson(db *pg.DB) gin.HandlerFunc{
+	return func(c *gin.Context){
+	
+		title := c.Query("title")
+		courseId,err := strconv.Atoi(c.Query("course_id"))
+		status1,err := strconv.Atoi(c.Query("status"))
+		if err != nil {
+			panic(err)
+		}
+
+		newLesson := &Lesson{
+			Title:   title,
+			CourseId: courseId,
+			Status: status1,
+			Teacher:0,
+			Creator:1,
+		}
+		_, err = db.Model(newLesson).Insert()
+		if err != nil {
+			panic(err)
+		}
+		//修改 course 的 lesson_num
+		courseMessage := _updateLessonCount(db,courseId)
+		
+		c.JSON(http.StatusOK,gin.H{
+			"message":courseMessage,
+		})
+	}
+}
+
+func _updateLessonCount(db *pg.DB,courseId int) string{
+			//查询这个course 里面有几个课程
+			countLesson, err := db.Model((*Lesson)(nil)).Where("course_id = ?",courseId).Count()
+			if err != nil {
+				panic(err)
+			}		
+	
+			//修改course lesson number
+			url := "http://127.0.0.1:8080/api/course/lessonnum"
+			values := Course{
+				Id : courseId,
+				LessonNum : countLesson,
+			}
+			json_data, err := json.Marshal(values);
+			if err != nil {
+				panic(err)
+			}
+	
+			client := &http.Client{}
+			req, err := http.NewRequest("PATCH",url, bytes.NewBuffer(json_data))
+			if err != nil {
+				panic(err)
+			}
+			req.Header.Set("Content-Type","application/json")
+			resp,err := client.Do(req)
+			if err != nil {
+				panic(err)
+			}
+			 defer resp.Body.Close()
+			b, err := ioutil.ReadAll(resp.Body)
+			if err != nil {
+				panic(err)
+			}
+	
+		return(string(b))
+}
+
+//改
+func PostLesson(db *pg.DB) gin.HandlerFunc{
+	return func(c *gin.Context){
+		var form Lesson
+
+		if err := c.ShouldBindJSON(&form); err != nil {
+			c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
+			return
+		}
+
+		_,err := db.Model(&form).Table("lessons").Column("title","subtitle","summary","teacher","tag","lang","speech_lang","status","content","start_date","duration").WherePK().Update()
+		if err != nil {
+			panic(err)
+		}
+		c.JSON(http.StatusOK,gin.H{
+			"message":"update ok",
+		})
+	}
+}
+
+
+//删
+func DeleteLesson(db *pg.DB) gin.HandlerFunc{
+	return func(c *gin.Context){
+		id,err := strconv.ParseInt(c.Param("lid"),10,64)
+		if err != nil {
+			panic(err)
+		}
+		lesson := &Lesson{
+			Id:int(id),
+			CourseId: int(0) ,
+		}
+		//删之前获取 course_id
+		err = db.Model(lesson).Column("course_id").WherePK().Select()
+		if err != nil {
+			panic(err)
+		}
+		course_id := lesson.CourseId
+
+		_, err = db.Model(lesson).WherePK().Delete()
+		if err != nil {
+			panic(err)
+		}
+
+		//修改 course 的 lesson_num
+		courseMessage := _updateLessonCount(db,course_id)
+
+	
+		c.JSON(http.StatusOK,gin.H{
+			"message":"delete "+c.Param("lid"),
+			"lesson_num":courseMessage,
+		})
+	}
+}

+ 3 - 0
db/migrations/2021-07-09-125349_new_course/down.sql

@@ -0,0 +1,3 @@
+-- This file should undo anything in `up.sql`
+DROP TABLE courses ;
+DROP TABLE lessons ;

+ 42 - 0
db/migrations/2021-07-09-125349_new_course/up.sql

@@ -0,0 +1,42 @@
+-- Your SQL goes here
+
+CREATE TABLE courses
+( 
+    id SERIAL PRIMARY KEY, 
+    cover VARCHAR(255), 
+    title VARCHAR(32) NOT NULL, 
+    subtitle VARCHAR(32),
+    summary VARCHAR(255),
+    teacher INTEGER NOT NULL, 
+    lang VARCHAR (8), 
+    speech_lang VARCHAR (8), 
+    status INTEGER NOT NULL DEFAULT(0), 
+    lesson_num INTEGER NOT NULL DEFAULT(0), 
+    content TEXT ,  
+    creator INTEGER NOT NULL, 
+    version INTEGER,
+    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
+    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
+);
+
+CREATE TABLE lessons
+(
+    id SERIAL PRIMARY KEY, 
+    course_id INTEGER NOT NULL,
+    course_uuid VARCHAR(36),
+    title VARCHAR(32) NOT NULL, 
+    subtitle VARCHAR(32), 
+    summary VARCHAR(255), 
+    status INTEGER  NOT NULL DEFAULT(0), 
+    cover VARCHAR(255), 
+    teacher INTEGER, 
+    lang VARCHAR(8), 
+    speech_lang VARCHAR(8), 
+    start_date TIMESTAMP, 
+    duration INTEGER, 
+    content TEXT,
+    creator INTEGER  NOT NULL,    
+    version INTEGER,
+    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
+    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
+);

+ 3 - 0
db/migrations/2021-07-15-131154_create_article/down.sql

@@ -0,0 +1,3 @@
+-- This file should undo anything in `up.sql`
+
+DROP TABLE articles

+ 18 - 0
db/migrations/2021-07-15-131154_create_article/up.sql

@@ -0,0 +1,18 @@
+-- Your SQL goes here
+
+CREATE TABLE articles (
+    id SERIAL PRIMARY KEY,
+    uuid         VARCHAR (36) ,
+    title        VARCHAR (32) NOT NULL,
+    subtitle     VARCHAR (32),
+    summary      VARCHAR (255),
+    content      TEXT,
+    owner_id     INTEGER  NOT NULL,
+    owner        VARCHAR (36),
+    setting      JSON,
+    status       INTEGER   NOT NULL DEFAULT (10),
+	version     INTEGER NOT NULL DEFAULT (1),
+    deleted_at  TIMESTAMP,
+    created_at  TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
+    updated_at  TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
+);

+ 3 - 0
db/migrations/2021-07-16-010532_create_collection/down.sql

@@ -0,0 +1,3 @@
+-- This file should undo anything in `up.sql`
+
+DROP TABLE collections

+ 18 - 0
db/migrations/2021-07-16-010532_create_collection/up.sql

@@ -0,0 +1,18 @@
+-- Your SQL goes here
+
+CREATE TABLE collections (
+    id SERIAL PRIMARY KEY,
+    uuid         VARCHAR (36) ,
+    title        VARCHAR (32) NOT NULL,
+    subtitle     VARCHAR (32),
+    summary      VARCHAR (255),
+    article_list TEXT,
+    status       INTEGER   NOT NULL DEFAULT (10),
+    creator_id   INTEGER,
+    owner        VARCHAR (36),
+    lang         CHAR (8),
+	version     INTEGER NOT NULL DEFAULT (1),
+    deleted_at  TIMESTAMP,
+    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
+    updated_at  TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
+);

+ 3 - 0
db/migrations/2021-07-16-130715_collection_article_list/down.sql

@@ -0,0 +1,3 @@
+-- This file should undo anything in `up.sql`
+
+DROP TABLE collection_article_lists

+ 8 - 0
db/migrations/2021-07-16-130715_collection_article_list/up.sql

@@ -0,0 +1,8 @@
+-- article 关联表
+CREATE TABLE article_lists (
+    id SERIAL PRIMARY KEY,
+    collection_id    INTEGER NOT NULL ,
+    article_id    INTEGER  NOT NULL ,
+    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
+);
+

+ 52 - 30
documents/zh/api/article.md

@@ -1,61 +1,83 @@
 # article
 
-## article文章
+## article 文章
+
 ```table
-CREATE TABLE article (
-    id           INTEGER PRIMARY KEY,
-    uuid         CHAR (36) ,
-    title        TEXT (50) NOT NULL,
+CREATE TABLE articles (
+    id SERIAL PRIMARY KEY,
+    uuid         VARCHAR (36) ,
+    title        VARCHAR (32) NOT NULL,
     subtitle     VARCHAR (32),
     summary      VARCHAR (255),
     content      TEXT,
-    owner_id     INTEGER,
-    owner        CHAR (36),
-    setting      TEXT,
+    owner_id     INTEGER  NOT NULL,
+    owner        VARCHAR (36),
+    setting      JSON,
     status       INTEGER   NOT NULL DEFAULT (10),
-    create_at  BIGINT,
-    update_at  BIGINT,
-    delete_at  BIGINT
+	version     INTEGER NOT NULL DEFAULT (1),
+    deleted_at  TIMESTAMP,
+    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
+    updated_at  TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
 );
 
 ```
-`uuid` 旧表中的主键 
 
-`setting` json格式。文章设置
+`uuid` 旧表中的主键
+
+`setting` json 格式。文章设置
+
+### API
+	
+`GET /api/article/:aid`
+根据id查询
+	
+`GET /api/article/title/:title`
+输入标题查询符合条件的 title% 
+	
+`PUT /api/article`
+
+新建文章
+`POST /api/article`
+修改
+
+`DELETE /api/article/:aid`
+删除
 
 ## article_list 关联表
+
 ```table
-CREATE TABLE article_list (
-    id            INTEGER      PRIMARY KEY AUTOINCREMENT,
-    collect_id    VARCHAR (36) NOT NULL REFERENCES collect (id),
-    collect_title TEXT,
-    article_id    VARCHAR (36)  NOT NULL REFERENCES article (id),
-    level         INTEGER  NOT NULL DEFAULT (1),
-    title         VARCHAR (64) NOT NULL,
+CREATE TABLE collection_article_lists (
+    id SERIAL PRIMARY KEY,
+    collect_id    INTEGER NOT NULL REFERENCES collections (id),
+    article_id    INTEGER  NOT NULL REFERENCES articles (id),
+    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
 );
 ```
-article 和 collect的关联表
 
-`level`在目录中的层级 1-8
+article 和 collect 的关联表
 
-`title`在目录中的文章标题
 
 ## 文集
+
 ```table
-CREATE TABLE collect (
-    id INTEGER PRIMARY KEY,
+CREATE TABLE collections (
+    id SERIAL PRIMARY KEY,
     uuid         VARCHAR (36) ,
     title        VARCHAR (32) NOT NULL,
     subtitle     VARCHAR (32),
     summary      VARCHAR (255),
     article_list TEXT,
     status       INTEGER   NOT NULL DEFAULT (10),
-    owner        CHAR (36),
+    creator_id   INTEGER,
+    owner        VARCHAR (36),
     lang         CHAR (8),
-    create_at  BIGINT,
-    update_at  BIGINT,
-    delete_at  BIGINT
+	version     INTEGER NOT NULL DEFAULT (1),
+    deleted_at  TIMESTAMP,
+    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
+    updated_at  TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
 );
 ```
 
-uuid 旧表中的主键 
+`uuid` 旧表中的主键
+
+`creator_id` 创建者。原来的表中是 owner uuid 导入后更新为int 然后删除owner

+ 16 - 3
documents/zh/api/course.md

@@ -21,7 +21,7 @@ CREATE TABLE course
     update_time INTEGER, 
     delete_time INTEGER , 
     content TEXT
-)
+);
 ```
 
 ```
@@ -45,6 +45,19 @@ CREATE TABLE 'lesson'
     'date' INTEGER, 
     'duration' INTEGER, 
     'content' TEXT
-)
+);
 
-```
+```
+
+## API
+
+GET /api/course/:cid
+PUT /api/course/?data=data
+POST /api/course/:cid/?data=data
+DELETE /api/course/:cid
+
+GET /api/lesson/:lid
+GET /api/lessons/:cid
+PUT /api/lesson/?data=data
+POST /api/lesson/:lid/?data=data
+DELETE /api/lesson/:lid

+ 34 - 19
documents/zh/api/readme.md

@@ -1,34 +1,38 @@
 # 后端开发文档
+
 ## 开发环境
-- golang
-- PostgreSQL
-- Redis
-- ES
 
-给vscode用的go
+-   golang
+-   PostgreSQL
+-   Redis
+-   ES
+
+给 vscode 用的 go
+
 ```
 sudo apt install yarnpkg golang-go
 ```
-## 文档资源
 
+## 文档资源
 
 ## 依赖
-https://github.com/go-redis/redis
 
+https://github.com/go-redis/redis
 
 ## 目录
-* [版本](channel.md)
-* [课程](course.md)
-* [字典](dict.md)
-* [工作组](group.md)
-* [语料库](palicanon.md)
-* [术语](term.md)
-* [译文](translation.md)
-* [文章文集](article.md)
-* [用户](user.md)
-* [逐词解析](wbw.md)
-* [全文搜索](search.md)
-* [其他工具表](others.md)
+
+-   [版本](channel.md)
+-   [课程](course.md)
+-   [字典](dict.md)
+-   [工作组](group.md)
+-   [语料库](palicanon.md)
+-   [术语](term.md)
+-   [译文](translation.md)
+-   [文章文集](article.md)
+-   [用户](user.md)
+-   [逐词解析](wbw.md)
+-   [全文搜索](search.md)
+-   [其他工具表](others.md)
 
 ```mermaid
 graph LR
@@ -60,3 +64,14 @@ collection_edit --> collection
 article_edit  --> article
 ```
 
+## 数据表设计
+
+数据表应包含如下字段
+
+```
+    id SERIAL PRIMARY KEY,
+    version     INTEGER NOT NULL DEFAULT (1),
+    created_at  TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
+    updated_at  TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
+    deleted_at  TIMESTAMP //可选
+```

+ 22 - 0
documents/zh/api/term.md

@@ -0,0 +1,22 @@
+# 术语
+
+```table
+CREATE TABLE terms (
+    id SERIAL PRIMARY KEY,
+    uuid          VARCHAR (36),
+    word          VARCHAR (256),
+    word_en       VARCHAR (256),
+    tag           VARCHAR (128),
+    meaning       TEXT,
+    other_meaning TEXT,
+    note          TEXT,
+    channal       VARCHAR (36),
+    owner         VARCHAR (36),
+    lang          VARCHAR (8),
+    hit           INTEGER  DEFAULT (0),
+	version     INTEGER NOT NULL DEFAULT (1),
+    deleted_at  TIMESTAMP,
+    created_at  TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
+    updated_at  TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
+);
+```

+ 60 - 13
documents/zh/api/user.md

@@ -1,8 +1,13 @@
 # 用户
+
 ## users
+
 用户信息
+
+旧表:user_info
+
 ```
-CREATE TABLE user_info (
+CREATE TABLE users (
     id           INTEGER    PRIMARY KEY AUTOINCREMENT,
     userid       TEXT       UNIQUE,
     path         CHAR (36),
@@ -16,31 +21,55 @@ CREATE TABLE user_info (
     setting      TEXT
 );
 ```
+
 #### `id`
-INTEGER 唯一自增id 
+
+INTEGER 唯一自增 id
+
 #### `userid`
-CHAR (36)  uuid
+
+CHAR (36) uuid
+
 #### `path`
+
 CHAR (36) 用户在服务器上私有文件的路径
+
 #### `username`
+
 TEXT (64) 用户登录名
+
 #### `password`
-TEXT 密码 md5加密
+
+TEXT 密码 md5 加密
+
 #### `nickname`
+
 TEXT (64) 昵称
+
 #### `email`
+
 TEXT (256) 电邮地址
+
 #### create_time
+
 INTEGER 账户建立时间
+
 #### `modify_time`
+
 INTEGER 数据修改时间
+
 #### `receive_time`
+
 INTEGER 服务器收到此数据时间
+
 #### `setting`
+
 TEXT 用户设置 json 数据
 
 ## profile
+
 用户简历
+
 ```
 CREATE TABLE user_profile (
     id SERIAL PRIMARY KEY,
@@ -51,21 +80,35 @@ CREATE TABLE user_profile (
     email     TEXT
 );
 ```
+
 #### `id`
-INTEGER 唯一自增id 
+
+INTEGER 唯一自增 id
+
 #### `user_id`
-CHAR (36)  uuid
+
+CHAR (36) uuid
+
 #### `bio`
-TEXT 
+
+TEXT
+
 #### `lang`
+
 简介语言,一个用户可以建立多个语言版本的简历。用户的显示与用户的语言设置有关。不能匹配到相同语言时,匹配相同语族,还是不行,就显示默认记录。
+
 #### `isdefault`
+
 是否是默认记录。
+
 #### `email`
+
 电邮地址
 
-## 编辑记录索引active_index
+## 编辑记录索引 active_index
+
 用户行为记录。以日期计算的使用与编辑有关功能的时间和操作次数
+
 ```table
 CREATE TABLE active_index (
     id SERIAL PRIMARY KEY,
@@ -73,16 +116,18 @@ CREATE TABLE active_index (
     user_uuid  VARCHAR (36),
     date     TIMESTAMP NOT NULL,
     duration INTEGER NOT NULL,
-    hit      INTEGER NOT NULL 
+    hit      INTEGER NOT NULL
 );
 ```
+
 `date` 日期
 
 `duration` 持续时间 毫秒
 
 `hit` 操作次数
 
-## 编辑记录edit_records
+## 编辑记录 edit_records
+
 ```table
 CREATE TABLE edit_records (
     id SERIAL PRIMARY KEY,
@@ -92,11 +137,12 @@ CREATE TABLE edit_records (
     end_at    TIMESTAMP,
     duration INTEGER,
     hit      INTEGER   DEFAULT (0),
-    timezone INTEGER   DEFAULT (0) 
+    timezone INTEGER   DEFAULT (0)
 );
 ```
 
 ## 编辑行为记录
+
 ```table
 CREATE TABLE active_log (
     id SERIAL PRIMARY KEY,
@@ -111,9 +157,11 @@ CREATE TABLE active_log (
 只添加不删除
 
 ### api
-PUT /api/useractive/log
+
+`PUT /api/useractive/log`
 
 body:
+
 ```
 {
     active:aid,
@@ -121,4 +169,3 @@ body:
     timezone:timezone,
 }
 ```
-