public async Task <IActionResult> PutSpecies([FromRoute] string id, [FromBody] Species species)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != species.Name)
            {
                return(BadRequest());
            }

            ctx.Entry(species).State = EntityState.Modified;

            try
            {
                await ctx.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!SpeciesExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }
            return(NoContent());
        }
        public async Task <IActionResult> PutColor([FromRoute] string id, [FromBody] Color color)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            ApplicationUser user = await GetCurrentUserAsync();

            var searchRole = ctx.UserRoles.Where(e => e.UserId == user.Id && e.RoleId == "Admin");

            if (searchRole == null)
            {
                return(Unauthorized());
            }

            if (id != color.Name)
            {
                return(BadRequest());
            }

            ctx.Entry(color).State = EntityState.Modified;

            try
            {
                await ctx.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ColorExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }
            return(NoContent());
        }
Exemple #3
0
        public async Task <IActionResult> PutBreed([FromRoute] string id, [FromBody] Breed breed)
        {
            ApplicationUser user = await GetCurrentUserAsync();

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (await userManager.IsInRoleAsync(user, "Admin"))
            {
                return(Unauthorized());
            }

            if (id != breed.Name)
            {
                return(BadRequest());
            }

            ctx.Entry(breed).State = EntityState.Modified;

            try
            {
                await ctx.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!BreedExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }
            return(NoContent());
        }
        public async Task <IActionResult> DeleteUsers([FromRoute] string username)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            ApplicationUser uti = await _context.User.SingleOrDefaultAsync(m => m.UserName == username);

            if (uti == null)
            {
                return(NotFound());
            }
            var role = await _userManager.GetRolesAsync(uti);

            var animalList = _context.Animal.Where(ani => ani.IdUser == uti.Id).ToList();

            foreach (Animal animal in animalList)
            {
                List <Announcement> announcements = await _context.Announcement.Where(anounc => anounc.IdAnimal == animal.Id).ToListAsync();

                foreach (Announcement announc in announcements)
                {
                    _context.Announcement.Remove(announc);
                    _context.SaveChanges();
                }
                _context.Animal.Remove(animal);
                _context.SaveChanges();
            }
            await _userManager.RemoveFromRolesAsync(uti, role);

            _context.User.Remove(uti);
            await _context.SaveChangesAsync();

            return(Ok());
        }