Esempio n. 1
0
        /// <summary>Adds a movie to the database.</summary>
        /// <param name="movie">The movie to add.</param>
        public void Add(Movie movie)
        {
            //Validate
            if (movie == null)
            {
                throw new ArgumentNullException("movie");
            }
            ObjectValidator.Validate(movie);

            //if (movie == null) return;

            try
            {
                AddCore(movie);
            }
            catch (Exception e)
            {
                throw new Exception("Add failed", e);
            };
        }
Esempio n. 2
0
        public void Add(Movie movie)
        {
            // crashes a system just for education purpose
            // Validation
            if (movie == null)
            {
                throw new ArgumentNullException(nameof(movie));
            }
            ObjectValidator.Validate(movie);
            // TODO: Validate
            //if (movie == null)
            //   return;

            //if (movie == null) return;
            try
            {
                AddCore(movie);
            } catch (Exception e)
            {
                throw new Exception("Add failed", e); // the e is usefull debugging (chains the two exceptions togethor)
            };
        }
Esempio n. 3
0
        public void Update(int id, Movie newMovie)
        {
            if (id <= 0)
            {
                //return;
                throw new ArgumentOutOfRangeException(nameof(id), "Id must be > 0");
            }
            if (newMovie == null)
            {
                //return;
                throw new ArgumentNullException(nameof(newMovie));
            }
            // if (!String.IsNullOrEmpty (newMovie.Validate()))
            //var context = new ValidationContext (newMovie);
            //var results = newMovie.Validate (context);
            var results = ObjectValidator.TryValidateObject(newMovie);

            if (results.Count() > 0)
            {
                //return;
                throw new ValidationException(
                          results.FirstOrDefault().ErrorMessage);
            }

            var existing = GetByNameCore(newMovie.Title);

            if (existing != null && existing.Id != id)
            {
                throw new ArgumentException("Movie must be unique");
            }
            try
            {
                UpdateCore(id, newMovie);
            }   catch (IOException ex)
            {
                throw new Exception("An error occurred updating the movie", ex);
            };
            //UpdateCore (id, newMovie);
        }
Esempio n. 4
0
        public void Update(int id, Movie newMovie)
        {
            //Validate
            if (id <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(id),
                                                      "Id must be > 0.");
            }
            if (newMovie == null)
            {
                throw new ArgumentNullException(nameof(newMovie));
            }

            var results = ObjectValidator.TryValidateObject(newMovie);

            if (results.Count() > 0)
            {
                throw new ValidationException(
                          results.FirstOrDefault().ErrorMessage);
            }

            //Must be unique
            var existing = GetByNameCore(newMovie.Title);

            if (existing != null && existing.Id != id)
            {
                throw new ArgumentException("Movie must be unique.");
            }

            try
            {
                UpdateCore(id, newMovie);
            }
            catch (IOException ex)
            {
                throw new Exception("An error occurred updating the movie.", ex);
            }
        }