public ActionResult Update(PhotosPO photos)
        {
            //Defaults redirect to index of photos controller passing albumId.
            ActionResult oResult = RedirectToAction("Index", "Photos", new { albumId = photos.AlbumId });

            if (ModelState.IsValid)
            {
                try
                {
                    //Passing photo object and photo location to use in stored procedure.
                    dataAccess.UpdatePhoto(PhotosMapper.MapPoToDO(photos, photos.PhotoLocation));
                    TempData["Message"] = "Photo successfully updated.";
                }
                catch (Exception ex)
                {
                    //Logs exception using exceptionLog class.
                    exceptionLog.ExceptionLog("Critical", ex.Message, "PhotosController", "Update", ex.StackTrace);
                    oResult = View(new { albumId = photos.AlbumId });
                }
            }
            else
            {
                //returns the albumId to the view.
                oResult = View(new { albumId = photos.AlbumId });
            }

            return(oResult);
        }
        public ActionResult Update(long photoId)
        {
            //Getting photo by photo Id and storing in a presentation object.
            PhotosPO mappedPhoto = PhotosMapper.MapDoToPO(dataAccess.ViewPhotoByPhotoId(photoId));

            //Puts Album name and Id into viewbag as a list to use for a dropdown list in view.
            ViewBag.DropDown = new List <SelectListItem>();
            List <AlbumDO> dataObjects = albumData.ReadAlbum();

            try
            {
                foreach (AlbumDO item in dataObjects)
                {
                    //Filling a SelectListItem with with all AlbumName and AlbumId properties.
                    ViewBag.DropDown.Add(new SelectListItem()
                    {
                        Text = item.AlbumName, Value = item.AlbumId.ToString()
                    });
                }
            }
            catch (Exception ex)
            {
                //Logs exception using exceptionLog class.
                exceptionLog.ExceptionLog("Critical", ex.Message, "PhotosController", "Update", ex.StackTrace);
            }

            return(View(mappedPhoto));
        }
示例#3
0
        /// <summary>
        /// Mapping a List from the  Data Layer to the Presentation layer.
        /// </summary>
        /// <param name="from">List from the Data Layer</param>
        /// <returns></returns>
        public static List <PhotosPO> MapDoToPO(List <PhotosDO> from)
        {
            List <PhotosPO> to = new List <PhotosPO>();

            try
            {
                foreach (PhotosDO item in from)
                {
                    PhotosPO mappedItem = MapDoToPO(item);
                    to.Add(mappedItem);
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            return(to);
        }
示例#4
0
        public static PhotosDO MapPoToDO(PhotosPO from, string filePath)
        {
            PhotosDO to = new PhotosDO();

            try
            {
                to.PhotoId       = from.PhotoId;
                to.AlbumId       = from.AlbumId;
                to.PhotoLocation = filePath;
                to.PhotoName     = from.PhotoName;
                to.PhotoDate     = from.PhotoDate;
                to.Description   = from.Description;
            }
            catch (Exception e)
            {
                throw e;
            }
            return(to);
        }
示例#5
0
        public static PhotosPO MapDoToPO(PhotosDO from)
        {
            PhotosPO to = new PhotosPO();

            try
            {
                to.PhotoId       = from.PhotoId;
                to.AlbumId       = from.AlbumId;
                to.PhotoLocation = from.PhotoLocation;
                to.PhotoName     = from.PhotoName;
                to.PhotoDate     = from.PhotoDate;
                to.Description   = from.Description;
            }
            catch (Exception e)
            {
                throw e;
            }
            return(to);
        }
        public ActionResult UploadPhoto(HttpPostedFileBase uploadedPhoto, PhotosPO photo)
        {
            //Defaults redirect to index of album controller.
            ActionResult oResult = RedirectToAction("Index", "Album");

            if (ModelState.IsValid)
            {
                try
                {
                    //Gets filepath
                    List <FileInfo> files = Directory.GetFiles("/").Select(path => new FileInfo(path)).ToList();

                    //Creates a unique id for naming save files to prevent overriding, saves file in
                    //userPhotos folder of current directory.
                    string newName      = Guid.NewGuid().ToString() + uploadedPhoto.FileName.Remove(0, uploadedPhoto.FileName.IndexOf('.'));
                    string pathToSaveTo = Path.Combine(Server.MapPath("/userPhotos/"), newName);

                    //Uploads photo to userPhotos folder in current directory.
                    uploadedPhoto.SaveAs(pathToSaveTo);
                    pathToSaveTo = $"~/userPhotos/{newName}";

                    //Adds photo to table using a stored procedure and properties gathered from user input.
                    dataAccess.CreatePhoto(PhotosMapper.MapPoToDO(photo, pathToSaveTo));

                    //Lets user know that upload was successful.
                    TempData["Message"] = "Photo upload successful.";
                }
                catch (Exception ex)
                {
                    //Logs exception using exceptionLog class.
                    exceptionLog.ExceptionLog("Critical", ex.Message, "PhotosController", "UploadPhoto", ex.StackTrace);
                    TempData["Error"] = "Oops there was a problem uploading your photo, please try again.";
                }
            }
            else
            {
                //Instanciating a new list of selectlistitem to fill dropdown.
                ViewBag.DropDown = new List <SelectListItem>();
                try
                {
                    //Stores Album name and Id in viewbag as a list to use for a dropdown list in view.
                    List <AlbumDO> dataObjects = albumData.ReadAlbum();
                    foreach (AlbumDO item in dataObjects)
                    {
                        ViewBag.DropDown.Add(new SelectListItem()
                        {
                            Text = item.AlbumName, Value = item.AlbumId.ToString()
                        });
                    }
                }
                catch (Exception e)
                {
                    //Logs exception using exceptionLog class.
                    exceptionLog.ExceptionLog("Critical", e.Message, "PhotosController", "UploadPhoto", e.StackTrace);
                }

                //Modelstate wasn't valid, returning photo to view.
                oResult = View(photo);
            }

            return(oResult);
        }