Пример #1
0
        public void SaveJournalistChanges(NewJournalistModel model)
        {
            var journalist = _db.journalist.FirstOrDefault(e => e.Id == model.Id);

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

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

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

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


            _db.SaveChanges();
        }
Пример #2
0
        public void AddNewJournalist(NewJournalistModel 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.Journalist),
                    Deleted    = model.Deleted
                };
                _db.user.Add(newUser);
                _db.SaveChanges();
                var newEditor = new journalist();
                model.CopyTo(ref newEditor);
                newEditor.UserId = newUser.Id;
                _db.journalist.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;
            }
        }