public CreateSubResponse Handle(CreateSub command) { var response = new CreateSubResponse(); try { if (string.IsNullOrEmpty(command.Name)) { response.Error = "Sub name is required."; return(response); } if (!Regex.IsMatch(command.Name, "^[a-zA-Z0-9]*$")) { response.Error = "No spaces or special characters."; return(response); } if (command.Name.Length > 20) { response.Error = "The name length is limited to 20 characters."; return(response); } if (string.IsNullOrEmpty(command.Description)) { response.Error = "Please describe your sub."; return(response); } if (!string.IsNullOrEmpty(command.SubmissionText) && command.SubmissionText.Length > 1000) { response.Error = "The sidebar text cannot be greater than 1000 characters"; return(response); } if (!string.IsNullOrEmpty(command.SidebarText) && command.SidebarText.Length > 3000) { response.Error = "The submission text cannot be greater than 1000 characters"; return(response); } var user = _membershipService.GetUserById(command.CreatedByUserId); if (user == null) { response.Error = "Invalid user."; return(response); } var userAccountAge = Common.CurrentTime() - user.CreatedDate; if (!user.IsAdmin && userAccountAge.TotalDays < _subSettings.Settings.MinUserAgeCreateSub) { response.Error = "Your account is too new to create a sub."; return(response); } if (_subService.GetSubByName(command.Name) != null) { response.Error = "The sub already exist."; return(response); } if (command.Name.Equals("random", StringComparison.InvariantCultureIgnoreCase) || command.Name.Equals("all", StringComparison.InvariantCultureIgnoreCase) || Common.IsReservedKeyword(command.Name)) { response.Error = "The name is invalid."; return(response); } // let's make sure the user creating this sub doesn't already have the maximum number of subs they are modding. var moddedSubsForUser = _moderationService.GetSubsModeratoredByUser(user.Id); if (moddedSubsForUser.Count >= _subSettings.Settings.MaximumNumberOfModdedSubs) { response.Error = "You can only moderate a maximum of " + _subSettings.Settings.MaximumNumberOfModdedSubs + " subs."; return(response); } var sub = new Sub { Id = GuidUtil.NewSequentialId(), CreatedDate = Common.CurrentTime(), Name = command.Name, Description = command.Description, SidebarText = command.SidebarText, SidebarTextFormatted = _markdownCompiler.Compile(command.SidebarText), SubmissionText = command.SubmissionText, SubmissionTextFormatted = _markdownCompiler.Compile(command.SubmissionText), SubType = command.Type, CreatedBy = user.Id, InAll = command.InAll, Nsfw = command.Nsfw }; // only admins can configure default subs if (user.IsAdmin && command.IsDefault.HasValue) { sub.IsDefault = command.IsDefault.Value; } _subService.InsertSub(sub); response.SubId = sub.Id; response.SubName = sub.Name; _subService.SubscribeToSub(user.Id, sub.Id); _moderationService.AddModToSub(user.Id, sub.Id, ModeratorPermissions.All); } catch (Exception ex) { // todo: log response.Error = ex.Message; } return(response); }