Exemplo n.º 1
0
        public IActionResult Edit(ImageEdit model)
        {
            EditImageCommand command = new EditImageCommand();

            if (!ModelState.IsValid)
            {
                return(View(model));
            }
            try
            {
                command.image = new AlbumImage {
                    ImageAlt = model.ImageAlt, ImageName = model.ImageName, ImageUrl = model.ImageUrl, ImageId = model.ImageId
                };

                //each tag should have a corrisponding tag name but just incase lets check
                if (model.TagIDs != null && model.TagIDs.Length > 0 && model.TagNames != null && model.TagNames.Length > 0)
                {
                    if (model.TagNames.Length == model.TagIDs.Length)
                    {
                        command.image.ImageTags = new List <AlbumImageTag>();
                        for (int i = 0; i < model.TagIDs.Length; i++)
                        {
                            command.image.ImageTags.Add(new AlbumImageTag {
                                ImageId = model.ImageId, ImageTagId = model.TagIDs[i], Name = model.TagNames[i]
                            });
                        }
                    }
                    else
                    {
                        throw new ArgumentException("Tag Name and Tag Ids don't have the same amount");
                    }
                }

                if (model.ImageFile != null && model.ImageFile.Length > 0)
                {
                    using (var ms = new MemoryStream())
                    {
                        model.ImageFile.CopyTo(ms);
                        command.image.Image = ms.ToArray();
                    }
                }
                this.service.Update(command);

                if (command.Response != EditImageCommand.SUCCESS)
                {
                    throw new ArgumentException(EditImageCommand.ERROR, "Business Logic Error");
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return(RedirectToAction("Edit", new { id = model.ImageId, message = "Image Saved!" }));
        }
Exemplo n.º 2
0
 public void Update(EditImageCommand command)
 {
     try
     {
         repository.Update(Convert(command.image));
     }
     catch (Exception ex)
     {
         command.Response = String.Format(EditImageCommand.ERROR, ex.Message);
         return;
     }
     command.Response = EditImageCommand.SUCCESS;
 }