From 9ec72a1980ca39404a14fd88c92e2a5939346c59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=87=8C=E6=B5=B7=E4=BB=B2=E5=AD=90?= Date: Mon, 30 Mar 2020 13:46:11 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A1=AE=E5=AE=9A=E6=9C=8D=E5=8A=A1=E7=BB=93?= =?UTF-8?q?=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 2 +- community/community-service.go | 28 ++++++++++++++++++++++++-- public/public-handler.go | 36 ++++++++++++++++++++++++++++++++-- server.go | 7 ++++++- service.rest | 17 +++++++++++++++- 5 files changed, 83 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index 786cb85..b97bbb5 100644 --- a/Makefile +++ b/Makefile @@ -2,4 +2,4 @@ build: go build . dev: - go run . \ No newline at end of file + go run . -d "root:root@tcp(i.myschools.me:6006)/rubbish?charset=utf8mb4&parseTime=True&loc=Local" \ No newline at end of file diff --git a/community/community-service.go b/community/community-service.go index 29fa575..00f058e 100644 --- a/community/community-service.go +++ b/community/community-service.go @@ -1,6 +1,8 @@ package community import ( + "sync" + "github.com/jinzhu/gorm" "yyjishu.com/rubbish-class/rubbish" ) @@ -10,17 +12,39 @@ type CommunityModel struct { Name string `gorm:"type:varchar(20);"` } +var once sync.Once + type Community struct { } -func (c *Community) Map() { - +func NewService() *Community { + c := &Community{} + once.Do(c.Init) + return c } +//Map 社区KV获取 +func (c *Community) Map() (*map[string]uint, error) { + var cs []CommunityModel + if err := rubbish.DB.Find(&cs).Error; err != nil { + return nil, err + } + data := make(map[string]uint) + for _, c := range cs { + data[c.Name] = c.ID + } + return &data, nil +} + +//Create 创建社区信息 func (c *Community) Create(model *CommunityModel) error { + if err := rubbish.DB.Create(model).Error; err != nil { + return err + } return nil } +//Init 社区表初始化 func (c *Community) Init() { rubbish.DB.AutoMigrate(&CommunityModel{}) } diff --git a/public/public-handler.go b/public/public-handler.go index a0d598c..8738717 100644 --- a/public/public-handler.go +++ b/public/public-handler.go @@ -1,7 +1,39 @@ package public -import "github.com/gin-gonic/gin" +import ( + "net/http" + "github.com/gin-gonic/gin" + "yyjishu.com/rubbish-class/community" +) + +//CommunityListHandler 社区列表 func CommunityListHandler(c *gin.Context) { - + service := community.NewService() + data, err := service.Map() + if err != nil { + c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{ + "err": err.Error(), + }) + return + } + c.JSON(http.StatusOK, data) +} + +//CommunityCreateHandler 社区创建 +func CommunityCreateHandler(c *gin.Context) { + var model community.CommunityModel + if err := c.ShouldBindJSON(&model); err != nil { + c.AbortWithStatusJSON(http.StatusBadRequest, nil) + return + } + service := community.NewService() + err := service.Create(&model) + if err != nil { + c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{ + "err": err.Error(), + }) + return + } + c.JSON(http.StatusOK, &model) } diff --git a/server.go b/server.go index b5afb89..5452242 100644 --- a/server.go +++ b/server.go @@ -10,6 +10,7 @@ import ( "yyjishu.com/rubbish-class/admin" "yyjishu.com/rubbish-class/app" "yyjishu.com/rubbish-class/public" + "yyjishu.com/rubbish-class/rubbish" ) var ( @@ -17,8 +18,11 @@ var ( ) func main() { + rubbish.Addr = flag.String("d", "root:root@tcp(127.0.0.1:3306)/rubbish?charset=utf8mb4\u0026parseTime=True\u0026loc=Local", "address for mysql") flag.Parse() + rubbish.Init() + //启动时检查与创建文件夹video,用于存放用户上传的视频文件 _, err := os.Stat("data") if err != nil { @@ -34,7 +38,8 @@ func main() { rbGroup := r.Group(`/rubbish`) { - rbGroup.GET(`/community/list`, public.CommunityListHandler) //社区列表 + rbGroup.GET(`/community/list`, public.CommunityListHandler) //社区列表,OK + rbGroup.POST(`/community/create`, public.CommunityCreateHandler) //社区列表,OK rbGroup.StaticFS(`/video`, http.Dir("./data")) authorized := rbGroup.Group(`/admin`, gin.BasicAuth(gin.Accounts{ diff --git a/service.rest b/service.rest index d35d40f..d270851 100644 --- a/service.rest +++ b/service.rest @@ -1,4 +1,4 @@ -@url=http://localhost:8080 +@url=http://localhost:8080/rubbish //@url=https://api.xintijiao.com @@ -6,6 +6,21 @@ @refresh_token=31_T-96lys-lZtR4B9OtRMkFOqyX_zxJNwMH6vcKxenk7enK5jeDZ6Av-Ipj9aVWr1dzdsMaCACoxbmEFKmReHwTgtQNrOpY3TdiTkM5pEyvIQ @openid=oYmRQxLw6UKdlQsZYIkRKbWlCijI +###############################公共部分############################################### + +POST {{url}}/community/create HTTP/1.1 +Content-Type: application/json + +{ + "name":"aaa小区" +} + +### + + +GET {{url}}/community/list HTTP/1.1 + + ############################### 以下微信小程序 ######################################## @jscode={{$guid}}