From a7e502ba9c911c6bf2f1c0f7642df205c379f9dd Mon Sep 17 00:00:00 2001 From: zicla Date: Mon, 25 Dec 2017 14:32:18 +0800 Subject: [PATCH] Add some new api of preference. --- build/conf/tank-nginx.conf | 2 +- build/conf/tank.json | 2 +- rest/config.go | 20 +++++++--- rest/context.go | 5 +++ rest/install.go | 15 ++++++- rest/preference_controller.go | 74 +++++++++++++++++++++++++++++++++++ rest/preference_dao.go | 55 ++++++++++++++++++++++++++ rest/preference_model.go | 15 +++++++ rest/preference_service.go | 18 +++++++++ 9 files changed, 197 insertions(+), 9 deletions(-) create mode 100644 rest/preference_controller.go create mode 100644 rest/preference_dao.go create mode 100644 rest/preference_model.go create mode 100644 rest/preference_service.go diff --git a/build/conf/tank-nginx.conf b/build/conf/tank-nginx.conf index e04de4e..a820bfb 100644 --- a/build/conf/tank-nginx.conf +++ b/build/conf/tank-nginx.conf @@ -3,7 +3,7 @@ server{ server_name tank.zicpo.cn; location / { - proxy_pass http://127.0.0.1:9090; + proxy_pass http://127.0.0.1:6010; proxy_set_header host $host; proxy_set_header X-Forwarded-For $remote_addr; proxy_pass_request_headers on; diff --git a/build/conf/tank.json b/build/conf/tank.json index 7d90b69..2ec1444 100644 --- a/build/conf/tank.json +++ b/build/conf/tank.json @@ -1,5 +1,5 @@ { - "ServerPort": 9090, + "ServerPort": 6010, "LogToConsole": true, "MysqlPort": 3306, "MysqlHost": "127.0.0.1", diff --git a/rest/config.go b/rest/config.go index 3fb5600..579b17f 100644 --- a/rest/config.go +++ b/rest/config.go @@ -1,13 +1,13 @@ package rest import ( + "encoding/json" + "flag" "fmt" "github.com/json-iterator/go" + "io/ioutil" "time" "unsafe" - "io/ioutil" - "encoding/json" - "flag" ) const ( @@ -21,12 +21,22 @@ const ( VERSION = "1.0.0" ) +/* +如果你需要在本地127.0.0.1创建默认的数据库和账号,使用以下语句。 +create database tank; +grant all privileges on tank.* to tank identified by 'tank123'; +flush privileges; +*/ +/* + 你也可以在运行时的参数中临时修改一些配置项: +-MysqlHost=127.0.0.1 -MysqlPort=3306 -MysqlSchema=tank -MysqlUserName=tank -MysqlPassword=tank123 +*/ var ( CONFIG = &Config{ //以下内容是默认配置项。 //默认监听端口号 - ServerPort: 9090, + ServerPort: 6010, //将日志输出到控制台。 LogToConsole: true, //mysql相关配置。 @@ -40,7 +50,7 @@ var ( MysqlUserName: "tank", //密码 MysqlPassword: "tank123", - //数据库连接信息。 + //数据库连接信息。这一项是上面几项组合而得,不可直接配置。 MysqlUrl: "%MysqlUserName:%MysqlPassword@tcp(%MysqlHost:%MysqlPort)/%MysqlSchema?charset=utf8&parseTime=True&loc=Local", //超级管理员用户名,只能包含英文和数字 AdminUsername: "admin", diff --git a/rest/context.go b/rest/context.go index 5c0f1c5..26e871b 100644 --- a/rest/context.go +++ b/rest/context.go @@ -104,6 +104,11 @@ func (this *Context) registerBeans() { this.registerBean(new(MatterDao)) this.registerBean(new(MatterService)) + //preference + this.registerBean(new(PreferenceController)) + this.registerBean(new(PreferenceDao)) + this.registerBean(new(PreferenceService)) + //session this.registerBean(new(SessionDao)) diff --git a/rest/install.go b/rest/install.go index 1ceb0df..3556838 100644 --- a/rest/install.go +++ b/rest/install.go @@ -5,8 +5,8 @@ import ( "github.com/jinzhu/gorm" "github.com/nu7hatch/gouuid" - "time" "regexp" + "time" ) //首次运行的时候,将自动安装数据库等内容。 @@ -46,6 +46,18 @@ func InstallDatabase() { LogInfo("创建Matter表") } + + preference := &Preference{} + hasTable = db.HasTable(preference) + if !hasTable { + createPreference := "CREATE TABLE `tank10_preference` (`uuid` char(36) NOT NULL,`name` varchar(45) DEFAULT NULL COMMENT '网站名称',`logo_url` varchar(255) DEFAULT NULL,`favicon_url` varchar(255) DEFAULT NULL,`footer_line1` varchar(1024) DEFAULT NULL,`footer_line2` varchar(1024) DEFAULT NULL,`sort` bigint(20) DEFAULT NULL,`modify_time` timestamp NULL DEFAULT NULL,`create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',PRIMARY KEY (`uuid`),UNIQUE KEY `id_UNIQUE` (`uuid`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='网站偏好设置表';" + db = db.Exec(createPreference) + if db.Error != nil { + LogPanic(db.Error) + } + LogInfo("创建Preference表") + } + session := &Session{} hasTable = db.HasTable(session) if !hasTable { @@ -87,7 +99,6 @@ func InstallDatabase() { LogPanic("超级管理员邮箱必填!") } - createUser := "CREATE TABLE `tank10_user` (`uuid` char(36) NOT NULL,`role` varchar(45) DEFAULT 'USER',`username` varchar(255) DEFAULT NULL COMMENT '昵称',`password` varchar(255) DEFAULT NULL COMMENT '密码',`email` varchar(45) DEFAULT NULL COMMENT '邮箱',`phone` varchar(45) DEFAULT NULL COMMENT '电话',`gender` varchar(45) DEFAULT 'UNKNOWN' COMMENT '性别,默认未知',`city` varchar(45) DEFAULT NULL COMMENT '城市',`avatar_url` varchar(255) DEFAULT NULL COMMENT '头像链接',`last_time` datetime DEFAULT NULL COMMENT '上次登录使劲按',`last_ip` varchar(45) DEFAULT NULL,`status` varchar(45) DEFAULT 'OK',`sort` bigint(20) DEFAULT NULL,`modify_time` timestamp NULL DEFAULT NULL,`create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',PRIMARY KEY (`uuid`),UNIQUE KEY `id_UNIQUE` (`uuid`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户表描述';" db = db.Exec(createUser) if db.Error != nil { diff --git a/rest/preference_controller.go b/rest/preference_controller.go new file mode 100644 index 0000000..c773068 --- /dev/null +++ b/rest/preference_controller.go @@ -0,0 +1,74 @@ +package rest + +import ( + "net/http" +) + +type PreferenceController struct { + BaseController + preferenceDao *PreferenceDao + preferenceService *PreferenceService +} + +//初始化方法 +func (this *PreferenceController) Init(context *Context) { + this.BaseController.Init(context) + + //手动装填本实例的Bean. 这里必须要用中间变量方可。 + b := context.GetBean(this.preferenceDao) + if b, ok := b.(*PreferenceDao); ok { + this.preferenceDao = b + } + + b = context.GetBean(this.preferenceService) + if b, ok := b.(*PreferenceService); ok { + this.preferenceService = b + } + +} + +//注册自己的路由。 +func (this *PreferenceController) RegisterRoutes() map[string]func(writer http.ResponseWriter, request *http.Request) { + + routeMap := make(map[string]func(writer http.ResponseWriter, request *http.Request)) + + //每个Controller需要主动注册自己的路由。 + routeMap["/api/preference/fetch"] = this.Wrap(this.Fetch, USER_ROLE_GUEST) + routeMap["/api/preference/edit"] = this.Wrap(this.Edit, USER_ROLE_ADMINISTRATOR) + return routeMap +} + +//查看某个偏好设置的详情。 +func (this *PreferenceController) Fetch(writer http.ResponseWriter, request *http.Request) *WebResult { + + preference := this.preferenceDao.Fetch() + + return this.Success(preference) + +} + +//修改 +func (this *PreferenceController) Edit(writer http.ResponseWriter, request *http.Request) *WebResult { + + //验证参数。 + name := request.FormValue("name") + if name == "" { + panic("name参数必填") + } + + logoUrl := request.FormValue("logoUrl") + faviconUrl := request.FormValue("faviconUrl") + footerLine1 := request.FormValue("footerLine1") + footerLine2 := request.FormValue("footerLine2") + + preference := this.preferenceDao.Fetch() + preference.Name = name + preference.LogoUrl = logoUrl + preference.FaviconUrl = faviconUrl + preference.FooterLine1 = footerLine1 + preference.FooterLine2 = footerLine2 + + preference = this.preferenceDao.Save(preference) + + return this.Success(preference) +} diff --git a/rest/preference_dao.go b/rest/preference_dao.go new file mode 100644 index 0000000..40a6125 --- /dev/null +++ b/rest/preference_dao.go @@ -0,0 +1,55 @@ +package rest + +import ( + _ "github.com/jinzhu/gorm/dialects/mysql" + "github.com/nu7hatch/gouuid" + "time" +) + +type PreferenceDao struct { + BaseDao +} + +//按照Id查询偏好设置 +func (this *PreferenceDao) Fetch() *Preference { + + // Read + var preference = &Preference{} + db := this.context.DB.First(preference) + if db.Error != nil { + + if db.Error.Error() == "record not found" { + preference.Name = "蓝眼云盘" + this.Create(preference) + return preference + } else { + return nil + } + + } + + return preference +} + +//创建 +func (this *PreferenceDao) Create(preference *Preference) *Preference { + + timeUUID, _ := uuid.NewV4() + preference.Uuid = string(timeUUID.String()) + preference.CreateTime = time.Now() + preference.ModifyTime = time.Now() + db := this.context.DB.Create(preference) + this.PanicError(db.Error) + + return preference +} + +//修改一个偏好设置 +func (this *PreferenceDao) Save(preference *Preference) *Preference { + + preference.ModifyTime = time.Now() + db := this.context.DB.Save(preference) + this.PanicError(db.Error) + + return preference +} diff --git a/rest/preference_model.go b/rest/preference_model.go new file mode 100644 index 0000000..625c953 --- /dev/null +++ b/rest/preference_model.go @@ -0,0 +1,15 @@ +package rest + +type Preference struct { + Base + Name string `json:"name"` + LogoUrl string `json:"logoUrl"` + FaviconUrl string `json:"faviconUrl"` + FooterLine1 string `json:"footerLine1"` + FooterLine2 string `json:"footerLine2"` +} + +// set File's table name to be `profiles` +func (Preference) TableName() string { + return TABLE_PREFIX + "preference" +} diff --git a/rest/preference_service.go b/rest/preference_service.go new file mode 100644 index 0000000..3923442 --- /dev/null +++ b/rest/preference_service.go @@ -0,0 +1,18 @@ +package rest + +//@Service +type PreferenceService struct { + Bean + preferenceDao *PreferenceDao +} + +//初始化方法 +func (this *PreferenceService) Init(context *Context) { + + //手动装填本实例的Bean. 这里必须要用中间变量方可。 + b := context.GetBean(this.preferenceDao) + if b, ok := b.(*PreferenceDao); ok { + this.preferenceDao = b + } + +}