Browse Source

:construction: add collection table

visuddhinanda 4 năm trước cách đây
mục cha
commit
7f5585c9b9

+ 12 - 0
api/main.go

@@ -66,5 +66,17 @@ func main() {
 	//删除
 	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.Run()
 }

+ 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"),
+		})
+	}
+}

+ 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
+);

+ 14 - 16
documents/zh/api/article.md

@@ -30,38 +30,36 @@ CREATE TABLE articles (
 
 ```table
 CREATE TABLE article_lists (
-    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,
+    id SERIAL PRIMARY KEY,
+    collect_id    INTEGER NOT NULL REFERENCES collections (id),
+    article_id    INTEGER  NOT NULL REFERENCES articles (id),
 );
 ```
 
 article 和 collect 的关联表
 
-`level`在目录中的层级 1-8
-
-`title`在目录中的文章标题
 
 ## 文集
 
 ```table
-CREATE TABLE collects (
-    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