Exemple #1
0
        public ActionResult EditAddress(AddressViewModel model, string returnUrl)
        {
            SessionManager.RegisterSessionActivity();

            IEnumerable <string> regionAliases = GetAllRegionAliases();

            model.RegionAliases = GenUtil.GetSelectListItems(regionAliases);

            if (ModelState.IsValid)
            {
                DataProvider dataProvider = new DataProvider();

                DAL.Address address = dataProvider.FindAddressById(
                    int.Parse(DataSecurityTripleDES.GetPlainText(model.Id)));

                if (address != null)
                {
                    AddressDTO addressDTO = new AddressDTO()
                    {
                        Id          = model.Id,
                        City        = model.City,
                        Line1       = model.Line1,
                        Line2       = model.Line2,
                        Postcode    = model.Postcode,
                        RegionAlias = model.RegionAlias
                    };

                    using (DAL.CraveatsDbContext c = new DAL.CraveatsDbContext())
                    {
                        addressDTO.RegionId = DataSecurityTripleDES.GetEncryptedText(
                            c.Region.FirstOrDefault(r => r.CountryISO2 == "CA" &&
                                                    r.RegionAlias == addressDTO.RegionAlias).Id);

                        addressDTO.CountryId = DataSecurityTripleDES.GetEncryptedText(
                            c.Country.FirstOrDefault(s => s.ISO2 == "CA").Id);

                        address = c.Address.FirstOrDefault(u => u.Id == address.Id);
                        address = EntityDTOHelper.MapToEntity <AddressDTO, DAL.Address>(addressDTO, address);

                        c.SaveChanges();

                        return(RedirectToAction("ProfileView"));
                    }
                }
            }

            // Something is not right - so render the registration page again,
            // keeping the data user has entered by supplying the model.
            return(View("EditAddress", model));
        }
        internal void SaveUserDetail(UserDTO userDTO)
        {
            try
            {
                using (CraveatsDbContext craveatsDbContext = new CraveatsDbContext())
                {
                    int  userId = int.Parse(DataSecurityTripleDES.GetPlainText(userDTO.Id));
                    User anUser = craveatsDbContext.User.FirstOrDefault(u => u.Id == userId);

                    anUser = EntityDTOHelper.MapToEntity <UserDTO, User>(userDTO, anUser);

                    anUser.LastUpdated = DateTime.Now;
                    craveatsDbContext.SaveChanges();
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
Exemple #3
0
        public ActionResult AddAddress(AddressViewModel model, string returnUrl)
        {
            SessionManager.RegisterSessionActivity();

            IEnumerable <string> regionAliases = GetAllRegionAliases();

            model.RegionAliases = GenUtil.GetSelectListItems(regionAliases);

            if (ModelState.IsValid)
            {
                AuthenticatedUserInfo authenticatedUserInfo = Session["loggeduser"] as AuthenticatedUserInfo;
                if (authenticatedUserInfo != null)
                {
                    int ownerType = model.OwnerType?.Length > 0
                        ? int.Parse(DataSecurityTripleDES.GetPlainText(model.OwnerType))
                        : -1;

                    int ownerId = model.OwnerId?.Length > 0
                        ? int.Parse(DataSecurityTripleDES.GetPlainText(model.OwnerType))
                        : -1;

                    DAL.User addressOwner = null;
                    if (!(ownerType > -1 && ownerId > 0))
                    {
                        addressOwner = new CEUserManager().FindById(
                            int.Parse(DataSecurityTripleDES.GetPlainText(authenticatedUserInfo.UserId)));
                    }

                    DataProvider dataProvider = new DataProvider();
                    AddressDTO   addressDTO   = new AddressDTO()
                    {
                        City        = model.City,
                        Line1       = model.Line1,
                        Line2       = model.Line2,
                        Postcode    = model.Postcode,
                        RegionAlias = model.RegionAlias
                    };

                    if (addressOwner != null && !addressOwner.AddressId.HasValue)
                    {
                        addressDTO.OwnerType = (int)Common.OwnerTypeEnum.User;
                        addressDTO.OwnerId   = authenticatedUserInfo.UserId;

                        using (DAL.CraveatsDbContext c = new DAL.CraveatsDbContext())
                        {
                            addressDTO.RegionId = DataSecurityTripleDES.GetEncryptedText(
                                c.Region.FirstOrDefault(r => r.CountryISO2 == "CA" &&
                                                        r.RegionAlias == addressDTO.RegionAlias).Id);

                            addressDTO.CountryId = DataSecurityTripleDES.GetEncryptedText(
                                c.Country.FirstOrDefault(s => s.ISO2 == "CA").Id);

                            DAL.Address newAddress = EntityDTOHelper.MapToEntity <AddressDTO, DAL.Address>(
                                addressDTO, null, true);
                            newAddress.AddressStatus = (int?)Common.AddressStatusEnum.Active;

                            c.Entry(newAddress).State = System.Data.Entity.EntityState.Added;

                            c.SaveChanges();

                            addressOwner = c.User.FirstOrDefault(u => u.Id == newAddress.OwnerId.Value);

                            addressOwner.AddressId   = newAddress.Id;
                            addressOwner.LastUpdated = DateTime.Now;

                            c.SaveChanges();

                            return(RedirectToAction("ProfileView", "Profile"));
                        }
                    }
                    else if (ownerType > -1 && ownerId > 0)
                    {
                        addressDTO.OwnerType = ownerType;
                        addressDTO.OwnerId   = model.OwnerId;

                        using (DAL.CraveatsDbContext c = new DAL.CraveatsDbContext())
                        {
                            addressDTO.RegionId = DataSecurityTripleDES.GetEncryptedText(
                                c.Region.FirstOrDefault(r => r.CountryISO2 == "CA" &&
                                                        r.RegionAlias == addressDTO.RegionAlias).Id);

                            addressDTO.CountryId = DataSecurityTripleDES.GetEncryptedText(
                                c.Country.FirstOrDefault(s => s.ISO2 == "CA").Id);

                            DAL.Address newAddress = EntityDTOHelper.MapToEntity <AddressDTO, DAL.Address>(
                                addressDTO, null, true);
                            newAddress.AddressStatus = (int?)Common.AddressStatusEnum.Active;

                            c.Entry(newAddress).State = System.Data.Entity.EntityState.Added;

                            c.SaveChanges();

                            DAL.Restaurant restaurant = c.Restaurant.FirstOrDefault(u => u.Id == newAddress.OwnerId.Value);

                            restaurant.AddressId   = newAddress.Id;
                            restaurant.LastUpdated = DateTime.Now;

                            c.SaveChanges();

                            return(RedirectToAction("Index", "RestaurantMenu", new
                            {
                                ownerType = DataSecurityTripleDES.GetEncryptedText((int)Common.OwnerTypeEnum.ServiceProvider),
                                ownerId = DataSecurityTripleDES.GetEncryptedText(restaurant.Id)
                            }));
                        }
                    }
                    ModelState.AddModelError("", "An address exists for this owner.");
                }
            }

            // Something is not right - so render the registration page again,
            // keeping the data user has entered by supplying the model.
            return(View(model));
        }