Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • deep C 20 posts 210 karma points
    Dec 15, 2021 @ 07:58
    deep C
    0

    Using notificationService in backend application's angular delete controller

    I have created a backend application (section) to handle communications and I need to be able to notify the user if a delete failed for some reason. While the notificationService is available in the edit controller, I can't seem to get it to work in the delete controller. Unfortunately the samples I could find were limited.

    Here is my code:

    Angular edit controller

    angular.module("umbraco").controller("CellularCarrier.EditController",
    function ($scope, $routeParams, cellularcarrierResource, notificationsService, navigationService) {
    
        $scope.loaded = false;
    
        if ($routeParams.id == -1) {
            $scope.cellularcarrier = {};
            $scope.loaded = true;
        } else {
            //get a cellularcarrier id -> service
            cellularcarrierResource.getById($routeParams.id).then(function (response) {
                $scope.cellularcarrier = response.data;
                $scope.loaded = true;
            });
        }
    
        $scope.save = function (cellularcarrier) {
            cellularcarrierResource.save(cellularcarrier).then(function (response) {
                $scope.cellularcarrier = response.data;
                $scope.cellularcarrierForm.$dirty = false;
                navigationService.syncTree({ tree: 'cellularcarrier', path: [-1, -1], forceReload: true });
                notificationsService.success("Success", cellularcarrier.Name + " has been saved");
            });
        };
    });
    

    Angular delete controller (where I would like to use notificationService)

    angular.module("umbraco").controller("CellularCarrier.DeleteController",
    function ($scope, cellularcarrierResource, navigationService, treeService) {
      $scope.delete = function (id) {
        cellularcarrierResource.deleteByID(id).then(function (response) {
          var i = parseInt(response.data);
          if (i > 0)
            treeService.removeNode($scope.currentNode);
          //else if (i == 0)
          //  notificationsService.error("Failed", "Cellular carrier can not be found.");
          //else if (i == -1)
          //  notificationsService.error("Failed", "Cellular carrier is referened and can't be deleted.");
    
          navigationService.hideNavigation();
        });
    
      };
      $scope.cancelDelete = function () {
        navigationService.hideNavigation();
      };
    });
    

    Cellular Carrier API Controller

    [PluginController("MailApp")]
    public class CellularCarrierAPIController : UmbracoAuthorizedJsonController {
      public dbStRose db = new dbStRose();  // Entity Framework
    
      public IEnumerable<CellularCarrier> GetAll() {
        return db.CellularCarriers.OrderBy(x => x.Name);
      }
    
      public CellularCarrier GetByID(int id) {
        return new CellularCarrier(db.CellularCarriers.Find(id));
      }
    
      public CellularCarrier PostSave(CellularCarrier carrier) {
    
        if (carrier.ID > 0) {
          CellularCarrier c = db.CellularCarriers.Find(carrier.ID);
          db.Entry(c).CurrentValues.SetValues(carrier);
        } else {
          db.CellularCarriers.Add(carrier);
        }
    
        db.SaveChanges();
    
        return carrier;
      }
    
      public int DeleteByID(int id) {
        CellularCarrier m = db.CellularCarriers.Find(id);
        if (m == null)
          return 0;
    
        if (m.Users.Count == 0) {
          db.CellularCarriers.Remove(m);
          db.SaveChanges();
          return id;
        }
    
        return -1;
      }
    
      protected override void Dispose(bool disposing) {
        if (disposing) {
          db.Dispose();
        }
        base.Dispose(disposing);
      }
    }
    

    Angular Resource File

    angular.module("umbraco.resources")
    .factory("cellularcarrierResource", function ($http) {
        return {
            getById: function (id) {
                return $http.get("backoffice/MailApp/CellularCarrierAPI/GetByID?id=" + id);
            },
            save: function (cellularcarrier) {
                return $http.post("backoffice/MailApp/CellularCarrierAPI/PostSave", angular.toJson(cellularcarrier));
            },
            deleteByID: function (id) {
                return $http.delete("backoffice/MailApp/CellularCarrierAPI/DeleteByID?id=" + id);
            }
        };
    });
    

    Any help you can give me would me much appreciated.

    Thanks

  • deep C 20 posts 210 karma points
    Dec 18, 2021 @ 21:31
    deep C
    100

    So the answer was to use the same function prototype as the edit controller. This made the treeService unavailable, so removenode could not be called. Instead, I used the navigationService.syncTree to refresh the subtree.

    Here is my working code:

    Angular delete controller

    angular.module("umbraco").controller("CellularCarrier.DeleteController",
      function ($scope, $routeParams, cellularcarrierResource, notificationsService, navigationService) {
      $scope.delete = function (id) {
        cellularcarrierResource.deleteByID(id).then(function (response) {
          var i = parseInt(response.data);
          if (i > 0) {
            navigationService.syncTree({ tree: 'cellularcarrier', path: [-1, -1], forceReload: true });
            notificationsService.success("Success", "Cellular carrier has been deleted");
          } else if (i == 0)
            notificationsService.error("Failed", "Cellular carrier not found.");
          else if (i == -1)
            notificationsService.error("Failed", "Cellular carrier is referened by ward members and can't be deleted.");
    
          navigationService.hideNavigation();
        });
    
      };
      $scope.cancelDelete = function () {
        navigationService.hideNavigation();
      };
    });
    
  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies