Exemple #1
0
        public ActionResult DeleteBuilding(int? buildingId)
        {
            if (!buildingId.HasValue)
            {
                return new HttpStatusCodeResult(System.Net.HttpStatusCode.BadRequest);
            }

            using (var db = new Models.OnLeaveContext())
            {
                var building = db.UtilityBuildings
                    .Include(b => b.UtilityBuildingLocales)
                    .Include(b => b.UtilityBuildingPhotoDetails)
                    .Include(b => b.UtilityBuildingPhotoDetails.Select(pd => pd.Photo))
                    .Include(b => b.UtilityBuidingFacilityDetails)
                    .Include(b => b.Rooms)
                    .Include(b => b.Periods.Select(p => p.RoomAmounts))
                    .Include(b => b.Offers)
                    .FirstOrDefault(b => b.UtilityBuildingId == buildingId);
                System.Diagnostics.Debug.Assert(building != null, "building not found");

                if (building.UserId != User.Identity.GetUserId() && !User.IsInRole("Root"))
                {
                    Elmah.ErrorSignal.FromCurrentContext().Raise(new Exception("avoid to delete building, which doesn't belong to user"));                    
                    // avoid to delete building, which doesn't belong to user
                    return new HttpStatusCodeResult(System.Net.HttpStatusCode.Forbidden);                    
                }

                db.UtilityBuildingLocales.RemoveRange(building.UtilityBuildingLocales);                
                db.UtilityBuildingPhotoDetails.RemoveRange(building.UtilityBuildingPhotoDetails);
                db.Photos.RemoveRange(building.UtilityBuildingPhotoDetails.Select(pd => pd.Photo));
                db.UtilityBuidingFacilityDetails.RemoveRange(building.UtilityBuidingFacilityDetails);
                db.RoomAmounts.RemoveRange(building.Periods.SelectMany(p => p.RoomAmounts));
                db.Rooms.RemoveRange(building.Rooms);
                db.Periods.RemoveRange(building.Periods);
                db.Offers.RemoveRange(building.Offers);
                db.UtilityBuildings.Remove(building);

                db.SaveChanges();
            }

            return new HttpStatusCodeResult(System.Net.HttpStatusCode.OK);
        }
Exemple #2
0
        public ActionResult EditBuilding(UtilityBuildingModel model)
        {
            
            if (ModelState.IsValid)
            {
                using (var db = new Models.OnLeaveContext())
                {
                    var buildingDB = db.UtilityBuildings
                        .Include(b => b.Periods)
                        .Include(b => b.Offers)
                        .Include(b => b.UtilityBuildingPhotoDetails)
                        .Include(b => b.UtilityBuildingLocales)
                        .FirstOrDefault(b => b.UtilityBuildingId == model.Id);
                    if (buildingDB == null || buildingDB.UserId != User.Identity.GetUserId())
                    {
                        new HttpStatusCodeResult(System.Net.HttpStatusCode.NotFound);
                    }

                    buildingDB.UtilityBuildingTypeId = model.UtilityBuildingTypeId;                    
                    buildingDB.PhoneNumber = model.PhoneNumber;
                    buildingDB.CityId = model.CityId;
                    buildingDB.Rating = model.Rating;
                    buildingDB.Size = model.Size;

                    if (User.IsInRole("Root"))
                    {
                        buildingDB.SearchRating = model.SearchRating;
                    }

                    var buildingLocaleBG = buildingDB.UtilityBuildingLocales.FirstOrDefault(l => l.LocaleId == (int)LocaleTypes.BG);
                    if (buildingLocaleBG != null)
                    {
                        buildingLocaleBG.Name = model.Name;
                        buildingLocaleBG.Description = model.Description;
                        buildingLocaleBG.ContactPerson = model.ContactPerson;
                        buildingLocaleBG.Address = model.Address;
                    }
                    else
                    {
                        buildingDB.UtilityBuildingLocales.Add(new UtilityBuildingLocale
                        {
                            LocaleId = (int)LocaleTypes.BG,
                            Name = model.Name,
                            Description = model.Description,
                            ContactPerson = model.ContactPerson,
                            Address = model.Address
                        });
                    }

                    var buildingLocaleEN = buildingDB.UtilityBuildingLocales.FirstOrDefault(l => l.LocaleId == (int)LocaleTypes.EN);
                    if (buildingLocaleEN != null)
                    {
                         if (string.IsNullOrWhiteSpace(model.ContactPersonEN)
                        && string.IsNullOrWhiteSpace(model.NameEN)
                        && string.IsNullOrWhiteSpace(model.DescriptionEN)
                        && string.IsNullOrWhiteSpace(model.AddressEN))
                         {
                             // remove if no data                             
                             db.UtilityBuildingLocales.Remove(buildingLocaleEN);
                         }
                         else
                         {
                             buildingLocaleEN.Name = model.NameEN;
                             buildingLocaleEN.Description = model.DescriptionEN;
                             buildingLocaleEN.ContactPerson = model.ContactPersonEN;
                             buildingLocaleEN.Address = model.AddressEN;
                         }
                    }
                    else
                    {
                        if (!string.IsNullOrWhiteSpace(model.ContactPersonEN)
                       || !string.IsNullOrWhiteSpace(model.NameEN)
                       || !string.IsNullOrWhiteSpace(model.DescriptionEN)
                       || !string.IsNullOrWhiteSpace(model.Address))
                        {
                            buildingDB.UtilityBuildingLocales.Add(new UtilityBuildingLocale
                            {
                                LocaleId = (int)LocaleTypes.EN,
                                Name = model.NameEN,
                                Description = model.DescriptionEN,
                                ContactPerson = model.ContactPersonEN,
                                Address = model.AddressEN
                            });
                        }
                    }

                     // update facilities info
                    var facilitiesToRemove = buildingDB.UtilityBuidingFacilityDetails
                        .Where(fd => !model.Facilities.Any(f => f.FacilityTypeId == fd.UtilityBuildingFacilityTypeId && f.Selected));                    

                    db.UtilityBuidingFacilityDetails.RemoveRange(facilitiesToRemove);

                    model.Facilities.Where(f => f.Selected).ToList().ForEach(f =>
                    {
                        if (!buildingDB.UtilityBuidingFacilityDetails.Any(fd => fd.UtilityBuildingFacilityTypeId == f.FacilityTypeId))
                        {
                            buildingDB.UtilityBuidingFacilityDetails.Add(new UtilityBuidingFacilityDetail
                            {
                                UtilityBuildingFacilityTypeId = f.FacilityTypeId,
                                UtilityBuildingId = model.Id
                            });
                        }
                    });

                    // periods
                    model.Periods.Where(p => !p.StartDate.HasValue).ToList().ForEach(p => p.StartDate = DateTime.Now.Date);
                    model.Periods.Where(p => !p.EndDate.HasValue).ToList().ForEach(p => p.EndDate = DateTime.Now.Date);

                    var periodsToRemove = buildingDB.Periods
                        .Where(pDB => 
                            !model.Periods.Where(p => p.PeriodId != 0).Any(p => p.PeriodId == pDB.PeriodId && p.RoomAmounts.Any(a => a.Amount != 0M)))
                            .ToList();

                    var periodsToAdd = model.Periods.Where(p => p.PeriodId == 0 && p.RoomAmounts.Any(a => a.Amount != 0M))
                        .Select(p => new Period
                        {
                            FromDate = p.FromDate,
                            ToDate = p.ToDate,
                            RoomAmounts = p.RoomAmounts.Where(a => a.Amount != 0M).Select(a => new RoomAmount{
                                Amount = a.Amount,
                                RoomTypeId = a.RoomTypeId
                            }).ToList()
                        }).ToList();

                    db.RoomAmounts.RemoveRange(periodsToRemove.SelectMany(p => p.RoomAmounts));
                    db.Periods.RemoveRange(periodsToRemove);

                    buildingDB.Periods.ToList().ForEach(pDB =>
                    {
                        var period = model.Periods.First(p => p.PeriodId == pDB.PeriodId);
                        pDB.FromDate = period.FromDate;
                        pDB.ToDate = period.ToDate;

                        var amountsToDelete = pDB.RoomAmounts
                            .Where(aDB => !period.RoomAmounts.Any(a => a.RoomAmountId == aDB.RoomAmountId && a.Amount != 0M))
                            .ToList();

                        db.RoomAmounts.RemoveRange(amountsToDelete);

                        pDB.RoomAmounts
                            .Where(aDB => period.RoomAmounts.Any(a => a.RoomTypeId == aDB.RoomTypeId))
                            .ToList()
                            .ForEach(aDB => { aDB.Amount = period.RoomAmounts.First(a => a.RoomTypeId == aDB.RoomTypeId).Amount; });

                        period.RoomAmounts
                            .Where(a => a.RoomAmountId == 0 && a.Amount != 0M)
                            .ToList()
                            .ForEach(a => pDB.RoomAmounts.Add(new RoomAmount
                            {
                                PeriodId = pDB.PeriodId,
                                RoomTypeId = a.RoomTypeId,
                                Amount = a.Amount
                            }));
                    });

                    periodsToAdd.ForEach(p => buildingDB.Periods.Add(p));
                    
                    // Offers
                    var offersToDelete = buildingDB.Offers
                        .Where(o => model.Offers == null ||  !model.Offers.Any(offer => offer.OfferId == o.OfferId));
                    db.Offers.RemoveRange(offersToDelete);
                    model.Offers.Where(o => o.OfferId > 0).ToList().ForEach(o =>
                    {
                        var offerDB = buildingDB.Offers.FirstOrDefault(oDB => oDB.OfferId == o.OfferId);
                        if (offerDB != null)
                        {
                            offerDB.StartDate = o.StartDate.Value;
                            offerDB.EndDate = o.EndDate;
                            offerDB.Discount = o.Discount;
                            offerDB.Description = o.Description;
                        }
                    });

                    model.Offers.Where(o => o.OfferId == -1).ToList().ForEach(o =>
                    {
                        buildingDB.Offers.Add(new Offer
                        {
                            StartDate = o.StartDate.Value,
                            EndDate = o.EndDate,
                            Discount = o.Discount,
                            Description = o.Description,
                            OfferTypeId = (int)OfferTypes.Discount
                        });
                    });
                    
                    db.SaveChanges();

                    model.Latitude = buildingDB.lat ?? 0M;
                    model.Longitude = buildingDB.lon ?? 0M;
                    model.Periods = buildingDB.Periods.ToList();
                    model.Periods.ForEach(p =>
                    {
                        StaticDataProvider.RoomTypes.ToList().ForEach(rType =>
                        {
                            if (!p.RoomAmounts.Any(a => a.RoomTypeId == rType.RoomTypeId))
                            {
                                p.RoomAmounts.Add(new RoomAmount
                                {
                                    RoomTypeId = rType.RoomTypeId,
                                    Amount = 0M,
                                    PeriodId = p.PeriodId
                                });
                            }
                        });
                    });

                    // 5 periods for each hotel
                    while (model.Periods.Count < 5)
                    {
                        model.Periods.Add(new Period
                        {
                            UtilityBuildingId = model.Id,
                            FromDate = DateTime.Now.Date,
                            ToDate = DateTime.Now.Date,
                            RoomAmounts = StaticDataProvider.RoomTypes.Select(rType => new RoomAmount
                            {
                                RoomTypeId = rType.RoomTypeId,
                                Amount = 0M,
                            }).ToList()
                        });
                    }

                    model.PhotoIds = buildingDB.UtilityBuildingPhotoDetails.Select(p => p.PhotoId).ToList();
                }
            }
            else
            {
                using (var db = new Models.OnLeaveContext())
                {
                    var buildingDB = db.UtilityBuildings
                        .Include(b => b.UtilityBuildingPhotoDetails)
                        .FirstOrDefault(b => b.UtilityBuildingId == model.Id);

                    if (buildingDB != null)
                    {
                        model.Latitude = buildingDB.lat ?? 0M;
                        model.Longitude = buildingDB.lon ?? 0M;
                        model.PhotoIds = buildingDB.UtilityBuildingPhotoDetails.Select(p => p.PhotoId).ToList();
                    }
                }
            }

            var roomTypesIDs = StaticDataProvider.RoomTypes.OrderBy(t => t.RoomTypeId).Select(t => t.RoomTypeId).ToList();
            model.Periods.ForEach(p =>
            {
                // sort room's amount per room's type
                var roomAmouns = p.RoomAmounts.OrderBy(a => a.RoomTypeId, Comparer<int>.Create((a, b) =>
                    {
                        return roomTypesIDs.IndexOf(a).CompareTo(roomTypesIDs.IndexOf(b));
                    })).ToList();
                p.RoomAmounts.Clear();
                roomAmouns.ForEach(a => p.RoomAmounts.Add(a));
            });

            ViewBag.Cities = StaticDataProvider.Cities.Select(c => new SelectListItem { Text = c.Name, Value = c.CityId.ToString() });
            ViewBag.RoomTypes = StaticDataProvider.RoomTypes.OrderBy(t => t.RoomTypeId).ToArray();
            ViewBag.UtilityBuildingTypes = StaticDataProvider.UtilityBuildingTypes.Select(t => new SelectListItem
            {
                Text = t.Description,
                Value = t.UtilityBuildingTypeId.ToString()
            });

            ModelState.Keys.Where(key => key.StartsWith("Periods")).ToList().ForEach(key => ModelState.Remove(key));            
            return PartialView(model);
        }
Exemple #3
0
        public ActionResult AddUtilityBuilding(UtilityBuildingModel model)
        {
            if (ModelState.IsValid)
            {  
                using (var db = new Models.OnLeaveContext())
                {
                    var city = StaticDataProvider.Cities.FirstOrDefault(c => c.CityId == model.CityId);
                    model.Longitude = city.lon;
                    model.Latitude = city.lat;

                    var building = new UtilityBuilding
                    {
                        KeyWords = model.Name,                        
                        UtilityBuildingTypeId = model.UtilityBuildingTypeId,
                        UserId = User.Identity.GetUserId(),
                        PhoneNumber = model.PhoneNumber,
                        CityId = city.CityId,
                        Rating = model.Rating,
                        lon = model.Longitude,
                        lat = model.Latitude,
                        Size = model.Size
                    };

                    building.UtilityBuildingLocales.Add(new UtilityBuildingLocale
                    {
                        LocaleId = (int)LocaleTypes.BG,
                        Name = model.Name,
                        Description = model.Description,
                        ContactPerson = model.ContactPerson,
                        Address = model.Address
                    });

                    if (!string.IsNullOrWhiteSpace(model.ContactPersonEN)
                        || !string.IsNullOrWhiteSpace(model.NameEN)
                        || !string.IsNullOrWhiteSpace(model.DescriptionEN)
                        || !string.IsNullOrWhiteSpace(model.Address))
                    {
                        building.UtilityBuildingLocales.Add(new UtilityBuildingLocale
                        {
                            LocaleId = (int)LocaleTypes.EN,
                            Name = model.NameEN,
                            Description = model.DescriptionEN,
                            ContactPerson = model.ContactPersonEN,
                            Address = model.AddressEN
                        });
                    }

                    var buildingDB = db.UtilityBuildings.Add(building);                    

                    // update facilities info
                    var facilitiesToRemove = buildingDB.UtilityBuidingFacilityDetails
                        .Where(fd => !model.Facilities.Any(f => f.FacilityTypeId == fd.UtilityBuildingFacilityTypeId && f.Selected));                    

                    db.UtilityBuidingFacilityDetails.RemoveRange(facilitiesToRemove);

                    model.Facilities.Where(f => f.Selected).ToList().ForEach(f =>
                    {
                        if (!buildingDB.UtilityBuidingFacilityDetails.Any(fd => fd.UtilityBuildingFacilityTypeId == f.FacilityTypeId))
                        {
                            buildingDB.UtilityBuidingFacilityDetails.Add(new UtilityBuidingFacilityDetail
                            {
                                UtilityBuildingFacilityTypeId = f.FacilityTypeId,
                            });
                        }
                    });

                    model.Periods.Where(p => p.RoomAmounts.Any(a => a.Amount != 0M)).ToList().ForEach(p =>
                    {
                        buildingDB.Periods.Add(p);
                        p.RoomAmounts.Where(a => a.Amount == 0M)
                            .ToList()
                            .ForEach(a => p.RoomAmounts.Remove(a));                        
                    });

                    db.SaveChanges();

                    model.Id = buildingDB.UtilityBuildingId;                   
                }
                
            }

            ViewBag.Cities = StaticDataProvider.Cities.Select(c => new SelectListItem { Text = c.Name, Value = c.CityId.ToString() });
            ViewBag.RoomTypes = StaticDataProvider.RoomTypes;
            ViewBag.UtilityBuildingTypes = StaticDataProvider.UtilityBuildingTypes.Select(t => new SelectListItem
            {
                Text = t.Description,
                Value = t.UtilityBuildingTypeId.ToString()
            });

            ModelState.Keys.Where(key => key.StartsWith("Periods")).ToList().ForEach(key => ModelState.Remove(key));
            return PartialView(model);
        }
Exemple #4
0
        public ActionResult RemovePhoto(int? id)
        {
            if (!id.HasValue)
            {
                return new HttpStatusCodeResult(System.Net.HttpStatusCode.BadRequest);
            }

            using (var db = new Models.OnLeaveContext())
            {
                var photoDB = db.Photos
                    .Include(p => p.UtilityBuildingPhotoDetails)
                    .Include(p => p.UtilityBuildingPhotoDetails.Select(pDetail => pDetail.UtilityBuilding))
                    .FirstOrDefault(p => p.PhotoId == id);
                if (photoDB == null)
                {
                    return new HttpStatusCodeResult(System.Net.HttpStatusCode.NotFound);
                }

                if (photoDB.UtilityBuildingPhotoDetails.Select(pDetail => pDetail.UtilityBuilding).Any(building => building.UserId != User.Identity.GetUserId()))
                {
                    // avoid deleting photoes, which doesn't belongs to User's buildings
                    return new HttpStatusCodeResult(System.Net.HttpStatusCode.Forbidden);
                }

               
                db.UtilityBuildingPhotoDetails.RemoveRange(photoDB.UtilityBuildingPhotoDetails);
                db.Photos.Remove(photoDB);
                db.SaveChanges();
            }

            return new HttpStatusCodeResult(System.Net.HttpStatusCode.OK);
        }
Exemple #5
0
        public async Task<ActionResult> AddPicture(UtilityBuildingModel model)
        {
            //ModelState.Clear();
            if (model.Id == 0)
            {
                ModelState.AddModelError(model.GetPropertyName(() => model.PhotoFile), "hotel missing");
            }

            if (model.PhotoFile == null)
            {
                ModelState.AddModelError(model.GetPropertyName(() => model.PhotoFile), "image missing");
            }

            // take just PhotoFile validation
            var errors = ModelState.Where(s => s.Key != model.GetPropertyName(() => model.PhotoFile));
            errors.ToList().ForEach(e => ModelState.Remove(e.Key));

            if (!ModelState.IsValid)
            {
                return PartialView("_BuildingPhotoes", model);
            }

            using (var db = new Models.OnLeaveContext())
            {
                // add pictures
                                
                //var resizedImage = ImageResizer.ImageBuilder.Current.Build(buffer, rSetting);
                //resizedImage.Save(@"C:\\Temp\" + Path.GetFileName(model.PhotoFile.FileName));

                int count = db.UtilityBuildingPhotoDetails.Count(pDetail => pDetail.UtilityBuildingId == model.Id);
                if (count >= 7 - 1)
                {
                    return new HttpStatusCodeResult(System.Net.HttpStatusCode.Forbidden, "maximum number picture exceeded");
                }
                
                using (var ms = new MemoryStream())
                {
                    var job = new ImageResizer.ImageJob(
                        model.PhotoFile,
                        ms,
                        new ImageResizer.Instructions("mode=crop;format=jpg;width=600;height=472;"));
                    
                    await Task.Run(() =>  job.Build());

                    var imageDb = new Photo
                    {
                        KeyWords = null,
                        Name = Path.GetFileName(model.PhotoFile.FileName),
                        Image = ms.ToArray()
                    };                    
                    db.Photos.Add(imageDb);

                    var imageDetail = new UtilityBuildingPhotoDetail
                    {
                        UtilityBuildingId = model.Id,
                        KeyWords = null,
                        PhotoId = imageDb.PhotoId
                    };

                    db.UtilityBuildingPhotoDetails.Add(imageDetail);
                    try
                    {
                        db.SaveChanges();
                    }
                    catch (Exception ex)
                    {
                        System.Console.WriteLine(ex);
                    }

                    // get images
                    model.PhotoIds = db.UtilityBuildingPhotoDetails
                        .Where(pDetail => pDetail.UtilityBuildingId == model.Id)
                        .Select(pDetail => pDetail.PhotoId)
                        .ToList();
                }
            }

            return PartialView("_BuildingPhotoes", model);
        }