Exemple #1
0
        public async Task <IActionResult> PutMovie([FromRoute] int id, [FromBody] Movie movie)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            //If the body and route parameters doesnt match return Bad request.
            if (id != movie.MovieId)
            {
                return(BadRequest());
            }

            //Chaning the state to modified
            _context.Entry(movie).State = EntityState.Modified;

            try
            {
                //saving changes
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!MovieExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Exemple #2
0
        public async Task <IActionResult> PutList([FromRoute] int id, [FromBody] List list)
        {
            //Checking modelstate
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            //cheking if the id and listId is the same
            if (id != list.ListId)
            {
                return(BadRequest());
            }

            _context.Entry(list).State = EntityState.Modified;

            //Trying to save the changes
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ListExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }
            //Returning statuscode 204 for No content in response
            return(NoContent());
        }
Exemple #3
0
        public async Task <IActionResult> PutGenre([FromRoute] int id, [FromBody] Genre genre)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != genre.GenreId)
            {
                return(BadRequest());
            }

            _context.Entry(genre).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!GenreExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Exemple #4
0
        public async Task <IActionResult> PutListItem([FromRoute] int id, [FromBody] ListItem listItem)
        {
            //checking the state of the model
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            //If the id of the list item that is gonna be replaced isnt the same as the object, return a status code 400
            if (id != listItem.ListItemId)
            {
                return(BadRequest());
            }

            //Sets the state to modified
            _context.Entry(listItem).State = EntityState.Modified;

            //Tries to save changes
            try
            {
                await _context.SaveChangesAsync();
            }

            // checking an unexpected number of rows has been modified
            catch (DbUpdateConcurrencyException)
            {
                if (!ListItemExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Exemple #5
0
        public async Task <IActionResult> PutTokenValidator([FromRoute] int id, [FromBody] TokenValidator tokenValidator)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            //Checking if the id matched the object Id
            if (id != tokenValidator.TokenId)
            {
                return(BadRequest());
            }

            //Changing state
            _context.Entry(tokenValidator).State = EntityState.Modified;


            //Trying to save
            try
            {
                await _context.SaveChangesAsync();
            }
            //Catches unexpected rows affected
            catch (DbUpdateConcurrencyException)
            {
                if (!TokenValidatorExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            //Return No Content response 204
            return(NoContent());
        }
Exemple #6
0
        public async Task <IActionResult> PutUser([FromRoute] int id, [FromBody] User user)
        {
            //Checking if the model state is valid
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            //checking if Route And body id is matching.
            if (id != user.UserId)
            {
                return(BadRequest());
            }

            //Changing state to modified
            _context.Entry(user).State = EntityState.Modified;

            //Trying to save
            try
            {
                await _context.SaveChangesAsync();
            }
            //catching unexpected amount of rows are affected during save.
            catch (DbUpdateConcurrencyException)
            {
                if (!UserExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            //Return status code 204
            return(NoContent());
        }
Exemple #7
0
        public async Task <IActionResult> PutPerson([FromRoute] int id, [FromBody] Person person)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            //Checking if Route id and Body id is the same
            if (id != person.PersonId)
            {
                return(BadRequest());
            }

            //changing the state to modified
            _context.Entry(person).State = EntityState.Modified;

            //trying to save
            try
            {
                await _context.SaveChangesAsync();
            }
            //if unexpected rows have been changed
            catch (DbUpdateConcurrencyException)
            {
                if (!PersonExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }