public HttpResponseMessage Save([FromBody]PropertyModel propertyModel)
        {
            try
            {
                ICollection<PropertyImage> propertyImages = new List<PropertyImage>();

                foreach(string base64Image in propertyModel.PropertyImages)
                {
                    PropertyImage propertyImage = new PropertyImage();
                    propertyImage.Image = System.Convert.FromBase64String(base64Image);
                    propertyImages.Add(propertyImage);
                }

                PropertyDetail propertyDetail = new PropertyDetail();
                propertyDetail.Title = propertyModel.Title.ToUpper();
                propertyDetail.Type = propertyModel.Type;
                propertyDetail.NumberOfBedrooms = propertyModel.NumberOfBedrooms;
                propertyDetail.Location = propertyModel.Location;
                propertyDetail.Price = propertyModel.Price;
                propertyDetail.Description = propertyModel.Description;
                propertyDetail.PropertyImages = propertyImages;
                propertyDetail.DateUploaded = System.DateTime.Now;
                propertyDetail.Status = StatusUtil.PropertyStatus.Available.ToString();

                bool result = PropertyPL.Save(propertyDetail);
                if (result)
                    return Request.CreateResponse(HttpStatusCode.OK, "Successful");
                else
                    return Request.CreateResponse(HttpStatusCode.BadRequest, "Failed");
            }
            catch (Exception ex)
            {
                var response = Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message);
                return response;
            }
        }
        public HttpResponseMessage UpdatePropertyAsSold([FromBody]PropertyModel propertyModel)
        {
            try
            {

                PropertyDetail propertyDetail = new PropertyDetail();
                propertyDetail.PropertyID = propertyModel.PropertyID;
                propertyDetail.Status = StatusUtil.PropertyStatus.Sold.ToString();

                bool result = PropertyPL.UpdatePropertyAsSold(propertyDetail);
                if (result)
                    return Request.CreateResponse(HttpStatusCode.OK, "Successful");
                else
                    return Request.CreateResponse(HttpStatusCode.BadRequest, "Failed");
            }
            catch (Exception ex)
            {
                var response = Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message);
                return response;
            }
        }
 public static bool Save(PropertyDetail propertyDetails)
 {
     try
     {
         using (var context = new PropertyDBEntities())
         {
             context.PropertyDetails.Add(propertyDetails);
             context.SaveChanges();
         }
         return true;
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
        public HttpResponseMessage Update([FromBody]PropertyModel propertyModel)
        {
            try
            {

                List<byte[]> newPropertyImages = new List<byte[]>();

                if (propertyModel.PropertyImages != null)
                {
                    foreach (string base64Image in propertyModel.PropertyImages)
                    {
                        byte[] propertyImage = System.Convert.FromBase64String(base64Image);
                        newPropertyImages.Add(propertyImage);
                    }
                }

                List<long> imageIDsToRemove = new List<long>();

                if (propertyModel.DeletedPropertyImages != null)
                {
                    foreach (string id in propertyModel.DeletedPropertyImages)
                    {
                        long imageID = System.Convert.ToInt64(id);
                        imageIDsToRemove.Add(imageID);
                    }
                }

                PropertyDetail propertyDetail = new PropertyDetail();
                propertyDetail.PropertyID = propertyModel.PropertyID;
                propertyDetail.Title = propertyModel.Title.ToUpper();
                propertyDetail.Type = propertyModel.Type;
                propertyDetail.NumberOfBedrooms = propertyModel.NumberOfBedrooms;
                propertyDetail.Location = propertyModel.Location;
                propertyDetail.Price = propertyModel.Price;
                propertyDetail.Description = propertyModel.Description;

                bool result = PropertyPL.Update(propertyDetail, imageIDsToRemove, newPropertyImages);
                if (result)
                    return Request.CreateResponse(HttpStatusCode.OK, "Successful");
                else
                    return Request.CreateResponse(HttpStatusCode.BadRequest, "Failed");
            }
            catch (Exception ex)
            {
                var response = Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message);
                return response;
            }
        }
        public static bool UpdatePropertyAsSold(PropertyDetail propertyDetails)
        {
            try
            {
                PropertyDetail existingPropertyDetails = new PropertyDetail();
                using (var context = new PropertyDBEntities())
                {
                    existingPropertyDetails = context.PropertyDetails.Include("PropertyImages")
                                    .Where(t => t.PropertyID == propertyDetails.PropertyID)
                                    .FirstOrDefault();
                }

                if (existingPropertyDetails != null)
                {
                    existingPropertyDetails.Status = propertyDetails.Status;

                    using (var context = new PropertyDBEntities())
                    {
                        context.Entry(existingPropertyDetails).State = EntityState.Modified;
                        context.SaveChanges();
                    }
                    return true;
                }
                else
                {
                    return false;
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public static bool Update(PropertyDetail propertyDetails, List<long> imageIDToRemove, List<byte[]> newImages)
        {
            try
            {
                PropertyDetail existingPropertyDetails = new PropertyDetail();
                using (var context = new PropertyDBEntities())
                {
                    existingPropertyDetails = context.PropertyDetails.Include("PropertyImages")
                                    .Where(t => t.PropertyID == propertyDetails.PropertyID)
                                    .FirstOrDefault();
                }

                if (existingPropertyDetails != null)
                {
                    existingPropertyDetails.Title = propertyDetails.Title;
                    existingPropertyDetails.Type = propertyDetails.Type;
                    existingPropertyDetails.NumberOfBedrooms = propertyDetails.NumberOfBedrooms;
                    existingPropertyDetails.Location = propertyDetails.Location;
                    existingPropertyDetails.Price = propertyDetails.Price;
                    existingPropertyDetails.Description = propertyDetails.Description;

                    using (var context = new PropertyDBEntities())
                    {
                        //Transaction block
                        using (var transaction = context.Database.BeginTransaction())
                        {
                            try
                            {
                                //Modifying just the property details
                                context.Entry(existingPropertyDetails).State = EntityState.Modified;
                                context.SaveChanges();

                                //Delete block for images that were removed from the property
                                IEnumerable<PropertyImage> existingImages = context.PropertyImages.Include("PropertyDetail")
                                                                .Where(t => imageIDToRemove.Contains(t.PropertyImageID))
                                                                .ToList();

                                if (existingImages != null && existingImages.ToList().Count != 0)
                                {
                                    context.PropertyImages.RemoveRange(existingImages);
                                    context.SaveChanges();
                                }

                                //Adding new images for the property
                                List<PropertyImage> newPropertyImages = new List<PropertyImage>();
                                foreach (byte[] newImage in newImages)
                                {
                                    PropertyImage newPropertyImage = new PropertyImage();
                                    newPropertyImage.PropertyID = propertyDetails.PropertyID;
                                    newPropertyImage.Image = newImage;

                                    newPropertyImages.Add(newPropertyImage);
                                }
                                if (newPropertyImages != null && newPropertyImages.Count != 0)
                                {
                                    context.PropertyImages.AddRange(newPropertyImages);
                                    context.SaveChanges();
                                }

                                //commit changes
                                transaction.Commit();
                            }
                            catch (Exception ex)
                            {
                                transaction.Rollback();
                                throw ex;
                            }
                        }

                    }
                    return true;
                }
                else
                {
                    return false;
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
 public static bool UpdatePropertyAsSold(PropertyDetail propertyDetails)
 {
     try
     {
         return PropertyDL.UpdatePropertyAsSold(propertyDetails);
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
 public static bool Update(PropertyDetail propertyDetails, List<long> imageIDToRemove, List<byte[]> newImages)
 {
     try
     {
         return PropertyDL.Update(propertyDetails, imageIDToRemove, newImages);
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
 public static bool Save(PropertyDetail propertyDetails)
 {
     try
     {
         return PropertyDL.Save(propertyDetails);
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }