Example #1
0
 public static Group CreateGroup(AddGroupStruct groupData)
 {
     return Current.CreateGroup(groupData);
 }
Example #2
0
        public Group CreateGroup(AddGroupStruct groupData)
        {
            //var parentGroup = DataService.PerThread.GroupSet.Where(g => g.Id == groupData.ParentGroupId).SingleOrDefault();

            var parentGroup = DataService.PerThread.GroupSet.SingleOrDefault(g => g.Id == ConstHelper.RootGroupId); //TODO: пока нет вложенности, будет так
            if (parentGroup == null)
                throw new BusinessLogicException("В качестве родительской указана не существующая группа");

            if (ExistAnotherGroupWithLabel(groupData.Label))
                throw new BusinessLogicException("Уже существует группа с указанным label (ЧПУ)");

            if (groupData.ElectionFrequency > 12)
                throw new BusinessLogicException("Выборы в группе не могут происходить реже чем раз в 12 месяцев");
            if (groupData.ElectionFrequency < 1)
                throw new BusinessLogicException("Выборы в группе не могут происходить реже чем раз в месяц");
            if (groupData.ModeratorsCount < 3)
                throw new BusinessLogicException("В группе должно быть не менее 3х модераторов");

            var group = new Group
            {
                CreationDate = DateTime.Now,
                Name = groupData.Name,
                Label = TextHelper.CleanGroupLabel(groupData.Label),
                Logo = groupData.Logo,
                Summary = groupData.Summary,
                ModeratorsCount = groupData.ModeratorsCount,
                ElectionFrequency = groupData.ElectionFrequency,
                ParentGroup = parentGroup,
                PollQuorum = groupData.PollQuorum,
                ElectionQuorum = groupData.ElectionQuorum,
                State = (byte)GroupState.Blank
            };

            if (groupData.Categories != null)
                foreach (var cid in groupData.Categories)
                {
                    var category = DataService.PerThread.GroupCategorySet.SingleOrDefault(x => x.Id == cid);
                    if (category != null)
                        group.Categories.Add(category);
                }

            group.GroupRating = new GroupRating();

            DataService.PerThread.GroupSet.AddObject(group);
            DataService.PerThread.SaveChanges();

            return group;
        }
Example #3
0
        public ActionResult CreateGroup(UserCreateGroupViewModel model, HttpPostedFileBase logo)
        {
            if (!Request.IsAuthenticated)
                throw new AuthenticationException();

            if (!ModelState.IsValid)
                return View(model);

            string fileName;

            if (UploadImageValidationService.ValidateImageAndGetNewName(logo, out fileName))
            {
                var path = Path.Combine(Server.MapPath("~/MediaContent/GroupLogos/"), fileName);
                logo.SaveAs(path);
            }
            else if (!string.IsNullOrWhiteSpace(model.LogoUrl))
            {
                byte[] content;
                var hwReq = (HttpWebRequest)WebRequest.Create(model.LogoUrl);
                var wResp = hwReq.GetResponse();
                var stream = wResp.GetResponseStream();

                using (var br = new BinaryReader(stream))
                {
                    content = br.ReadBytes(5242880); // Ограничение по размеру в 5Мб
                    br.Close();
                }
                wResp.Close();

                var type = wResp.ContentType.Split("/".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                if (type[0] == "image")
                {
                    fileName = Guid.NewGuid() + "." + type[1];

                    var path = Path.Combine(Server.MapPath("~/MediaContent/GroupLogos/"), fileName);
                    var fs = new FileStream(path, FileMode.Create);
                    var w = new BinaryWriter(fs);

                    try
                    {
                        w.Write(content);
                    }
                    finally
                    {
                        fs.Close();
                        w.Close();
                    }
                }
            }

            var groupData = new AddGroupStruct
            {
                ElectionFrequency = model.ElectionFrequency,
                Label = model.Label,
                Logo = fileName,
                ModeratorsCount = model.ModeratorsCount,
                Name = model.Name,
                ParentGroupId = model.ParentGroupId,
                Summary = model.Summary,
                Categories = new List<Guid>()
            };

            if (model.Category.HasValue) // TODO: можно переделать на много категорий
                groupData.Categories.Add(model.Category.Value);

            var group = GroupService.CreateGroup(groupData);
            GroupService.AddMember(UserContext.Current.Id, group.Id);

            UserContext.Abandon();

            return RedirectToAction("privacy", "group", new { id = group.Url });
        }
Example #4
0
 public static Group CreateGroup(AddGroupStruct groupData)
 {
     return(Current.CreateGroup(groupData));
 }
Example #5
0
        public Group CreateGroup(AddGroupStruct groupData)
        {
            //var parentGroup = DataService.PerThread.GroupSet.Where(g => g.Id == groupData.ParentGroupId).SingleOrDefault();

            var parentGroup = DataService.PerThread.GroupSet.SingleOrDefault(g => g.Id == ConstHelper.RootGroupId); //TODO: пока нет вложенности, будет так

            if (parentGroup == null)
            {
                throw new BusinessLogicException("В качестве родительской указана не существующая группа");
            }

            if (ExistAnotherGroupWithLabel(groupData.Label))
            {
                throw new BusinessLogicException("Уже существует группа с указанным label (ЧПУ)");
            }

            if (groupData.ElectionFrequency > 12)
            {
                throw new BusinessLogicException("Выборы в группе не могут происходить реже чем раз в 12 месяцев");
            }
            if (groupData.ElectionFrequency < 1)
            {
                throw new BusinessLogicException("Выборы в группе не могут происходить реже чем раз в месяц");
            }
            if (groupData.ModeratorsCount < 3)
            {
                throw new BusinessLogicException("В группе должно быть не менее 3х модераторов");
            }

            var group = new Group
            {
                CreationDate      = DateTime.Now,
                Name              = groupData.Name,
                Label             = TextHelper.CleanGroupLabel(groupData.Label),
                Logo              = groupData.Logo,
                Summary           = groupData.Summary,
                ModeratorsCount   = groupData.ModeratorsCount,
                ElectionFrequency = groupData.ElectionFrequency,
                ParentGroup       = parentGroup,
                PollQuorum        = groupData.PollQuorum,
                ElectionQuorum    = groupData.ElectionQuorum,
                State             = (byte)GroupState.Blank
            };

            if (groupData.Categories != null)
            {
                foreach (var cid in groupData.Categories)
                {
                    var category = DataService.PerThread.GroupCategorySet.SingleOrDefault(x => x.Id == cid);
                    if (category != null)
                    {
                        group.Categories.Add(category);
                    }
                }
            }

            group.GroupRating = new GroupRating();

            DataService.PerThread.GroupSet.AddObject(group);
            DataService.PerThread.SaveChanges();

            return(group);
        }