添加文章管理实现

master
18650180552 2019-05-17 17:45:50 +08:00
parent e1a9009d95
commit c5a4c7edab
6 changed files with 186 additions and 110 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@ glide.yaml
api_user.exe
user.exe
background.exe
backgroun.exe

View File

@ -2,16 +2,10 @@
<project version="4">
<component name="ChangeListManager">
<list default="true" id="882ad3e2-74c3-43b0-b9f1-5497ec2bf4ca" name="Default Changelist" comment="">
<change afterPath="$PROJECT_DIR$/.idea/background.iml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/modules.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/vcs.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/controller/blog.go" afterDir="false" />
<change afterPath="$PROJECT_DIR$/model/blog.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/config/config.go" beforeDir="false" afterPath="$PROJECT_DIR$/config/config.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/db/sqlManager.go" beforeDir="false" afterPath="$PROJECT_DIR$/db/sqlManager.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/controller/blog.go" beforeDir="false" afterPath="$PROJECT_DIR$/controller/blog.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/main.go" beforeDir="false" afterPath="$PROJECT_DIR$/main.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/model/blog.go" beforeDir="false" afterPath="$PROJECT_DIR$/model/blog.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/user.yaml" beforeDir="false" afterPath="$PROJECT_DIR$/user.yaml" afterDir="false" />
</list>
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
@ -21,26 +15,31 @@
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="FileEditorManager">
<leaf>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/controller/file.go">
<provider selected="true" editor-type-id="text-editor" />
</entry>
</file>
<leaf SIDE_TABS_SIZE_LIMIT_KEY="300">
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/user.yaml">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="253">
<caret line="11" column="7" selection-start-line="11" selection-start-column="7" selection-end-line="11" selection-end-column="7" />
<state relative-caret-position="299">
<caret line="13" column="22" selection-start-line="13" selection-start-column="22" selection-end-line="13" selection-end-column="22" />
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="true">
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/main.go">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="194">
<caret line="91" column="24" selection-start-line="91" selection-start-column="17" selection-end-line="91" selection-end-column="24" />
<state relative-caret-position="202">
<caret line="92" column="46" selection-start-line="92" selection-start-column="46" selection-end-line="92" selection-end-column="46" />
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/README.md">
<provider selected="true" editor-type-id="split-provider[text-editor;markdown-preview-editor]">
<state split_layout="SPLIT">
<first_editor />
<second_editor />
</state>
</provider>
</entry>
@ -48,19 +47,28 @@
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/controller/blog.go">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="207">
<caret line="9" column="10" selection-start-line="9" selection-start-column="10" selection-end-line="9" selection-end-column="10" />
<state relative-caret-position="248">
<caret line="78" column="19" selection-start-line="78" selection-start-column="19" selection-end-line="78" selection-end-column="19" />
<folding>
<element signature="e#20#78#0" expanded="true" />
<element signature="e#20#97#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://C:/Go/src/strconv/atoi.go">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="187">
<caret line="201" column="5" selection-start-line="201" selection-start-column="5" selection-end-line="201" selection-end-column="5" />
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/controller/user.go">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="439">
<state relative-caret-position="2093">
<caret line="116" column="18" selection-start-line="116" selection-start-column="18" selection-end-line="116" selection-end-column="18" />
</state>
</provider>
@ -69,7 +77,7 @@
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/model/model.go">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="374">
<state relative-caret-position="989">
<caret line="50" column="5" selection-start-line="50" selection-start-column="5" selection-end-line="50" selection-end-column="5" />
<folding>
<element signature="e#15#109#0" expanded="true" />
@ -78,14 +86,11 @@
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<file pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/model/blog.go">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="391">
<caret line="17" column="10" selection-start-line="17" selection-start-column="10" selection-end-line="17" selection-end-column="10" />
<folding>
<element signature="e#15#42#0" expanded="true" />
</folding>
<state relative-caret-position="115">
<caret line="5" selection-start-line="5" selection-end-line="5" />
</state>
</provider>
</entry>
@ -94,7 +99,7 @@
<entry file="file://$PROJECT_DIR$/db/db.go">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="285">
<caret line="124" column="70" selection-start-line="124" selection-start-column="70" selection-end-line="124" selection-end-column="70" />
<caret line="135" column="22" selection-start-line="135" selection-start-column="22" selection-end-line="135" selection-end-column="22" />
</state>
</provider>
</entry>
@ -102,7 +107,7 @@
<file pinned="false" current-in-tab="false">
<entry file="file://C:/Go/src/database/sql/sql.go">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="285">
<state relative-caret-position="59179">
<caret line="2586" column="5" selection-start-line="2586" selection-start-column="5" selection-end-line="2586" selection-end-column="5" />
</state>
</provider>
@ -124,9 +129,9 @@
<component name="IdeDocumentHistory">
<option name="CHANGED_PATHS">
<list>
<option value="$PROJECT_DIR$/model/blog.go" />
<option value="$PROJECT_DIR$/controller/blog.go" />
<option value="$PROJECT_DIR$/main.go" />
<option value="$PROJECT_DIR$/controller/blog.go" />
<option value="$PROJECT_DIR$/model/blog.go" />
</list>
</option>
</component>
@ -142,27 +147,7 @@
</navigator>
<panes>
<pane id="Scope" />
<pane id="ProjectPane">
<subPane>
<expand>
<path>
<item name="background" type="b2602c69:ProjectViewProjectNode" />
<item name="background" type="462c0819:PsiDirectoryNode" />
</path>
<path>
<item name="background" type="b2602c69:ProjectViewProjectNode" />
<item name="background" type="462c0819:PsiDirectoryNode" />
<item name="controller" type="462c0819:PsiDirectoryNode" />
</path>
<path>
<item name="background" type="b2602c69:ProjectViewProjectNode" />
<item name="background" type="462c0819:PsiDirectoryNode" />
<item name="redis" type="462c0819:PsiDirectoryNode" />
</path>
</expand>
<select />
</subPane>
</pane>
<pane id="ProjectPane" />
</panes>
</component>
<component name="PropertiesComponent">
@ -193,18 +178,19 @@
</option>
</component>
<component name="RunManager">
<configuration name="go build main.go" type="GoApplicationRunConfiguration" factoryName="Go Application" temporary="true" nameIsGenerated="true">
<configuration name="backgroun" type="GoApplicationRunConfiguration" factoryName="Go Application" temporary="true">
<module name="background" />
<working_directory value="$PROJECT_DIR$/" />
<kind value="FILE" />
<filePath value="$PROJECT_DIR$/main.go" />
<package value="background" />
<directory value="$PROJECT_DIR$/" />
<output_directory value="$PROJECT_DIR$" />
<method v="2" />
</configuration>
<recent_temporary>
<list>
<item itemvalue="Go Build.go build main.go" />
<item itemvalue="Go Build.backgroun" />
</list>
</recent_temporary>
</component>
@ -212,15 +198,9 @@
<frame x="-8" y="32" width="1936" height="1056" extended-state="6" />
<editor active="true" />
<layout>
<window_info id="Favorites" side_tool="true" />
<window_info content_ui="combo" id="Project" order="0" visible="true" weight="0.2497332" />
<window_info content_ui="combo" id="Project" order="0" visible="true" weight="0.2524013" />
<window_info id="Structure" order="1" side_tool="true" weight="0.25" />
<window_info anchor="bottom" id="LuaCheck" />
<window_info anchor="bottom" id="Docker" show_stripe_button="false" />
<window_info anchor="bottom" id="Database Changes" />
<window_info anchor="bottom" id="Version Control" />
<window_info anchor="bottom" id="Terminal" />
<window_info anchor="bottom" id="Event Log" side_tool="true" />
<window_info id="Favorites" order="2" side_tool="true" />
<window_info anchor="bottom" id="Message" order="0" />
<window_info anchor="bottom" id="Find" order="1" />
<window_info active="true" anchor="bottom" id="Run" order="2" visible="true" weight="0.32960895" />
@ -228,21 +208,24 @@
<window_info anchor="bottom" id="Cvs" order="4" weight="0.25" />
<window_info anchor="bottom" id="Inspection" order="5" weight="0.4" />
<window_info anchor="bottom" id="TODO" order="6" />
<window_info anchor="right" id="Google Cloud Storage" show_stripe_button="false" />
<window_info anchor="right" id="JSON Utils" />
<window_info anchor="right" id="Database" />
<window_info anchor="bottom" id="LuaCheck" order="7" />
<window_info anchor="bottom" id="Docker" order="8" show_stripe_button="false" />
<window_info anchor="bottom" id="Version Control" order="9" />
<window_info anchor="bottom" id="Database Changes" order="10" />
<window_info anchor="bottom" id="Terminal" order="11" />
<window_info anchor="bottom" id="Event Log" order="12" side_tool="true" />
<window_info anchor="right" id="Commander" internal_type="SLIDING" order="0" type="SLIDING" weight="0.4" />
<window_info anchor="right" id="Ant Build" order="1" weight="0.25" />
<window_info anchor="right" content_ui="combo" id="Hierarchy" order="2" weight="0.25" />
<window_info anchor="right" id="Google Cloud Storage" order="3" show_stripe_button="false" />
<window_info anchor="right" id="Database" order="4" />
<window_info anchor="right" id="JSON Utils" order="5" />
</layout>
</component>
<component name="TypeScriptGeneratedFilesManager">
<option name="version" value="1" />
</component>
<component name="editorHistoryManager">
<entry file="file://$PROJECT_DIR$/controller/file.go">
<provider selected="true" editor-type-id="text-editor" />
</entry>
<entry file="file://$PROJECT_DIR$/redis/redis.go">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="-4342">
@ -253,16 +236,19 @@
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/user.yaml">
<entry file="file://$PROJECT_DIR$/controller/file.go">
<provider selected="true" editor-type-id="text-editor" />
</entry>
<entry file="file://$PROJECT_DIR$/controller/user.go">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="253">
<caret line="11" column="7" selection-start-line="11" selection-start-column="7" selection-end-line="11" selection-end-column="7" />
<state relative-caret-position="2093">
<caret line="116" column="18" selection-start-line="116" selection-start-column="18" selection-end-line="116" selection-end-column="18" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/model/model.go">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="374">
<state relative-caret-position="989">
<caret line="50" column="5" selection-start-line="50" selection-start-column="5" selection-end-line="50" selection-end-column="5" />
<folding>
<element signature="e#15#109#0" expanded="true" />
@ -270,51 +256,63 @@
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/controller/user.go">
<entry file="file://C:/Go/src/database/sql/sql.go">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="439">
<caret line="116" column="18" selection-start-line="116" selection-start-column="18" selection-end-line="116" selection-end-column="18" />
<state relative-caret-position="59179">
<caret line="2586" column="5" selection-start-line="2586" selection-start-column="5" selection-end-line="2586" selection-end-column="5" />
</state>
</provider>
</entry>
<entry file="file://C:/Go/src/database/sql/sql.go">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="285">
<caret line="2586" column="5" selection-start-line="2586" selection-start-column="5" selection-end-line="2586" selection-end-column="5" />
<entry file="file://$PROJECT_DIR$/README.md">
<provider selected="true" editor-type-id="split-provider[text-editor;markdown-preview-editor]">
<state split_layout="SPLIT">
<first_editor />
<second_editor />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/db/db.go">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="285">
<caret line="124" column="70" selection-start-line="124" selection-start-column="70" selection-end-line="124" selection-end-column="70" />
<caret line="135" column="22" selection-start-line="135" selection-start-column="22" selection-end-line="135" selection-end-column="22" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/model/blog.go">
<entry file="file://C:/Go/src/strconv/atoi.go">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="391">
<caret line="17" column="10" selection-start-line="17" selection-start-column="10" selection-end-line="17" selection-end-column="10" />
<folding>
<element signature="e#15#42#0" expanded="true" />
</folding>
<state relative-caret-position="187">
<caret line="201" column="5" selection-start-line="201" selection-start-column="5" selection-end-line="201" selection-end-column="5" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/controller/blog.go">
<entry file="file://$PROJECT_DIR$/user.yaml">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="207">
<caret line="9" column="10" selection-start-line="9" selection-start-column="10" selection-end-line="9" selection-end-column="10" />
<folding>
<element signature="e#20#78#0" expanded="true" />
</folding>
<state relative-caret-position="299">
<caret line="13" column="22" selection-start-line="13" selection-start-column="22" selection-end-line="13" selection-end-column="22" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/main.go">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="194">
<caret line="91" column="24" selection-start-line="91" selection-start-column="17" selection-end-line="91" selection-end-column="24" />
<state relative-caret-position="202">
<caret line="92" column="46" selection-start-line="92" selection-start-column="46" selection-end-line="92" selection-end-column="46" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/controller/blog.go">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="248">
<caret line="78" column="19" selection-start-line="78" selection-start-column="19" selection-end-line="78" selection-end-column="19" />
<folding>
<element signature="e#20#97#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/model/blog.go">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="115">
<caret line="5" selection-start-line="5" selection-end-line="5" />
</state>
</provider>
</entry>

View File

@ -1,11 +1,59 @@
package controller
import (
"background/db"
"background/logs"
"background/model"
"fmt"
"github.com/gin-gonic/gin"
"strconv"
)
func GetPageParaFromQuery(c *gin.Context) (int,int){
limit := c.Query("limit")
offset := c.Query("offset")
iLmit,e := strconv.Atoi(limit)
if nil != e{
return 0,0
}
iOffset,e := strconv.Atoi(offset)
if nil != e{
return 0,0
}
return iLmit,iOffset
}
func GetArticles(c *gin.Context) {
type ReqArticles struct {
Name string `json:"name"`
}
var req ReqArticles
rsp := RespBase{"ERR",-1,nil}
defer func() {
c.JSON(200,rsp)
}()
e := c.BindJSON(&req)
if nil != e{
logs.Error(e.Error())
return
}
article := []model.Doc{}
limit,offset := GetPageParaFromQuery(c)
var sql string
if req.Name != ""{
sql = fmt.Sprintf("select * from doc where doc.name like '%%%s%%' limit %d offset %d",req.Name,limit,offset*limit)
}else{
sql = fmt.Sprintf("select * from doc limit %d offset %d",limit,offset*limit)
}
e = db.GetMysqlClient().Query2(sql,&article)
if nil != e{
logs.Error(e.Error())
return
}
rsp.Data = article
rsp.Status = 0
rsp.Msg = "OK"
}
func AddArticle(c *gin.Context) {
rsp := RespBase{Msg:"FAIL", Status:210,}
@ -24,6 +72,10 @@ func AddArticle(c *gin.Context) {
logs.Error(er.Error())
return
}
if req.Title == ""{
rsp.Msg = "title required"
return
}
e := model.CreateDoc(
model.Doc{
Type:req.Type,
@ -38,4 +90,15 @@ func AddArticle(c *gin.Context) {
}
rsp.Msg = "OK"
rsp.Status = 0
}
}
func ArticlesType(c *gin.Context) {
rsp := RespBase{Msg:"FAIL", Status:210,}
defer func() {
c.JSON(200,rsp)
}()
rsp.Data = model.GetArticlesType()
rsp.Msg = "OK"
rsp.Status = 0
}

View File

@ -89,8 +89,9 @@ func main() {
/** 修改密码**/
api.POST("modify_pass",middle.AuthMiddle,userController.ModifyPasswd)
api.POST("articles",controller.GetArticles)
api.PUT("article",controller.AddArticle) // 添加文章
api.GET("article_type",controller.ArticlesType) //获取所有文章分类
}
e := r.Run(":" + strconv.Itoa(config.GetPort()))

View File

@ -3,7 +3,6 @@ package model
import (
"background/db"
"background/logs"
"errors"
"fmt"
)
@ -13,14 +12,28 @@ type Doc struct {
Type int64 `sql:"type" json:"type"`
Content string `sql:"content" json:"content"`
Author string `sql:"author" json:"author"`
}
}
func CreateDoc(doc Doc) error {
if doc.ID == 0{
return errors.New("wrong primary key")
type ArticleType struct {
Id int64 `sql:"id" json:"id"`
Name string `sql:"name" json:"name"`
Author string `sql:"author" json:"author"`
}
func GetArticlesType() []ArticleType{
ret := []ArticleType{}
sql := fmt.Sprintf("select * from article_type");
e := db.GetBlogMysql().Query2(sql,&ret)
if nil != e{
logs.Error(e.Error())
return nil
}
return ret
}
func CreateDoc(doc Doc) error {
sql := fmt.Sprintf("insert into doc(title,type,content,author) values ('%s','%d','%s','%s')",doc.Title,doc.Type,doc.Content,doc.Author)
_,e := db.GetMysqlClient().Query(sql)
_,e := db.GetBlogMysql().Query(sql)
if nil != e{
logs.Error(e.Error())
return e

View File

@ -18,10 +18,10 @@ mysql:
max_open: 100
MaxIdle: 99
mysql2:
addr: 118.24.238.198
user: server
password: server
db: background
addr: 35.240.179.248
user: root
password: a7458969
db: blog
max_open: 100
MaxIdle: 99