コード例 #1
0
        public async Task <IActionResult> Edit(int id, [Bind("PlaylistId,Public,PlaylistImage,PlaylistName")] PlaylistModel playlistModel)
        {
            if (id != playlistModel.PlaylistId)
            {
                return(NotFound());
            }

            PlaylistModel orig = _context.Playlists.AsNoTracking().Where(p => p.PlaylistId == id).FirstOrDefault();

            playlistModel.PlaylistImage = orig.PlaylistImage;

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(playlistModel);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!PlaylistModelExists(playlistModel.PlaylistId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(playlistModel));
        }
コード例 #2
0
        public async Task <IActionResult> Edit(int id, [Bind("SongId,SongName,SongImagePath,SongPath,Genre,ReleaseDate")] SongModel songModel)
        {
            if (id != songModel.SongId)
            {
                return(NotFound());
            }

            SongModel origSongModel = _context.Songs.AsNoTracking().Where(s => songModel.SongId == id).FirstOrDefault();

            songModel.SongImagePath = origSongModel.SongImagePath;
            songModel.SongPath      = origSongModel.SongPath;

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(songModel);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!SongModelExists(songModel.SongId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Display)));
            }
            return(View(songModel));
        }
コード例 #3
0
        public async Task <IActionResult> Edit(int id, [Bind("ArtistName,FirstName,LastName,UserId,Username,Email,Password")] ArtistModel artistModel, IFormFile artistImage)
        {
            if (!HttpContext.Session.GetInt32(SessionConsts.UserId).HasValue)
            {
                return(Unauthorized());
            }

            if (id != artistModel.UserId)
            {
                return(NotFound());
            }

            // keeping original image if artist doesn't change it
            ArtistModel originalArtist = _context.Artists.Where(a => a.UserId == id).FirstOrDefault();

            if (artistImage == null)
            {
                artistModel.ArtistImage = originalArtist.ArtistImage;
            }

            if (originalArtist.Username != artistModel.Username)
            {
                string error = CheckIfUserNameExists(artistModel.Username);
                if (error != "")
                {
                    ViewData["Error"] = error;
                    return(View());
                }
            }
            if (originalArtist.Email != artistModel.Email)
            {
                string error = CheckIfEmailExists(artistModel.Email);
                if (error != "")
                {
                    ViewData["Error"] = error;
                    return(View());
                }
            }
            if (ModelState.IsValid)
            {
                if (artistImage != null)
                {
                    artistModel.ArtistImage = FileHelper.SaveFile(artistImage, "images", artistImage.FileName);
                }

                // To prevent two instances with the same id tracked
                originalArtist.ArtistName  = artistModel.ArtistName;
                originalArtist.FirstName   = artistModel.FirstName;
                originalArtist.LastName    = artistModel.LastName;
                originalArtist.ArtistImage = artistModel.ArtistImage;
                originalArtist.Username    = artistModel.Username;
                originalArtist.Email       = artistModel.Email;
                // originalArtist.FirstName = artistModel.FirstName;

                try
                {
                    _context.Update(originalArtist);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ArtistModelExists(artistModel.UserId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }

                var userId = HttpContext.Session.GetInt32(SessionConsts.UserId).Value;
                var user   = await _context.Users.SingleOrDefaultAsync(u => u.UserId == userId);

                if (user.Username == "admin")
                {
                    return(RedirectToAction("Index", "UserModels"));
                }
                else
                {
                    return(RedirectToAction("Details", "ArtistModels"));
                }
            }
            return(View(artistModel));
        }
コード例 #4
0
        public async Task <IActionResult> Edit(int id, [Bind("UserId,Username,FirstName,LastName,Email,Password")] UserModel userModel)
        {
            if (!HttpContext.Session.GetInt32(SessionConsts.UserId).HasValue)
            {
                return(Unauthorized());
            }

            if (id != userModel.UserId)
            {
                return(NotFound());
            }
            UserModel user = _context.Users.SingleOrDefault(u => u.UserId == id);

            if (userModel.Username != user.Username)
            {
                string error = CheckIfUserNameExists(userModel.Username);
                if (error != "")
                {
                    ViewData["Error"] = error;
                    return(View());
                }
            }
            if (userModel.Email != user.Email)
            {
                string error = CheckIfEmailExists(userModel.Email);
                if (error != "")
                {
                    ViewData["Error"] = error;
                    return(View());
                }
            }
            if (ModelState.IsValid)
            {
                // To prevent two instances with the same id tracked
                user.FirstName = userModel.FirstName;
                user.LastName  = userModel.LastName;
                user.Username  = userModel.Username;
                user.Email     = userModel.Email;
                try
                {
                    _context.Update(user);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!UserModelExists(userModel.UserId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }

                var userId = HttpContext.Session.GetInt32(SessionConsts.UserId).Value;
                var user1  = await _context.Users.SingleOrDefaultAsync(u => u.UserId == userId);

                if (user1.Username == "admin")
                {
                    return(RedirectToAction("Index", "UserModels"));
                }
                else
                {
                    return(RedirectToAction("Details", "UserModels"));
                }
            }
            return(View(userModel));
        }