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
}
}
最佳答案
您可能不想覆盖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
方法用于其他目的,请将其删除。编辑
解决以下评论:
我不同意。它绝对可以在抽象界面中。您可以将
PlayerFileService
转换为abstract
class
并将findByOwner
设置为具体方法,但是不必这样做。它可以是接口(interface)中的抽象方法,这在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"']
希望对您有所帮助。