public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(HttpNotFound());
            }

            using (var db = new MusicStationDbContext())
            {
                var song = db.Songs
                           .Where(s => s.Id == id)
                           .Include(s => s.User)
                           .FirstOrDefault();

                if (!IsAuthorizedToChange(song))
                {
                    return(new HttpStatusCodeResult(HttpStatusCode.Forbidden));
                }

                if (song == null)
                {
                    return(HttpNotFound());
                }

                var model = new SongEditModel();

                model.Id      = song.Id;
                model.Artist  = song.Artist;
                model.Title   = song.Title;
                model.Details = song.Details;
                model.Genre   = song.Genre;

                return(View(model));
            }
        }
Пример #2
0
        public ActionResult Edit(int id, SongEditModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            if (model.SongId != id)
            {
                ModelState.AddModelError("", "ID Mismatch");
                return(View(model));
            }

            var service = CreateSongService();


            if (service.UpdateSong(model))
            {
                TempData["SaveResult"] = "Your Song was successfully updated!";
                return(RedirectToAction("Index"));
            }

            ModelState.AddModelError("", "Your Song could not be updated.");

            return(View(model));
        }
        public ActionResult Edit(SongEditModel model, HttpPostedFileBase image)
        {
            if (ModelState.IsValid)
            {
                using (var db = new MusicStationDbContext())
                {
                    var song = db.Songs
                               .Where(s => s.Id == model.Id)
                               .FirstOrDefault();

                    var allowedImageTypes = new[] { "image/jpg", "image/jpeg", "image/png" };

                    if (song == null)
                    {
                        return(HttpNotFound());
                    }

                    var originalImageUploadPath = Server.MapPath(song.ImagePath);

                    if (image == null || !allowedImageTypes.Contains(image.ContentType))
                    {
                        model.ImagePath = song.ImagePath;
                    }
                    else
                    {
                        var imagesPath           = "/Content/Images/";
                        var newImageName         = song.Id.ToString() + image.FileName;
                        var newImageUploadPath   = imagesPath + newImageName;
                        var newImagePhysicalPath = Server.MapPath(newImageUploadPath);
                        if (song.ImagePath != null)
                        {
                            System.IO.File.Delete(originalImageUploadPath);
                        }

                        image.SaveAs(newImagePhysicalPath);
                        model.ImagePath = newImageUploadPath;
                    }

                    song.Id        = model.Id;
                    song.Artist    = model.Artist;
                    song.Title     = model.Title;
                    song.Details   = model.Details;
                    song.Genre     = model.Genre;
                    song.ImagePath = model.ImagePath;

                    db.Entry(song).State = EntityState.Modified;
                    db.SaveChanges();

                    return(RedirectToAction("Details", new { id = song.Id }));
                }
            }
            return(View(model));
        }
Пример #4
0
        public async Task Update(string id, SongEditModel model)
        {
            Song song = this.context.Songs.Find(id);

            song.Name = model.Name;
            // song.Path = model.Path;
            song.CategoryId = model.CategoryId;
            song.Text       = model.Text;

            this.context.Songs.Update(song);
            await this.context.SaveChangesAsync();
        }
Пример #5
0
        public ActionResult Edit(int id)
        {
            var detailModel = CreateSongService().GetSongById(id);
            var editModel   =
                new SongEditModel
            {
                SongId  = detailModel.SongId,
                Title   = detailModel.Title,
                Content = detailModel.Content
            };

            return(View(editModel));
        }
Пример #6
0
        public ActionResult Edit(int id)
        {
            var service = CreateSongService();
            var detail  = service.GetSongById(id);
            var model   =
                new SongEditModel
            {
                SongId  = detail.SongId,
                Title   = detail.Title,
                Content = detail.Content,
                Link    = detail.Link
            };

            return(View(model));
        }
Пример #7
0
        public bool UpdateSong(SongEditModel model)
        {
            using (var ctx = new ApplicationDbContext())
            {
                var entity =
                    ctx
                    .Song
                    .Single(e => e.SongId == model.SongId && e.OwnerId == _userId);

                entity.Title       = model.Title;
                entity.Content     = model.Content;
                entity.ModifiedUtc = DateTimeOffset.UtcNow;

                return(ctx.SaveChanges() == 1);
            }
        }
Пример #8
0
        public IHttpActionResult Put(SongEditModel song)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var service = new SongService();

            if (!service.UpdateSong(song))
            {
                return(InternalServerError());
            }

            return(Ok());
        }
Пример #9
0
        public async Task <IActionResult> Put(string id, SongEditModel model)
        {
            var userId = this.userManager.GetUserId(this.User);

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

            if (this.songService.IsOwn(id, userId))
            {
                return(this.StatusCode(StatusCodes.Status406NotAcceptable));
            }

            await this.songService.Update(id, model);

            return(this.Ok());
        }
Пример #10
0
        public ActionResult Edit(int id, SongEditModel model)
        {
            if (model.SongId != id)
            {
                ModelState.AddModelError("", "Nice try!");
                model.SongId = id;
                return(View(model));
            }

            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            if (!CreateSongService().UpdateSong(model))
            {
                ModelState.AddModelError("", "Unable to update song");
                return(View(model));
            }

            TempData["SaveResult"] = "Your song was saved";

            return(RedirectToAction("Index"));
        }
Пример #11
0
 public bool EditSong(SongEditModel model)
 {
     throw new NotImplementedException();
 }