如果用户在 grails 中被删除,如何在 update() 中设置当前数据
How to set a current data in update() if the user is deleted in grails
删除用户时,我必须在'Date deletedDate'中设置当前数据。为此,我必须在 Update() 方法中执行此操作:如果删除了人员,请将 deletedDate 设置为当前日期。
我知道要设置我必须做的当前日期:
deletedDate = new Date(System.currentTimeMillis() - 30000))
但不确定如何在更新内部执行此操作,或者最好创建一个服务 class 来做。如果我必须创建一个新服务,我怎么知道用户何时被删除?
在此先致谢
域:
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)
}
}
尝试以下方法(在类似情况下对我有用):
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)
}
}
删除用户时,我必须在'Date deletedDate'中设置当前数据。为此,我必须在 Update() 方法中执行此操作:如果删除了人员,请将 deletedDate 设置为当前日期。
我知道要设置我必须做的当前日期:
deletedDate = new Date(System.currentTimeMillis() - 30000))
但不确定如何在更新内部执行此操作,或者最好创建一个服务 class 来做。如果我必须创建一个新服务,我怎么知道用户何时被删除?
在此先致谢
域:
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)
}
}
尝试以下方法(在类似情况下对我有用):
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)
}
}