public static PropertyAdvSummaryModel ToSummaryModel(this PropertyAdv entity) { PropertyAdvSummaryModel model = new PropertyAdvSummaryModel(); model.Id = entity.Id; model.Name = entity.Name; model.Price = entity.Price; model.Type = entity.PropertyType.Name; //model.Area = $"{entity.Area.Value} {entity.Area.Unit.Name}"; model.AdvAge = $"{DateTime.Today.Subtract(entity.StartOn).Days} Days"; if (entity.Images?.Count > 0) { model.MainImageUrl = model.AlternateImageUrl = entity.Images.ToList()[0].ImageUrl; if (entity.Images.Count > 1) { model.AlternateImageUrl = entity.Images.ToList()[1].ImageUrl; } } else { model.MainImageUrl = model.AlternateImageUrl = "~/images/p_advs/none.png"; } model.Location = $"{entity.Neighborhood.Name} {entity.Neighborhood.City.Name}"; return(model); }
public static PropertyAdvModel ToModel(this PropertyAdv entity) { PropertyAdvModel model = new PropertyAdvModel(); model.Id = entity.Id; model.Name = entity.Name; model.Price = entity.Price; model.Description = entity.Description; model.PostedOn = entity.PostedOn; model.ActiveTill = entity.ActiveTill; model.Neighborhood = entity.Neighborhood.ToModel(); model.PostedBy = entity.PostedBy.ToModel(); model.Area = entity.Area.ToModel(); //model.Status = entity.Status.ToModel() //model.Type = entity.AdvType.ToEntity(); if (entity.Images != null) { foreach (var img in entity.Images) { model.Images.Add(Convert.ToBase64String(img.Content)); } } return(model); }
public IActionResult Update(CreatePropertyAdvModel model) { PropertyHubContext db = new PropertyHubContext(); PropertyAdv entity = model.ToEntity(); string temp = Request.Form["features"]; if (!string.IsNullOrWhiteSpace(temp)) { foreach (var fId in temp.Split(',')) { entity.Features.Add(new PropertyAdvsFeatures { Advertisement = entity, Feature = new PropertyFeature { Id = Convert.ToInt32(fId) } }); } } int counter = 0; foreach (IFormFile file in Request.Form.Files) { if (file.Length > 0) { string ext = file.FileName.Substring(file.FileName.LastIndexOf(".")); string fileName = $"{DateTime.Now.Ticks}_{++counter}{ext}"; string url = $"~/images/p_advs/{fileName}"; string path = hostingEnv.WebRootPath + $@"\images\p_advs\{fileName}"; using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read)) { file.CopyTo(fs); } var find = db.PropertyImage.Where(x => x.Priority.Equals(counter)).FirstOrDefault(); if (find != null && !string.IsNullOrWhiteSpace(find.ImageUrl)) { find.ImageUrl = url; db.SaveChanges(); } else { var findII = db.PropertyImage.OrderByDescending(x => x.Priority).FirstOrDefault(); int count = ++findII.Priority; entity.Images.Add(new PropertyImage { ImageUrl = url, Priority = counter, DateCreated = DateTime.Now }); } } } new PropertyAdvsHandler().UpdatePropertyAdv(entity, model.Id); return(RedirectToAction("manage")); }
public static PropertyAdv ToEntity(this PropertyAdvModel model) { PropertyAdv entity = new PropertyAdv(); entity.Id = model.Id; entity.Name = model.Name; entity.Price = model.Price; entity.Description = model.Description; entity.PostedOn = model.PostedOn; entity.ActiveTill = model.ActiveTill; entity.Neighborhood = model.Neighborhood.ToEntity(); entity.PostedBy = model.PostedBy.ToEntity(); entity.Status = model.Status.ToEntity(); entity.Type = model.AdvType.ToEntity(); entity.Area = model.Area.ToEntity(); return(entity); }
public PropertyAdv UpdatePropertyAdv(PropertyAdv entity, int Id) { using (PropertyHubContext context = new PropertyHubContext()) { PropertyAdv found = (from adv in context.PropertyAdvs where adv.Id == Id select adv).First(); if (!(string.IsNullOrWhiteSpace(entity.Name))) { found.Id = entity.Id; found.Name = entity.Name; found.Price = entity.Price; //found.CityId = entity.Neighborhood.City.Id; //found.AreaUnitId = entity.Area.Unit.Id; //found.AdvType = entity.AdvType.Id; //found.PropertyTypeId = entity.PropertyType.Id; //found.BlockId = entity.Neighborhood.Id; //found.SchemeId = entity.Neighborhood.Parent.Id; //found.StartDate = entity.StartOn; //found.Area = entity.Area; found.Beds = entity.Beds; found.Baths = entity.Baths; } foreach (var item in entity.Images) { found.Images.Add(item); } context.Entry(entity.AdvStatus).State = EntityState.Unchanged; context.Entry(entity.AdvType).State = EntityState.Unchanged; context.Entry(entity.Neighborhood).State = EntityState.Unchanged; context.Entry(entity.PostedBy).State = EntityState.Unchanged; context.Entry(entity.PropertyType).State = EntityState.Unchanged; context.Entry(entity.Area.Unit).State = EntityState.Unchanged; // context.Entry(entity.Images).State = EntityState.Unchanged; foreach (var f in entity.Features) { context.Entry(f.Feature).State = EntityState.Unchanged; } //context.Add(entity); context.SaveChanges(); } return(entity); }
public IActionResult Post(PropertyAdvModel model) { model.Neighborhood = new NeighborhoodModel { Id = Convert.ToInt32(Request.Form["neighborhoodsddl"]) }; model.AdvType = new AdvTypeModel { Id = Convert.ToInt32(Request.Form["advtype"]) }; model.PostedBy = new UserModel { Id = (HttpContext.Session.Get <UserModel>(WebUtil.CURRENT_USER).Id) }; // this will currently login user model.PostedOn = DateTime.Now; //current date time model.Status = new AdvStatusModel { Id = 1 }; //pending PropertyAdv entity = model.ToEntity(); //images if (Request.Form.Files != null && Request.Form.Files.Count > 0) { string[] phcaptions = Request.Form["phcaption"].ToArray(); for (int i = 0; i < Request.Form.Files.Count; i++) { IFormFile file = Request.Form.Files[i]; if (!string.IsNullOrWhiteSpace(file.FileName) && file.Length != 0) { AdvImage advImage = new AdvImage(); advImage.DisplayRank = i + 1; advImage.Caption = phcaptions[i]; using (MemoryStream ms = new MemoryStream()) { file.CopyTo(ms); advImage.Content = ms.ToArray(); } entity.Images.Add(advImage); } } } new PropertyHubHandler().AddAdvertizement(entity); return(RedirectToAction("index", "home")); }
public PropertyAdv AddPropertyAdv(PropertyAdv entity) { using (PropertyHubContext context = new PropertyHubContext()) { context.Entry(entity.AdvStatus).State = EntityState.Unchanged; context.Entry(entity.AdvType).State = EntityState.Unchanged; context.Entry(entity.Neighborhood).State = EntityState.Unchanged; context.Entry(entity.PostedBy).State = EntityState.Unchanged; context.Entry(entity.PropertyType).State = EntityState.Unchanged; context.Entry(entity.Area.Unit).State = EntityState.Unchanged; foreach (var f in entity.Features) { context.Entry(f.Feature).State = EntityState.Unchanged; } context.Add(entity); context.SaveChanges(); } return(entity); }
public IActionResult Create(CreatePropertyAdvModel model) { PropertyAdv entity = model.ToEntity(); string temp = Request.Form["features"]; if (!string.IsNullOrWhiteSpace(temp)) { foreach (var fId in temp.Split(',')) { entity.Features.Add(new PropertyAdvsFeatures { Advertisement = entity, Feature = new PropertyFeature { Id = Convert.ToInt32(fId) } }); } } int counter = 0; foreach (IFormFile file in Request.Form.Files) { if (file.Length > 0) { string ext = file.FileName.Substring(file.FileName.LastIndexOf(".")); string fileName = $"{DateTime.Now.Ticks}_{++counter}{ext}"; string url = $"~/images/p_advs/{fileName}"; string path = hostingEnv.WebRootPath + $@"\images\p_advs\{fileName}"; using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read)) { file.CopyTo(fs); } entity.Images.Add(new PropertyImage { Priority = counter, ImageUrl = url, DateCreated = DateTime.Now }); } } new PropertyAdvsHandler().AddPropertyAdv(entity); return(RedirectToAction("manage")); }
public static CreatePropertyAdvModel ToModel(this PropertyAdv Entity) { CreatePropertyAdvModel Model = new CreatePropertyAdvModel(); Model.Id = Entity.Id; Model.Name = Entity.Name; Model.Price = Entity.Price; Model.CityId = Entity.Neighborhood.City.Id; Model.AreaUnitId = Entity.Area.Unit.Id; Model.AdvType = Entity.AdvType.Id; Model.PropertyTypeId = Entity.PropertyType.Id; Model.BlockId = Entity.Neighborhood.Id; Model.SchemeId = Entity.Neighborhood.Parent.Id; Model.StartDate = Entity.StartOn; Model.Area = Entity.Area.Value; Model.Beds = Entity.Beds; Model.Baths = Entity.Baths; //entity. //Model.BlockId = new Neighborhood { Id = Entity.Neighborhood.Id }; //Model.AdvStatus = new AdvStatus { Id = 1 }; // by default adv is in pending status //Model.AdvType = new AdvType { Id = Entity.AdvType }; //Model.Area = new PropertyArea { Value = Entity.Area, Unit = new AreaUnit { Id = Entity.AreaUnitId } }; //entity.Images if (Entity.Images?.Count > 0) { Model.MainImageUrl = Model.AlternateImageUrl = Entity.Images.ToList()[0].ImageUrl; if (Entity.Images.Count > 1) { Model.AlternateImageUrl = Entity.Images.ToList()[1].ImageUrl; } } else { Model.MainImageUrl = Model.AlternateImageUrl = "~/images/p_advs/none.png"; } return(Model); }
public static PropertyAdv ToEntity(this CreatePropertyAdvModel model) { PropertyAdv entity = new PropertyAdv(); entity.Id = model.Id; entity.Name = model.Name; entity.Price = model.Price; entity.Beds = model.Beds; entity.Baths = model.Baths; entity.Neighborhood = new Neighborhood { Id = model.BlockId }; entity.AdvStatus = new AdvStatus { Id = 1 }; // by default adv is in pending status entity.AdvType = new AdvType { Id = model.AdvType }; entity.Area = new PropertyArea { Value = model.Area, Unit = new AreaUnit { Id = model.AreaUnitId } }; //entity.Images entity.IsActive = true; // by default adv is active entity.PostedBy = new User { Id = 3 }; // currently login user will be assigned here entity.PostedOn = DateTime.Today; entity.PropertyType = new PropertyType { Id = model.AdvType }; entity.StartOn = model.StartDate; return(entity); }