public IHttpActionResult PutPhoto(int id, Photo photo)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != photo.PhotoId)
            {
                return BadRequest();
            }

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

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

            return StatusCode(HttpStatusCode.NoContent);
        }
        private string HandleFileUpload(ref Photo photo)
        {
            string filePath = @"~\Images\defaultAccomodationPhoto.jpg";
            
            if (Request.Files.Count > 0)
            {
                HttpPostedFileBase file = Request.Files[0];
                if (file.ContentLength > 0 && _allowedTypes.Contains(file.ContentType))
                {
                    try
                    {
                        using (var bitmap = new Bitmap(file.InputStream))
                        {
                        }
                    }
                    catch
                    {
                        ModelState.AddModelError("PhotoUrl", "The file type is not supported");
                        return "none";
                    }

                    string fileName = Path.GetFileName(file.FileName);
                    filePath = Path.Combine(@"~\Images\Photos", fileName);
                    string fullPath = Path.Combine(Server.MapPath(@"~\Images\Photos"), fileName);
                    file.SaveAs(fullPath);

                    Account account = new Account(
                                  "bitbooking",
                                  "131162311141994",
                                  "yqy4VSrjuxaGeP8BUMgHwTozpfw");

                    Cloudinary cloudinary = new Cloudinary(account);

                    var uploadParams = new ImageUploadParams()
                    {
                        File = new FileDescription(fullPath)
                    };
                    var uploadResult = cloudinary.Upload(uploadParams);

                    FileInfo uploadedFileToServer = new FileInfo(fullPath);
                    uploadedFileToServer.Delete();

                    return uploadResult.Uri.AbsolutePath; //new photo URL from Cloudinary
                }
                else
                {
                    if (file.ContentLength > 0 && !_allowedTypes.Contains(file.ContentType))
                    {
                        ModelState.AddModelError("PhotoUrl", "The file type is not supported");
                        return "none";
                    }

                }
            }
            //photo.PhotoUrl = filePath;
            return filePath;
        }
        public IHttpActionResult PostPhoto(Photo photo)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.Images.Add(photo);
            db.SaveChanges();

            return CreatedAtRoute("DefaultApi", new { id = photo.PhotoId }, photo);
        }
        // GET: Rooms
        public ActionResult Index()
        {
            try
            {
                string userid = User.Identity.GetUserId();

                int accID = db.Users.FirstOrDefault(x => x.Id == userid).AccomodationId;

           
            List<Room> rooms_temp = new List<Room>();

            rooms_temp = db.Rooms.Include(x => x.RoomType).Where(x => x.AccomodationId==accID).ToList();

            List<Photo> photoList = new List<Photo>();

            photoList = db.Images.Where(x => x.AccomodationId == accID).ToList();

            List<Photo> newPhotoList = new List<Photo>();
         

            foreach(var photo in photoList)
            {
                var photoX = new Photo
                {
                    AccomodationId = photo.AccomodationId,
                    FacilityId = photo.FacilityId,
                    PhotoId = photo.PhotoId,
                    PhotoUrl = photo.PhotoUrl,
                    Priority = photo.Priority,
                    RoomTypeId = photo.RoomTypeId
                };
                newPhotoList.Add(photoX);
            }


            foreach(var room in rooms_temp)
            {
                room.RoomType.ListOfPhotos = newPhotoList.Where(x => x.RoomTypeId == room.RoomTypeId).ToList();
            }


            return Json(rooms_temp.ToList(), JsonRequestBehavior.AllowGet);
            }
            catch (Exception e)
            {

                //throw new UnauthorizedAccessException(e.Message);
                return Json(null);
            };
        }
 public ActionResult TypeAdd(int? id)
 {
     Photo test = new Photo { RoomTypeId=(int)id, Priority=4};
     ViewBag.RoomTypeId = new SelectList(db.RoomTypes, "RoomTypeId", "RoomTypeName");
     return View(test);
 }
            public void PhotosEdit()
            {

                
                PhotosController controller = new PhotosController();
                Photo photoInfo = new Photo();
                photoInfo.PhotoUrl = "http://res.cloudinary.com/bitbooking/image/upload/bo_2px_solid_rgb:202020,r_5/v1441716099/2_barfns.jpg";
                photoInfo.AccomodationId = 1;
                photoInfo.PhotoId = 1;
                photoInfo.Priority = 1;
                photoInfo.RoomTypeId = 1;
                var result = controller.Edit(photoInfo);
                Assert.IsInstanceOfType(result, typeof(RedirectToRouteResult));
                

            }
        public void UploadFile(string facilityId, string AccomodationId)
        {
            if (HttpContext.Current.Request.Files.AllKeys.Any())
            {
                if (HttpContext.Current.Request.Files.AllKeys.Any())
                {
                    var httpPostedFile = HttpContext.Current.Request.Files["file"];
                    bool folderExists = Directory.Exists(HttpContext.Current.Server.MapPath("~/UploadedDocuments"));
                    if (!folderExists)
                        Directory.CreateDirectory(HttpContext.Current.Server.MapPath("~/UploadedDocuments"));
                    var fileSavePath = Path.Combine(HttpContext.Current.Server.MapPath("~/UploadedDocuments"),
                                                    httpPostedFile.FileName);
                    httpPostedFile.SaveAs(fileSavePath);

                    if (File.Exists(fileSavePath))
                    {
                        Account account = new Account("bitbooking", "131162311141994", "yqy4VSrjuxaGeP8BUMgHwTozpfw");
                        Cloudinary cloudinary = new Cloudinary(account);

                        var uploadParams = new ImageUploadParams()  //upload to cloudinary
                        {
                            File = new FileDescription(fileSavePath),
                            Transformation = new Transformation().Crop("fill").Width(720).Height(480)

                        };

                        var uploadResult = cloudinary.Upload(uploadParams);
                        System.IO.File.Delete(fileSavePath);

                        try
                        {
                            int accomodationID = 0;
                            int newFacilityId = 0;
                            bool parseSuccedAcc = Int32.TryParse(AccomodationId, out accomodationID);
                            bool parseSuccedFacility = Int32.TryParse(facilityId, out newFacilityId);
                            if (parseSuccedAcc == true & parseSuccedFacility == true)
                            {
                                string photoUrl = "http://res.cloudinary.com" + uploadResult.Uri.AbsolutePath;
                                Photo newPhoto = new Photo();
                                newPhoto.AccomodationId = accomodationID;
                                newPhoto.PhotoUrl = photoUrl;
                                newPhoto.RoomTypeId = 1;
                                newPhoto.FacilityId = newFacilityId;
                                newPhoto.Priority = 99;
                                db.Images.Add(newPhoto);
                                db.SaveChanges();

                                try
                                {
                                    var facility = db.AccomodationFacilities.Single(x => x.AccomodationFacilityId == newFacilityId);
                                    facility.PhotoUrl = photoUrl;
                                    db.Entry(facility).State = EntityState.Modified;
                                    db.SaveChanges();
                                }
                                catch(Exception e)
                                {

                                }
                            }
                        }
                        catch (Exception e)
                        {

                        }

                    }


                    //// http://www.codeproject.com/Tips/900200/SFTP-File-Upload-Using-ASP-NET-Web-API-and-Angular

                }
            }
        }
        public ActionResult AccomodationPhotos()
        {
            Photo test = new Photo { RoomTypeId=1, Priority = 2 };
            ViewBag.RoomTypeId = new SelectList(db.RoomTypes, "RoomTypeId", "RoomTypeName");
            //return View(test);
            return View(test);

        }