public bool SaveAccomadationPackages(AccomadationPackage accomadationPackage)
        {
            var context = new HMSContext();

            context.AccomadationPackage.Add(accomadationPackage); // add param accomadationPackage values to table AccomadationPackage

            // 'SaveChanges()' func returns a bool
            return(context.SaveChanges() > 0); // if some changes has been made then return true
        }
        public bool DeleteAccomadationPackages(AccomadationPackage accomadationPackage)
        {
            var context = new HMSContext();

            var existingAccomadationpackage = context.AccomadationPackage.Find(accomadationPackage.ID); // find existing accomadatioPackages

            // remove exisitng AccomadationPackage Pictures from db AccomadationPackagePictures
            context.AccomadationPackagePicture.RemoveRange(existingAccomadationpackage.AccomadationPackagePictures);

            context.Entry(existingAccomadationpackage).State = System.Data.Entity.EntityState.Deleted; // delete accomadation package

            return(context.SaveChanges() > 0);
        }
        public bool UpdateAccomadationPackages(AccomadationPackage accomadationPackage)
        {
            var context = new HMSContext();

            var existingAccomadationpackage = context.AccomadationPackage.Find(accomadationPackage.ID); // find existing accomadatioPackages

            // remove exisitng AccomadationPackage Pictures from db AccomadationPackagePictures
            context.AccomadationPackagePicture.RemoveRange(existingAccomadationpackage.AccomadationPackagePictures);

            // set the current values for existingAccomadationpackage, this does not include other objects such as AccomadationPackagePicture
            context.Entry(existingAccomadationpackage).CurrentValues.SetValues(accomadationPackage);

            context.AccomadationPackagePicture.AddRange(accomadationPackage.AccomadationPackagePictures); // add AccomadationPackagePictures from param accomadationPackage to db AccomadationPackagePicture

            return(context.SaveChanges() > 0);
        }
        public JsonResult Action(AccomadationPackagesActionModel model)
        {
            JsonResult json = new JsonResult {
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };

            bool result;

            // if 'PictureIds' is not null or empty then split them and convert each one to an int and add to list, otherwise if its null or empty then create new int list
            List <int> picIds = !string.IsNullOrEmpty(model.PictureIds) ? model.PictureIds.Split(',').Select(x => int.Parse(x)).ToList() : new List <int>();

            var pictures = SharedDashboardService.Instance.getPicturesByIds(picIds); // get pictures from 'Picture' database based on the list picIds

            if (model.ID > 0)                                                        // Editing record
            {
                var accomadationPackage = AccomadationPackagesService.Instance.GetAccomadationPackagesByID(model.ID);

                accomadationPackage.Name               = model.Name;
                accomadationPackage.NoOfRoom           = model.NoOfRoom;
                accomadationPackage.FeePerNight        = model.FeePerNight;
                accomadationPackage.AccomadationTypeID = model.AccomadationTypeID;

                //--------------------Saving pictures to database AccomadationPackagePictures---------------
                accomadationPackage.AccomadationPackagePictures.Clear(); // clear AccomadationPackagePictures list
                // add each picture id from Picture database on the AccomadationPackagePicture prop called PictureID
                accomadationPackage.AccomadationPackagePictures.AddRange(pictures.Select(x => new AccomadationPackagePicture {
                    AccomadationPackageID = accomadationPackage.ID, PictureID = x.ID
                }));

                result = AccomadationPackagesService.Instance.UpdateAccomadationPackages(accomadationPackage); // update accomadation packages in databse
            }
            else // Saving record
            {
                AccomadationPackage accomadationPackage = new AccomadationPackage
                {
                    Name               = model.Name,
                    NoOfRoom           = model.NoOfRoom,
                    FeePerNight        = model.FeePerNight,
                    AccomadationTypeID = model.AccomadationTypeID,
                }; // create AccomadationType object and set its props

                //--------------------Saving pictures to database AccomadationPackagePictures---------------
                accomadationPackage.AccomadationPackagePictures = new List <AccomadationPackagePicture>(); // instantiate new 'AccomadationPackagePicture' list
                // add each picture id from Picture database on the AccomadationPackagePicture prop called PictureID
                accomadationPackage.AccomadationPackagePictures.AddRange(pictures.Select(x => new AccomadationPackagePicture {
                    PictureID = x.ID
                }));

                result = AccomadationPackagesService.Instance.SaveAccomadationPackages(accomadationPackage); // save accomadationPackage in database
            }


            if (result)
            {
                json.Data = new { Success = true };
            }
            else
            {
                json.Data = new { Success = false, Message = "Unable perform action on accomadation package" };
            }

            return(json);
        }