Grails:如何覆盖生成的服务方法? [英] Grails: How to override generated service method?

查看:78
本文介绍了Grails:如何覆盖生成的服务方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Grails新手在这里.我想重写此处显示的生成服务的get()和list()方法.

Grails novice here. I would like to override the get() and list() methods of the generated service shown here.

@Service(PlayerFile)
interface PlayerFileService {
    PlayerFile get(Serializable id)
    List<PlayerFile> list(Map args)
    Long count()
    void delete(Serializable id)
    PlayerFile save(PlayerFile playerFile)
}

为什么?默认的服务/支架模型显示PlayerFile类的所有域对象.我想对此进行过滤,以便仅显示属于已登录用户(即所有者)的对象.

Why? The default service/scaffolding model shows all domain objects for the PlayerFile class. I want to filter this so that only the objects belonging to the logged in user (i.e. the owner) are displayed.

感谢您提出的建议.

这是PlayerFile域类

Here is the PlayerFile domain class

class PlayerFile {
    String playersJson
    Date dateCreated
    Date lastUpdated

    static belongsTo = [owner: User]

    static constraints = {
        playersJson sqlType: 'text', nullable: false, widget: 'textarea'
        owner nullable: false, editable: false
    }
}

推荐答案

您可能不想覆盖 get 方法.您可能想要一种新的查询方法...

You probably don't want to override the get method. You probably want a new query method...

@Service(PlayerFile)
interface PlayerFileService {
    PlayerFile get(Serializable id)
    List<PlayerFile> list(Map args)
    Long count()
    void delete(Serializable id)
    PlayerFile save(PlayerFile playerFile)


    List<PlayerFile> findByOwner(User owner)
}

如果不需要 get 方法用于其他目的,请将其删除.

If you don't need the get method for other purposes, delete it.

编辑

解决以下评论:

我将上面显示的findByOwner()的代码放在哪里?不可能是添加到抽象界面中.

Where do I put the code for findByOwner() shown above? It can't be added to an abstract interface.

我不同意.它绝对可以在抽象界面中.您可以将 PlayerFileService 转换为 abstract class ,并将 findByOwner 设置为具体方法,但不必这样做.它可以是接口中的抽象方法,这在GORM数据服务中是很常见的.

I don't agree with that. It can definitely be in an abstract interface. You could turn PlayerFileService into an abstract class and make findByOwner a concrete method, but you do not have to. It can be an abstract method in an interface, which is the common thing in a GORM Data Service like this.

请参见 https://github.com/jeffbrown/rock298query 上的项目.

https://github.com/jeffbrown/rock298query/blob/3eb713a5ac2692be1fd9666eb7e6b26c8c9a0921/grails-app/services/rock298query/PlayerFileService.groovy

package rock298query

import grails.gorm.services.Service

@Service(PlayerFile)
interface PlayerFileService {
    PlayerFile get(Serializable id)
    List<PlayerFile> list(Map args)
    Long count()
    void delete(Serializable id)
    PlayerFile save(PlayerFile playerFile)

    List<PlayerFile> findByOwner(User owner)
}

https://github.com/jeffbrown/rock298query/blob/3eb713a5ac2692be1fd9666eb7e6b26c8c9a0921/grails-app/init/rock298query/BootStrap.groovy

package rock298query

class BootStrap {

    UserService userService
    PlayerFileService playerFileService

    def init = { servletContext ->
        def jeff = userService.save('Jeff')
        def jake = userService.save('Jake')

        jeff.addToFiles(new PlayerFile(playersJson: '{"title":"Jeff File 1"'))
        jeff.addToFiles(new PlayerFile(playersJson: '{"title":"Jeff File 2"'))
        jeff.addToFiles(new PlayerFile(playersJson: '{"title":"Jeff File 3"'))

        userService.save jeff

        jake.addToFiles(new PlayerFile(playersJson: '{"title":"Jake File 1"'))
        jake.addToFiles(new PlayerFile(playersJson: '{"title":"Jake File 2"'))
        jake.addToFiles(new PlayerFile(playersJson: '{"title":"Jake File 3"'))

        userService.save jake

    }
    def destroy = {
    }
}

https://github.com/jeffbrown/rock298query/blob/3eb713a5ac2692be1fd9666eb7e6b26c8c9a0921/grails-app/controllers/rock298query/DemoController.groovy

package rock298query

class DemoController {

    PlayerFileService playerFileService
    UserService userService

    def jakeFiles() {
        def jake = userService.find('Jake')

        def files = playerFileService.findByOwner(jake)

        render files*.playersJson
    }

    def jeffFiles() {
        def jeff = userService.find('Jeff')

        def files = playerFileService.findByOwner(jeff)

        render files*.playersJson
    }
}

一切似乎正常.

~ $ curl  http://localhost:8080/demo/jakeFiles
['{"title":"Jake File 1"', '{"title":"Jake File 2"', '{"title":"Jake File 3"']
~ $ 
~ $ curl  http://localhost:8080/demo/jeffFiles
['{"title":"Jeff File 1"', '{"title":"Jeff File 2"', '{"title":"Jeff File 3"']

我希望能帮上忙.

这篇关于Grails:如何覆盖生成的服务方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆