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 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; } }