9 changed files with 197 additions and 9 deletions
@ -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) |
|||
} |
|||
@ -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 |
|||
} |
|||
@ -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" |
|||
} |
|||
@ -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 |
|||
} |
|||
|
|||
} |
|||
Loading…
Reference in new issue