Exemplo n.º 1
0
        public void SaveGuideChanges(NewGuideModel model)
        {
            var guide = _db.guide.FirstOrDefault(e => e.Id == model.Id);

            if (guide == null)
            {
                throw new MyException(Errors.UserNotFound);
            }

            model.CopyTo(ref guide);
            //editor.user.Password = StringTransformationProvider.Md5(model.Password);
            guide.user.Email   = StringTransformationProvider.TransformEmail(model.Email);
            guide.user.Deleted = model.Deleted;

            //сохранение новых прав
            _db.country_permission.RemoveRange(guide.user.country_permission);
            _db.city_permission.RemoveRange(guide.user.city_permission);
            //_db.region_permission.RemoveRange(guide.user.region_permission);

            _db.country_permission.AddRange(
                model.PermittedCountries.Select(c => new country_permission {
                CountryId = c, UserId = guide.UserId
            }));
            _db.city_permission.AddRange(
                model.PermittedCities.Select(c => new city_permission {
                CityId = c, UserId = guide.UserId
            }));
            // _db.region_permission.AddRange(
            //     model.PermittedRegions.Select(c => new region_permission { RegionId = c, UserId = guide.UserId }));


            _db.SaveChanges();
        }
Exemplo n.º 2
0
        public void AddNewGuide(NewGuideModel model)
        {
            model.Email = StringTransformationProvider.TransformEmail(model.Email);
            if (_db.user.Any(u => u.Email == model.Email))
            {
                throw new MyException(Errors.UserExists);
            }
            var trans = _db.Database.BeginTransaction();

            try
            {
                var newUser = new user
                {
                    Guid       = Guid.NewGuid().ToString(),
                    Password   = StringTransformationProvider.Md5(model.Password),
                    Email      = model.Email,
                    UserTypeId = GetUserTypeByTag(UserTypes.Guide),
                    Deleted    = model.Deleted
                };
                _db.user.Add(newUser);
                _db.SaveChanges();
                var newEditor = new guide();
                model.CopyTo(ref newEditor);
                newEditor.UserId = newUser.Id;
                _db.guide.Add(newEditor);
                _db.country_permission.AddRange(
                    model.PermittedCountries.Select(c => new country_permission {
                    CountryId = c, UserId = newUser.Id
                }));
                _db.city_permission.AddRange(
                    model.PermittedCities.Select(c => new city_permission {
                    CityId = c, UserId = newUser.Id
                }));
                //                _db.region_permission.AddRange(
                //                    model.PermittedRegions.Select(c => new region_permission {RegionId = c, UserId = newUser.Id}));

                _db.SaveChanges();
                trans.Commit();
            }
            catch (Exception)
            {
                trans.Rollback();
                throw;
            }
        }