删除用户时,我必须在'Date deletedDate'中设置当前数据。为此,我必须在Update()方法中执行此操作:如果已删除人员,请将deletedDate设置为当前日期。
我知道要设置我必须做的当前日期:
deletedDate = new Date(System.currentTimeMillis() - 30000))
但不确定如何在更新内部执行此操作或者更好地创建要执行的服务类。如果我必须创建新服务,我怎么知道删除用户的时间?
先谢谢
域名:
class Person{
..
String nameID
Date deletedDate
}
控制器:
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)
}
}
答案 0 :(得分:1)
尝试以下内容(在类似的情况下,它适用于我):
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)
}
}