Grails:如何覆盖生成的服务方法?

时间:2019-06-30 13:51:39

标签: grails

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

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

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

感谢您提出的建议。

这是PlayerFile域类

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
    }
}

1 个答案:

答案 0 :(得分:1)

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

@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方法用于其他目的,请将其删除。

编辑

解决以下评论:

  

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

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

查看位于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"']

我希望有帮助。