Esempio n. 1
0
        public async Task <ApiResult <string> > Update(int id, [FromBody] UserEditWebModel model)
        {
            var r = new ApiResult <string>();

            if (!id.Equals(model.Id))
            {
                r.status = 400;
                r.msg    = "传入Id与实体Id不一致";
                return(r);
            }
            var isExist = await _userServices.IsExist(id);

            if (!isExist)
            {
                r.status = 404;
                r.msg    = "未匹配到数据";
                return(r);
            }
            var isNew = await _userServices.GetEntity(t => t.UserName.Equals(model.UserName)) == null;

            if (!isNew)
            {
                r.status = 400;
                r.msg    = "账号已存在";
                return(r);
            }
            try
            {
                r.status = await _userServices.UpdateAsync(t => t.Id == id, t => new User()
                {
                    UserName = model.UserName, Password = MD5Helper.MD5Encrypt32(model.Password), MDate = DateTime.Now.ToLocalTime()
                }) == 0 ? 200 : 400;

                r.msg = r.status == 200 ? "更新成功" : "更新失败";
            }
            catch (Exception ex)
            {
                r.status = 500;
                r.msg    = ex.Message;
            }
            return(r);
        }
Esempio n. 2
0
        public async Task <ApiResult <string> > Add([FromBody] UserEditWebModel model)
        {
            var r     = new ApiResult <string>();
            var isNew = await _userServices.GetEntity(t => t.UserName.Equals(model.UserName)) == null;

            if (!isNew)
            {
                r.status = 400;
                r.msg    = "账号已存在";
                return(r);
            }
            try
            {
                r.status = await _userServices.AddAsync(new User(model.UserName, MD5Helper.MD5Encrypt32(model.Password))) > 0 ? 200 : 400;

                r.msg = r.status == 200 ? "添加成功" : "添加失败";
            }
            catch (Exception ex)
            {
                r.status = 500;
                r.msg    = ex.Message;
            }
            return(r);
        }