Ejemplo n.º 1
0
        private void removeGodFathers(int id)
        {
            var godfathers = _context.AnimalUsers.Where(e => e.AnimalFK == id);

            foreach (AnimalUsers godkey in godfathers.ToList())
            {
                _context.Remove(godkey);
                _context.SaveChanges();
            }
        }
Ejemplo n.º 2
0
        public Shared.Animal DeleteAnimal(int shelterId, int animalId)
        {
            var pickedAnimal = _context.Animals
                               .FirstOrDefault(x => x.Id == animalId && x.ShelterId == shelterId);

            _context.Remove(pickedAnimal);
            _context.SaveChanges();

            return(pickedAnimal);
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> Edit(int id, [Bind("RoleID,RoleName")] Roles roles)
        {
            if (!GetAuthorization(2, 'u'))
            {
                return(NotFound());
            }
            ViewBag.Permission = getPermissions();
            if (id != roles.RoleID)
            {
                return(NotFound());
            }
            if (!checkValues(roles))
            {
                setViewBags(id);
                return(View(roles));
            }
            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(roles);
                    var componenets = _context.Components.ToList();
                    var roleRules   = _context.RoleAuthorization.Where(e => e.RoleFK == id);

                    foreach (RoleAuthorization roleAutho in roleRules.ToList())
                    {
                        _context.Remove(roleAutho);
                        _context.SaveChanges();
                    }

                    foreach (var comp in componenets)
                    {
                        string   selectedOptions     = Request.Form[comp.Name].ToString();
                        string[] selectedOptionsList = selectedOptions.Split(',');

                        RoleAuthorization rAutho = new RoleAuthorization
                        {
                            ComponentFK = comp.ComponentID,
                            RoleFK      = roles.RoleID,
                            Create      = false,
                            Read        = false,
                            Update      = false,
                            Delete      = false
                        };

                        if (selectedOptionsList[0] != "")
                        {
                            foreach (var val in selectedOptionsList)
                            {
                                int crudID = Int32.Parse(val);
                                switch (crudID)
                                {
                                case 1:
                                    rAutho.Create = true;
                                    break;

                                case 2:
                                    rAutho.Read = true;
                                    break;

                                case 3:
                                    rAutho.Update = true;
                                    break;

                                case 4:
                                    rAutho.Delete = true;
                                    break;

                                default:
                                    break;
                                }
                            }
                        }
                        if (rAutho.Delete || rAutho.Create || rAutho.Update)
                        {
                            rAutho.Read = true;
                        }
                        _context.Add(rAutho);
                        _context.SaveChanges();
                    }

                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!RolesExists(roles.RoleID))
                    {
                        System.Diagnostics.Debug.WriteLine("*************************");
                        System.Diagnostics.Debug.WriteLine("Caught Exception");
                        System.Diagnostics.Debug.WriteLine("*************************");
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                TempData["Message"] = "Permissão editada com sucesso!";
                return(RedirectToAction(nameof(Index)));
            }
            setViewBags(id);
            return(View(roles));
        }