gpt4 book ai didi

grails - 如果在grails中删除了用户,如何在update()中设置当前数据

转载 作者:行者123 更新时间:2023-12-02 15:21:48 26 4
gpt4 key购买 nike

删除用户后,我必须在“删除日期”中设置当前数据。为此,我必须在Update()方法中执行此操作:如果删除了个人,请将DeletedDate设置为当前日期。

我知道要设置当前日期,我必须执行以下操作:

DeletedDate =新日期(System.currentTimeMillis()-30000))

但是不确定如何在更新内进行操作,或者最好是创建一个服务类来进行操作。如果必须创建新服务,如何知道何时删除用户?

先谢谢了

域:

class Person{
..
String nameID
Date deletedDate
}

Controller :
 def update(Long id, Long version) {
def personInstance = Person.get(id)
if (!personInstance) {
flash.message = message(code: 'default.not.found.message', args: [message(code: 'person.label', default: 'Person'), id])
redirect(action: "list")
return
}
if (version != null) {
if (personInstance.version > version) {
personInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
[message(code: 'person.label', default: 'Person')] as Object[],
"Another user has updated this Person while you were editing")
render(view: "edit", model: [personInstance: personInstance])
return
}
}
personInstance.properties = params
if (!personInstance.save(flush: true)) {
render(view: "edit", model: [personInstance: personInstance])
return
}
flash.message = message(code: 'default.updated.message', args: [message(code: 'person.label', default: 'Person'), personInstance.id])
redirect(action: "show", id: personInstance.id)
}


def delete(Long id) {
def personInstance = Person.get(id)
if (!personInstance) {
flash.message = message(code: 'default.not.found.message', args: [message(code: 'person.label', default: 'Person'), id])
redirect(action: "list")
return
}
try {
personInstance.delete(flush: true)
flash.message = message(code: 'default.deleted.message', args: [message(code: 'person.label', default: 'Person'), id])
redirect(action: "list")
}
catch (DataIntegrityViolationException e) {
flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'person.label', default: 'Person'), id])
redirect(action: "show", id: id)
}
}

最佳答案

尝试以下操作(在类似情况下对我有用):

def delete(Long id) {
def personInstance = Person.get(id)
if (!personInstance) {
flash.message = message(code: 'default.not.found.message', args: [message(code: 'person.label', default: 'Person'), id])
redirect(action: "list")
return
}
try {
// deleteDate should be saved as well
personInstance.deletedDate = new Date(System.currentTimeMillis() - 30000))
personInstance.delete(flush: true)
flash.message = message(code: 'default.deleted.message', args: [message(code: 'person.label', default: 'Person'), id])
redirect(action: "list")
}
catch (DataIntegrityViolationException e) {
flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'person.label', default: 'Person'), id])
redirect(action: "show", id: id)
}
}

关于grails - 如果在grails中删除了用户,如何在update()中设置当前数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34064786/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com