65 Star 395 Fork 128

admpub/nging

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
dbAccount.go 2.99 KB
一键复制 编辑 原始数据 按行查看 历史
admpub 提交于 2020-02-21 10:57 . 表名称统一增加nging前缀
/*
Nging is a toolbox for webmasters
Copyright (C) 2018-present Wenhui Shen <swh@admpub.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package model
import (
"errors"
"github.com/webx-top/com"
"github.com/webx-top/db"
"github.com/webx-top/echo"
"github.com/admpub/nging/application/dbschema"
"github.com/admpub/nging/application/library/dbmanager/driver/mysql"
"github.com/admpub/nging/application/model/base"
)
func NewDbAccount(ctx echo.Context) *DbAccount {
return &DbAccount{
NgingDbAccount: &dbschema.NgingDbAccount{},
Base: base.New(ctx),
}
}
type DbAccount struct {
*dbschema.NgingDbAccount
*base.Base
}
func (a *DbAccount) SetOptions() error {
options := echo.H{}
charset := a.Formx(`charset`).String()
if len(charset) > 0 {
if !com.InSlice(charset, mysql.Charsets) {
return a.E(`字符集charset值无效`)
}
options.Set(`charset`, charset)
}
b, _ := com.JSONEncode(options)
a.Options = com.Bytes2str(b)
return nil
}
func (a *DbAccount) setDefaultValue() {
if len(a.NgingDbAccount.Engine) == 0 {
a.NgingDbAccount.Engine = `mysql`
}
switch a.NgingDbAccount.Engine {
case `mysql`:
if len(a.NgingDbAccount.Host) == 0 {
a.NgingDbAccount.Host = `127.0.0.1:3306`
}
if len(a.NgingDbAccount.User) == 0 {
a.NgingDbAccount.User = `root`
}
case `redis`:
if len(a.NgingDbAccount.Host) == 0 {
a.NgingDbAccount.Host = `127.0.0.1:6379`
}
if len(a.NgingDbAccount.Name) == 0 {
a.NgingDbAccount.Name = `0`
}
}
}
func (a *DbAccount) Add() (interface{}, error) {
if len(a.NgingDbAccount.Title) == 0 {
return nil, errors.New(a.T(`请输入标题`))
}
num, err := a.Count(nil, db.And(db.Cond{`uid`: a.Uid}, db.Cond{`title`: a.Title}))
if err != nil {
return nil, err
}
if num > 0 {
return nil, errors.New(a.T(`标题已存在,请设置为一个从未使用过的标题`))
}
a.setDefaultValue()
return a.NgingDbAccount.Add()
}
func (a *DbAccount) Edit(id uint, mw func(db.Result) db.Result, args ...interface{}) error {
if len(a.NgingDbAccount.Title) == 0 {
return errors.New(a.T(`请输入标题`))
}
num, err := a.Count(nil, db.And(
db.Cond{`uid`: a.Uid},
db.Cond{`title`: a.Title},
db.Cond{`id`: db.NotEq(id)},
))
if err != nil {
return err
}
if num > 0 {
return errors.New(a.T(`标题已存在,请设置为一个从未使用过的标题`))
}
a.setDefaultValue()
return a.NgingDbAccount.Edit(mw, args...)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/admpub/nging.git
git@gitee.com:admpub/nging.git
admpub
nging
nging
v2.2.0

搜索帮助