Open Closed

Delete action and returned type #164


0
cicciottino created

what is the best practice about the response to confirm the deletion?

i saw in the taskever live esample:

taskever/src/Taskever.Application/Tasks/TaskAppService.cs

public DeleteTaskOutput DeleteTask(DeleteTaskInput input) 
         { 
             var task = _taskRepository.FirstOrDefault(input.Id); 
             if (task == null) 
             { 
                 throw new Exception("Can not found the task!"); 
             } 
 
 
             var currentUser = _userRepository.Load(AbpUser.CurrentUserId.Value); 
             if (!_taskPolicy.CanDeleteTask(currentUser, task)) 
             { 
                 throw new UserFriendlyException("You can not delete this task!"); 
             } 
 
 
             _taskRepository.Delete(task); 
 
 
             return new DeleteTaskOutput();  //<------------is mapping missing????? or is automatic?
         }

id the "DeleteTaskOutput" not mapped?, the json output is always "zero"

{"success":true,"result":{"id":0},"error":null,"unAuthorizedRequest":false}

and anyway this contains only the entity's id, is it better to serve the entire entity to output or the only id is enough?(is there a best practice for the Delete operation?)

Then how can i remove the deleted entity from the angular collection?(in the case i return the only id, or the entire entity in the json)

taskService.deleteTask({
                    id: document.id
                }).success(function(value) {
                    
                    var index = vm.task.indexOf(value.id);  //<-- it doesn't work, does it expect the entire entity instead of the "id"?
                    vm.tasks.splice(index, 1);
                    
                });

thanks a lot


2 Answer(s)
  • 0
    cicciottino created

    i did so, but i don't like

    in the application layer

    public DeleteDocumentOutput DeleteDocument(DeleteDocumentInput input)
            { 
                var document = _documentRepository.FirstOrDefault(input.Id); 
                if (document == null) 
                 { 
                    throw new Exception("Can not found the document!"); 
                 } 
    
     
                _documentRepository.Delete(document);
    
                return new DeleteDocumentOutput 
                {
                    Id = document.Id   //<----
                }; 
            }
    

    in the angular controller

    documentService.deleteDocument({
                        id: document.id
                    }).success(function(value) {
                        
    
                        vm.documents.forEach(function (element, index, array) {
                            if (element.id === value.id) {
                                vm.documents.splice(index,1);
                            }
                        });
    
                        vm.refreshDocuments();
    		});
    

    could i do it better?

  • 0
    hikalkan created
    Support Team

    Hi,

    Don't mind taskever too much since it's an old project, ABP is changed much after it.

    So, just return void from DeleteDocument document. You don't need to Id in the client side, it's already in your hands! Also, don't use for and if, simply use libraries like underscore.js to easily find and remove it.