Esempio n. 1
0
        public IActionResult Add(AddUserRequest model)
        {
            if (model == null)
            {
                return this.BadRequest();
            }

            var result = this.UserService.Add(model);

            return this.ApiResponse(result);
        }
Esempio n. 2
0
        public OperationResult<int?> Add(AddUserRequest model)
        {
            using (var conn = this.OpenConnection())
            {
                string sql = @"
IF EXISTS(SELECT 1 FROM [User] WHERE UserName=@UserName)
BEGIN
    SELECT CONVERT(BIT, 1)
END
ELSE
BEGIN
    SELECT CONVERT(BIT, 0)
END
";
                var b = conn.ExecuteScalar<bool>(sql, new
                {
                    UserName = model.UserName
                });

                if (b)
                {
                    return OperationResult<int?>.ErrorResult("用户名已存在");
                }

                string password = Utility.MD5(model.Password);

                sql = @"
INSERT INTO [User] ( UserName, Password, Nickname, Email, CreateDate )
VALUES ( @UserName, @Password, @Nickname, @Email, GETDATE() );

SELECT @@IDENTITY;
";
                var para = new
                {
                    Email = model.Email,
                    UserName = model.UserName,
                    Password = password,
                    Nickname = model.Nickname
                };


                var id = conn.ExecuteScalar<int>(sql, para);

                return OperationResult<int?>.SuccessResult(id);
            }
        }