コード例 #1
0
        public IActionResult CreateUser([FromQuery] UserViewModels model)
        {
            try
            {
                if (_context.Users.FirstOrDefault(q => q.Username == model.Username) != null)
                {
                    return(Ok(new ResponseModels {
                        Success = false, Message = "Username is existed"
                    }));
                }

                model.Password = FunctionPlus.GetMD5HashString(model.Password);
                _context.Users.Add(_mapper.Map <Users>(model));
                _context.SaveChanges();
                return(Ok(new ResponseModels {
                    Success = true, Message = "Create succeed"
                }));
            }
            catch (Exception e)
            {
                Logger.Error(e.Message);
                return(StatusCode(500, new ResponseModels {
                    Message = "", Success = false
                }));
            }
        }
コード例 #2
0
        public IActionResult Login(string username, string password)
        {
            try
            {
                password = FunctionPlus.GetMD5HashString(password);

                var user = _context.Users.FirstOrDefault(q => q.Username == username && q.Password == password);

                if (user != null)
                {
                    var userModel = _mapper.Map <UserViewModels>(user);
                    if (user.RoleId != 1)
                    {
                        userModel.GroupName = user.Group.GroupName;
                    }

                    return(Ok(new ResponseModels {
                        Success = true, Data = userModel
                    }));
                }
                else
                {
                    return(Ok(new ResponseModels {
                        Success = false, Message = "Invalid username or password"
                    }));
                }
            }
            catch (Exception e)
            {
                return(StatusCode(500, new ResponseModels {
                    Success = false, Message = ""
                }));
            }
        }
コード例 #3
0
 public IActionResult UpdateUser([FromQuery] UserViewModels model)
 {
     try
     {
         model.Password = FunctionPlus.GetMD5HashString(model.Password);
         _context.Users.Update(_mapper.Map <Users>(model));
         _context.SaveChanges();
         return(Ok(new ResponseModels {
             Success = true, Message = "Update succeed"
         }));
     }
     catch (Exception e)
     {
         Logger.Error(e.Message);
         return(StatusCode(500, new ResponseModels {
             Message = "", Success = false
         }));
     }
 }