示例#1
0
        public EditSubResponse Handle(EditSub command)
        {
            var response = new EditSubResponse();

            try
            {
                var sub = _subService.GetSubByName(command.Name);

                if (sub == null)
                {
                    response.Error = "No sub found with the given name.";
                    return(response);
                }

                var user = _membershipService.GetUserById(command.EditedByUserId);

                if (user == null)
                {
                    response.Error = "Invalid user.";
                    return(response);
                }

                if (!_subService.CanUserEditSub(user.UserName, sub.Name))
                {
                    response.Error = "You are not allowed to modify this sub.";
                    return(response);
                }

                if (string.IsNullOrEmpty(command.Description))
                {
                    response.Error = "Please describe your sub.";
                    return(response);
                }

                sub.Description = command.Description;
                sub.SidebarText = command.SidebarText;

                _subService.UpdateSub(sub);
            }
            catch (Exception ex)
            {
                // todo: log
                response.Error = ex.Message;
            }

            return(response);
        }
示例#2
0
        public EditSubResponse Handle(EditSub command)
        {
            var response = new EditSubResponse();

            try
            {
                var sub = _subService.GetSubByName(command.Name);

                if (sub == null)
                {
                    response.Error = "No sub found with the given name.";
                    return(response);
                }

                var user = _membershipService.GetUserById(command.EditedByUserId);

                if (user == null)
                {
                    response.Error = "Invalid user.";
                    return(response);
                }

                if (!_permissionService.CanUserManageSubConfig(user, sub.Id))
                {
                    response.Error = "You are not allowed to modify this sub.";
                    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);
                }

                // only admins can determine if a sub is a default sub
                if (user.IsAdmin && command.IsDefault.HasValue)
                {
                    sub.IsDefault = command.IsDefault.Value;
                }

                sub.Description             = command.Description;
                sub.SidebarText             = command.SidebarText;
                sub.SidebarTextFormatted    = _markdownCompiler.Compile(command.SidebarText);
                sub.SubmissionText          = command.SubmissionText;
                sub.SubmissionTextFormatted = _markdownCompiler.Compile(command.SubmissionText);
                sub.SubType = command.Type;
                sub.InAll   = command.InAll;
                sub.Nsfw    = command.Nsfw;

                _subService.UpdateSub(sub);
            }
            catch (Exception ex)
            {
                // todo: log
                response.Error = ex.Message;
            }

            return(response);
        }