Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

controller 撰寫標準 #50

Open
smlsunxie opened this issue Sep 28, 2013 · 0 comments
Open

controller 撰寫標準 #50

smlsunxie opened this issue Sep 28, 2013 · 0 comments

Comments

@smlsunxie
Copy link
Member

針對 foodprint 進行重構相關程式

controller

參考 batchRouteController

所有更新統一由 domainService 處理,未來要加入交易安全可以更方便,標準更新函式如下

    def create() {
        def ${propertyName} = ${className}.get(id)
        render (contentType: 'text/json') {
            domainService.save(${propertyName})
        }
    }


    def update() {

        def ${propertyName} = ${className}.get(id)
        render (contentType: 'text/json') {
            domainService.save(${propertyName}, params)
        }

    }
    def delete(Long id) {

        def result
        def ${propertyName} = ${className}.get(id)
        try {

            result = domainService.delete(${propertyName})

        }catch(e){
            log.error e
            def msg = message(code: 'default.message.delete.failed', args: [${propertyName}, e.getMessage()])
            result = [success:false, message: msg] 
        }

        render (contentType: 'text/json') {
            result
        }


    }

以後使用 generate-all 將會預設實作完成上述函式

包括回傳訊息的格式也統一為

[success: success, message: msg, errors: errors]

範例:

{
    "success": false,
    "message": "品項:柳丁,途程項次:2 更新失敗",
    "errors": {
        "sequence": "品項途程項次:2 必須唯一"
    }
}

這樣的好處在於若 request 是由 extjs 前端的 form panel 透過 form.submit 送出,則會自動將各自的錯誤訊息歸屬在各自的欄位,如下圖:

2013-09-28 6 57 22

因為所有更新程序都已包在 domainService 所以回傳訊息皆自動產生上述格式,不需再撰寫相關程式,有多個欄位產生錯誤,也會自動新增

@smlsunxie smlsunxie reopened this Oct 15, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant