コード例 #1
0
        public async Task <IActionResult> PutMovie(long id, Movie movie)
        {
            if (id != movie.Id)
            {
                return(BadRequest());
            }

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

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

            return(NoContent());
        }
コード例 #2
0
        public async Task <IActionResult> PutMovie(long id, Movie movie)
        {
            if (id != movie.Id)
            {
                return(BadRequest());
            }

            if (movie.Comments != null)
            {
                // update or delete existing comments
                var existingComments = _context.Comments.Where(a => a.Movie.Id == movie.Id);
                foreach (var c in existingComments)
                {
                    var newValue = movie.Comments.FirstOrDefault(a => a.Id == c.Id);
                    if (newValue != null)
                    {
                        newValue.Movie                 = movie;
                        _context.Entry(c).State        = EntityState.Detached;
                        _context.Entry(newValue).State = EntityState.Modified;
                    }
                    else
                    {
                        _context.Comments.Remove(c);
                    }
                }

                // create new comments
                foreach (var c in movie.Comments.Where(a => a.Id == 0))
                {
                    _context.Comments.Add(c);
                }
            }

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

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

            return(NoContent());
        }
コード例 #3
0
ファイル: Commands.cs プロジェクト: iamkoch/NancyMovie
        public void SaveMovie(MoviesDbContext context, Movie movie)
        {
            if (movie.Id <= 0)
            {
                context.Entry(movie).State = EntityState.Added;
            }
            else
            {
                context.Entry(movie).State = EntityState.Modified;
            }

            context.SaveChanges();
        }
コード例 #4
0
        public void Update(T item, Func <T, bool> findByIDPredecate)
        {
            var local = Context.Set <T>()
                        .Local
                        .FirstOrDefault(findByIDPredecate); // (f => f.ID == item.ID);

            if (local != null)
            {
                Context.Entry(local).State = EntityState.Detached;
            }

            Context.Entry(item).State = EntityState.Modified;
            Context.SaveChanges();
        }
コード例 #5
0
        public void DeleteShouldDeleteUser()
        {
            var options = new DbContextOptionsBuilder <MoviesDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(DeleteShouldDeleteUser))
                          .Options;

            using (var context = new MoviesDbContext(options))
            {
                var userService = new UserService(context, config, registerValidator, roleValidator);

                var newUser = new RegisterPostModel
                {
                    Email     = "*****@*****.**",
                    FirstName = "alina",
                    LastName  = "demian",
                    Password  = "******",
                    Username  = "******"
                };

                userService.Register(newUser);

                User addedUser = context.Users.Last();

                context.Entry(addedUser).State = EntityState.Detached;

                //var addedUser = context.Users.Where(u => u.Username == "alina3").FirstOrDefault();

                userService.DeleteUser(addedUser.Id);

                int users = userService.GetAll().Count();

                Assert.Zero(users);
            }
        }
コード例 #6
0
        public async Task <IActionResult> PutComment(int id, Comment comment)
        {
            if (id != comment.Id)
            {
                return(BadRequest());
            }

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

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

            return(NoContent());
        }
コード例 #7
0
        public void UpsertShouldUpdateARoleOrAddANewOne()
        {
            var options = new DbContextOptionsBuilder <MoviesDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(UpsertShouldUpdateARoleOrAddANewOne))
                          .Options;

            using (var context = new MoviesDbContext(options))
            {
                var roleService = new RoleService(context);

                roleService.Create(roleAdmin);

                var addedRole = context.Roles.Last();

                int id = addedRole.Id;

                Role newRole = new Role
                {
                    Name        = "Admiiiin",
                    Description = "admiiin"
                };

                context.Entry(addedRole).State = EntityState.Detached;

                var updatedRole = roleService.Upsert(id, newRole);

                var lastRole = context.Roles.Last();

                Assert.AreEqual(lastRole.Name, newRole.Name);
            }
        }
コード例 #8
0
        public IHttpActionResult PutDirector(int id, Director director)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != director.Id)
            {
                return(BadRequest());
            }

            db.Entry(director).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!DirectorExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    return(BadRequest());
                    // throw; -> gledamo da uvek izbegnemo ovakvu praksu, time sto cemo samo staviti bad request umesto da mu prosledimo ex
                }
            }

            return(Ok(director));
        }
コード例 #9
0
        public async Task <IActionResult> PutDirector(int id, Director director)
        {
            if (id != director.Id)
            {
                return(BadRequest());
            }

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

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!DirectorExists(id))
                {
                    return(NotFound("director not found"));
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
コード例 #10
0
        public IHttpActionResult Put(int id, Movie movie)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != movie.Id)
            {
                return(BadRequest());
            }

            db.Entry(movie).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!MovieExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    return(BadRequest());
                }
            }

            return(Ok(movie));
        }
コード例 #11
0
        public IHttpActionResult PutClient(int id, Client client)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != client.Id)
            {
                return(BadRequest());
            }

            db.Entry(client).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ClientExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
コード例 #12
0
 public ActionResult Edit([Bind(Include = "Id,Title,Director,Studio,StudioAddress,Year,LeadMaleActorId,LeadFemaleActorId")] Movie movie)
 {
     if (ModelState.IsValid)
     {
         db.Entry(movie).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(movie));
 }
コード例 #13
0
 public ActionResult Edit([Bind(Include = "Id,City,Street")] Address address)
 {
     if (ModelState.IsValid)
     {
         db.Entry(address).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(address));
 }
コード例 #14
0
 public ActionResult Edit([Bind(Include = "Id,Name,Age")] Actor actor)
 {
     if (ModelState.IsValid)
     {
         db.Entry(actor).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(actor));
 }
コード例 #15
0
 public ActionResult Edit(Movie movie)
 {
     if (ModelState.IsValid)
     {
         db.Entry(movie).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(movie));
 }
コード例 #16
0
 public ActionResult Edit([Bind(Include = "GenreId,GenreName")] Genre genre)
 {
     if (ModelState.IsValid)
     {
         db.Entry(genre).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(genre));
 }
コード例 #17
0
 public ActionResult Edit([Bind(Include = "Id,Title,Year,Director,LeadingMaleRole,LeadingMaleActorAge,LeadingFemaleRole,LeadingFemaleActorAge")] Movie movie)
 {
     if (ModelState.IsValid)
     {
         db.Entry(movie).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(movie));
 }
コード例 #18
0
 public ActionResult Edit([Bind(Include = "Id,Name,Address,Age,MovieCard,Telephone,IsSubscribed")] Client client)
 {
     if (ModelState.IsValid)
     {
         db.Entry(client).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(client));
 }
コード例 #19
0
ファイル: MoviesController.cs プロジェクト: Monika35/NovoRepo
 public ActionResult Edit([Bind(Include = "id,Name,Rating,DownloadURL,ImgURL")] Movie movie)
 {
     if (ModelState.IsValid)
     {
         db.Entry(movie).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(movie));
 }
コード例 #20
0
 public ActionResult Edit([Bind(Include = "FilmId,FilmName,Duration,DirectorName,Rating,AgeRating,GenreId")] Film film)
 {
     if (ModelState.IsValid)
     {
         db.Entry(film).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "GenreName", film.GenreId);
     return(View(film));
 }
コード例 #21
0
 public ActionResult Edit([Bind(Include = "Id,Name,AddressId")] Studio studio)
 {
     if (ModelState.IsValid)
     {
         db.Entry(studio).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     ViewBag.AddressId = new SelectList(db.Addresses, "Id", "City", studio.AddressId);
     return(View(studio));
 }
コード例 #22
0
 public ActionResult Edit([Bind(Include = "SeriesId,SeriesName,NumberOfSeasons,Rating,AgeRating,GenreId")] Series series)
 {
     if (ModelState.IsValid)
     {
         db.Entry(series).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "GenreName", series.GenreId);
     return(View(series));
 }
コード例 #23
0
 public ActionResult Edit([Bind(Include = "Id,Name,Age,StudioId,AddressId")] Person person)
 {
     if (ModelState.IsValid)
     {
         db.Entry(person).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     ViewBag.AddressId = new SelectList(db.Addresses, "Id", "City", person.AddressId);
     ViewBag.StudioId  = new SelectList(db.Studios, "Id", "Name", person.StudioId);
     return(View(person));
 }
コード例 #24
0
 public ActionResult Edit([Bind(Include = "Id,Date,Title,DirectorId,LeadingMaleRoleId,LeadingFemaleRoleId")] Movie movie)
 {
     if (ModelState.IsValid)
     {
         db.Entry(movie).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     ViewBag.DirectorId          = new SelectList(db.People, "Id", "Name", movie.DirectorId);
     ViewBag.LeadingFemaleRoleId = new SelectList(db.People, "Id", "Name", movie.LeadingFemaleRoleId);
     ViewBag.LeadingMaleRoleId   = new SelectList(db.People, "Id", "Name", movie.LeadingMaleRoleId);
     return(View(movie));
 }
コード例 #25
0
 public ActionResult Edit(Genre genre)
 {
     if (ModelState.IsValid)
     {
         using (var database = new MoviesDbContext())
         {
             database.Entry(genre).State = System.Data.Entity.EntityState.Modified;
             database.SaveChanges();
             TempData["Success"] = "Жанрът е редактиран успешно.";
             return(RedirectToAction("Index"));
         }
     }
     TempData["Danger"] = "Некоректни данни. Моля, опитайте отново.";
     return(View(genre));
 }
コード例 #26
0
        public IHttpActionResult Put([FromODataUri] int key, Person person)
        {
            if (!this.ModelState.IsValid)
            {
                return(BadRequest(this.ModelState));
            }

            var currentPerson = this.context.People.Find(key);

            if (currentPerson == null)
            {
                return(NotFound());
            }

            // person.PersonId = key;
            person.PersonId = currentPerson.PersonId;
            context.Entry(currentPerson).CurrentValues.SetValues(person);
            this.context.SaveChanges();

            return(StatusCode(HttpStatusCode.NoContent));
        }
コード例 #27
0
        public void UpsertShouldUpdateMovie()
        {
            var options = new DbContextOptionsBuilder <MoviesDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(UpsertShouldUpdateMovie))
                          .Options;

            using (var context = new MoviesDbContext(options))
            {
                var movieService = new MovieService(context);
                var userService  = new UserService(context, config, registerValidator, roleValidator);

                userService.Register(user);

                var addedUser = context.Users.Last();

                movieService.Create(movie, addedUser);

                var addedMovie = context.Movies.Last();

                context.Entry(addedMovie).State = EntityState.Detached;

                int id = addedMovie.Id;

                Movie movieToBeUpdated = new Movie
                {
                    Title       = "newMovie",
                    Description = "description",
                    Duration    = 120,
                    Year        = 2018,
                    Director    = "Alina",
                    Date        = DateTime.Now,
                    Rating      = 9,
                    Watched     = "no"
                };

                Movie updatedMovie = movieService.Upsert(id, movieToBeUpdated);

                Assert.AreEqual(updatedMovie.Title, "newMovie");
            }
        }
コード例 #28
0
        public void UpdateUserShouldUpdateUserDetails()
        {
            var options = new DbContextOptionsBuilder <MoviesDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(UpdateUserShouldUpdateUserDetails))
                          .Options;

            using (var context = new MoviesDbContext(options))
            {
                var userService = new UserService(context, config, registerValidator, roleValidator);

                userService.Register(user);

                var addedUser = context.Users.Last();

                int id = addedUser.Id;

                //int id = (from user in context.Users orderby user.Id descending select user.Id).First();
                //string username = (from user in context.Users orderby user.Id descending select user.Username).First();

                Assert.AreEqual(addedUser.Username, user.Username);

                User newUserDetails = new User
                {
                    FirstName = "alina2",
                    LastName  = "demian",
                    Username  = "******",
                    Email     = "*****@*****.**",
                    Password  = "******"
                };

                context.Entry(addedUser).State = EntityState.Detached;

                userService.UpdateUser(id, newUserDetails);

                string newUsername = (from user in context.Users orderby user.Id descending select user.Username).First();

                Assert.AreEqual(newUsername, newUserDetails.Username);
            }
        }
コード例 #29
0
        public ActionResult Edit(string id, EditUserViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                using (var database = new MoviesDbContext())
                {
                    // Get user from database
                    var user = database.Users.FirstOrDefault(u => u.Id == id);

                    // Check if user axist
                    if (user == null)
                    {
                        return(HttpNotFound());
                    }

                    // If password field is not empty, change password
                    if (!string.IsNullOrEmpty(viewModel.Password))
                    {
                        var hasher       = new PasswordHasher();
                        var passwordHash = hasher.HashPassword(viewModel.Password);
                        user.PasswordHash = passwordHash;
                    }

                    // Set user properties
                    user.Email    = viewModel.User.Email;
                    user.FullName = viewModel.User.FullName;
                    user.UserName = viewModel.User.Email;
                    this.SetUserRoles(viewModel, user, database);

                    // Save changes
                    database.Entry(user).State = EntityState.Modified;
                    database.SaveChanges();

                    return(RedirectToAction("List"));
                }
            }
            return(View(viewModel));
        }
コード例 #30
0
ファイル: MoviesServiceTests.cs プロジェクト: AnaDomide/Lab7
        public void UpsertShouldChangeTheFildValues()
        {
            var options = new DbContextOptionsBuilder <MoviesDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(UpsertShouldChangeTheFildValues))
                          .EnableSensitiveDataLogging()
                          .Options;

            using (var context = new MoviesDbContext(options))
            {
                var movieService = new MoviesService(context);
                var original     = movieService.Create(new ExamenNet.ViewModels.MoviePostModel
                {
                    Title       = "Original",
                    Director    = "dir1",
                    DateAdded   = new DateTime(),
                    Duration    = 100,
                    Description = "asdvadfbdbsb",
                    Genre       = "Comedy",
                    ReleaseYear = 2000,
                    Rating      = 3,
                    Watched     = 0
                }, null);


                var movie = new ExamenNet.ViewModels.MoviePostModel
                {
                    Title = "upsert"
                };

                context.Entry(original).State = EntityState.Detached;

                var result = movieService.Upsert(1, movie);

                Assert.IsNotNull(original);
                Assert.AreEqual("upsert", result.Title);
            }
        }
コード例 #31
0
        public MovieDetails Put(int id, [FromBody] MovieDetails updatedMovie)
        {
            try
            {
                var movieDetails = context.Movies.SingleOrDefault(m => m.Id == updatedMovie.Id);

                if (movieDetails != null)
                {
                    movieDetails.IsReleased = updatedMovie.IsReleased;
                    movieDetails.IsWatched  = updatedMovie.IsWatched;

                    context.Entry(movieDetails).State = Microsoft.Data.Entity.EntityState.Modified;

                    context.SaveChanges();
                    return(movieDetails);
                }

                return(null);
            }
            catch
            {
                return(null);
            }
        }
コード例 #32
0
ファイル: Commands.cs プロジェクト: iamkoch/NancyMovie
 public void DeleteMovie(MoviesDbContext context, Movie movie)
 {
     context.Entry(movie).State = EntityState.Deleted;
     context.SaveChanges();
 }