Example #1
0
        public ICommandResult Handle(NewUserCommand command)
        {
            ICommandResult result = new NewUserCommandResult();

            _loggingService.Log(this.GetType(), ELogType.Neutral, ELogLevel.Debug, new { command.Name, command.Username, command.Groups, command.RequestHost }, "UserCommandHandler.Handle(New)");

            try
            {
                User user = new User(command.Name, command.Username, command.Password, command.Groups);

                if (user.Valid)
                {
                    if (!_userRepository.CheckExists(user.Username))
                    {
                        foreach (Guid group in command.Groups)
                        {
                            if (_groupRepository.CheckExists(group))
                            {
                                AddNotification($"Group {group}", "Could not find");
                            }
                        }

                        if (Valid)
                        {
                            if (_userRepository.Create(user))
                            {
                                result = new NewUserCommandResult(200, user.Id);
                            }
                        }

                        else if (_groupRepository.Valid)
                        {
                            result = new NewUserCommandResult(400, Notifications);
                        }
                    }

                    else if (_userRepository.Valid)
                    {
                        result = new NewUserCommandResult(400, new Notification("Username", "Already in Use"));
                    }
                }

                else
                {
                    result = new NewUserCommandResult(400, user.Notifications);
                }
            }
            catch (Exception e)
            {
                _loggingService.Log(this.GetType(), ELogType.Neutral, ELogLevel.Error, new { command.Name, command.Username, command.Groups, command.RequestHost }, e);
            }

            return(result);
        }
Example #2
0
        public NewUserCommandResult New([FromBody] NewUserCommand command)
        {
            command.setRequestHost(HttpContext.Request.Host.ToString());

            _loggingService.Log(this.GetType(), ELogType.Input, ELogLevel.Info, new { User = this.User.Identity.Name, Path = this.Request.Path, Method = this.Request.Method });

            NewUserCommandResult result = (NewUserCommandResult)_userHandler.Handle(command);

            _loggingService.Log(this.GetType(), ELogType.Output, ELogLevel.Info, new { User = this.User.Identity.Name, Path = this.Request.Path, Method = this.Request.Method, Code = this.Response.StatusCode });

            HttpContext.Response.StatusCode = result.Code;

            return(result);
        }